Rev 142 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 142 | Rev 149 | ||
|---|---|---|---|
| Line 2... | Line 2... | ||
| 2 | Title: Interrupt UART library with receive/transmit circular buffers |
2 | Title: Interrupt UART library with receive/transmit circular buffers |
| 3 | Author: Peter Fleury <pfleury@gmx.ch> http://jump.to/fleury |
3 | Author: Peter Fleury <pfleury@gmx.ch> http://jump.to/fleury |
| 4 | File: |
4 | File: $Id: uart.c,v 1.5.2.10 2005/11/15 19:49:12 peter Exp $ |
| 5 | Software: AVR-GCC 3.3 |
5 | Software: AVR-GCC 3.3 |
| 6 | Hardware: any AVR with built-in UART, |
6 | Hardware: any AVR with built-in UART, |
| 7 | tested on AT90S8515 at 4 Mhz and ATmega at 1Mhz |
7 | tested on AT90S8515 at 4 Mhz and ATmega at 1Mhz |
| 8 | Extensions:- uart_puti by M. Thomas 9/2004 |
- | |
| 9 | - uart_puthex_nibble and _byte by M.T. 9/2004 |
- | |
| 10 | - uart_putbin_byte by M.T. 1/2005 |
- | |
| 11 | 8 | ||
| 12 | DESCRIPTION: |
9 | DESCRIPTION: |
| 13 | An interrupt is generated when the UART has finished transmitting or |
10 | An interrupt is generated when the UART has finished transmitting or |
| 14 | receiving a byte. The interrupt handling routines use circular buffers |
11 | receiving a byte. The interrupt handling routines use circular buffers |
| 15 | for buffering received and transmitted data. |
12 | for buffering received and transmitted data. |
| Line 24... | Line 21... | ||
| 24 | 21 | ||
| 25 | NOTES: |
22 | NOTES: |
| 26 | Based on Atmel Application Note AVR306 |
23 | Based on Atmel Application Note AVR306 |
| 27 | |
24 | |
| 28 | *************************************************************************/ |
25 | *************************************************************************/ |
| 29 | #include <stdlib.h> |
- | |
| 30 | #include <stdio.h> |
- | |
| 31 | - | ||
| 32 | #include <avr/io.h> |
26 | #include <avr/io.h> |
| 33 | #include <avr/interrupt.h> |
27 | #include <avr/interrupt.h> |
| 34 | #include <avr/pgmspace.h> |
28 | #include <avr/pgmspace.h> |
| 35 | - | ||
| 36 | #include |
29 | #include "uart.h" |
| 37 | - | ||
| 38 | 30 | ||
| 39 | static int uart_putchar(char c, FILE *stream); |
- | |
| 40 | static void Uart_InitHw(unsigned int baudrate); |
- | |
| 41 | - | ||
| 42 | static FILE myStdOut = FDEV_SETUP_STREAM(uart_putchar, NULL,_FDEV_SETUP_WRITE); |
- | |
| 43 | - | ||
| 44 | static int uart_putchar(char c, FILE *stream) { |
- | |
| 45 | if (c == '\n') uart_putchar('\r', stream); |
- | |
| 46 | uart_putc(c); |
- | |
| 47 | return 0; |
- | |
| 48 | } |
- | |
| 49 | - | ||
| 50 | 31 | ||
| 51 | /* |
32 | /* |
| 52 | * constants and macros |
33 | * constants and macros |
| 53 | */ |
34 | */ |
| 54 | 35 | ||
| Line 56... | Line 37... | ||
| 56 | #define UART_RX_BUFFER_MASK ( UART_RX_BUFFER_SIZE - 1) |
37 | #define UART_RX_BUFFER_MASK ( UART_RX_BUFFER_SIZE - 1) |
| 57 | #define UART_TX_BUFFER_MASK ( UART_TX_BUFFER_SIZE - 1) |
38 | #define UART_TX_BUFFER_MASK ( UART_TX_BUFFER_SIZE - 1) |
| 58 | 39 | ||
| 59 | #if ( UART_RX_BUFFER_SIZE & UART_RX_BUFFER_MASK ) |
40 | #if ( UART_RX_BUFFER_SIZE & UART_RX_BUFFER_MASK ) |
| 60 | #error RX buffer size is not a power of 2 |
41 | #error RX buffer size is not a power of 2 |
| 61 | #endif |
42 | #endif |
| 62 | #if ( UART_TX_BUFFER_SIZE & UART_TX_BUFFER_MASK ) |
43 | #if ( UART_TX_BUFFER_SIZE & UART_TX_BUFFER_MASK ) |
| 63 | #error TX buffer size is not a power of 2 |
44 | #error TX buffer size is not a power of 2 |
| 64 | #endif |
45 | #endif |
| 65 | 46 | ||
| 66 | #if defined(__AVR_AT90S2313__) \ |
47 | #if defined(__AVR_AT90S2313__) \ |
| 67 | || defined(__AVR_AT90S4414__) || defined(__AVR_AT90S4434__) \ |
48 | || defined(__AVR_AT90S4414__) || defined(__AVR_AT90S4434__) \ |
| 68 | || defined(__AVR_AT90S8515__) || defined(__AVR_AT90S8535__) |
49 | || defined(__AVR_AT90S8515__) || defined(__AVR_AT90S8535__) \ |
| - | 50 | || defined(__AVR_ATmega103__) |
|
| 69 | /* old AVR classic with one UART */ |
51 | /* old AVR classic or ATmega103 with one UART */ |
| 70 | #define AT90_UART |
52 | #define AT90_UART |
| 71 | #define UART0_RECEIVE_INTERRUPT SIG_UART_RECV |
53 | #define UART0_RECEIVE_INTERRUPT SIG_UART_RECV |
| 72 | #define UART0_TRANSMIT_INTERRUPT SIG_UART_DATA |
54 | #define UART0_TRANSMIT_INTERRUPT SIG_UART_DATA |
| 73 | #define UART0_STATUS USR |
55 | #define UART0_STATUS USR |
| 74 | #define UART0_CONTROL UCR |
56 | #define UART0_CONTROL UCR |
| 75 | #define UART0_DATA UDR |
57 | #define UART0_DATA UDR |
| 76 | #define UART0_UDRIE UDRIE |
58 | #define UART0_UDRIE UDRIE |
| 77 | #elif defined(__AVR_AT90S2333__) || defined(__AVR_AT90S4433__) |
59 | #elif defined(__AVR_AT90S2333__) || defined(__AVR_AT90S4433__) |
| 78 | /* old AVR classic with one UART */ |
60 | /* old AVR classic with one UART */ |
| 79 | #define AT90_UART |
61 | #define AT90_UART |
| 80 | #define UART0_RECEIVE_INTERRUPT SIG_UART_RECV |
62 | #define UART0_RECEIVE_INTERRUPT SIG_UART_RECV |
| 81 | #define UART0_TRANSMIT_INTERRUPT SIG_UART_DATA |
63 | #define UART0_TRANSMIT_INTERRUPT SIG_UART_DATA |
| 82 | #define UART0_STATUS UCSRA |
64 | #define UART0_STATUS UCSRA |
| 83 | #define UART0_CONTROL UCSRB |
65 | #define UART0_CONTROL UCSRB |
| 84 | #define UART0_DATA UDR |
66 | #define UART0_DATA UDR |
| 85 | #define UART0_UDRIE UDRIE |
67 | #define UART0_UDRIE UDRIE |
| 86 | #elif defined(__AVR_ATmega8__) || defined(__AVR_ATmega16__) || defined(__AVR_ATmega32__) \ |
68 | #elif defined(__AVR_ATmega8__) || defined(__AVR_ATmega16__) || defined(__AVR_ATmega32__) \ |
| 87 | || defined(__AVR_ATmega8515__) || defined(__AVR_ATmega8535__) \ |
69 | || defined(__AVR_ATmega8515__) || defined(__AVR_ATmega8535__) \ |
| 88 | || defined(__AVR_ATmega323__) |
70 | || defined(__AVR_ATmega323__) |
| 89 | /* |
71 | /* ATmega with one USART */ |
| 90 | #define ATMEGA_USART |
72 | #define ATMEGA_USART |
| 91 | #define UART0_RECEIVE_INTERRUPT SIG_UART_RECV |
73 | #define UART0_RECEIVE_INTERRUPT SIG_UART_RECV |
| 92 | #define UART0_TRANSMIT_INTERRUPT SIG_UART_DATA |
74 | #define UART0_TRANSMIT_INTERRUPT SIG_UART_DATA |
| 93 | #define UART0_STATUS UCSRA |
75 | #define UART0_STATUS UCSRA |
| 94 | #define UART0_CONTROL UCSRB |
76 | #define UART0_CONTROL UCSRB |
| 95 | #define UART0_DATA UDR |
77 | #define UART0_DATA UDR |
| 96 | #define UART0_UDRIE UDRIE |
78 | #define UART0_UDRIE UDRIE |
| 97 | #elif defined(__AVR_ATmega163__) |
79 | #elif defined(__AVR_ATmega163__) |
| 98 | /* |
80 | /* ATmega163 with one UART */ |
| 99 | #define ATMEGA_UART |
81 | #define ATMEGA_UART |
| 100 | #define UART0_RECEIVE_INTERRUPT SIG_UART_RECV |
82 | #define UART0_RECEIVE_INTERRUPT SIG_UART_RECV |
| 101 | #define UART0_TRANSMIT_INTERRUPT SIG_UART_DATA |
83 | #define UART0_TRANSMIT_INTERRUPT SIG_UART_DATA |
| 102 | #define UART0_STATUS UCSRA |
84 | #define UART0_STATUS UCSRA |
| 103 | #define UART0_CONTROL UCSRB |
85 | #define UART0_CONTROL UCSRB |
| 104 | #define UART0_DATA UDR |
86 | #define UART0_DATA UDR |
| 105 | #define UART0_UDRIE UDRIE |
87 | #define UART0_UDRIE UDRIE |
| 106 | #elif defined(__AVR_ATmega162__) |
88 | #elif defined(__AVR_ATmega162__) |
| 107 | /* |
89 | /* ATmega with two USART */ |
| 108 | #define ATMEGA_USART0 |
90 | #define ATMEGA_USART0 |
| 109 | #define ATMEGA_USART1 |
91 | #define ATMEGA_USART1 |
| 110 | #define UART0_RECEIVE_INTERRUPT SIG_USART0_RECV |
92 | #define UART0_RECEIVE_INTERRUPT SIG_USART0_RECV |
| 111 | #define UART1_RECEIVE_INTERRUPT SIG_USART1_RECV |
93 | #define UART1_RECEIVE_INTERRUPT SIG_USART1_RECV |
| 112 | #define UART0_TRANSMIT_INTERRUPT SIG_USART0_DATA |
94 | #define UART0_TRANSMIT_INTERRUPT SIG_USART0_DATA |
| 113 | #define UART1_TRANSMIT_INTERRUPT SIG_USART1_DATA |
95 | #define UART1_TRANSMIT_INTERRUPT SIG_USART1_DATA |
| Line 118... | Line 100... | ||
| 118 | #define UART1_STATUS UCSR1A |
100 | #define UART1_STATUS UCSR1A |
| 119 | #define UART1_CONTROL UCSR1B |
101 | #define UART1_CONTROL UCSR1B |
| 120 | #define UART1_DATA UDR1 |
102 | #define UART1_DATA UDR1 |
| 121 | #define UART1_UDRIE UDRIE1 |
103 | #define UART1_UDRIE UDRIE1 |
| 122 | #elif defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__) |
104 | #elif defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__) |
| 123 | /* |
105 | /* ATmega with two USART */ |
| 124 | #define ATMEGA_USART0 |
106 | #define ATMEGA_USART0 |
| 125 | #define ATMEGA_USART1 |
107 | #define ATMEGA_USART1 |
| 126 | #define UART0_RECEIVE_INTERRUPT SIG_UART0_RECV |
108 | #define UART0_RECEIVE_INTERRUPT SIG_UART0_RECV |
| 127 | #define UART1_RECEIVE_INTERRUPT SIG_UART1_RECV |
109 | #define UART1_RECEIVE_INTERRUPT SIG_UART1_RECV |
| 128 | #define UART0_TRANSMIT_INTERRUPT SIG_UART0_DATA |
110 | #define UART0_TRANSMIT_INTERRUPT SIG_UART0_DATA |
| 129 | #define UART1_TRANSMIT_INTERRUPT SIG_UART1_DATA |
111 | #define UART1_TRANSMIT_INTERRUPT SIG_UART1_DATA |
| 130 | #define UART0_STATUS UCSR0A |
112 | #define UART0_STATUS UCSR0A |
| 131 | #define UART0_CONTROL UCSR0B |
113 | #define UART0_CONTROL UCSR0B |
| 132 | #define UART0_DATA UDR0 |
114 | #define UART0_DATA UDR0 |
| 133 | #define UART0_UDRIE UDRIE0 |
115 | #define UART0_UDRIE UDRIE0 |
| 134 | #define UART1_STATUS UCSR1A |
116 | #define UART1_STATUS UCSR1A |
| 135 | #define UART1_CONTROL UCSR1B |
117 | #define UART1_CONTROL UCSR1B |
| 136 | #define UART1_DATA UDR1 |
118 | #define UART1_DATA UDR1 |
| 137 | #define UART1_UDRIE UDRIE1 |
119 | #define UART1_UDRIE UDRIE1 |
| 138 | #elif defined(__AVR_ATmega103__) |
- | |
| 139 | /* ATMega with one UART */ |
- | |
| 140 | #error "AVR ATmega103 currently not supported by this libaray !" |
- | |
| 141 | #elif defined(__AVR_ATmega161__) |
120 | #elif defined(__AVR_ATmega161__) |
| 142 | /* ATmega with UART */ |
121 | /* ATmega with UART */ |
| 143 | #error "AVR ATmega161 currently not supported by this libaray !" |
122 | #error "AVR ATmega161 currently not supported by this libaray !" |
| 144 | #elif defined(__AVR_ATmega169__) |
123 | #elif defined(__AVR_ATmega169__) |
| - | 124 | /* ATmega with one USART */ |
|
| - | 125 | #define ATMEGA_USART |
|
| - | 126 | #define UART0_RECEIVE_INTERRUPT SIG_USART_RECV |
|
| - | 127 | #define UART0_TRANSMIT_INTERRUPT SIG_USART_DATA |
|
| - | 128 | #define UART0_STATUS UCSRA |
|
| - | 129 | #define UART0_CONTROL UCSRB |
|
| - | 130 | #define UART0_DATA UDR |
|
| - | 131 | #define UART0_UDRIE UDRIE |
|
| - | 132 | #elif defined(__AVR_ATmega48__) ||defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__) |
|
| - | 133 | #define ATMEGA_USART0 |
|
| - | 134 | #define UART0_RECEIVE_INTERRUPT SIG_USART_RECV |
|
| - | 135 | #define UART0_TRANSMIT_INTERRUPT SIG_USART_DATA |
|
| - | 136 | #define UART0_STATUS UCSR0A |
|
| - | 137 | #define UART0_CONTROL UCSR0B |
|
| - | 138 | #define UART0_DATA UDR0 |
|
| - | 139 | #define UART0_UDRIE UDRIE0 |
|
| - | 140 | #elif defined(__AVR_ATtiny2313__) |
|
| - | 141 | #define ATMEGA_USART |
|
| - | 142 | #define UART0_RECEIVE_INTERRUPT SIG_USART0_RX |
|
| - | 143 | #define UART0_TRANSMIT_INTERRUPT SIG_USART0_UDRE |
|
| - | 144 | #define UART0_STATUS UCSRA |
|
| - | 145 | #define UART0_CONTROL UCSRB |
|
| - | 146 | #define UART0_DATA UDR |
|
| - | 147 | #define UART0_UDRIE UDRIE |
|
| - | 148 | #else |
|
| 145 | #error " |
149 | #error "no UART definition for MCU available" |
| 146 | #endif |
150 | #endif |
| 147 | 151 | ||
| 148 | 152 | ||
| 149 | /* |
153 | /* |
| 150 | * module global variables |
154 | * module global variables |
| 151 | */ |
155 | */ |
| Line 154... | Line 158... | ||
| 154 | static volatile unsigned char UART_TxHead; |
158 | static volatile unsigned char UART_TxHead; |
| 155 | static volatile unsigned char UART_TxTail; |
159 | static volatile unsigned char UART_TxTail; |
| 156 | static volatile unsigned char UART_RxHead; |
160 | static volatile unsigned char UART_RxHead; |
| 157 | static volatile unsigned char UART_RxTail; |
161 | static volatile unsigned char UART_RxTail; |
| 158 | static volatile unsigned char UART_LastRxError; |
162 | static volatile unsigned char UART_LastRxError; |
| - | 163 | ||
| - | 164 | #if defined( ATMEGA_USART1 ) |
|
| - | 165 | static volatile unsigned char UART1_TxBuf[UART_TX_BUFFER_SIZE]; |
|
| - | 166 | static volatile unsigned char UART1_RxBuf[UART_RX_BUFFER_SIZE]; |
|
| - | 167 | static volatile unsigned char UART1_TxHead; |
|
| - | 168 | static volatile unsigned char UART1_TxTail; |
|
| - | 169 | static volatile unsigned char UART1_RxHead; |
|
| - | 170 | static volatile unsigned char UART1_RxTail; |
|
| - | 171 | static volatile unsigned char UART1_LastRxError; |
|
| - | 172 | #endif |
|
| 159 | 173 | ||
| 160 | 174 | ||
| 161 | 175 | ||
| 162 |
|
176 | SIGNAL(UART0_RECEIVE_INTERRUPT) |
| 163 | /************************************************************************* |
177 | /************************************************************************* |
| 164 | Function: UART Receive Complete interrupt |
178 | Function: UART Receive Complete interrupt |
| 165 | Purpose: called when the UART has received a character |
179 | Purpose: called when the UART has received a character |
| 166 | **************************************************************************/ |
180 | **************************************************************************/ |
| 167 | { |
181 | { |
| Line 172... | Line 186... | ||
| 172 | 186 | ||
| 173 | 187 | ||
| 174 | /* read UART status register and UART data register */ |
188 | /* read UART status register and UART data register */ |
| 175 | usr = UART0_STATUS; |
189 | usr = UART0_STATUS; |
| 176 | data = UART0_DATA; |
190 | data = UART0_DATA; |
| 177 | 191 | ||
| 178 | /* */ |
192 | /* */ |
| 179 | #if defined( AT90_UART ) |
193 | #if defined( AT90_UART ) |
| 180 | lastRxError = (usr & (_BV(FE)|_BV(DOR)) ); |
194 | lastRxError = (usr & (_BV(FE)|_BV(DOR)) ); |
| 181 | #elif defined( ATMEGA_USART ) |
195 | #elif defined( ATMEGA_USART ) |
| 182 | lastRxError = (usr & (_BV(FE)|_BV(DOR)) ); |
196 | lastRxError = (usr & (_BV(FE)|_BV(DOR)) ); |
| 183 | #elif defined( ATMEGA_USART0 ) |
197 | #elif defined( ATMEGA_USART0 ) |
| 184 | lastRxError = (usr & (_BV(FE0)|_BV(DOR0)) ); |
198 | lastRxError = (usr & (_BV(FE0)|_BV(DOR0)) ); |
| 185 | #elif defined ( ATMEGA_UART ) |
199 | #elif defined ( ATMEGA_UART ) |
| 186 | lastRxError = (usr & (_BV(FE)|_BV(DOR)) ); |
200 | lastRxError = (usr & (_BV(FE)|_BV(DOR)) ); |
| 187 | #endif |
201 | #endif |
| 188 | 202 | ||
| 189 | /* calculate buffer index */ |
203 | /* calculate buffer index */ |
| 190 | tmphead = ( UART_RxHead + 1) & UART_RX_BUFFER_MASK; |
204 | tmphead = ( UART_RxHead + 1) & UART_RX_BUFFER_MASK; |
| 191 | 205 | ||
| 192 | if ( tmphead == UART_RxTail ) { |
206 | if ( tmphead == UART_RxTail ) { |
| 193 | /* error: receive buffer overflow */ |
207 | /* error: receive buffer overflow */ |
| 194 | lastRxError = UART_BUFFER_OVERFLOW >> 8; |
208 | lastRxError = UART_BUFFER_OVERFLOW >> 8; |
| 195 | }else{ |
209 | }else{ |
| 196 | /* store new index */ |
210 | /* store new index */ |
| Line 200... | Line 214... | ||
| 200 | } |
214 | } |
| 201 | UART_LastRxError = lastRxError; |
215 | UART_LastRxError = lastRxError; |
| 202 | } |
216 | } |
| 203 | 217 | ||
| 204 | 218 | ||
| 205 |
|
219 | SIGNAL(UART0_TRANSMIT_INTERRUPT) |
| 206 | /************************************************************************* |
220 | /************************************************************************* |
| 207 | Function: UART Data Register Empty interrupt |
221 | Function: UART Data Register Empty interrupt |
| 208 | Purpose: called when the UART is ready to transmit the next byte |
222 | Purpose: called when the UART is ready to transmit the next byte |
| 209 | **************************************************************************/ |
223 | **************************************************************************/ |
| 210 | { |
224 | { |
| Line 218... | Line 232... | ||
| 218 | /* get one byte from buffer and write it to UART */ |
232 | /* get one byte from buffer and write it to UART */ |
| 219 | UART0_DATA = UART_TxBuf[tmptail]; /* start transmission */ |
233 | UART0_DATA = UART_TxBuf[tmptail]; /* start transmission */ |
| 220 | }else{ |
234 | }else{ |
| 221 | /* tx buffer empty, disable UDRE interrupt */ |
235 | /* tx buffer empty, disable UDRE interrupt */ |
| 222 | UART0_CONTROL &= ~_BV(UART0_UDRIE); |
236 | UART0_CONTROL &= ~_BV(UART0_UDRIE); |
| 223 | } |
237 | } |
| 224 | } |
- | |
| 225 | - | ||
| 226 | - | ||
| 227 | - | ||
| 228 | /** |
- | |
| 229 | * Wrapper function for uart_init. |
- | |
| 230 | */ |
- | |
| 231 | void Uart_Init() { |
- | |
| 232 | Uart_InitHw((UART_BAUD_SELECT((BAUD),F_OSC))); |
- | |
| 233 | stdout = &myStdOut; |
- | |
| 234 | } |
238 | } |
| 235 | 239 | ||
| 236 | 240 | ||
| 237 | /************************************************************************* |
241 | /************************************************************************* |
| 238 | Function: uart_init() |
242 | Function: uart_init() |
| 239 | Purpose: initialize UART and set baudrate |
243 | Purpose: initialize UART and set baudrate |
| 240 | Input: baudrate using macro UART_BAUD_SELECT() |
244 | Input: baudrate using macro UART_BAUD_SELECT() |
| 241 | Returns: none |
245 | Returns: none |
| 242 | **************************************************************************/ |
246 | **************************************************************************/ |
| 243 |
|
247 | void uart_init(unsigned int baudrate) |
| 244 | { |
248 | { |
| 245 | UART_TxHead = 0; |
249 | UART_TxHead = 0; |
| 246 | UART_TxTail = 0; |
250 | UART_TxTail = 0; |
| 247 | UART_RxHead = 0; |
251 | UART_RxHead = 0; |
| 248 | UART_RxTail = 0; |
252 | UART_RxTail = 0; |
| Line 250... | Line 254... | ||
| 250 | #if defined( AT90_UART ) |
254 | #if defined( AT90_UART ) |
| 251 | /* set baud rate */ |
255 | /* set baud rate */ |
| 252 | UBRR = (unsigned char)baudrate; |
256 | UBRR = (unsigned char)baudrate; |
| 253 | 257 | ||
| 254 | /* enable UART receiver and transmmitter and receive complete interrupt */ |
258 | /* enable UART receiver and transmmitter and receive complete interrupt */ |
| 255 | UART0_CONTROL = _BV(RXCIE)|_BV(RXEN)| |
259 | UART0_CONTROL = _BV(RXCIE)|_BV(RXEN)|_BV(TXEN); |
| 256 | 260 | ||
| 257 | #elif defined (ATMEGA_USART) |
261 | #elif defined (ATMEGA_USART) |
| 258 | /* Set baud rate */ |
262 | /* Set baud rate */ |
| - | 263 | if ( baudrate & 0x8000 ) |
|
| - | 264 | { |
|
| - | 265 | UART0_STATUS = (1<<U2X); //Enable 2x speed |
|
| - | 266 | baudrate &= ~0x8000; |
|
| - | 267 | } |
|
| 259 | UBRRH = (unsigned char)(baudrate>>8); |
268 | UBRRH = (unsigned char)(baudrate>>8); |
| 260 | UBRRL = (unsigned char) baudrate; |
269 | UBRRL = (unsigned char) baudrate; |
| 261 | 270 | ||
| 262 | /* Enable USART receiver and transmitter and receive complete interrupt */ |
271 | /* Enable USART receiver and transmitter and receive complete interrupt */ |
| 263 | UART0_CONTROL = _BV(RXCIE)|(1<<RXEN)|(1<<TXEN); |
272 | UART0_CONTROL = _BV(RXCIE)|(1<<RXEN)|(1<<TXEN); |
| 264 | 273 | ||
| 265 | /* Set frame format: asynchronous, 8data, no parity, 1stop bit */ |
274 | /* Set frame format: asynchronous, 8data, no parity, 1stop bit */ |
| 266 | #ifdef URSEL |
275 | #ifdef URSEL |
| Line 269... | Line 278... | ||
| 269 | UCSRC = (3<<UCSZ0); |
278 | UCSRC = (3<<UCSZ0); |
| 270 | #endif |
279 | #endif |
| 271 | 280 | ||
| 272 | #elif defined (ATMEGA_USART0 ) |
281 | #elif defined (ATMEGA_USART0 ) |
| 273 | /* Set baud rate */ |
282 | /* Set baud rate */ |
| - | 283 | if ( baudrate & 0x8000 ) |
|
| - | 284 | { |
|
| - | 285 | UART0_STATUS = (1<<U2X0); //Enable 2x speed |
|
| - | 286 | baudrate &= ~0x8000; |
|
| - | 287 | } |
|
| 274 | UBRR0H = (unsigned char)(baudrate>>8); |
288 | UBRR0H = (unsigned char)(baudrate>>8); |
| 275 | UBRR0L = (unsigned char) baudrate; |
289 | UBRR0L = (unsigned char) baudrate; |
| 276 | 290 | ||
| 277 | /* Enable USART receiver and transmitter and receive complete interrupt */ |
291 | /* Enable USART receiver and transmitter and receive complete interrupt */ |
| 278 | UART0_CONTROL = _BV(RXCIE0)|(1<<RXEN0)|(1<<TXEN0); |
292 | UART0_CONTROL = _BV(RXCIE0)|(1<<RXEN0)|(1<<TXEN0); |
| 279 | 293 | ||
| 280 | /* Set frame format: asynchronous, 8data, no parity, 1stop bit */ |
294 | /* Set frame format: asynchronous, 8data, no parity, 1stop bit */ |
| 281 | #ifdef URSEL0 |
295 | #ifdef URSEL0 |
| 282 | UCSR0C = (1<<URSEL0)|(3<<UCSZ00); |
296 | UCSR0C = (1<<URSEL0)|(3<<UCSZ00); |
| 283 | #else |
297 | #else |
| 284 | UCSR0C = (3<<UCSZ00); |
298 | UCSR0C = (3<<UCSZ00); |
| 285 | #endif |
299 | #endif |
| 286 | 300 | ||
| 287 | #elif defined ( ATMEGA_UART ) |
301 | #elif defined ( ATMEGA_UART ) |
| 288 | /* set baud rate */ |
302 | /* set baud rate */ |
| - | 303 | if ( baudrate & 0x8000 ) |
|
| - | 304 | { |
|
| - | 305 | UART0_STATUS = (1<<U2X); //Enable 2x speed |
|
| - | 306 | baudrate &= ~0x8000; |
|
| - | 307 | } |
|
| 289 | UBRRHI = (unsigned char)(baudrate>>8); |
308 | UBRRHI = (unsigned char)(baudrate>>8); |
| 290 | UBRR = (unsigned char) baudrate; |
309 | UBRR = (unsigned char) baudrate; |
| 291 | 310 | ||
| 292 | /* Enable UART receiver and transmitter and receive complete interrupt */ |
311 | /* Enable UART receiver and transmitter and receive complete interrupt */ |
| 293 | UART0_CONTROL = _BV(RXCIE)|(1<<RXEN)|(1<<TXEN); |
312 | UART0_CONTROL = _BV(RXCIE)|(1<<RXEN)|(1<<TXEN); |
| Line 360... | Line 379... | ||
| 360 | void uart_puts(const char *s ) |
379 | void uart_puts(const char *s ) |
| 361 | { |
380 | { |
| 362 | while (*s) |
381 | while (*s) |
| 363 | uart_putc(*s++); |
382 | uart_putc(*s++); |
| 364 | 383 | ||
| 365 | }/* uart_puts */ |
384 | }/* uart_puts */ |
| 366 | 385 | ||
| 367 | 386 | ||
| 368 | /************************************************************************* |
387 | /************************************************************************* |
| 369 | Function: uart_puts_p() |
388 | Function: uart_puts_p() |
| 370 | Purpose: transmit string from program memory to UART |
389 | Purpose: transmit string from program memory to UART |
| 371 | Input: program memory string to be transmitted |
390 | Input: program memory string to be transmitted |
| 372 | Returns: none |
391 | Returns: none |
| 373 | **************************************************************************/ |
392 | **************************************************************************/ |
| 374 | void uart_puts_p(const char *progmem_s ) |
393 | void uart_puts_p(const char *progmem_s ) |
| 375 | { |
394 | { |
| 376 | register char c; |
395 | register char c; |
| 377 | 396 | ||
| 378 | while ( (c = pgm_read_byte(progmem_s++)) ) |
397 | while ( (c = pgm_read_byte(progmem_s++)) ) |
| 379 | uart_putc(c); |
398 | uart_putc(c); |
| 380 | 399 | ||
| 381 | }/* uart_puts_p */ |
400 | }/* uart_puts_p */ |
| - | 401 | ||
| - | 402 | ||
| - | 403 | /* |
|
| - | 404 | * these functions are only for ATmegas with two USART |
|
| - | 405 | */ |
|
| - | 406 | #if defined( ATMEGA_USART1 ) |
|
| - | 407 | ||
| - | 408 | SIGNAL(UART1_RECEIVE_INTERRUPT) |
|
| - | 409 | /************************************************************************* |
|
| - | 410 | Function: UART1 Receive Complete interrupt |
|
| - | 411 | Purpose: called when the UART1 has received a character |
|
| - | 412 | **************************************************************************/ |
|
| - | 413 | { |
|
| - | 414 | unsigned char tmphead; |
|
| - | 415 | unsigned char data; |
|
| - | 416 | unsigned char usr; |
|
| - | 417 | unsigned char lastRxError; |
|
| - | 418 | ||
| - | 419 | ||
| - | 420 | /* read UART status register and UART data register */ |
|
| - | 421 | usr = UART1_STATUS; |
|
| - | 422 | data = UART1_DATA; |
|
| - | 423 | ||
| - | 424 | /* */ |
|
| - | 425 | lastRxError = (usr & (_BV(FE1)|_BV(DOR1)) ); |
|
| - | 426 | ||
| - | 427 | /* calculate buffer index */ |
|
| - | 428 | tmphead = ( UART1_RxHead + 1) & UART_RX_BUFFER_MASK; |
|
| - | 429 | ||
| - | 430 | if ( tmphead == UART1_RxTail ) { |
|
| - | 431 | /* error: receive buffer overflow */ |
|
| - | 432 | lastRxError = UART_BUFFER_OVERFLOW >> 8; |
|
| - | 433 | }else{ |
|
| - | 434 | /* store new index */ |
|
| - | 435 | UART1_RxHead = tmphead; |
|
| - | 436 | /* store received data in buffer */ |
|
| - | 437 | UART1_RxBuf[tmphead] = data; |
|
| - | 438 | } |
|
| - | 439 | UART1_LastRxError = lastRxError; |
|
| - | 440 | } |
|
| - | 441 | ||
| - | 442 | ||
| - | 443 | SIGNAL(UART1_TRANSMIT_INTERRUPT) |
|
| - | 444 | /************************************************************************* |
|
| - | 445 | Function: UART1 Data Register Empty interrupt |
|
| - | 446 | Purpose: called when the UART1 is ready to transmit the next byte |
|
| - | 447 | **************************************************************************/ |
|
| - | 448 | { |
|
| - | 449 | unsigned char tmptail; |
|
| - | 450 | ||
| - | 451 | ||
| - | 452 | if ( UART1_TxHead != UART1_TxTail) { |
|
| - | 453 | /* calculate and store new buffer index */ |
|
| - | 454 | tmptail = (UART1_TxTail + 1) & UART_TX_BUFFER_MASK; |
|
| - | 455 | UART1_TxTail = tmptail; |
|
| - | 456 | /* get one byte from buffer and write it to UART */ |
|
| - | 457 | UART1_DATA = UART1_TxBuf[tmptail]; /* start transmission */ |
|
| - | 458 | }else{ |
|
| - | 459 | /* tx buffer empty, disable UDRE interrupt */ |
|
| - | 460 | UART1_CONTROL &= ~_BV(UART1_UDRIE); |
|
| - | 461 | } |
|
| - | 462 | } |
|
| - | 463 | ||
| - | 464 | ||
| - | 465 | /************************************************************************* |
|
| - | 466 | Function: uart1_init() |
|
| - | 467 | Purpose: initialize UART1 and set baudrate |
|
| - | 468 | Input: baudrate using macro UART_BAUD_SELECT() |
|
| - | 469 | Returns: none |
|
| - | 470 | **************************************************************************/ |
|
| - | 471 | void uart1_init(unsigned int baudrate) |
|
| - | 472 | { |
|
| - | 473 | UART1_TxHead = 0; |
|
| - | 474 | UART1_TxTail = 0; |
|
| - | 475 | UART1_RxHead = 0; |
|
| - | 476 | UART1_RxTail = 0; |
|
| - | 477 | ||
| - | 478 | ||
| - | 479 | /* Set baud rate */ |
|
| - | 480 | if ( baudrate & 0x8000 ) |
|
| - | 481 | { |
|
| - | 482 | UART1_STATUS = (1<<U2X1); //Enable 2x speed |
|
| - | 483 | baudrate &= ~0x8000; |
|
| - | 484 | } |
|
| - | 485 | UBRR1H = (unsigned char)(baudrate>>8); |
|
| - | 486 | UBRR1L = (unsigned char) baudrate; |
|
| - | 487 | ||
| - | 488 | /* Enable USART receiver and transmitter and receive complete interrupt */ |
|
| - | 489 | UART1_CONTROL = _BV(RXCIE1)|(1<<RXEN1)|(1<<TXEN1); |
|
| - | 490 | ||
| - | 491 | /* Set frame format: asynchronous, 8data, no parity, 1stop bit */ |
|
| - | 492 | #ifdef URSEL1 |
|
| - | 493 | UCSR1C = (1<<URSEL1)|(3<<UCSZ10); |
|
| - | 494 | #else |
|
| - | 495 | UCSR1C = (3<<UCSZ10); |
|
| - | 496 | #endif |
|
| - | 497 | }/* uart_init */ |
|
| - | 498 | ||
| - | 499 | ||
| - | 500 | /************************************************************************* |
|
| - | 501 | Function: uart1_getc() |
|
| - | 502 | Purpose: return byte from ringbuffer |
|
| - | 503 | Returns: lower byte: received byte from ringbuffer |
|
| - | 504 | higher byte: last receive error |
|
| - | 505 | **************************************************************************/ |
|
| - | 506 | unsigned int uart1_getc(void) |
|
| - | 507 | { |
|
| - | 508 | unsigned char tmptail; |
|
| - | 509 | unsigned char data; |
|
| - | 510 | ||
| - | 511 | ||
| - | 512 | if ( UART1_RxHead == UART1_RxTail ) { |
|
| - | 513 | return UART_NO_DATA; /* no data available */ |
|
| - | 514 | } |
|
| - | 515 | ||
| - | 516 | /* calculate /store buffer index */ |
|
| - | 517 | tmptail = (UART1_RxTail + 1) & UART_RX_BUFFER_MASK; |
|
| - | 518 | UART1_RxTail = tmptail; |
|
| - | 519 | ||
| - | 520 | /* get data from receive buffer */ |
|
| - | 521 | data = UART1_RxBuf[tmptail]; |
|
| - | 522 | ||
| - | 523 | return (UART1_LastRxError << 8) + data; |
|
| - | 524 | ||
| - | 525 | }/* uart1_getc */ |
|
| - | 526 | ||
| - | 527 | ||
| - | 528 | /************************************************************************* |
|
| - | 529 | Function: uart1_putc() |
|
| - | 530 | Purpose: write byte to ringbuffer for transmitting via UART |
|
| - | 531 | Input: byte to be transmitted |
|
| - | 532 | Returns: none |
|
| - | 533 | **************************************************************************/ |
|
| - | 534 | void uart1_putc(unsigned char data) |
|
| - | 535 | { |
|
| - | 536 | unsigned char tmphead; |
|
| - | 537 | ||
| - | 538 | ||
| - | 539 | tmphead = (UART1_TxHead + 1) & UART_TX_BUFFER_MASK; |
|
| - | 540 | ||
| - | 541 | while ( tmphead == UART1_TxTail ){ |
|
| - | 542 | ;/* wait for free space in buffer */ |
|
| - | 543 | } |
|
| - | 544 | ||
| - | 545 | UART1_TxBuf[tmphead] = data; |
|
| - | 546 | UART1_TxHead = tmphead; |
|
| - | 547 | ||
| - | 548 | /* enable UDRE interrupt */ |
|
| - | 549 | UART1_CONTROL |= _BV(UART1_UDRIE); |
|
| - | 550 | ||
| - | 551 | }/* uart1_putc */ |
|
| - | 552 | ||
| - | 553 | ||
| - | 554 | /************************************************************************* |
|
| - | 555 | Function: uart1_puts() |
|
| - | 556 | Purpose: transmit string to UART1 |
|
| - | 557 | Input: string to be transmitted |
|
| - | 558 | Returns: none |
|
| - | 559 | **************************************************************************/ |
|
| - | 560 | void uart1_puts(const char *s ) |
|
| - | 561 | { |
|
| - | 562 | while (*s) |
|
| - | 563 | uart1_putc(*s++); |
|
| - | 564 | ||
| - | 565 | }/* uart1_puts */ |
|
| - | 566 | ||
| 382 | 567 | ||
| 383 | /************************************************************************* |
568 | /************************************************************************* |
| 384 | Function: |
569 | Function: uart1_puts_p() |
| 385 | Purpose: transmit |
570 | Purpose: transmit string from program memory to UART1 |
| 386 | Input: |
571 | Input: program memory string to be transmitted |
| 387 | Returns: none |
572 | Returns: none |
| 388 | This functions has been added by Martin Thomas <eversmith@heizung-thomas.de> |
- | |
| 389 | Don't blame P. Fleury if it doesn't work ;-) |
- | |
| 390 | **************************************************************************/ |
573 | **************************************************************************/ |
| 391 | void |
574 | void uart1_puts_p(const char *progmem_s ) |
| 392 | { |
575 | { |
| - | 576 | register char c; |
|
| - | 577 | ||
| - | 578 | while ( (c = pgm_read_byte(progmem_s++)) ) |
|
| 393 |
|
579 | uart1_putc(c); |
| 394 | 580 | ||
| 395 |
|
581 | }/* uart1_puts_p */ |
| 396 | 582 | ||
| 397 | }/* uart_puti */ |
- | |
| 398 | - | ||
| 399 | /************************************************************************* |
- | |
| 400 | Function: uart_puthex_nibble() |
- | |
| 401 | Purpose: transmit lower nibble as ASCII-hex to UART |
- | |
| 402 | Input: byte value |
- | |
| 403 | Returns: none |
- | |
| 404 | This functions has been added by Martin Thomas <eversmith@heizung-thomas.de> |
- | |
| 405 | Don't blame P. Fleury if it doesn't work ;-) |
- | |
| 406 | **************************************************************************/ |
- | |
| 407 | void uart_puthex_nibble(const unsigned char b) |
- | |
| 408 | { |
- | |
| 409 | unsigned char c = b & 0x0f; |
- | |
| 410 | if (c>9) c += 'A'-10; |
- | |
| 411 | else c += '0'; |
- | |
| 412 | uart_putc(c); |
- | |
| 413 | } /* uart_puthex_nibble */ |
- | |
| 414 | - | ||
| 415 | /************************************************************************* |
- | |
| 416 | Function: uart_puthex_byte() |
- | |
| 417 | Purpose: transmit upper and lower nibble as ASCII-hex to UART |
- | |
| 418 | Input: byte value |
- | |
| 419 | Returns: none |
- | |
| 420 | This functions has been added by Martin Thomas <eversmith@heizung-thomas.de> |
- | |
| 421 | Don't blame P. Fleury if it doesn't work ;-) |
- | |
| 422 | **************************************************************************/ |
- | |
| 423 | void uart_puthex_byte(const unsigned char b) |
- | |
| 424 | { |
- | |
| 425 | uart_puthex_nibble(b>>4); |
- | |
| 426 | uart_puthex_nibble(b); |
- | |
| 427 | } /* uart_puthex_byte */ |
- | |
| 428 | 583 | ||
| 429 | /************************************************************************* |
- | |
| 430 | Function: uart_putbin_byte() |
- | |
| 431 | Purpose: transmit byte as ASCII-bin to UART |
- | |
| 432 | Input: byte value |
- | |
| 433 | Returns: none |
- | |
| 434 | This functions has been added by Martin Thomas <eversmith@heizung-thomas.de> |
- | |
| 435 | Don't blame P. Fleury if it doesn't work ;-) |
- | |
| 436 | **************************************************************************/ |
- | |
| 437 | void uart_putbin_byte(const unsigned char b) |
- | |
| 438 | { |
- | |
| 439 | #if 0 |
- | |
| 440 | char buffer[sizeof(int)*8+1]; |
- | |
| 441 | - | ||
| 442 | uart_puts( itoa(b, buffer, 2) ); |
- | |
| 443 | #else |
- | |
| 444 | signed char i; |
- | |
| 445 | for (i=7;i>=0;i--) { |
- | |
| 446 | if (b & (1<<i)) { |
- | |
| 447 | uart_putc('1'); |
- | |
| 448 | } |
- | |
| 449 | else { |
- | |
| 450 | uart_putc('0'); |
- | |
| 451 | } |
- | |
| 452 | } |
- | |
| 453 | #endif |
584 | #endif |
| 454 | } /* uart_putbin_byte */ |
- | |