Rev 1468 | Rev 1578 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1468 | Rev 1576 | ||
|---|---|---|---|
| Line 169... | Line 169... | ||
| 169 | static volatile unsigned char UART1_RxHead; |
169 | static volatile unsigned char UART1_RxHead; |
| 170 | static volatile unsigned char UART1_RxTail; |
170 | static volatile unsigned char UART1_RxTail; |
| 171 | static volatile unsigned char UART1_LastRxError; |
171 | static volatile unsigned char UART1_LastRxError; |
| 172 | #endif |
172 | #endif |
| 173 | 173 | ||
| - | 174 | ||
| - | 175 | static uint8_t parityEnabled = 0; |
|
| - | 176 | static uint8_t parityOdd = 0; |
|
| - | 177 | static uint8_t twoStopBits = 0; |
|
| - | 178 | static uint8_t charSize = 8; |
|
| - | 179 | ||
| - | 180 | ||
| - | 181 | static void uart_updateConfig() |
|
| - | 182 | { |
|
| - | 183 | uint8_t CHARSIZE_MASK = 0; |
|
| - | 184 | if (charSize=5) { |
|
| - | 185 | CHARSIZE_MASK = (0<<UCSZ12) | (0<<UCSZ11) | (0<<UCSZ10); |
|
| - | 186 | } |
|
| - | 187 | else if (charSize=6) { |
|
| - | 188 | CHARSIZE_MASK = (0<<UCSZ12) | (0<<UCSZ11) | (1<<UCSZ10); |
|
| - | 189 | } |
|
| - | 190 | else if (charSize=7) { |
|
| - | 191 | CHARSIZE_MASK = (0<<UCSZ12) | (1<<UCSZ11) | (0<<UCSZ10); |
|
| - | 192 | } |
|
| - | 193 | else if (charSize=8) { |
|
| - | 194 | CHARSIZE_MASK = (0<<UCSZ12) | (1<<UCSZ11) | (1<<UCSZ10); |
|
| - | 195 | } |
|
| - | 196 | else if (charSize=9) { |
|
| - | 197 | CHARSIZE_MASK = (1<<UCSZ12) | (1<<UCSZ11) | (1<<UCSZ10); |
|
| - | 198 | } |
|
| - | 199 | UCSR1C = (parityEnabled<<UPM10) | (parityOdd<<UPM11) | (twoStopBits<<USBS) | CHARSIZE_MASK; |
|
| - | 200 | } |
|
| - | 201 | ||
| - | 202 | ||
| - | 203 | void uart_setParity(uint8_t enabled, uint8_t odd) |
|
| - | 204 | { |
|
| - | 205 | parityEnabled = enabled; |
|
| - | 206 | parityOdd = odd; |
|
| - | 207 | uart_updateConfig(); |
|
| - | 208 | } |
|
| - | 209 | ||
| - | 210 | ||
| - | 211 | void uart_setStopbits(uint8_t nrStopbits) |
|
| - | 212 | { |
|
| - | 213 | twoStopBits = (nrStopbits == 2) ? 1 : 0; |
|
| - | 214 | uart_updateConfig(); |
|
| - | 215 | } |
|
| - | 216 | ||
| - | 217 | ||
| - | 218 | void uart_setDatabits(uint8_t nrDatabits) |
|
| - | 219 | { |
|
| - | 220 | if (nrDatabits >=5 && nrDatabits <=9) { |
|
| - | 221 | charSize = nrDatabits; |
|
| - | 222 | } |
|
| - | 223 | uart_updateConfig(); |
|
| - | 224 | } |
|
| 174 | 225 | ||
| 175 | 226 | ||
| 176 | SIGNAL(UART0_RECEIVE_INTERRUPT) |
227 | SIGNAL(UART0_RECEIVE_INTERRUPT) |
| 177 | /************************************************************************* |
228 | /************************************************************************* |
| 178 | Function: UART Receive Complete interrupt |
229 | Function: UART Receive Complete interrupt |
| Line 210... | Line 261... | ||
| 210 | /* store new index */ |
261 | /* store new index */ |
| 211 | UART_RxHead = tmphead; |
262 | UART_RxHead = tmphead; |
| 212 | /* store received data in buffer */ |
263 | /* store received data in buffer */ |
| 213 | UART_RxBuf[tmphead] = data; |
264 | UART_RxBuf[tmphead] = data; |
| 214 | } |
265 | } |
| 215 | UART_LastRxError = lastRxError; |
266 | UART_LastRxError = lastRxError; |
| 216 | } |
267 | } |
| 217 | 268 | ||
| 218 | 269 | ||
| 219 | SIGNAL(UART0_TRANSMIT_INTERRUPT) |
270 | SIGNAL(UART0_TRANSMIT_INTERRUPT) |
| 220 | /************************************************************************* |
271 | /************************************************************************* |
| 221 | Function: UART Data Register Empty interrupt |
272 | Function: UART Data Register Empty interrupt |
| 222 | Purpose: called when the UART is ready to transmit the next byte |
273 | Purpose: called when the UART is ready to transmit the next byte |
| 223 | **************************************************************************/ |
274 | **************************************************************************/ |
| 224 | { |
275 | { |
| 225 | unsigned char tmptail; |
276 | unsigned char tmptail; |
| 226 | 277 | ||
| 227 | 278 | ||
| 228 | if ( UART_TxHead != UART_TxTail) { |
279 | if ( UART_TxHead != UART_TxTail) { |
| 229 | /* calculate and store new buffer index */ |
280 | /* calculate and store new buffer index */ |
| 230 | tmptail = (UART_TxTail + 1) & UART_TX_BUFFER_MASK; |
281 | tmptail = (UART_TxTail + 1) & UART_TX_BUFFER_MASK; |
| 231 | UART_TxTail = tmptail; |
282 | UART_TxTail = tmptail; |
| 232 | /* get one byte from buffer and write it to UART */ |
283 | /* get one byte from buffer and write it to UART */ |
| Line 250... | Line 301... | ||
| 250 | UART_TxTail = 0; |
301 | UART_TxTail = 0; |
| 251 | UART_RxHead = 0; |
302 | UART_RxHead = 0; |
| 252 | UART_RxTail = 0; |
303 | UART_RxTail = 0; |
| 253 | 304 | ||
| 254 | #if defined( AT90_UART ) |
305 | #if defined( AT90_UART ) |
| 255 | /* set baud rate */ |
306 | /* set baud rate */ |
| 256 | UBRR = (unsigned char)baudrate; |
307 | UBRR = (unsigned char)baudrate; |
| 257 | 308 | ||
| 258 | /* enable UART receiver and transmmitter and receive complete interrupt */ |
309 | /* enable UART receiver and transmmitter and receive complete interrupt */ |
| 259 | UART0_CONTROL = _BV(RXCIE)|_BV(RXEN)|_BV(TXEN); |
310 | UART0_CONTROL = _BV(RXCIE)|_BV(RXEN)|_BV(TXEN); |
| 260 | 311 | ||
| Line 328... | Line 379... | ||
| 328 | unsigned char data; |
379 | unsigned char data; |
| 329 | 380 | ||
| 330 | 381 | ||
| 331 | if ( UART_RxHead == UART_RxTail ) { |
382 | if ( UART_RxHead == UART_RxTail ) { |
| 332 | return UART_NO_DATA; /* no data available */ |
383 | return UART_NO_DATA; /* no data available */ |
| 333 | } |
384 | } |
| 334 | 385 | ||
| 335 | /* calculate /store buffer index */ |
386 | /* calculate /store buffer index */ |
| 336 | tmptail = (UART_RxTail + 1) & UART_RX_BUFFER_MASK; |
387 | tmptail = (UART_RxTail + 1) & UART_RX_BUFFER_MASK; |
| 337 | UART_RxTail = tmptail; |
388 | UART_RxTail = tmptail; |
| 338 | 389 | ||
| 339 | /* get data from receive buffer */ |
390 | /* get data from receive buffer */ |
| Line 486... | Line 537... | ||
| 486 | UBRR1L = (unsigned char) baudrate; |
537 | UBRR1L = (unsigned char) baudrate; |
| 487 | 538 | ||
| 488 | /* Enable USART receiver and transmitter and receive complete interrupt */ |
539 | /* Enable USART receiver and transmitter and receive complete interrupt */ |
| 489 | UART1_CONTROL = _BV(RXCIE1)|(1<<RXEN1)|(1<<TXEN1); |
540 | UART1_CONTROL = _BV(RXCIE1)|(1<<RXEN1)|(1<<TXEN1); |
| 490 | 541 | ||
| 491 |
|
542 | // use previously set values (or default to 8bit, no parity, 1 stopbit) |
| 492 | #ifdef URSEL1 |
- | |
| 493 | UCSR1C = (1<<URSEL1)|(3<<UCSZ10); |
- | |
| 494 | #else |
- | |
| 495 |
|
543 | uart_updateConfig(); |
| 496 | #endif |
- | |
| 497 | }/* uart_init */ |
544 | }/* uart_init */ |
| 498 | 545 | ||
| 499 | 546 | ||
| 500 | /************************************************************************* |
547 | /************************************************************************* |
| 501 | Function: uart1_getc() |
548 | Function: uart1_getc() |