Skip to content

Commit c32caa9

Browse files
committed
Serial: support alternate modes
1 parent 9b74cbc commit c32caa9

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

cores/arduino/Serial.cpp

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,46 @@ using namespace arduino;
3030

3131
void UART::begin(unsigned long baudrate, uint16_t config) {
3232
begin(baudrate);
33-
_serial->format();
33+
int bits = 8;
34+
mbed::SerialBase::Parity parity = mbed::SerialBase::None;
35+
int stop_bits = 1;
36+
37+
switch (config & SERIAL_DATA_MASK) {
38+
case SERIAL_DATA_7:
39+
bits = 7;
40+
break;
41+
case SERIAL_DATA_8:
42+
bits = 8;
43+
break;
44+
/*
45+
case SERIAL_DATA_9:
46+
bits = 9;
47+
break;
48+
*/
49+
}
50+
51+
switch (config & SERIAL_STOP_BIT_MASK) {
52+
case SERIAL_STOP_BIT_1:
53+
stop_bits = 1;
54+
break;
55+
case SERIAL_STOP_BIT_2:
56+
stop_bits = 2;
57+
break;
58+
}
59+
60+
switch (config & SERIAL_PARITY_MASK) {
61+
case SERIAL_PARITY_EVEN:
62+
parity = mbed::SerialBase::Even;
63+
break;
64+
case SERIAL_PARITY_ODD:
65+
parity = mbed::SerialBase::Odd;
66+
break;
67+
case SERIAL_PARITY_NONE:
68+
parity = mbed::SerialBase::None;
69+
break;
70+
}
71+
72+
_serial->format(bits, parity, stop_bits);
3473
}
3574

3675
void UART::begin(unsigned long baudrate) {
@@ -40,7 +79,9 @@ void UART::begin(unsigned long baudrate) {
4079
if (rts != NC) {
4180
_serial->set_flow_control(mbed::SerialBase::Flow::RTSCTS, rts, cts);
4281
}
43-
_serial->attach(mbed::callback(this, &UART::on_rx), mbed::SerialBase::RxIrq);
82+
if (_serial != NULL) {
83+
_serial->attach(mbed::callback(this, &UART::on_rx), mbed::SerialBase::RxIrq);
84+
}
4485
}
4586

4687
void UART::on_rx() {
@@ -52,6 +93,7 @@ void UART::on_rx() {
5293
void UART::end() {
5394
if (_serial != NULL) {
5495
delete _serial;
96+
_serial = NULL;
5597
}
5698
}
5799

0 commit comments

Comments
 (0)