Skip to content

Commit 94ac6b9

Browse files
authored
Merge pull request #12033 from mprse/static_pinmap_serial_fix
static pin-map: patch for SerialBase class
2 parents b7c3b35 + b6c25b1 commit 94ac6b9

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
lines changed

drivers/SerialBase.h

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,9 @@ class SerialBase : private NonCopyable<SerialBase> {
330330
/** Initialize serial port
331331
*/
332332
void _init();
333+
void _init_direct();
334+
/* Pointer to serial init function */
335+
void (SerialBase::*_init_func)();
333336

334337
/** Deinitialize serial port
335338
*/
@@ -345,18 +348,22 @@ class SerialBase : private NonCopyable<SerialBase> {
345348
bool _rx_asynch_set = false;
346349
#endif
347350

348-
serial_t _serial {};
349-
Callback<void()> _irq[IrqCnt];
350-
int _baud;
351-
bool _rx_enabled = true;
352-
bool _tx_enabled = true;
353-
const PinName _tx_pin;
354-
const PinName _rx_pin;
351+
serial_t _serial {};
352+
Callback<void()> _irq[IrqCnt];
353+
int _baud;
354+
bool _rx_enabled = true;
355+
bool _tx_enabled = true;
356+
const PinName _tx_pin;
357+
const PinName _rx_pin;
358+
const serial_pinmap_t *_static_pinmap = NULL;
359+
void (SerialBase::*_set_flow_control_dp_func)(Flow, PinName, PinName) = NULL;
355360

356361
#if DEVICE_SERIAL_FC
357-
Flow _flow_type = Disabled;
358-
PinName _flow1 = NC;
359-
PinName _flow2 = NC;
362+
Flow _flow_type = Disabled;
363+
PinName _flow1 = NC;
364+
PinName _flow2 = NC;
365+
const serial_fc_pinmap_t *_static_pinmap_fc = NULL;
366+
void (SerialBase::*_set_flow_control_sp_func)(Flow, const serial_fc_pinmap_t &) = NULL;
360367
#endif
361368

362369
#endif

drivers/source/SerialBase.cpp

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,16 @@ SerialBase::SerialBase(PinName tx, PinName rx, int baud) :
2929
#endif
3030
_baud(baud),
3131
_tx_pin(tx),
32-
_rx_pin(rx)
32+
_rx_pin(rx),
33+
_init_func(&SerialBase::_init)
3334
{
3435
// No lock needed in the constructor
3536

3637
for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) {
3738
_irq[i] = NULL;
3839
}
3940

40-
_init();
41+
(this->*_init_func)();
4142
}
4243

4344
SerialBase::SerialBase(const serial_pinmap_t &static_pinmap, int baud) :
@@ -50,17 +51,17 @@ SerialBase::SerialBase(const serial_pinmap_t &static_pinmap, int baud) :
5051
_serial(),
5152
_baud(baud),
5253
_tx_pin(static_pinmap.tx_pin),
53-
_rx_pin(static_pinmap.rx_pin)
54+
_rx_pin(static_pinmap.rx_pin),
55+
_static_pinmap(&static_pinmap),
56+
_init_func(&SerialBase::_init_direct)
5457
{
5558
// No lock needed in the constructor
5659

5760
for (size_t i = 0; i < sizeof _irq / sizeof _irq[0]; i++) {
5861
_irq[i] = NULL;
5962
}
6063

61-
serial_init_direct(&_serial, &static_pinmap);
62-
serial_baud(&_serial, _baud);
63-
serial_irq_handler(&_serial, SerialBase::_irq_handler, (uint32_t)this);
64+
(this->*_init_func)();
6465
}
6566

6667
void SerialBase::baud(int baudrate)
@@ -150,7 +151,21 @@ void SerialBase::_init()
150151
{
151152
serial_init(&_serial, _tx_pin, _rx_pin);
152153
#if DEVICE_SERIAL_FC
153-
set_flow_control(_flow_type, _flow1, _flow2);
154+
if (_set_flow_control_dp_func) {
155+
(this->*_set_flow_control_dp_func)(_flow_type, _flow1, _flow2);
156+
}
157+
#endif
158+
serial_baud(&_serial, _baud);
159+
serial_irq_handler(&_serial, SerialBase::_irq_handler, (uint32_t)this);
160+
}
161+
162+
void SerialBase::_init_direct()
163+
{
164+
serial_init_direct(&_serial, _static_pinmap);
165+
#if DEVICE_SERIAL_FC
166+
if (_static_pinmap_fc && _set_flow_control_dp_func) {
167+
(this->*_set_flow_control_sp_func)(_flow_type, *_static_pinmap_fc);
168+
}
154169
#endif
155170
serial_baud(&_serial, _baud);
156171
serial_irq_handler(&_serial, SerialBase::_irq_handler, (uint32_t)this);
@@ -166,7 +181,7 @@ void SerialBase::enable_input(bool enable)
166181
lock();
167182
if (_rx_enabled != enable) {
168183
if (enable && !_tx_enabled) {
169-
_init();
184+
(this->*_init_func)();
170185
}
171186

172187
core_util_critical_section_enter();
@@ -203,7 +218,7 @@ void SerialBase::enable_output(bool enable)
203218
lock();
204219
if (_tx_enabled != enable) {
205220
if (enable && !_rx_enabled) {
206-
_init();
221+
(this->*_init_func)();
207222
}
208223

209224
core_util_critical_section_enter();
@@ -289,6 +304,8 @@ SerialBase::~SerialBase()
289304
#if DEVICE_SERIAL_FC
290305
void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2)
291306
{
307+
MBED_ASSERT(_static_pinmap == NULL); // this function must be used when serial object has been created using dynamic pin-map constructor
308+
_set_flow_control_dp_func = &SerialBase::set_flow_control;
292309
lock();
293310

294311
_flow_type = type;
@@ -318,9 +335,13 @@ void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2)
318335

319336
void SerialBase::set_flow_control(Flow type, const serial_fc_pinmap_t &static_pinmap)
320337
{
338+
MBED_ASSERT(_static_pinmap != NULL); // this function must be used when serial object has been created using static pin-map constructor
339+
_set_flow_control_sp_func = &SerialBase::set_flow_control;
321340
lock();
341+
_static_pinmap_fc = &static_pinmap;
342+
_flow_type = type;
322343
FlowControl flow_type = (FlowControl)type;
323-
serial_set_flow_control_direct(&_serial, flow_type, &static_pinmap);
344+
serial_set_flow_control_direct(&_serial, flow_type, _static_pinmap_fc);
324345
unlock();
325346
}
326347
#endif

0 commit comments

Comments
 (0)