From c355ad37e59b9411ddc514159f858692ae4fff95 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Fri, 20 Jun 2025 22:52:39 -0300 Subject: [PATCH 1/5] feat(uart): fixes loopback function after IDF changes IDF 5.4.1 has added a new function called uart_release_pin() that is called whenever new pins are set or when uart driver is deleted. This has a side effect that causes RX pin to never work again with the loopback function. Other changes also have removed some GPIO setup that was necessary for the GPIO loopback mode work. The PR forces a full RX Pin setup in order to make it work in GPIO Matrix with Loopback TX Signal --- cores/esp32/esp32-hal-uart.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index 5311aff4f37..7efac214d53 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -35,7 +35,7 @@ #include "driver/rtc_io.h" #include "driver/lp_io.h" -#include "soc/uart_periph.h" +#include "soc/uart_pins.h" #include "esp_private/uart_share_hw_ctrl.h" static int s_uart_debug_nr = 0; // UART number for debug output @@ -1391,7 +1391,7 @@ unsigned long uartDetectBaudrate(uart_t *uart) { This is intended to make an internal loopback connection using IOMUX The function uart_internal_loopback() shall be used right after Arduino Serial.begin(...) This code "replaces" the physical wiring for connecting TX <--> RX in a loopback -*/ + // gets the right TX or RX SIGNAL, based on the UART number from gpio_sig_map.h #ifdef CONFIG_IDF_TARGET_ESP32P4 @@ -1416,6 +1416,7 @@ unsigned long uartDetectBaudrate(uart_t *uart) { #define UART_RX_SIGNAL(uartNumber) (uartNumber == UART_NUM_0 ? U0RXD_IN_IDX : U1RXD_IN_IDX) #endif #endif // ifdef CONFIG_IDF_TARGET_ESP32P4 +*/ /* This function internally binds defined UARTs TX signal with defined RX pin of any UART (same or different). @@ -1427,7 +1428,12 @@ void uart_internal_loopback(uint8_t uartNum, int8_t rxPin) { log_e("UART%d is not supported for loopback or RX pin %d is invalid.", uartNum, rxPin); return; } - esp_rom_gpio_connect_out_signal(rxPin, UART_TX_SIGNAL(uartNum), false, false); + // forces rxPin to use GPIO Matrix and setup the pin to receive UART TX Signal - IDF 5.4.1 Change with uart_release_pin() + gpio_func_sel((gpio_num_t) rxPin, PIN_FUNC_GPIO); + gpio_pullup_en((gpio_num_t) rxPin); + gpio_input_enable((gpio_num_t) rxPin); + esp_rom_gpio_connect_in_signal(rxPin, uart_periph_signal[uartNum].pins[SOC_UART_RX_PIN_IDX].signal, false); + esp_rom_gpio_connect_out_signal(rxPin, uart_periph_signal[uartNum].pins[SOC_UART_TX_PIN_IDX].signal, false, false); } /* From b966275d19744bbb247433c5be96091ea132212f Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Fri, 20 Jun 2025 23:03:59 -0300 Subject: [PATCH 2/5] feat(uart): adds missing include file --- cores/esp32/esp32-hal-uart.c | 1 + 1 file changed, 1 insertion(+) diff --git a/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index 7efac214d53..def253c5060 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -32,6 +32,7 @@ #include "driver/gpio.h" #include "hal/gpio_hal.h" #include "esp_rom_gpio.h" +#include "esp_private/gpio.h" #include "driver/rtc_io.h" #include "driver/lp_io.h" From f7045983291f16104f1f6b5a28da75425b0b7060 Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Fri, 20 Jun 2025 23:09:13 -0300 Subject: [PATCH 3/5] feat(uart): removes not necessary part of the code --- cores/esp32/esp32-hal-uart.c | 39 ++++-------------------------------- 1 file changed, 4 insertions(+), 35 deletions(-) diff --git a/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index def253c5060..f232021dfd1 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -1383,41 +1383,10 @@ unsigned long uartDetectBaudrate(uart_t *uart) { #endif } -/* - These functions are for testing purpose only and can be used in Arduino Sketches - Those are used in the UART examples -*/ - -/* - This is intended to make an internal loopback connection using IOMUX - The function uart_internal_loopback() shall be used right after Arduino Serial.begin(...) - This code "replaces" the physical wiring for connecting TX <--> RX in a loopback - - -// gets the right TX or RX SIGNAL, based on the UART number from gpio_sig_map.h -#ifdef CONFIG_IDF_TARGET_ESP32P4 -#define UART_TX_SIGNAL(uartNumber) \ - (uartNumber == UART_NUM_0 \ - ? UART0_TXD_PAD_OUT_IDX \ - : (uartNumber == UART_NUM_1 \ - ? UART1_TXD_PAD_OUT_IDX \ - : (uartNumber == UART_NUM_2 ? UART2_TXD_PAD_OUT_IDX : (uartNumber == UART_NUM_3 ? UART3_TXD_PAD_OUT_IDX : UART4_TXD_PAD_OUT_IDX)))) -#define UART_RX_SIGNAL(uartNumber) \ - (uartNumber == UART_NUM_0 \ - ? UART0_RXD_PAD_IN_IDX \ - : (uartNumber == UART_NUM_1 \ - ? UART1_RXD_PAD_IN_IDX \ - : (uartNumber == UART_NUM_2 ? UART2_RXD_PAD_IN_IDX : (uartNumber == UART_NUM_3 ? UART3_RXD_PAD_IN_IDX : UART4_RXD_PAD_IN_IDX)))) -#else -#if SOC_UART_HP_NUM > 2 -#define UART_TX_SIGNAL(uartNumber) (uartNumber == UART_NUM_0 ? U0TXD_OUT_IDX : (uartNumber == UART_NUM_1 ? U1TXD_OUT_IDX : U2TXD_OUT_IDX)) -#define UART_RX_SIGNAL(uartNumber) (uartNumber == UART_NUM_0 ? U0RXD_IN_IDX : (uartNumber == UART_NUM_1 ? U1RXD_IN_IDX : U2RXD_IN_IDX)) -#else -#define UART_TX_SIGNAL(uartNumber) (uartNumber == UART_NUM_0 ? U0TXD_OUT_IDX : U1TXD_OUT_IDX) -#define UART_RX_SIGNAL(uartNumber) (uartNumber == UART_NUM_0 ? U0RXD_IN_IDX : U1RXD_IN_IDX) -#endif -#endif // ifdef CONFIG_IDF_TARGET_ESP32P4 -*/ +/////////////////////////// +// These functions are for testing purpose only and can be used in Arduino Sketches +// Those are used in the UART examples and CI +/////////////////////////// /* This function internally binds defined UARTs TX signal with defined RX pin of any UART (same or different). From 0301a25801a67db12023c17c3bc8591fae66ce6d Mon Sep 17 00:00:00 2001 From: Sugar Glider Date: Sat, 21 Jun 2025 00:12:07 -0300 Subject: [PATCH 4/5] fix(uart): commentaries style fix Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- cores/esp32/esp32-hal-uart.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index f232021dfd1..6dc7c3a2a9d 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -1383,10 +1383,10 @@ unsigned long uartDetectBaudrate(uart_t *uart) { #endif } -/////////////////////////// -// These functions are for testing purpose only and can be used in Arduino Sketches -// Those are used in the UART examples and CI -/////////////////////////// +/* + * These functions are for testing purposes only and can be used in Arduino Sketches. + * They are utilized in the UART examples and CI. + */ /* This function internally binds defined UARTs TX signal with defined RX pin of any UART (same or different). From 4df11efba181950c38b67070d06dd205da06edae Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Sat, 21 Jun 2025 10:58:18 +0000 Subject: [PATCH 5/5] ci(pre-commit): Apply automatic fixes --- cores/esp32/esp32-hal-uart.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index 6dc7c3a2a9d..3c9e6bf178b 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -1399,10 +1399,10 @@ void uart_internal_loopback(uint8_t uartNum, int8_t rxPin) { return; } // forces rxPin to use GPIO Matrix and setup the pin to receive UART TX Signal - IDF 5.4.1 Change with uart_release_pin() - gpio_func_sel((gpio_num_t) rxPin, PIN_FUNC_GPIO); - gpio_pullup_en((gpio_num_t) rxPin); - gpio_input_enable((gpio_num_t) rxPin); - esp_rom_gpio_connect_in_signal(rxPin, uart_periph_signal[uartNum].pins[SOC_UART_RX_PIN_IDX].signal, false); + gpio_func_sel((gpio_num_t)rxPin, PIN_FUNC_GPIO); + gpio_pullup_en((gpio_num_t)rxPin); + gpio_input_enable((gpio_num_t)rxPin); + esp_rom_gpio_connect_in_signal(rxPin, uart_periph_signal[uartNum].pins[SOC_UART_RX_PIN_IDX].signal, false); esp_rom_gpio_connect_out_signal(rxPin, uart_periph_signal[uartNum].pins[SOC_UART_TX_PIN_IDX].signal, false, false); }