1 | #include "UART.h"
|
2 |
|
3 |
|
4 | static volatile unsigned char UART_RxBuf[UART_RX_BUFFER_SIZE];
|
5 | static volatile unsigned char UART_RxHead;
|
6 | static volatile unsigned char UART_RxTail;
|
7 | static volatile unsigned char UART_LastRxError;
|
8 |
|
9 | static volatile unsigned char UART_TxBuf[UART_TX_BUFFER_SIZE];
|
10 | static volatile unsigned char UART_TxHead;
|
11 | static volatile unsigned char UART_TxTail;
|
12 |
|
13 | void sendchar(unsigned char c){
|
14 | while ( !( UCSR0A & (1<<UDRE0)) );
|
15 | UDR0 = c;
|
16 | }
|
17 |
|
18 | void sendUSART(char *s){
|
19 | while(*s){
|
20 | sendchar(*s);
|
21 | s++;
|
22 | }
|
23 | }
|
24 |
|
25 | void init_USART(){
|
26 | UART_TxHead = 0;
|
27 | UART_TxTail = 0;
|
28 | UART_RxHead = 0;
|
29 | UART_RxTail = 0;
|
30 |
|
31 | unsigned int ubrr = MYUBRR;
|
32 | /*Set baud rate */
|
33 | UBRR0H = (unsigned char)(ubrr>>8);
|
34 | UBRR0L = (unsigned char)ubrr;
|
35 | /*Enable receiver and transmitter */
|
36 | UCSR0B = (1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0);
|
37 | /* Set frame format: 8data, 2stop bit */
|
38 | UCSR0C = (1<<USBS0)|(3<<UCSZ00);
|
39 | }
|
40 |
|
41 | unsigned int uart_getc(void)
|
42 | {
|
43 | unsigned char tmptail;
|
44 | unsigned char data;
|
45 |
|
46 |
|
47 | if ( UART_RxHead == UART_RxTail ) {
|
48 | return UART_NO_DATA; /* no data available */
|
49 | }
|
50 |
|
51 | /* calculate /store buffer index */
|
52 | tmptail = (UART_RxTail + 1) & UART_RX_BUFFER_MASK;
|
53 | UART_RxTail = tmptail;
|
54 |
|
55 | /* get data from receive buffer */
|
56 | data = UART_RxBuf[tmptail];
|
57 |
|
58 | return (UART_LastRxError << 8) + data;
|
59 | }
|
60 |
|
61 | void uart_putc_buffert(unsigned char data)
|
62 | {
|
63 | unsigned char tmphead;
|
64 |
|
65 |
|
66 | tmphead = (UART_TxHead + 1) & UART_TX_BUFFER_MASK;
|
67 |
|
68 | while ( tmphead == UART_TxTail ){;}
|
69 |
|
70 | UART_TxBuf[tmphead] = data;
|
71 | UART_TxHead = tmphead;
|
72 |
|
73 | UCSR0B |= (1<<UDRIE0);
|
74 | }
|
75 |
|
76 |
|
77 | SIGNAL( USART_RX_vect )
|
78 | {
|
79 | unsigned char tmphead;
|
80 | unsigned char data;
|
81 | unsigned char usr;
|
82 | unsigned char lastRxError;
|
83 |
|
84 |
|
85 | /* read UART status register and UART data register */
|
86 | usr = UCSR0A;
|
87 | data = UDR0;
|
88 |
|
89 |
|
90 | lastRxError = (usr & (_BV(FE0)|_BV(DOR0)) );
|
91 |
|
92 | /* calculate buffer index */
|
93 | tmphead = ( UART_RxHead + 1) & UART_RX_BUFFER_MASK;
|
94 |
|
95 | if ( tmphead == UART_RxTail ) {
|
96 | /* error: receive buffer overflow */
|
97 | lastRxError = UART_BUFFER_OVERFLOW >> 8;
|
98 | }else{
|
99 | /* store new index */
|
100 | UART_RxHead = tmphead;
|
101 | /* store received data in buffer */
|
102 | UART_RxBuf[tmphead] = data;
|
103 | }
|
104 | UART_LastRxError = lastRxError;
|
105 | }
|
106 |
|
107 |
|
108 | SIGNAL( SIG_USART_DATA )
|
109 | {
|
110 | unsigned char tmptail;
|
111 |
|
112 |
|
113 | if ( UART_TxHead != UART_TxTail) {
|
114 | /* calculate and store new buffer index */
|
115 | tmptail = (UART_TxTail + 1) & UART_TX_BUFFER_MASK;
|
116 | UART_TxTail = tmptail;
|
117 | /* get one byte from buffer and write it to UART */
|
118 | UDR0 = UART_TxBuf[tmptail]; /* start transmission */
|
119 | }else{
|
120 | /* tx buffer empty, disable UDRE interrupt */
|
121 | UCSR0B &= ~(1<<UDRIE0);
|
122 | }
|
123 | }
|