@@ -26,13 +26,13 @@ namespace mbed {
26
26
BufferedSerial::BufferedSerial (PinName tx, PinName rx, int baud):
27
27
SerialBase (tx, rx, baud)
28
28
{
29
- enable_rx_irq ();
29
+ update_rx_irq ();
30
30
}
31
31
32
32
BufferedSerial::BufferedSerial (const serial_pinmap_t &static_pinmap, int baud):
33
33
SerialBase (static_pinmap, baud)
34
34
{
35
- enable_rx_irq ();
35
+ update_rx_irq ();
36
36
}
37
37
38
38
BufferedSerial::~BufferedSerial ()
@@ -184,15 +184,7 @@ ssize_t BufferedSerial::write(const void *buffer, size_t length)
184
184
data_written++;
185
185
}
186
186
187
- core_util_critical_section_enter ();
188
- if (_tx_enabled && !_tx_irq_enabled) {
189
- // only write to hardware in one place
190
- BufferedSerial::tx_irq ();
191
- if (!_txbuf.empty ()) {
192
- enable_tx_irq ();
193
- }
194
- }
195
- core_util_critical_section_exit ();
187
+ update_tx_irq ();
196
188
}
197
189
198
190
api_unlock ();
@@ -228,15 +220,7 @@ ssize_t BufferedSerial::read(void *buffer, size_t length)
228
220
data_read++;
229
221
}
230
222
231
- core_util_critical_section_enter ();
232
- if (_rx_enabled && !_rx_irq_enabled) {
233
- // only read from hardware in one place
234
- BufferedSerial::rx_irq ();
235
- if (!_rxbuf.full ()) {
236
- enable_rx_irq ();
237
- }
238
- }
239
- core_util_critical_section_exit ();
223
+ update_rx_irq ();
240
224
241
225
api_unlock ();
242
226
@@ -329,27 +313,44 @@ void BufferedSerial::tx_irq(void)
329
313
}
330
314
}
331
315
332
- /* These are all called from critical section
333
- * Attatch IRQ routines to the serial device.
316
+ /* Attach Rx-IRQ routine to the serial device eventually.
334
317
*/
335
- void BufferedSerial::enable_rx_irq ()
318
+ void BufferedSerial::update_rx_irq ()
336
319
{
337
- SerialBase::attach (callback (this , &BufferedSerial::rx_irq), RxIrq);
338
- _rx_irq_enabled = true ;
320
+ core_util_critical_section_enter ();
321
+ if (_rx_enabled && !_rx_irq_enabled) {
322
+ BufferedSerial::rx_irq ();
323
+ if (!_rxbuf.full ()) {
324
+ SerialBase::attach (callback (this , &BufferedSerial::rx_irq), RxIrq);
325
+ _rx_irq_enabled = true ;
326
+ }
327
+ }
328
+ core_util_critical_section_exit ();
339
329
}
340
330
331
+ /* This is called called from critical section or interrupt context */
341
332
void BufferedSerial::disable_rx_irq ()
342
333
{
343
334
SerialBase::attach (NULL , RxIrq);
344
335
_rx_irq_enabled = false ;
345
336
}
346
337
347
- void BufferedSerial::enable_tx_irq ()
338
+ /* Attach Tx-IRQ routine to the serial device eventually.
339
+ */
340
+ void BufferedSerial::update_tx_irq ()
348
341
{
349
- SerialBase::attach (callback (this , &BufferedSerial::tx_irq), TxIrq);
350
- _tx_irq_enabled = true ;
342
+ core_util_critical_section_enter ();
343
+ if (_tx_enabled && !_tx_irq_enabled) {
344
+ BufferedSerial::tx_irq ();
345
+ if (!_txbuf.empty ()) {
346
+ SerialBase::attach (callback (this , &BufferedSerial::tx_irq), TxIrq);
347
+ _tx_irq_enabled = true ;
348
+ }
349
+ }
350
+ core_util_critical_section_exit ();
351
351
}
352
352
353
+ /* This is called called from critical section or interrupt context */
353
354
void BufferedSerial::disable_tx_irq ()
354
355
{
355
356
SerialBase::attach (NULL , TxIrq);
@@ -360,6 +361,7 @@ int BufferedSerial::enable_input(bool enabled)
360
361
{
361
362
api_lock ();
362
363
SerialBase::enable_input (enabled);
364
+ update_rx_irq (); // Eventually enable rx-interrupt to handle incoming data
363
365
api_unlock ();
364
366
365
367
return 0 ;
@@ -369,6 +371,7 @@ int BufferedSerial::enable_output(bool enabled)
369
371
{
370
372
api_lock ();
371
373
SerialBase::enable_output (enabled);
374
+ update_tx_irq (); // Eventually enable tx-interrupt to flush buffered data
372
375
api_unlock ();
373
376
374
377
return 0 ;
0 commit comments