2 // Product: Serial library for QPnano and Arduino
4 // Date: 25 November 2010
6 // +-----------------------+
7 // | d e c i s i o n s |
8 // +-----------------------+
9 // | a n d d e s i g n s |
10 // +-----------------------+
12 // Copyright (C) 2010 Decisions and Designs Pty Ltd. All rights reserved.
14 // This software may be distributed and modified under the terms of the GNU
15 // General Public License version 2 (GPL) as published by the Free Software
16 // Foundation and appearing in the file GPL.TXT included in the packaging of
17 // this file. Please note that GPL Section 2[b] requires that all works based
18 // on this software must also be made publicly available under the terms of
19 // the GPL ("Copyleft").
21 // Contact information:
22 // Decisions and Designs Web site: http://www.decisions-and-designs.com.au
26 // Serial hardware IO driver.
29 #include "serial_io.h"
34 #define abs(x) ((x)>0?(x):-(x))
36 #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
39 #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
44 #if defined(__AVR_ATmega1280__)
51 #endif /* __AVR_ATmega1280__ */
53 /* Adapted baud rate initialization code from function:
54 * void HardwareSerial::begin(long baud)
55 * in file: HardwareSerial.cpp - Hardware serial library for Wiring
56 * Copyright (c) 2006 Nicholas Zambetti. All right reserved.
57 * Modified 23 November 2006 by David A. Mellis
59 uint16_t baudSetting(uint32_t baud, uint8_t *use_u2x)
61 // Calculate optimum baud clock rate and source
62 // U2X mode is needed for baud rates higher than (CPU Hz / 16)
63 if (baud > F_CPU / 16) {
66 // figure out if U2X mode would allow for a better connection
67 // calculate the percent difference between the baud-rate specified and
68 // the real baud rate for both U2X and non-U2X mode (0-255 error percent)
69 uint8_t nonu2x_baud_error = abs((int)(255-((F_CPU/(16*(((F_CPU/8/baud-1)/2)+1))*255)/baud)));
70 uint8_t u2x_baud_error = abs((int)(255-((F_CPU/(8*(((F_CPU/4/baud-1)/2)+1))*255)/baud)));
72 // prefer non-U2X mode because it handles clock skew better
73 *use_u2x = (nonu2x_baud_error > u2x_baud_error);
77 return (F_CPU / 4 / baud - 1) / 2;
79 return (F_CPU / 8 / baud - 1) / 2;
83 /* Initialise hardware and attach the receiver and/or transmitter to a HSM.
84 If rx or tx are NULL then an assignment will not be made for that part.
85 This allows two different HSMs to use the tx and rx part but the baud
86 rate will be set by the last caller */
87 void initSerial(uint32_t baud, QActive *rx, QActive *tx)
92 if (!rx && !tx) return;
93 /* Set the baud rate */
94 baud_count = baudSetting(baud, &use_u2x);
95 #if defined(__AVR_ATmega8__)
101 UBRRH = baud_count >> 8;
109 UBRR0H = baud_count >> 8;
113 #if defined(__AVR_ATmega8__)
129 #if defined(__AVR_ATmega8__)
138 #if defined(__AVR_ATmega1280__)
139 void initSerial1(uint32_t baud, QActive *rx, QActive *tx)
144 if (!rx && !tx) return;
145 /* Set the baud rate */
146 baud_count = baudSetting(baud, &use_u2x);
152 UBRR1H = baud_count >> 8;
168 void initSerial2(uint32_t baud, QActive *rx, QActive *tx)
173 if (!rx && !tx) return;
174 /* Set the baud rate */
175 baud_count = baudSetting(baud, &use_u2x);
181 UBRR2H = baud_count >> 8;
197 void initSerial3(uint32_t baud, QActive *rx, QActive *tx)
202 if (!rx && !tx) return;
203 /* Set the baud rate */
204 baud_count = baudSetting(baud, &use_u2x);
210 UBRR3H = baud_count >> 8;
225 #endif /* __AVR_ATmega1280__ */
227 uint8_t serialTransmit(uint8_t ch)
229 volatile uint8_t z = 0;
\r
231 #if defined(__AVR_ATmega8__)
232 if (!(UCSRA & (1 << UDRE))) // tx register is not empty yet
233 return SERIAL_TRANSMIT_BUSY;
234 UDR = ch; // load the char (clears tx empty flag)
235 sbi(UCSRB, UDRIE); // enable tx empty interrupt
237 if (!(UCSR0A & (1 << UDRE0))) { // as above
239 return SERIAL_TRANSMIT_BUSY;
244 return SERIAL_SUCCESS;
247 #if defined(__AVR_ATmega1280__)
248 uint8_t serial1Transmit(uint8_t ch)
250 if (!(UCSR1A & (1 << UDRE1))) { // as above
251 return SERIAL_TRANSMIT_BUSY;
\r
255 return SERIAL_SUCCESS;
258 uint8_t serial2Transmit(uint8_t ch)
260 if (!(UCSR2A & (1 << UDRE2))) { // as above
261 return SERIAL_TRANSMIT_BUSY;
\r
265 return SERIAL_SUCCESS;
268 uint8_t serial3Transmit(uint8_t ch)
270 if (!(UCSR3A & (1 << UDRE3))) // as above
271 return SERIAL_TRANSMIT_BUSY;
274 return SERIAL_SUCCESS;
276 #endif /* __AVR_ATmega1280__ */
278 void serialTransmitComplete(void)
280 #if defined(__AVR_ATmega8__)
281 sbi(UCSRB, TXCIE); // enable tx complete interrupt
283 sbi(UCSR0B, TXCIE0); // enable tx complete interrupt
287 #if defined(__AVR_ATmega1280__)
288 void serial1TransmitComplete(void)
290 sbi(UCSR1B, TXCIE1); // enable tx complete interrupt
293 void serial2TransmitComplete(void)
295 sbi(UCSR2B, TXCIE2); // enable tx complete interrupt
298 void serial3TransmitComplete(void)
300 sbi(UCSR3B, TXCIE3); // enable tx complete interrupt
302 #endif /* __AVR_ATmega1280__ */
304 #if defined(__AVR_ATmega8__)
305 // Serial character received
312 // spurious interrupt (no handler) need to discard the character(!)
317 // sense and signal receiver overrun, parity, or frame error
318 if (UCSRA & (1 << DOR)) {
319 pa.msg.status |= 1 << SERIAL_STATUS_OVERRUN;
321 if (UCSRA & (1 << FE)) {
322 pa.msg.status |= 1 << SERIAL_STATUS_FRAME;
324 if (UCSRA & (1 << UPE)) {
325 pa.msg.status |= 1 << SERIAL_STATUS_PARITY;
327 // fetch char into message (also clears any error flags)
330 QActive_postISR(serial_rx, SERIAL_RX_ERROR_SIG, pa.param);
332 QActive_postISR(serial_rx, SERIAL_RX_DATA_SIG, pa.param);
337 // Transmitter data register empty
340 // one shot interrupt, re-armed by serialTransmit()
342 // sanity check handler is defined
344 QActive_postISR(serial_tx, SERIAL_TX_EMPTY_SIG, (QParam)0);
349 // Transmission complete
352 // one shot interrupt, re-armed by serialTransmitComplete()
354 // sanity check handler is defined
356 QActive_postISR(serial_tx, SERIAL_TX_COMPLETE_SIG, (QParam)0);
362 #if defined(__AVR_ATmega1280__) || !defined(__AVR_ATmega8__)
364 // Serial character received
365 #if defined(__AVR_ATmega1280__)
375 // spurious interrupt (no handler) need to discard the character(!)
380 // sense and signal receiver overrun, parity, or frame error
381 if (UCSR0A & (1 << DOR0)) {
382 pa.msg.status |= 1 << SERIAL_STATUS_OVERRUN;
384 if (UCSR0A & (1 << FE0)) {
385 pa.msg.status |= 1 << SERIAL_STATUS_FRAME;
387 if (UCSR0A & (1 << UPE0)) {
388 pa.msg.status |= 1 << SERIAL_STATUS_PARITY;
390 // fetch char into message (also clears any error flags)
393 QActive_postISR(serial_rx, SERIAL_RX_ERROR_SIG, pa.param);
395 QActive_postISR(serial_rx, SERIAL_RX_DATA_SIG, pa.param);
400 // Transmitter data register empty
401 #if defined(__AVR_ATmega1280__)
402 ISR(USART0_UDRE_vect)
407 // one shot interrupt, re-armed by serialTransmit()
409 // sanity check handler is defined
411 QActive_postISR(serial_tx, SERIAL_TX_EMPTY_SIG, (QParam)0);
416 #if defined(__AVR_ATmega1280__)
422 // one shot interrupt, re-armed by serialTransmitComplete()
424 // sanity check handler is defined
426 QActive_postISR(serial_tx, SERIAL_TX_COMPLETE_SIG, (QParam)0);
430 #endif // defined(__AVR_ATmega1280__) | !defined(__AVR_ATmega8__)
432 #if defined(__AVR_ATmega1280__)
436 pa.msg.port = USART1;
439 // spurious interrupt (no handler) need to discard the character(!)
444 // sense and signal receiver overrun, parity, or frame error
445 if (UCSR1A & (1 << DOR1)) {
446 pa.msg.status |= 1 << SERIAL_STATUS_OVERRUN;
448 if (UCSR1A & (1 << FE1)) {
449 pa.msg.status |= 1 << SERIAL_STATUS_FRAME;
451 if (UCSR1A & (1 << UPE1)) {
452 pa.msg.status |= 1 << SERIAL_STATUS_PARITY;
454 // fetch char into message (also clears any error flags)
457 QActive_postISR(serial1_rx, SERIAL1_RX_ERROR_SIG, pa.param);
459 QActive_postISR(serial1_rx, SERIAL1_RX_DATA_SIG, pa.param);
464 ISR(USART1_UDRE_vect)
466 // one shot interrupt, re-armed by serialTransmit()
468 // sanity check handler is defined
470 QActive_postISR(serial1_tx, SERIAL1_TX_EMPTY_SIG, (QParam)0);
477 // one shot interrupt, re-armed by serialTransmitComplete()
479 // sanity check handler is defined
481 QActive_postISR(serial1_tx, SERIAL1_TX_COMPLETE_SIG, (QParam)0);
489 pa.msg.port = USART2;
492 // spurious interrupt (no handler) need to discard the character(!)
497 // sense and signal receiver overrun, parity, or frame error
498 if (UCSR2A & (1 << DOR2)) {
499 pa.msg.status |= 1 << SERIAL_STATUS_OVERRUN;
501 if (UCSR2A & (1 << FE2)) {
502 pa.msg.status |= 1 << SERIAL_STATUS_FRAME;
504 if (UCSR2A & (1 << UPE2)) {
505 pa.msg.status |= 1 << SERIAL_STATUS_PARITY;
507 // fetch char into message (also clears any error flags)
510 QActive_postISR(serial2_rx, SERIAL2_RX_ERROR_SIG, pa.param);
512 QActive_postISR(serial2_rx, SERIAL2_RX_DATA_SIG, pa.param);
517 ISR(USART2_UDRE_vect)
519 // one shot interrupt, re-armed by serialTransmit()
521 // sanity check handler is defined
523 QActive_postISR(serial2_tx, SERIAL2_TX_EMPTY_SIG, (QParam)0);
530 // one shot interrupt, re-armed by serialTransmitComplete()
532 // sanity check handler is defined
534 QActive_postISR(serial2_tx, SERIAL2_TX_COMPLETE_SIG, (QParam)0);
542 pa.msg.port = USART3;
545 // spurious interrupt (no handler) need to discard the character(!)
550 // sense and signal receiver overrun, parity, or frame error
551 if (UCSR3A & (1 << DOR3)) {
552 pa.msg.status |= 1 << SERIAL_STATUS_OVERRUN;
554 if (UCSR3A & (1 << FE3)) {
555 pa.msg.status |= 1 << SERIAL_STATUS_FRAME;
557 if (UCSR3A & (1 << UPE3)) {
558 pa.msg.status |= 1 << SERIAL_STATUS_PARITY;
560 // fetch char into message (also clears any error flags)
563 QActive_postISR(serial3_rx, SERIAL3_RX_ERROR_SIG, pa.param);
565 QActive_postISR(serial3_rx, SERIAL3_RX_DATA_SIG, pa.param);
570 ISR(USART3_UDRE_vect)
572 // one shot interrupt, re-armed by serialTransmit()
574 // sanity check handler is defined
576 QActive_postISR(serial3_tx, SERIAL3_TX_EMPTY_SIG, (QParam)0);
583 // one shot interrupt, re-armed by serialTransmitComplete()
585 // sanity check handler is defined
587 QActive_postISR(serial3_tx, SERIAL3_TX_COMPLETE_SIG, (QParam)0);
591 #endif // defined(__AVR_ATmega1280__)