18
18
19
19
Modified 23 November 2006 by David A. Mellis
20
20
Modified 28 September 2010 by Mark Sproul
21
+ Modified 14 August 2012 by Alarus
21
22
*/
22
23
23
24
#include < stdlib.h>
@@ -109,13 +110,22 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
109
110
#endif
110
111
{
111
112
#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
+ };
113
119
#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
+ };
115
126
#else
116
127
#error UDR not defined
117
128
#endif
118
- store_char (c, &rx_buffer);
119
129
}
120
130
#endif
121
131
#endif
@@ -126,8 +136,12 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
126
136
#define serialEvent1_implemented
127
137
SIGNAL (USART1_RX_vect)
128
138
{
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
+ };
131
145
}
132
146
#elif defined(SIG_USART1_RECV)
133
147
#error SIG_USART1_RECV
@@ -139,8 +153,12 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
139
153
#define serialEvent2_implemented
140
154
SIGNAL (USART2_RX_vect)
141
155
{
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
+ };
144
162
}
145
163
#elif defined(SIG_USART2_RECV)
146
164
#error SIG_USART2_RECV
@@ -152,8 +170,12 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
152
170
#define serialEvent3_implemented
153
171
SIGNAL (USART3_RX_vect)
154
172
{
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
+ };
157
179
}
158
180
#elif defined(SIG_USART3_RECV)
159
181
#error SIG_USART3_RECV
@@ -274,7 +296,7 @@ ISR(USART3_UDRE_vect)
274
296
HardwareSerial::HardwareSerial (ring_buffer *rx_buffer, ring_buffer *tx_buffer,
275
297
volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
276
298
volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
277
- volatile uint8_t *udr,
299
+ volatile uint8_t *ucsrc, volatile uint8_t * udr,
278
300
uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x)
279
301
{
280
302
_rx_buffer = rx_buffer;
@@ -283,6 +305,7 @@ HardwareSerial::HardwareSerial(ring_buffer *rx_buffer, ring_buffer *tx_buffer,
283
305
_ubrrl = ubrrl;
284
306
_ucsra = ucsra;
285
307
_ucsrb = ucsrb;
308
+ _ucsrc = ucsrc;
286
309
_udr = udr;
287
310
_rxen = rxen;
288
311
_txen = txen;
@@ -335,6 +358,53 @@ void HardwareSerial::begin(unsigned long baud)
335
358
cbi (*_ucsrb, _udrie);
336
359
}
337
360
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
+
338
408
void HardwareSerial::end ()
339
409
{
340
410
// wait for transmission of outgoing data
@@ -411,23 +481,23 @@ HardwareSerial::operator bool() {
411
481
// Preinstantiate Objects //////////////////////////////////////////////////////
412
482
413
483
#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);
415
485
#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);
417
487
#elif defined(USBCON)
418
488
// do nothing - Serial object and buffers are initialized in CDC code
419
489
#else
420
490
#error no serial port defined (port 0)
421
491
#endif
422
492
423
493
#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);
425
495
#endif
426
496
#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);
428
498
#endif
429
499
#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);
431
501
#endif
432
502
433
503
#endif // whole file
0 commit comments