@@ -26,13 +26,13 @@ namespace mbed {
2626BufferedSerial::BufferedSerial (PinName tx, PinName rx, int baud):
2727 SerialBase (tx, rx, baud)
2828{
29- enable_rx_irq ();
29+ update_rx_irq ();
3030}
3131
3232BufferedSerial::BufferedSerial (const serial_pinmap_t &static_pinmap, int baud):
3333 SerialBase (static_pinmap, baud)
3434{
35- enable_rx_irq ();
35+ update_rx_irq ();
3636}
3737
3838BufferedSerial::~BufferedSerial ()
@@ -184,15 +184,7 @@ ssize_t BufferedSerial::write(const void *buffer, size_t length)
184184 data_written++;
185185 }
186186
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 ();
196188 }
197189
198190 api_unlock ();
@@ -228,15 +220,7 @@ ssize_t BufferedSerial::read(void *buffer, size_t length)
228220 data_read++;
229221 }
230222
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 ();
240224
241225 api_unlock ();
242226
@@ -329,27 +313,44 @@ void BufferedSerial::tx_irq(void)
329313 }
330314}
331315
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.
334317 */
335- void BufferedSerial::enable_rx_irq ()
318+ void BufferedSerial::update_rx_irq ()
336319{
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 ();
339329}
340330
331+ /* This is called called from critical section or interrupt context */
341332void BufferedSerial::disable_rx_irq ()
342333{
343334 SerialBase::attach (NULL , RxIrq);
344335 _rx_irq_enabled = false ;
345336}
346337
347- void BufferedSerial::enable_tx_irq ()
338+ /* Attach Tx-IRQ routine to the serial device eventually.
339+ */
340+ void BufferedSerial::update_tx_irq ()
348341{
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 ();
351351}
352352
353+ /* This is called called from critical section or interrupt context */
353354void BufferedSerial::disable_tx_irq ()
354355{
355356 SerialBase::attach (NULL , TxIrq);
@@ -360,6 +361,7 @@ int BufferedSerial::enable_input(bool enabled)
360361{
361362 api_lock ();
362363 SerialBase::enable_input (enabled);
364+ update_rx_irq (); // Eventually enable rx-interrupt to handle incoming data
363365 api_unlock ();
364366
365367 return 0 ;
@@ -369,6 +371,7 @@ int BufferedSerial::enable_output(bool enabled)
369371{
370372 api_lock ();
371373 SerialBase::enable_output (enabled);
374+ update_tx_irq (); // Eventually enable tx-interrupt to flush buffered data
372375 api_unlock ();
373376
374377 return 0 ;
0 commit comments