-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmdb_uart.c
88 lines (72 loc) · 1.73 KB
/
mdb_uart.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "common.h"
#include "mdb_uart.h"
void mdb_uart_init()
{
UCSR1B = _BV(RXCIE1) | _BV(RXEN1) | _BV(TXEN1);
UBRR1H = 0;
UBRR1L = 95; /* 9600bps @ 14745600Hz */
UCSR1C |= _BV(UCSZ11) | _BV(UCSZ10); /* 9bit mode */
UCSR1B |= _BV(UCSZ12); /* 9bit mode */
////////////////
////////////////
mdb_rx.in = 0;
mdb_rx.out = 0;
mdb_rx.count = 0;
}
void mdb_uart_send(unsigned short *buf, unsigned char length)
{
for (unsigned char i=0; i<length; i++)
{
mdb_uart_putc( buf[i] >> 8, buf[i]);
}
}
void mdb_uart_flush()
{
// mdb_uart_init();
mdb_rx.in = 0;
mdb_rx.out = 0;
mdb_rx.count = 0;
}
void mdb_uart_putc(char mode, char data)
{
loop_until_bit_is_set(UCSR1A, UDRE1);
if (mode) {
UCSR1B |= _BV(TXB81);
} else {
UCSR1B &= ~_BV(TXB81);
}
UDR1 = data;
}
/*
* return -1, means no data in queue
* otherwise, means a data
*/
signed short mdb_uart_deq()
{
unsigned short rev;
if (mdb_rx.count == 0) {
rev = -1; /* -1 means no data in queue */
} else {
rev = mdb_rx.queue[mdb_rx.out];
mdb_rx.out = (mdb_rx.out + 1) % MDB_RX_MAX;
mdb_rx.count--;
}
return rev;
}
ISR(USART1_RX_vect)
{
static unsigned char mode;
static unsigned short data;
mode = (UCSR1B & _BV(RXB81)) >> RXB81;
data = (mode << 8) | UDR1;
if (mdb_rx.count >= MDB_RX_MAX) { /* queue full! */
mdb_rx.queue[0] = data;
mdb_rx.in = 1;
mdb_rx.out = 0;
mdb_rx.count = 1;
} else { /* enqueue normally */
mdb_rx.queue[mdb_rx.in] = data;
mdb_rx.in = (mdb_rx.in + 1) % MDB_RX_MAX;
mdb_rx.count++;
}
}