@@ -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
4344SerialBase::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
6667void 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
290305void 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
319336void 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