1818
1919 Modified 23 November 2006 by David A. Mellis
2020 Modified 28 September 2010 by Mark Sproul
21+ Modified 12 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;
@@ -333,6 +356,92 @@ void HardwareSerial::begin(unsigned long baud)
333356 cbi (*_ucsrb, _udrie);
334357}
335358
359+ void HardwareSerial::begin (unsigned long baud, byte databits, char parity, byte stopbits)
360+ {
361+ uint16_t baud_setting;
362+ uint8_t config_setting;
363+ bool use_u2x = true ;
364+
365+ #if F_CPU == 16000000UL
366+ // hardcoded exception for compatibility with the bootloader shipped
367+ // with the Duemilanove and previous boards and the firmware on the 8U2
368+ // on the Uno and Mega 2560.
369+ if (baud == 57600 ) {
370+ use_u2x = false ;
371+ }
372+ #endif
373+
374+ try_again:
375+
376+ if (use_u2x) {
377+ *_ucsra = 1 << _u2x;
378+ baud_setting = (F_CPU / 4 / baud - 1 ) / 2 ;
379+ } else {
380+ *_ucsra = 0 ;
381+ baud_setting = (F_CPU / 8 / baud - 1 ) / 2 ;
382+ }
383+
384+ if ((baud_setting > 4095 ) && use_u2x)
385+ {
386+ use_u2x = false ;
387+ goto try_again;
388+ }
389+
390+ // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
391+ *_ubrrh = baud_setting >> 8 ;
392+ *_ubrrl = baud_setting;
393+
394+ // set number of data bits
395+ config_setting = *_ubrrh;
396+ config_setting = *_ucsrc;
397+ if (databits == 5 )
398+ {
399+ config_setting |= B10000000;
400+ }
401+ else if (databits == 6 )
402+ {
403+ config_setting |= B10000010;
404+ }
405+ else if (databits == 7 )
406+ {
407+ config_setting |= B10000100;
408+ }
409+ else // (databits == 8)
410+ {
411+ config_setting |= B10000110;
412+ }
413+
414+ // set parity
415+ if ((parity == ' O' )|(parity == ' o' ))
416+ {
417+ config_setting |= B10110000;
418+ }
419+ else if ((parity == ' E' )|(parity == ' e' ))
420+ {
421+ config_setting |= B10100000;
422+ }
423+ else // ((parity == 'N')|(parity == 'n')))
424+ {
425+ config_setting |= B10000000;
426+ }
427+
428+ // set number of stop bits
429+ if (stopbits == 2 )
430+ {
431+ config_setting |= B10001000;
432+ }
433+ else // (stopbits == 1)
434+ {
435+ config_setting |= B10000000;
436+ }
437+ *_ucsrc = config_setting
438+
439+ sbi (*_ucsrb, _rxen);
440+ sbi (*_ucsrb, _txen);
441+ sbi (*_ucsrb, _rxcie);
442+ cbi (*_ucsrb, _udrie);
443+ }
444+
336445void HardwareSerial::end ()
337446{
338447 // wait for transmission of outgoing data
@@ -405,23 +514,23 @@ HardwareSerial::operator bool() {
405514// Preinstantiate Objects //////////////////////////////////////////////////////
406515
407516#if defined(UBRRH) && defined(UBRRL)
408- HardwareSerial Serial (&rx_buffer, &tx_buffer, &UBRRH, &UBRRL, &UCSRA, &UCSRB, &UDR, RXEN, TXEN, RXCIE, UDRIE, U2X);
517+ HardwareSerial Serial (&rx_buffer, &tx_buffer, &UBRRH, &UBRRL, &UCSRA, &UCSRB, &UCSRC, & UDR, RXEN, TXEN, RXCIE, UDRIE, U2X);
409518#elif defined(UBRR0H) && defined(UBRR0L)
410- HardwareSerial Serial (&rx_buffer, &tx_buffer, &UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UDR0, RXEN0, TXEN0, RXCIE0, UDRIE0, U2X0);
519+ HardwareSerial Serial (&rx_buffer, &tx_buffer, &UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UCSR0C, & UDR0, RXEN0, TXEN0, RXCIE0, UDRIE0, U2X0);
411520#elif defined(USBCON)
412521 // do nothing - Serial object and buffers are initialized in CDC code
413522#else
414523 #error no serial port defined (port 0)
415524#endif
416525
417526#if defined(UBRR1H)
418- HardwareSerial Serial1 (&rx_buffer1, &tx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UDR1, RXEN1, TXEN1, RXCIE1, UDRIE1, U2X1);
527+ HardwareSerial Serial1 (&rx_buffer1, &tx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UCSR1C, & UDR1, RXEN1, TXEN1, RXCIE1, UDRIE1, U2X1);
419528#endif
420529#if defined(UBRR2H)
421- HardwareSerial Serial2 (&rx_buffer2, &tx_buffer2, &UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UDR2, RXEN2, TXEN2, RXCIE2, UDRIE2, U2X2);
530+ HardwareSerial Serial2 (&rx_buffer2, &tx_buffer2, &UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UCSR2C, & UDR2, RXEN2, TXEN2, RXCIE2, UDRIE2, U2X2);
422531#endif
423532#if defined(UBRR3H)
424- HardwareSerial Serial3 (&rx_buffer3, &tx_buffer3, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UDR3, RXEN3, TXEN3, RXCIE3, UDRIE3, U2X3);
533+ HardwareSerial Serial3 (&rx_buffer3, &tx_buffer3, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UCSR3C, & UDR3, RXEN3, TXEN3, RXCIE3, UDRIE3, U2X3);
425534#endif
426535
427536#endif // whole file
0 commit comments