Skip to content

Commit bef6854

Browse files
committed
Serial, SerialBase, UARTSerial: Add explicit pinmap support
1 parent 3d2bebd commit bef6854

File tree

6 files changed

+87
-0
lines changed

6 files changed

+87
-0
lines changed

drivers/Serial.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ class Serial : public SerialBase, public Stream, private NonCopyable<Serial> {
7373
*/
7474
Serial(PinName tx, PinName rx, const char *name = NULL, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
7575

76+
/** Create a Serial port, connected to the specified transmit and receive pins
77+
*
78+
* @param explicit_pinmap reference to strucure which holds static pinmap.
79+
* @param name The name of the stream associated with this serial port (optional)
80+
* @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE or 9600)
81+
*
82+
* @note
83+
* Either tx or rx may be specified as NC (Not Connected) if unused
84+
*/
85+
Serial(const serial_pinmap_t &explicit_pinmap, const char *name = NULL, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
7686

7787
/** Create a Serial port, connected to the specified transmit and receive pins, with the specified baud
7888
*
@@ -85,6 +95,16 @@ class Serial : public SerialBase, public Stream, private NonCopyable<Serial> {
8595
*/
8696
Serial(PinName tx, PinName rx, int baud);
8797

98+
/** Create a Serial port, connected to the specified transmit and receive pins, with the specified baud
99+
*
100+
* @param explicit_pinmap reference to strucure which holds static pinmap.
101+
* @param baud The baud rate of the serial port
102+
*
103+
* @note
104+
* Either tx or rx may be specified as NC (Not Connected) if unused
105+
*/
106+
Serial(const serial_pinmap_t &explicit_pinmap, int baud);
107+
88108
/* Stream gives us a FileHandle with non-functional poll()/readable()/writable. Pass through
89109
* the calls from the SerialBase instead for backwards compatibility. This problem is
90110
* part of why Stream and Serial should be deprecated.

drivers/SerialBase.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,13 @@ class SerialBase : private NonCopyable<SerialBase> {
200200
* @param flow2 the second flow control pin (CTS for RTSCTS)
201201
*/
202202
void set_flow_control(Flow type, PinName flow1 = NC, PinName flow2 = NC);
203+
204+
/** Set the flow control type on the serial port
205+
*
206+
* @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
207+
* @param pinmap reference to strucure which holds static pinmap
208+
*/
209+
void set_flow_control(Flow type, const serial_fc_pinmap_t &explicit_pinmap);
203210
#endif
204211

205212
static void _irq_handler(uint32_t id, SerialIrq irq_type);
@@ -313,6 +320,7 @@ class SerialBase : private NonCopyable<SerialBase> {
313320
#if !defined(DOXYGEN_ONLY)
314321
protected:
315322
SerialBase(PinName tx, PinName rx, int baud);
323+
SerialBase(const serial_pinmap_t &explicit_pinmap, int baud);
316324
virtual ~SerialBase();
317325

318326
int _base_getc();

drivers/UARTSerial.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ class UARTSerial : private SerialBase, public FileHandle, private NonCopyable<UA
5858
* @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
5959
*/
6060
UARTSerial(PinName tx, PinName rx, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
61+
62+
/** Create a UARTSerial port, connected to the specified transmit and receive pins, with a particular baud rate.
63+
* @param explicit_pinmap reference to strucure which holds static pinmap
64+
* @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
65+
*/
66+
UARTSerial(const serial_pinmap_t &explicit_pinmap, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
67+
6168
virtual ~UARTSerial();
6269

6370
/** Equivalent to POSIX poll(). Derived from FileHandle.

drivers/source/Serial.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,18 @@ Serial::Serial(PinName tx, PinName rx, const char *name, int baud) : SerialBase(
2424
{
2525
}
2626

27+
Serial::Serial(const serial_pinmap_t &explicit_pinmap, const char *name, int baud) : SerialBase(explicit_pinmap, baud), Stream(name)
28+
{
29+
}
30+
2731
Serial::Serial(PinName tx, PinName rx, int baud): SerialBase(tx, rx, baud), Stream(NULL)
2832
{
2933
}
3034

35+
Serial::Serial(const serial_pinmap_t &explicit_pinmap, int baud): SerialBase(explicit_pinmap, baud), Stream(NULL)
36+
{
37+
}
38+
3139
int Serial::_getc()
3240
{
3341
// Mutex is already held

drivers/source/SerialBase.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,29 @@ SerialBase::SerialBase(PinName tx, PinName rx, int baud) :
4040
_init();
4141
}
4242

43+
SerialBase::SerialBase(const serial_pinmap_t &explicit_pinmap, int baud) :
44+
#if DEVICE_SERIAL_ASYNCH
45+
_thunk_irq(this), _tx_usage(DMA_USAGE_NEVER),
46+
_rx_usage(DMA_USAGE_NEVER), _tx_callback(NULL),
47+
_rx_callback(NULL), _tx_asynch_set(false),
48+
_rx_asynch_set(false),
49+
#endif
50+
_serial(),
51+
_baud(baud),
52+
_tx_pin(explicit_pinmap.tx_pin),
53+
_rx_pin(explicit_pinmap.rx_pin)
54+
{
55+
// No lock needed in the constructor
56+
57+
for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) {
58+
_irq[i] = NULL;
59+
}
60+
61+
serial_init_direct(&_serial, &explicit_pinmap);
62+
serial_baud(&_serial, _baud);
63+
serial_irq_handler(&_serial, SerialBase::_irq_handler, (uint32_t)this);
64+
}
65+
4366
void SerialBase::baud(int baudrate)
4467
{
4568
lock();
@@ -292,6 +315,14 @@ void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2)
292315
}
293316
unlock();
294317
}
318+
319+
void SerialBase::set_flow_control(Flow type, const serial_fc_pinmap_t &explicit_pinmap)
320+
{
321+
lock();
322+
FlowControl flow_type = (FlowControl)type;
323+
serial_set_flow_control_direct(&_serial, flow_type, &explicit_pinmap);
324+
unlock();
325+
}
295326
#endif
296327

297328
#if DEVICE_SERIAL_ASYNCH

drivers/source/UARTSerial.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ UARTSerial::UARTSerial(PinName tx, PinName rx, int baud) :
3636
enable_rx_irq();
3737
}
3838

39+
UARTSerial::UARTSerial(const serial_pinmap_t &explicit_pinmap, int baud) :
40+
SerialBase(explicit_pinmap, baud),
41+
_blocking(true),
42+
_tx_irq_enabled(false),
43+
_rx_irq_enabled(false),
44+
_tx_enabled(true),
45+
_rx_enabled(true),
46+
_dcd_irq(NULL)
47+
{
48+
/* Attatch IRQ routines to the serial device. */
49+
enable_rx_irq();
50+
}
51+
3952
UARTSerial::~UARTSerial()
4053
{
4154
delete _dcd_irq;

0 commit comments

Comments
 (0)