1818
1919 Modified 23 November 2006 by David A. Mellis
2020 Modified 28 September 2010 by Mark Sproul
21+ Modified 14 August 2012 by Alarus
2122*/
2223
2324#include < stdlib.h>
@@ -109,13 +110,22 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
109110#endif
110111 {
111112 #if defined(UDR0)
112- unsigned char c = UDR0;
113+ if (bit_is_clear (UCSR0A, UPE0)) {
114+ unsigned char c = UDR0;
115+ store_char (c, &rx_buffer);
116+ } else {
117+ unsigned char c = UDR0;
118+ };
113119 #elif defined(UDR)
114- unsigned char c = UDR;
120+ if (bit_is_clear (UCSRA, PE)) {
121+ unsigned char c = UDR;
122+ store_char (c, &rx_buffer);
123+ } else {
124+ unsigned char c = UDR;
125+ };
115126 #else
116127 #error UDR not defined
117128 #endif
118- store_char (c, &rx_buffer);
119129 }
120130#endif
121131#endif
@@ -126,8 +136,12 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
126136 #define serialEvent1_implemented
127137 SIGNAL (USART1_RX_vect)
128138 {
129- unsigned char c = UDR1;
130- store_char (c, &rx_buffer1);
139+ if (bit_is_clear (UCSR1A, UPE1)) {
140+ unsigned char c = UDR1;
141+ store_char (c, &rx_buffer1);
142+ } else {
143+ unsigned char c = UDR1;
144+ };
131145 }
132146#elif defined(SIG_USART1_RECV)
133147 #error SIG_USART1_RECV
@@ -139,8 +153,12 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
139153 #define serialEvent2_implemented
140154 SIGNAL (USART2_RX_vect)
141155 {
142- unsigned char c = UDR2;
143- store_char (c, &rx_buffer2);
156+ if (bit_is_clear (UCSR2A, UPE2)) {
157+ unsigned char c = UDR2;
158+ store_char (c, &rx_buffer2);
159+ } else {
160+ unsigned char c = UDR2;
161+ };
144162 }
145163#elif defined(SIG_USART2_RECV)
146164 #error SIG_USART2_RECV
@@ -152,8 +170,12 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
152170 #define serialEvent3_implemented
153171 SIGNAL (USART3_RX_vect)
154172 {
155- unsigned char c = UDR3;
156- store_char (c, &rx_buffer3);
173+ if (bit_is_clear (UCSR3A, UPE3)) {
174+ unsigned char c = UDR3;
175+ store_char (c, &rx_buffer3);
176+ } else {
177+ unsigned char c = UDR3;
178+ };
157179 }
158180#elif defined(SIG_USART3_RECV)
159181 #error SIG_USART3_RECV
@@ -274,7 +296,7 @@ ISR(USART3_UDRE_vect)
274296HardwareSerial::HardwareSerial (ring_buffer *rx_buffer, ring_buffer *tx_buffer,
275297 volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
276298 volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
277- volatile uint8_t *udr,
299+ volatile uint8_t *ucsrc, volatile uint8_t * udr,
278300 uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x)
279301{
280302 _rx_buffer = rx_buffer;
@@ -283,6 +305,7 @@ HardwareSerial::HardwareSerial(ring_buffer *rx_buffer, ring_buffer *tx_buffer,
283305 _ubrrl = ubrrl;
284306 _ucsra = ucsra;
285307 _ucsrb = ucsrb;
308+ _ucsrc = ucsrc;
286309 _udr = udr;
287310 _rxen = rxen;
288311 _txen = txen;
@@ -335,6 +358,53 @@ void HardwareSerial::begin(unsigned long baud)
335358 cbi (*_ucsrb, _udrie);
336359}
337360
361+ void HardwareSerial::begin (unsigned long baud, byte config)
362+ {
363+ uint16_t baud_setting;
364+ uint8_t current_config;
365+ bool use_u2x = true ;
366+
367+ #if F_CPU == 16000000UL
368+ // hardcoded exception for compatibility with the bootloader shipped
369+ // with the Duemilanove and previous boards and the firmware on the 8U2
370+ // on the Uno and Mega 2560.
371+ if (baud == 57600 ) {
372+ use_u2x = false ;
373+ }
374+ #endif
375+
376+ try_again:
377+
378+ if (use_u2x) {
379+ *_ucsra = 1 << _u2x;
380+ baud_setting = (F_CPU / 4 / baud - 1 ) / 2 ;
381+ } else {
382+ *_ucsra = 0 ;
383+ baud_setting = (F_CPU / 8 / baud - 1 ) / 2 ;
384+ }
385+
386+ if ((baud_setting > 4095 ) && use_u2x)
387+ {
388+ use_u2x = false ;
389+ goto try_again;
390+ }
391+
392+ // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
393+ *_ubrrh = baud_setting >> 8 ;
394+ *_ubrrl = baud_setting;
395+
396+ // set number of data bits
397+ current_config = *_ubrrh;
398+ current_config = *_ucsrc;
399+ current_config |= config;
400+ *_ucsrc = current_config;
401+
402+ sbi (*_ucsrb, _rxen);
403+ sbi (*_ucsrb, _txen);
404+ sbi (*_ucsrb, _rxcie);
405+ cbi (*_ucsrb, _udrie);
406+ }
407+
338408void HardwareSerial::end ()
339409{
340410 // wait for transmission of outgoing data
@@ -411,23 +481,23 @@ HardwareSerial::operator bool() {
411481// Preinstantiate Objects //////////////////////////////////////////////////////
412482
413483#if defined(UBRRH) && defined(UBRRL)
414- HardwareSerial Serial (&rx_buffer, &tx_buffer, &UBRRH, &UBRRL, &UCSRA, &UCSRB, &UDR, RXEN, TXEN, RXCIE, UDRIE, U2X);
484+ HardwareSerial Serial (&rx_buffer, &tx_buffer, &UBRRH, &UBRRL, &UCSRA, &UCSRB, &UCSRC, & UDR, RXEN, TXEN, RXCIE, UDRIE, U2X);
415485#elif defined(UBRR0H) && defined(UBRR0L)
416- HardwareSerial Serial (&rx_buffer, &tx_buffer, &UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UDR0, RXEN0, TXEN0, RXCIE0, UDRIE0, U2X0);
486+ HardwareSerial Serial (&rx_buffer, &tx_buffer, &UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UCSR0C, & UDR0, RXEN0, TXEN0, RXCIE0, UDRIE0, U2X0);
417487#elif defined(USBCON)
418488 // do nothing - Serial object and buffers are initialized in CDC code
419489#else
420490 #error no serial port defined (port 0)
421491#endif
422492
423493#if defined(UBRR1H)
424- HardwareSerial Serial1 (&rx_buffer1, &tx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UDR1, RXEN1, TXEN1, RXCIE1, UDRIE1, U2X1);
494+ HardwareSerial Serial1 (&rx_buffer1, &tx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UCSR1C, & UDR1, RXEN1, TXEN1, RXCIE1, UDRIE1, U2X1);
425495#endif
426496#if defined(UBRR2H)
427- HardwareSerial Serial2 (&rx_buffer2, &tx_buffer2, &UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UDR2, RXEN2, TXEN2, RXCIE2, UDRIE2, U2X2);
497+ HardwareSerial Serial2 (&rx_buffer2, &tx_buffer2, &UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UCSR2C, & UDR2, RXEN2, TXEN2, RXCIE2, UDRIE2, U2X2);
428498#endif
429499#if defined(UBRR3H)
430- HardwareSerial Serial3 (&rx_buffer3, &tx_buffer3, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UDR3, RXEN3, TXEN3, RXCIE3, UDRIE3, U2X3);
500+ HardwareSerial Serial3 (&rx_buffer3, &tx_buffer3, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UCSR3C, & UDR3, RXEN3, TXEN3, RXCIE3, UDRIE3, U2X3);
431501#endif
432502
433503#endif // whole file
0 commit comments