@@ -28,25 +28,21 @@ UARTSerial::UARTSerial(PinName tx, PinName rx, int baud) :
28
28
_blocking (true ),
29
29
_tx_irq_enabled (false ),
30
30
_rx_irq_enabled (false ),
31
- _tx_enabled (true ),
32
- _rx_enabled (true ),
33
31
_dcd_irq (NULL )
34
32
{
35
33
/* Attatch IRQ routines to the serial device. */
36
- enable_rx_irq ();
34
+ update_rx_irq ();
37
35
}
38
36
39
37
UARTSerial::UARTSerial (const serial_pinmap_t &static_pinmap, int baud) :
40
38
SerialBase (static_pinmap, baud),
41
39
_blocking (true ),
42
40
_tx_irq_enabled (false ),
43
41
_rx_irq_enabled (false ),
44
- _tx_enabled (true ),
45
- _rx_enabled (true ),
46
42
_dcd_irq (NULL )
47
43
{
48
44
/* Attatch IRQ routines to the serial device. */
49
- enable_rx_irq ();
45
+ update_rx_irq ();
50
46
}
51
47
52
48
UARTSerial::~UARTSerial ()
@@ -196,14 +192,7 @@ ssize_t UARTSerial::write(const void *buffer, size_t length)
196
192
data_written++;
197
193
}
198
194
199
- core_util_critical_section_enter ();
200
- if (_tx_enabled && !_tx_irq_enabled) {
201
- UARTSerial::tx_irq (); // only write to hardware in one place
202
- if (!_txbuf.empty ()) {
203
- enable_tx_irq ();
204
- }
205
- }
206
- core_util_critical_section_exit ();
195
+ update_tx_irq ();
207
196
}
208
197
209
198
api_unlock ();
@@ -238,14 +227,7 @@ ssize_t UARTSerial::read(void *buffer, size_t length)
238
227
data_read++;
239
228
}
240
229
241
- core_util_critical_section_enter ();
242
- if (_rx_enabled && !_rx_irq_enabled) {
243
- UARTSerial::rx_irq (); // only read from hardware in one place
244
- if (!_rxbuf.full ()) {
245
- enable_rx_irq ();
246
- }
247
- }
248
- core_util_critical_section_exit ();
230
+ update_rx_irq ();
249
231
250
232
api_unlock ();
251
233
@@ -352,25 +334,40 @@ void UARTSerial::tx_irq(void)
352
334
}
353
335
}
354
336
355
- /* These are all called from critical section */
356
- void UARTSerial::enable_rx_irq ()
337
+ void UARTSerial::update_rx_irq ()
357
338
{
358
- SerialBase::attach (callback (this , &UARTSerial::rx_irq), RxIrq);
359
- _rx_irq_enabled = true ;
339
+ core_util_critical_section_enter ();
340
+ if (_rx_enabled && !_rx_irq_enabled) {
341
+ UARTSerial::rx_irq ();
342
+ if (!_rxbuf.full ()) {
343
+ SerialBase::attach (callback (this , &UARTSerial::rx_irq), RxIrq);
344
+ _rx_irq_enabled = true ;
345
+ }
346
+ }
347
+ core_util_critical_section_exit ();
360
348
}
361
349
350
+ /* This is called called from critical section or interrupt context */
362
351
void UARTSerial::disable_rx_irq ()
363
352
{
364
353
SerialBase::attach (NULL , RxIrq);
365
354
_rx_irq_enabled = false ;
366
355
}
367
356
368
- void UARTSerial::enable_tx_irq ()
357
+ void UARTSerial::update_tx_irq ()
369
358
{
370
- SerialBase::attach (callback (this , &UARTSerial::tx_irq), TxIrq);
371
- _tx_irq_enabled = true ;
359
+ core_util_critical_section_enter ();
360
+ if (_tx_enabled && !_tx_irq_enabled) {
361
+ UARTSerial::tx_irq ();
362
+ if (!_txbuf.empty ()) {
363
+ SerialBase::attach (callback (this , &UARTSerial::tx_irq), TxIrq);
364
+ _tx_irq_enabled = true ;
365
+ }
366
+ }
367
+ core_util_critical_section_exit ();
372
368
}
373
369
370
+ /* This is called called from critical section or interrupt context */
374
371
void UARTSerial::disable_tx_irq ()
375
372
{
376
373
SerialBase::attach (NULL , TxIrq);
@@ -381,6 +378,7 @@ int UARTSerial::enable_input(bool enabled)
381
378
{
382
379
api_lock ();
383
380
SerialBase::enable_input (enabled);
381
+ update_rx_irq (); // Eventually enable rx-interrupt to handle incoming data
384
382
api_unlock ();
385
383
386
384
return 0 ;
@@ -390,6 +388,7 @@ int UARTSerial::enable_output(bool enabled)
390
388
{
391
389
api_lock ();
392
390
SerialBase::enable_output (enabled);
391
+ update_tx_irq (); // Eventually enable tx-interrupt to flush buffered data
393
392
api_unlock ();
394
393
395
394
return 0 ;
0 commit comments