Skip to content

Commit e0069d8

Browse files
authored
Merge pull request #14678 from ghseb/uartserial-shadowing-2
Serial: remove shadowing member variables
2 parents ae62f9d + 77b5ffc commit e0069d8

File tree

2 files changed

+35
-34
lines changed

2 files changed

+35
-34
lines changed

drivers/include/drivers/BufferedSerial.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,19 +302,19 @@ class BufferedSerial:
302302
ssize_t write_unbuffered(const char *buf_ptr, size_t length);
303303

304304
/** Enable processing of byte reception IRQs and register a callback to
305-
* process them.
305+
* process them if the IRQs are not yet enabled and reception is enabled.
306306
*/
307-
void enable_rx_irq();
307+
void update_rx_irq();
308308

309309
/** Disable processing of byte reception IRQs and de-register callback to
310310
* process them.
311311
*/
312312
void disable_rx_irq();
313313

314314
/** Enable processing of byte transmission IRQs and register a callback to
315-
* process them.
315+
* process them if the IRQs are not yet enabled and transmission is enabled.
316316
*/
317-
void enable_tx_irq();
317+
void update_tx_irq();
318318

319319
/** Disable processing of byte transmission IRQs and de-register callback to
320320
* process them.
@@ -335,8 +335,6 @@ class BufferedSerial:
335335
bool _blocking = true;
336336
bool _tx_irq_enabled = false;
337337
bool _rx_irq_enabled = false;
338-
bool _tx_enabled = true;
339-
bool _rx_enabled = true;
340338
InterruptIn *_dcd_irq = nullptr;
341339

342340
/** Device Hanged up

drivers/source/BufferedSerial.cpp

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ namespace mbed {
2626
BufferedSerial::BufferedSerial(PinName tx, PinName rx, int baud):
2727
SerialBase(tx, rx, baud)
2828
{
29-
enable_rx_irq();
29+
update_rx_irq();
3030
}
3131

3232
BufferedSerial::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

3838
BufferedSerial::~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 */
341332
void 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 */
353354
void 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

Comments
 (0)