@@ -29,15 +29,16 @@ SerialBase::SerialBase(PinName tx, PinName rx, int baud) :
29
29
#endif
30
30
_baud (baud),
31
31
_tx_pin (tx),
32
- _rx_pin (rx)
32
+ _rx_pin (rx),
33
+ _init_func (&SerialBase::_init)
33
34
{
34
35
// No lock needed in the constructor
35
36
36
37
for (size_t i = 0 ; i < sizeof _irq / sizeof _irq[0 ]; i++) {
37
38
_irq[i] = NULL ;
38
39
}
39
40
40
- _init ();
41
+ ( this ->*_init_func) ();
41
42
}
42
43
43
44
SerialBase::SerialBase (const serial_pinmap_t &static_pinmap, int baud) :
@@ -50,17 +51,17 @@ SerialBase::SerialBase(const serial_pinmap_t &static_pinmap, int baud) :
50
51
_serial (),
51
52
_baud (baud),
52
53
_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)
54
57
{
55
58
// No lock needed in the constructor
56
59
57
60
for (size_t i = 0 ; i < sizeof _irq / sizeof _irq[0 ]; i++) {
58
61
_irq[i] = NULL ;
59
62
}
60
63
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)();
64
65
}
65
66
66
67
void SerialBase::baud (int baudrate)
@@ -150,7 +151,21 @@ void SerialBase::_init()
150
151
{
151
152
serial_init (&_serial, _tx_pin, _rx_pin);
152
153
#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
+ }
154
169
#endif
155
170
serial_baud (&_serial, _baud);
156
171
serial_irq_handler (&_serial, SerialBase::_irq_handler, (uint32_t )this );
@@ -166,7 +181,7 @@ void SerialBase::enable_input(bool enable)
166
181
lock ();
167
182
if (_rx_enabled != enable) {
168
183
if (enable && !_tx_enabled) {
169
- _init ();
184
+ ( this ->*_init_func) ();
170
185
}
171
186
172
187
core_util_critical_section_enter ();
@@ -203,7 +218,7 @@ void SerialBase::enable_output(bool enable)
203
218
lock ();
204
219
if (_tx_enabled != enable) {
205
220
if (enable && !_rx_enabled) {
206
- _init ();
221
+ ( this ->*_init_func) ();
207
222
}
208
223
209
224
core_util_critical_section_enter ();
@@ -289,6 +304,8 @@ SerialBase::~SerialBase()
289
304
#if DEVICE_SERIAL_FC
290
305
void SerialBase::set_flow_control (Flow type, PinName flow1, PinName flow2)
291
306
{
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;
292
309
lock ();
293
310
294
311
_flow_type = type;
@@ -318,9 +335,13 @@ void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2)
318
335
319
336
void SerialBase::set_flow_control (Flow type, const serial_fc_pinmap_t &static_pinmap)
320
337
{
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;
321
340
lock ();
341
+ _static_pinmap_fc = &static_pinmap;
342
+ _flow_type = type;
322
343
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 );
324
345
unlock ();
325
346
}
326
347
#endif
0 commit comments