Skip to content

Commit 4312b0b

Browse files
author
Bogdan Marinescu
committed
Added a baud argument for (Raw)Serial objects
This commit adds a `baud` argument for Serial and RawSerial objects: - there is a new `Serial` constructor with a mandatory baud rate argument. The old constructor also got a `baud` argument with a default value (to keep backward compatibility). - the `RawSerial` constructor also got a `baud` argument with a default value. There's also a new configuration parameter (`default-serial-baud-rate`) that can be used to specify the default value of the above `baud` arguments.
1 parent 58c12f1 commit 4312b0b

File tree

7 files changed

+32
-8
lines changed

7 files changed

+32
-8
lines changed

hal/api/RawSerial.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,16 @@ namespace mbed {
5050
class RawSerial: public SerialBase {
5151

5252
public:
53-
/** Create a RawSerial port, connected to the specified transmit and receive pins
53+
/** Create a RawSerial port, connected to the specified transmit and receive pins, with the specified baud.
5454
*
5555
* @param tx Transmit pin
5656
* @param rx Receive pin
57+
* @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_CORE_DEFAULT_SERIAL_BAUD_RATE)
5758
*
5859
* @note
5960
* Either tx or rx may be specified as NC if unused
6061
*/
61-
RawSerial(PinName tx, PinName rx);
62+
RawSerial(PinName tx, PinName rx, int baud = MBED_CONF_CORE_DEFAULT_SERIAL_BAUD_RATE);
6263

6364
/** Write a char to the serial port
6465
*

hal/api/Serial.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,25 @@ class Serial : public SerialBase, public Stream {
5959
*
6060
* @param tx Transmit pin
6161
* @param rx Receive pin
62+
* @param name The name of the stream associated with this serial port (optional)
63+
* @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_CORE_DEFAULT_SERIAL_BAUD_RATE)
6264
*
6365
* @note
6466
* Either tx or rx may be specified as NC if unused
6567
*/
66-
Serial(PinName tx, PinName rx, const char *name=NULL);
68+
Serial(PinName tx, PinName rx, const char *name=NULL, int baud = MBED_CONF_CORE_DEFAULT_SERIAL_BAUD_RATE);
69+
70+
71+
/** Create a Serial port, connected to the specified transmit and receive pins, with the specified baud
72+
*
73+
* @param tx Transmit pin
74+
* @param rx Receive pin
75+
* @param baud The baud rate of the serial port
76+
*
77+
* @note
78+
* Either tx or rx may be specified as NC if unused
79+
*/
80+
Serial(PinName tx, PinName rx, int baud);
6781

6882
protected:
6983
virtual int _getc();

hal/api/SerialBase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ class SerialBase {
230230
#endif
231231

232232
protected:
233-
SerialBase(PinName tx, PinName rx);
233+
SerialBase(PinName tx, PinName rx, int baud);
234234
virtual ~SerialBase() {
235235
}
236236

hal/common/RawSerial.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
namespace mbed {
2525

26-
RawSerial::RawSerial(PinName tx, PinName rx) : SerialBase(tx, rx) {
26+
RawSerial::RawSerial(PinName tx, PinName rx, int baud) : SerialBase(tx, rx, baud) {
2727
// No lock needed in the constructor
2828
}
2929

hal/common/Serial.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020

2121
namespace mbed {
2222

23-
Serial::Serial(PinName tx, PinName rx, const char *name) : SerialBase(tx, rx), Stream(name) {
23+
Serial::Serial(PinName tx, PinName rx, const char *name, int baud) : SerialBase(tx, rx, baud), Stream(name) {
24+
}
25+
26+
Serial::Serial(PinName tx, PinName rx, int baud): SerialBase(tx, rx, baud), Stream(NULL) {
2427
}
2528

2629
int Serial::_getc() {

hal/common/SerialBase.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,20 @@ namespace mbed {
2323

2424
static void donothing() {};
2525

26-
SerialBase::SerialBase(PinName tx, PinName rx) :
26+
SerialBase::SerialBase(PinName tx, PinName rx, int baud) :
2727
#if DEVICE_SERIAL_ASYNCH
2828
_thunk_irq(this), _tx_usage(DMA_USAGE_NEVER),
2929
_rx_usage(DMA_USAGE_NEVER),
3030
#endif
31-
_serial(), _baud(9600) {
31+
_serial(), _baud(baud) {
3232
// No lock needed in the constructor
3333

3434
for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) {
3535
_irq[i].attach(donothing);
3636
}
3737

3838
serial_init(&_serial, tx, rx);
39+
serial_baud(&_serial, _baud);
3940
serial_irq_handler(&_serial, SerialBase::_irq_handler, (uint32_t)this);
4041
}
4142

mbed_lib.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
"stdio-flush-at-exit": {
1515
"help": "Enable or disable the flush of standard I/O's at exit.",
1616
"value": true
17+
},
18+
19+
"default-serial-baud-rate": {
20+
"help": "Default baud rate for a Serial or RawSerial instance (if not specified in the constructor)",
21+
"value": 9600
1722
}
1823
},
1924
"target_overrides": {

0 commit comments

Comments
 (0)