@@ -192,14 +192,7 @@ ssize_t UARTSerial::write(const void *buffer, size_t length)
192
192
data_written++;
193
193
}
194
194
195
- core_util_critical_section_enter ();
196
- if (_tx_enabled && !_tx_irq_enabled) {
197
- UARTSerial::tx_irq (); // only write to hardware in one place
198
- if (!_txbuf.empty ()) {
199
- enable_tx_irq ();
200
- }
201
- }
202
- core_util_critical_section_exit ();
195
+ enable_tx_irq ();
203
196
}
204
197
205
198
api_unlock ();
@@ -234,14 +227,7 @@ ssize_t UARTSerial::read(void *buffer, size_t length)
234
227
data_read++;
235
228
}
236
229
237
- core_util_critical_section_enter ();
238
- if (_rx_enabled && !_rx_irq_enabled) {
239
- UARTSerial::rx_irq (); // only read from hardware in one place
240
- if (!_rxbuf.full ()) {
241
- enable_rx_irq ();
242
- }
243
- }
244
- core_util_critical_section_exit ();
230
+ enable_rx_irq ();
245
231
246
232
api_unlock ();
247
233
@@ -351,8 +337,15 @@ void UARTSerial::tx_irq(void)
351
337
/* These are all called from critical section */
352
338
void UARTSerial::enable_rx_irq ()
353
339
{
354
- SerialBase::attach (callback (this , &UARTSerial::rx_irq), RxIrq);
355
- _rx_irq_enabled = true ;
340
+ core_util_critical_section_enter ();
341
+ if (_rx_enabled && !_rx_irq_enabled) {
342
+ UARTSerial::rx_irq (); // only read from hardware in one place
343
+ if (!_rxbuf.full ()) {
344
+ SerialBase::attach (callback (this , &UARTSerial::rx_irq), RxIrq);
345
+ _rx_irq_enabled = true ;
346
+ }
347
+ }
348
+ core_util_critical_section_exit ();
356
349
}
357
350
358
351
void UARTSerial::disable_rx_irq ()
@@ -363,8 +356,15 @@ void UARTSerial::disable_rx_irq()
363
356
364
357
void UARTSerial::enable_tx_irq ()
365
358
{
366
- SerialBase::attach (callback (this , &UARTSerial::tx_irq), TxIrq);
367
- _tx_irq_enabled = true ;
359
+ core_util_critical_section_enter ();
360
+ if (_tx_enabled && !_tx_irq_enabled) {
361
+ UARTSerial::tx_irq (); // only write to hardware in one place
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 ();
368
368
}
369
369
370
370
void UARTSerial::disable_tx_irq ()
@@ -377,6 +377,7 @@ int UARTSerial::enable_input(bool enabled)
377
377
{
378
378
api_lock ();
379
379
SerialBase::enable_input (enabled);
380
+ enable_rx_irq (); // Enable interrupt to handle incoming data
380
381
api_unlock ();
381
382
382
383
return 0 ;
@@ -386,6 +387,7 @@ int UARTSerial::enable_output(bool enabled)
386
387
{
387
388
api_lock ();
388
389
SerialBase::enable_output (enabled);
390
+ enable_tx_irq (); // Enable interrupt to flush buffered data
389
391
api_unlock ();
390
392
391
393
return 0 ;
0 commit comments