Skip to content

Commit a72d05b

Browse files
committed
Merge pull request #109 from Alarus/master
Serial.begin() parameter to set data bits, parity, stop bits.
2 parents cd43ca3 + 76850b1 commit a72d05b

File tree

2 files changed

+139
-16
lines changed

2 files changed

+139
-16
lines changed

hardware/arduino/cores/arduino/HardwareSerial.cpp

+85-15
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
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)
274296
HardwareSerial::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+
338408
void 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

hardware/arduino/cores/arduino/HardwareSerial.h

+54-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1818
1919
Modified 28 September 2010 by Mark Sproul
20+
Modified 14 August 2012 by Alarus
2021
*/
2122

2223
#ifndef HardwareSerial_h
@@ -37,6 +38,7 @@ class HardwareSerial : public Stream
3738
volatile uint8_t *_ubrrl;
3839
volatile uint8_t *_ucsra;
3940
volatile uint8_t *_ucsrb;
41+
volatile uint8_t *_ucsrc;
4042
volatile uint8_t *_udr;
4143
uint8_t _rxen;
4244
uint8_t _txen;
@@ -48,9 +50,10 @@ class HardwareSerial : public Stream
4850
HardwareSerial(ring_buffer *rx_buffer, ring_buffer *tx_buffer,
4951
volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
5052
volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
51-
volatile uint8_t *udr,
53+
volatile uint8_t *ucsrc, volatile uint8_t *udr,
5254
uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x);
5355
void begin(unsigned long);
56+
void begin(unsigned long, byte);
5457
void end();
5558
virtual int available(void);
5659
virtual int peek(void);
@@ -65,6 +68,56 @@ class HardwareSerial : public Stream
6568
operator bool();
6669
};
6770

71+
// Define config for Serial.begin(baud, config);
72+
#define _5n1_ 0x80
73+
#define _5N1_ 0x80
74+
#define _6n1_ 0x82
75+
#define _6N1_ 0x82
76+
#define _7n1_ 0x84
77+
#define _7N1_ 0x84
78+
#define _8n1_ 0x86
79+
#define _8N1_ 0x86
80+
#define _5n2_ 0x88
81+
#define _5N2_ 0x88
82+
#define _6n2_ 0x8A
83+
#define _6N2_ 0x8A
84+
#define _7n2_ 0x8C
85+
#define _7N2_ 0x8C
86+
#define _8n2_ 0x8E
87+
#define _8N2_ 0x8E
88+
#define _5e1_ 0xA0
89+
#define _5E1_ 0xA0
90+
#define _6e1_ 0xA2
91+
#define _6E1_ 0xA2
92+
#define _7e1_ 0xA4
93+
#define _7E1_ 0xA4
94+
#define _8e1_ 0xA6
95+
#define _8E1_ 0xA6
96+
#define _5e2_ 0xA8
97+
#define _5E2_ 0xA8
98+
#define _6e2_ 0xAA
99+
#define _6E2_ 0xAA
100+
#define _7e2_ 0xAC
101+
#define _7E2_ 0xAC
102+
#define _8e2_ 0xAE
103+
#define _8E2_ 0xAE
104+
#define _5o1_ 0xB0
105+
#define _5O1_ 0xB0
106+
#define _6o1_ 0xB2
107+
#define _6O1_ 0xB2
108+
#define _7o1_ 0xB4
109+
#define _7O1_ 0xB4
110+
#define _8o1_ 0xB6
111+
#define _8O1_ 0xB6
112+
#define _5o2_ 0xB8
113+
#define _5O2_ 0xB8
114+
#define _6o2_ 0xBA
115+
#define _6O2_ 0xBA
116+
#define _7o2_ 0xBC
117+
#define _7O2_ 0xBC
118+
#define _8o2_ 0xBE
119+
#define _8O2_ 0xBE
120+
68121
#if defined(UBRRH) || defined(UBRR0H)
69122
extern HardwareSerial Serial;
70123
#elif defined(USBCON)

0 commit comments

Comments
 (0)