Skip to content

Commit 6ebe14c

Browse files
committed
fix(uart): terminates uart driver whenever rx and tx are detached
1 parent e7df08a commit 6ebe14c

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

cores/esp32/HardwareSerial.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,6 @@ void HardwareSerial::end() {
483483
// including any tasks or debug message channel (log_x()) - but not for IDF log messages!
484484
_onReceiveCB = NULL;
485485
_onReceiveErrorCB = NULL;
486-
if (uartGetDebug() == _uart_nr) {
487-
uartSetDebug(0);
488-
}
489486
_rxFIFOFull = 0;
490487
uartEnd(_uart_nr); // fully detach all pins and delete the UART driver
491488
_destroyEventTask(); // when IDF uart driver is deleted, _eventTask must finish too

cores/esp32/esp32-hal-uart.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,13 +284,21 @@ static bool _uartDetachBus_RX(void *busptr) {
284284
// sanity check - it should never happen
285285
assert(busptr && "_uartDetachBus_RX bus NULL pointer.");
286286
uart_t *bus = (uart_t *)busptr;
287+
if (bus->_txPin < 0) { // both rx and tx pins are detached, terminate the uart driver
288+
uartEnd(bus->num);
289+
return true;
290+
}
287291
return _uartDetachPins(bus->num, bus->_rxPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
288292
}
289293

290294
static bool _uartDetachBus_TX(void *busptr) {
291295
// sanity check - it should never happen
292296
assert(busptr && "_uartDetachBus_TX bus NULL pointer.");
293297
uart_t *bus = (uart_t *)busptr;
298+
if (bus->_rxPin < 0) { // both rx and tx pins are detached, terminate the uart driver
299+
uartEnd(bus->num);
300+
return true;
301+
}
294302
return _uartDetachPins(bus->num, UART_PIN_NO_CHANGE, bus->_txPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
295303
}
296304

@@ -986,6 +994,9 @@ void uartEnd(uint8_t uart_num) {
986994
if (uart_is_driver_installed(uart_num)) {
987995
uart_driver_delete(uart_num);
988996
}
997+
if (uartGetDebug() == uart_num) {
998+
uartSetDebug(0);
999+
}
9891000
UART_MUTEX_UNLOCK();
9901001
}
9911002

0 commit comments

Comments
 (0)