18
18
19
19
Modified 23 November 2006 by David A. Mellis
20
20
Modified 28 September 2010 by Mark Sproul
21
+ Modified 12 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;
@@ -333,6 +356,92 @@ void HardwareSerial::begin(unsigned long baud)
333
356
cbi (*_ucsrb, _udrie);
334
357
}
335
358
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
+
336
445
void HardwareSerial::end ()
337
446
{
338
447
// wait for transmission of outgoing data
@@ -405,23 +514,23 @@ HardwareSerial::operator bool() {
405
514
// Preinstantiate Objects //////////////////////////////////////////////////////
406
515
407
516
#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);
409
518
#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);
411
520
#elif defined(USBCON)
412
521
// do nothing - Serial object and buffers are initialized in CDC code
413
522
#else
414
523
#error no serial port defined (port 0)
415
524
#endif
416
525
417
526
#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);
419
528
#endif
420
529
#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);
422
531
#endif
423
532
#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);
425
534
#endif
426
535
427
536
#endif // whole file
0 commit comments