Skip to content

Commit 66c43f7

Browse files
committed
feat(uart): C wrapper to end related Serial object
1 parent 6f614a0 commit 66c43f7

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

cores/esp32/HardwareSerial.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,31 @@ HardwareSerial Serial5(5);
6161
extern void HWCDCSerialEvent(void) __attribute__((weak));
6262
#endif
6363

64+
// C-callable helper used by HAL when pins are detached and the high-level
65+
// HardwareSerial instance must be finalized.
66+
extern "C" void hal_uart_notify_pins_detached(int uart_num) {
67+
log_d("hal_uart_notify_pins_detached: Notifying HardwareSerial for UART%d", uart_num);
68+
switch (uart_num) {
69+
case UART_NUM_0: Serial0.end(); break;
70+
#if SOC_UART_NUM > 1
71+
case UART_NUM_1: Serial1.end(); break;
72+
#endif
73+
#if SOC_UART_NUM > 2
74+
case UART_NUM_2: Serial2.end(); break;
75+
#endif
76+
#if SOC_UART_NUM > 3
77+
case UART_NUM_3: Serial3.end(); break;
78+
#endif
79+
#if SOC_UART_NUM > 4
80+
case UART_NUM_4: Serial4.end(); break;
81+
#endif
82+
#if SOC_UART_NUM > 5
83+
case UART_NUM_4: Serial5.end(); break;
84+
#endif
85+
default: break;
86+
}
87+
}
88+
6489
#if USB_SERIAL_IS_DEFINED == 1 // Native USB CDC Event
6590
// Used by Hardware Serial for USB CDC events
6691
extern void USBSerialEvent(void) __attribute__((weak));

cores/esp32/esp32-hal-uart.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
static int s_uart_debug_nr = 0; // UART number for debug output
4343
#define REF_TICK_BAUDRATE_LIMIT 250000 // this is maximum UART badrate using REF_TICK as clock
4444

45+
/* C prototype for the notifier implemented in HardwareSerial.cpp */
46+
extern void hal_uart_notify_pins_detached(int uart_num);
47+
4548
struct uart_struct_t {
4649

4750
#if !CONFIG_DISABLE_HAL_LOCKS
@@ -293,7 +296,7 @@ static bool _uartDetachBus_RX(void *busptr) {
293296
}
294297
if (bus->_txPin < 0) { // both rx and tx pins are detached, terminate the uart driver
295298
log_d("_uartDetachBus_RX: both RX and TX pins detached for UART%d, terminating driver", bus->num);
296-
uartEnd(bus->num);
299+
hal_uart_notify_pins_detached(bus->num);
297300
return true;
298301
}
299302
return _uartDetachPins(bus->num, bus->_rxPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
@@ -312,7 +315,7 @@ static bool _uartDetachBus_TX(void *busptr) {
312315
}
313316
if (bus->_rxPin < 0) { // both rx and tx pins are detached, terminate the uart driver
314317
log_d("_uartDetachBus_TX: both RX and TX pins detached for UART%d, terminating driver", bus->num);
315-
uartEnd(bus->num);
318+
hal_uart_notify_pins_detached(bus->num);
316319
return true;
317320
}
318321
return _uartDetachPins(bus->num, UART_PIN_NO_CHANGE, bus->_txPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);

0 commit comments

Comments
 (0)