From 55b823cc38037666afee69f9f89b1cab6c477253 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Fri, 3 Oct 2025 12:22:49 -0300 Subject: [PATCH 1/7] feat(uart): refactor PeriMan detaching function init Removed uart_init_PeriMan function and related comments. --- cores/esp32/esp32-hal-uart.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index 7b194679caa..3b88dae5901 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -479,6 +479,10 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t ret &= perimanSetPinBus(rxPin, ESP32_BUS_TYPE_UART_RX, (void *)uart, uart_num, -1); if (ret) { uart->_rxPin = rxPin; + // set Peripheral Manager deInit Callback for this UART pin + if (perimanGetBusDeinit(ESP32_BUS_TYPE_UART_RX) == NULL) { + perimanSetBusDeinit(ESP32_BUS_TYPE_UART_RX, _uartDetachBus_RX); + } } } if (!ret) { @@ -502,6 +506,10 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t ret &= perimanSetPinBus(txPin, ESP32_BUS_TYPE_UART_TX, (void *)uart, uart_num, -1); if (ret) { uart->_txPin = txPin; + // set Peripheral Manager deInit Callback for this UART pin + if (perimanGetBusDeinit(ESP32_BUS_TYPE_UART_TX) == NULL) { + perimanSetBusDeinit(ESP32_BUS_TYPE_UART_TX, _uartDetachBus_TX); + } } } if (!ret) { @@ -525,6 +533,10 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t ret &= perimanSetPinBus(ctsPin, ESP32_BUS_TYPE_UART_CTS, (void *)uart, uart_num, -1); if (ret) { uart->_ctsPin = ctsPin; + // set Peripheral Manager deInit Callback for this UART pin + if (perimanGetBusDeinit(ESP32_BUS_TYPE_UART_CTS) == NULL) { + perimanSetBusDeinit(ESP32_BUS_TYPE_UART_CTS, _uartDetachBus_CTS); + } } } if (!ret) { @@ -548,6 +560,10 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t ret &= perimanSetPinBus(rtsPin, ESP32_BUS_TYPE_UART_RTS, (void *)uart, uart_num, -1); if (ret) { uart->_rtsPin = rtsPin; + // set Peripheral Manager deInit Callback for this UART pin + if (perimanGetBusDeinit(ESP32_BUS_TYPE_UART_RTS) == NULL) { + perimanSetBusDeinit(ESP32_BUS_TYPE_UART_RTS, _uartDetachBus_RTS); + } } } if (!ret) { @@ -567,14 +583,6 @@ int8_t uart_get_TxPin(uint8_t uart_num) { return _uart_bus_array[uart_num]._txPin; } -void uart_init_PeriMan(void) { - // set Peripheral Manager deInit Callback for each UART pin - perimanSetBusDeinit(ESP32_BUS_TYPE_UART_RX, _uartDetachBus_RX); - perimanSetBusDeinit(ESP32_BUS_TYPE_UART_TX, _uartDetachBus_TX); - perimanSetBusDeinit(ESP32_BUS_TYPE_UART_CTS, _uartDetachBus_CTS); - perimanSetBusDeinit(ESP32_BUS_TYPE_UART_RTS, _uartDetachBus_RTS); -} - // Routines that take care of UART events will be in the HardwareSerial Class code void uartGetEventQueue(uart_t *uart, QueueHandle_t *q) { // passing back NULL for the Queue pointer when UART is not initialized yet From a82038051a302773f8cc9d0cb371f027b3fdb1c6 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Fri, 3 Oct 2025 12:26:41 -0300 Subject: [PATCH 2/7] feat(uart): remove uart_init_PeriMan function declaration Removed unused uart_init_PeriMan function declaration. --- cores/esp32/esp32-hal-uart.h | 1 - 1 file changed, 1 deletion(-) diff --git a/cores/esp32/esp32-hal-uart.h b/cores/esp32/esp32-hal-uart.h index 41b005aa151..3af0d7ab96c 100644 --- a/cores/esp32/esp32-hal-uart.h +++ b/cores/esp32/esp32-hal-uart.h @@ -79,7 +79,6 @@ bool uartSetPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t ctsPin, in // helper functions int8_t uart_get_RxPin(uint8_t uart_num); int8_t uart_get_TxPin(uint8_t uart_num); -void uart_init_PeriMan(void); // Enables or disables HW Flow Control function -- needs also to set CTS and/or RTS pins // UART_HW_FLOWCTRL_DISABLE = 0x0 disable hardware flow control From 6a512b1d1b929f377dc7e64305fee2f37d89e0fb Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Fri, 3 Oct 2025 12:28:06 -0300 Subject: [PATCH 3/7] feat(uart): remove uart_init_PeriMan call in constructor Removed call to uart_init_PeriMan in HardwareSerial destructor. --- cores/esp32/HardwareSerial.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index 6d762da21fb..3651667fe07 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -136,8 +136,6 @@ HardwareSerial::HardwareSerial(uint8_t uart_nr) } } #endif - // set deinit function in the Peripheral Manager - uart_init_PeriMan(); } HardwareSerial::~HardwareSerial() { From 5954b23a81869375ce6f4638e01dfe34c6ab42a4 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Fri, 3 Oct 2025 14:00:21 -0300 Subject: [PATCH 4/7] feat(uart): reorganize Serial0 setup call to show in log_v within CDC --- cores/esp32/main.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cores/esp32/main.cpp b/cores/esp32/main.cpp index fb11ff4a5c7..4d1ec770c07 100644 --- a/cores/esp32/main.cpp +++ b/cores/esp32/main.cpp @@ -50,19 +50,19 @@ __attribute__((weak)) uint64_t getArduinoSetupWaitTime_ms(void) { } void loopTask(void *pvParameters) { -#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL) - // sets UART0 (default console) RX/TX pins as already configured in boot or as defined in variants/pins_arduino.h - Serial0.setPins(gpioNumberToDigitalPin(SOC_RX0), gpioNumberToDigitalPin(SOC_TX0)); - // time in ms that the sketch may wait before starting its execution - default is zero - // usually done for opening the Serial Monitor and seeing all debug messages delay(getArduinoSetupWaitTime_ms()); -#endif #if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG printBeforeSetupInfo(); #else if (shouldPrintChipDebugReport()) { printBeforeSetupInfo(); } +#endif +#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL) + // sets UART0 (default console) RX/TX pins as already configured in boot or as defined in variants/pins_arduino.h + Serial0.setPins(gpioNumberToDigitalPin(SOC_RX0), gpioNumberToDigitalPin(SOC_TX0)); + // time in ms that the sketch may wait before starting its execution - default is zero + // usually done for opening the Serial Monitor and seeing all debug messages #endif setup(); #if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG From 3190bd5073a35aaeffa96b33b9a19bc701b86d55 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Fri, 3 Oct 2025 14:21:11 -0300 Subject: [PATCH 5/7] feat(hwcdc): Refactor HWCDC constructor for bus deinitialization Refactor HWCDC constructor to conditionally set bus deinitialization for USB DM and DP. --- cores/esp32/HWCDC.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/cores/esp32/HWCDC.cpp b/cores/esp32/HWCDC.cpp index 062317d9f53..94943577004 100644 --- a/cores/esp32/HWCDC.cpp +++ b/cores/esp32/HWCDC.cpp @@ -253,8 +253,6 @@ static void ARDUINO_ISR_ATTR cdc0_write_char(char c) { } HWCDC::HWCDC() { - perimanSetBusDeinit(ESP32_BUS_TYPE_USB_DM, HWCDC::deinit); - perimanSetBusDeinit(ESP32_BUS_TYPE_USB_DP, HWCDC::deinit); // SOF in ISR causes problems for uploading firmware // lastSOF_ms = 0; // SOF_TIMEOUT = 5; @@ -323,15 +321,19 @@ void HWCDC::begin(unsigned long baud) { // delay(10); // USB Host has to enumerate it again // Peripheral Manager setting for USB D+ D- pins - uint8_t pin = USB_INT_PHY0_DM_GPIO_NUM; - if (!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DM, (void *)this, -1, -1)) { + // Peripheral Manager setting for USB D+ D- pins + if (perimanGetBusDeinit(ESP32_BUS_TYPE_USB_DM) == NULL) { + perimanSetBusDeinit(ESP32_BUS_TYPE_USB_DM, HWCDC::deinit); + } + if (!perimanSetPinBus(USB_INT_PHY0_DM_GPIO_NUM, ESP32_BUS_TYPE_USB_DM, (void *)this, -1, -1)) { goto err; } - pin = USB_INT_PHY0_DP_GPIO_NUM; - if (!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DP, (void *)this, -1, -1)) { + if (perimanGetBusDeinit(ESP32_BUS_TYPE_USB_DP) == NULL) { + perimanSetBusDeinit(ESP32_BUS_TYPE_USB_DP, HWCDC::deinit); + } + if (!perimanSetPinBus(USB_INT_PHY0_DP_GPIO_NUM, ESP32_BUS_TYPE_USB_DP, (void *)this, -1, -1)) { goto err; } - // Configure PHY // USB_Serial_JTAG use internal PHY USB_SERIAL_JTAG.conf0.phy_sel = 0; From c423b21f3f4975521f1754b0a85cb5cd643a9e62 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Fri, 3 Oct 2025 14:43:42 -0300 Subject: [PATCH 6/7] fix(hwcdc): fixes problem with log message --- cores/esp32/HWCDC.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cores/esp32/HWCDC.cpp b/cores/esp32/HWCDC.cpp index 94943577004..06eacdab46b 100644 --- a/cores/esp32/HWCDC.cpp +++ b/cores/esp32/HWCDC.cpp @@ -321,17 +321,18 @@ void HWCDC::begin(unsigned long baud) { // delay(10); // USB Host has to enumerate it again // Peripheral Manager setting for USB D+ D- pins - // Peripheral Manager setting for USB D+ D- pins + uint8_t pin = USB_INT_PHY0_DM_GPIO_NUM; if (perimanGetBusDeinit(ESP32_BUS_TYPE_USB_DM) == NULL) { perimanSetBusDeinit(ESP32_BUS_TYPE_USB_DM, HWCDC::deinit); } - if (!perimanSetPinBus(USB_INT_PHY0_DM_GPIO_NUM, ESP32_BUS_TYPE_USB_DM, (void *)this, -1, -1)) { + if (!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DM, (void *)this, -1, -1)) { goto err; } + pin = USB_INT_PHY0_DP_GPIO_NUM; if (perimanGetBusDeinit(ESP32_BUS_TYPE_USB_DP) == NULL) { perimanSetBusDeinit(ESP32_BUS_TYPE_USB_DP, HWCDC::deinit); } - if (!perimanSetPinBus(USB_INT_PHY0_DP_GPIO_NUM, ESP32_BUS_TYPE_USB_DP, (void *)this, -1, -1)) { + if (!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DP, (void *)this, -1, -1)) { goto err; } // Configure PHY From afc20fc889ec128e709c181e80c907ff7aab825c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Tue, 7 Oct 2025 13:26:44 +0000 Subject: [PATCH 7/7] ci(pre-commit): Apply automatic fixes --- cores/esp32/HWCDC.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/esp32/HWCDC.cpp b/cores/esp32/HWCDC.cpp index 06eacdab46b..d5c9aaafb61 100644 --- a/cores/esp32/HWCDC.cpp +++ b/cores/esp32/HWCDC.cpp @@ -324,14 +324,14 @@ void HWCDC::begin(unsigned long baud) { uint8_t pin = USB_INT_PHY0_DM_GPIO_NUM; if (perimanGetBusDeinit(ESP32_BUS_TYPE_USB_DM) == NULL) { perimanSetBusDeinit(ESP32_BUS_TYPE_USB_DM, HWCDC::deinit); - } + } if (!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DM, (void *)this, -1, -1)) { goto err; } pin = USB_INT_PHY0_DP_GPIO_NUM; if (perimanGetBusDeinit(ESP32_BUS_TYPE_USB_DP) == NULL) { perimanSetBusDeinit(ESP32_BUS_TYPE_USB_DP, HWCDC::deinit); - } + } if (!perimanSetPinBus(pin, ESP32_BUS_TYPE_USB_DP, (void *)this, -1, -1)) { goto err; }