Skip to content

Commit 01e4d5d

Browse files
SuGliderCopilotpre-commit-ci-lite[bot]
authored
feat(uart): fixes loopback function after IDF changes (#11492)
* 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 * feat(uart): adds missing include file * feat(uart): removes not necessary part of the code * fix(uart): commentaries style fix Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent ccda9c5 commit 01e4d5d

File tree

1 file changed

+11
-35
lines changed

1 file changed

+11
-35
lines changed

cores/esp32/esp32-hal-uart.c

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@
3232
#include "driver/gpio.h"
3333
#include "hal/gpio_hal.h"
3434
#include "esp_rom_gpio.h"
35+
#include "esp_private/gpio.h"
3536

3637
#include "driver/rtc_io.h"
3738
#include "driver/lp_io.h"
38-
#include "soc/uart_periph.h"
39+
#include "soc/uart_pins.h"
3940
#include "esp_private/uart_share_hw_ctrl.h"
4041

4142
static int s_uart_debug_nr = 0; // UART number for debug output
@@ -1383,39 +1384,9 @@ unsigned long uartDetectBaudrate(uart_t *uart) {
13831384
}
13841385

13851386
/*
1386-
These functions are for testing purpose only and can be used in Arduino Sketches
1387-
Those are used in the UART examples
1388-
*/
1389-
1390-
/*
1391-
This is intended to make an internal loopback connection using IOMUX
1392-
The function uart_internal_loopback() shall be used right after Arduino Serial.begin(...)
1393-
This code "replaces" the physical wiring for connecting TX <--> RX in a loopback
1394-
*/
1395-
1396-
// gets the right TX or RX SIGNAL, based on the UART number from gpio_sig_map.h
1397-
#ifdef CONFIG_IDF_TARGET_ESP32P4
1398-
#define UART_TX_SIGNAL(uartNumber) \
1399-
(uartNumber == UART_NUM_0 \
1400-
? UART0_TXD_PAD_OUT_IDX \
1401-
: (uartNumber == UART_NUM_1 \
1402-
? UART1_TXD_PAD_OUT_IDX \
1403-
: (uartNumber == UART_NUM_2 ? UART2_TXD_PAD_OUT_IDX : (uartNumber == UART_NUM_3 ? UART3_TXD_PAD_OUT_IDX : UART4_TXD_PAD_OUT_IDX))))
1404-
#define UART_RX_SIGNAL(uartNumber) \
1405-
(uartNumber == UART_NUM_0 \
1406-
? UART0_RXD_PAD_IN_IDX \
1407-
: (uartNumber == UART_NUM_1 \
1408-
? UART1_RXD_PAD_IN_IDX \
1409-
: (uartNumber == UART_NUM_2 ? UART2_RXD_PAD_IN_IDX : (uartNumber == UART_NUM_3 ? UART3_RXD_PAD_IN_IDX : UART4_RXD_PAD_IN_IDX))))
1410-
#else
1411-
#if SOC_UART_HP_NUM > 2
1412-
#define UART_TX_SIGNAL(uartNumber) (uartNumber == UART_NUM_0 ? U0TXD_OUT_IDX : (uartNumber == UART_NUM_1 ? U1TXD_OUT_IDX : U2TXD_OUT_IDX))
1413-
#define UART_RX_SIGNAL(uartNumber) (uartNumber == UART_NUM_0 ? U0RXD_IN_IDX : (uartNumber == UART_NUM_1 ? U1RXD_IN_IDX : U2RXD_IN_IDX))
1414-
#else
1415-
#define UART_TX_SIGNAL(uartNumber) (uartNumber == UART_NUM_0 ? U0TXD_OUT_IDX : U1TXD_OUT_IDX)
1416-
#define UART_RX_SIGNAL(uartNumber) (uartNumber == UART_NUM_0 ? U0RXD_IN_IDX : U1RXD_IN_IDX)
1417-
#endif
1418-
#endif // ifdef CONFIG_IDF_TARGET_ESP32P4
1387+
* These functions are for testing purposes only and can be used in Arduino Sketches.
1388+
* They are utilized in the UART examples and CI.
1389+
*/
14191390

14201391
/*
14211392
This function internally binds defined UARTs TX signal with defined RX pin of any UART (same or different).
@@ -1427,7 +1398,12 @@ void uart_internal_loopback(uint8_t uartNum, int8_t rxPin) {
14271398
log_e("UART%d is not supported for loopback or RX pin %d is invalid.", uartNum, rxPin);
14281399
return;
14291400
}
1430-
esp_rom_gpio_connect_out_signal(rxPin, UART_TX_SIGNAL(uartNum), false, false);
1401+
// forces rxPin to use GPIO Matrix and setup the pin to receive UART TX Signal - IDF 5.4.1 Change with uart_release_pin()
1402+
gpio_func_sel((gpio_num_t)rxPin, PIN_FUNC_GPIO);
1403+
gpio_pullup_en((gpio_num_t)rxPin);
1404+
gpio_input_enable((gpio_num_t)rxPin);
1405+
esp_rom_gpio_connect_in_signal(rxPin, uart_periph_signal[uartNum].pins[SOC_UART_RX_PIN_IDX].signal, false);
1406+
esp_rom_gpio_connect_out_signal(rxPin, uart_periph_signal[uartNum].pins[SOC_UART_TX_PIN_IDX].signal, false, false);
14311407
}
14321408

14331409
/*

0 commit comments

Comments
 (0)