From dd7518101c79c9a9cbd66751b0d0e7bc47c0540f Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 24 Oct 2024 09:48:48 -0300 Subject: [PATCH 1/2] feat(uart): deal with esp32-p4 pin setup and detach --- cores/esp32/HardwareSerial.cpp | 11 +++++++++++ cores/esp32/HardwareSerial.h | 4 ---- cores/esp32/esp32-hal-uart.c | 2 ++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index a6a7573f6e3..dcf82ae314a 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -340,14 +340,24 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in #if SOC_UART_HP_NUM > 2 // may save some flash bytes... case UART_NUM_2: if (rxPin < 0 && txPin < 0) { +#ifdef RX2 // do not change RX2/TX2 if it has already been set before rxPin = _rxPin < 0 ? (int8_t)RX2 : _rxPin; +#endif +#ifdef TX2 txPin = _txPin < 0 ? (int8_t)TX2 : _txPin; +#endif } break; #endif } } + // if no RX/TX pins are defined, it will not start the UART driver + if (rxPin < 0 && txPin < 0) { + log_e("No RX/TX pins defined. Please set RX/TX pins."); + HSERIAL_MUTEX_UNLOCK(); + return; + } // IDF UART driver keeps Pin setting on restarting. Negative Pin number will keep it unmodified. // it will detach previous UART attached pins @@ -425,6 +435,7 @@ void HardwareSerial::end() { if (uartGetDebug() == _uart_nr) { uartSetDebug(0); } + uart_set_loop_back(_uart_nr, false); // disable loopback mode, if previously enabled _rxFIFOFull = 0; uartEnd(_uart_nr); // fully detach all pins and delete the UART driver _destroyEventTask(); // when IDF uart driver is deleted, _eventTask must finish too diff --git a/cores/esp32/HardwareSerial.h b/cores/esp32/HardwareSerial.h index 8eb7f2c91a6..a33d5def34d 100644 --- a/cores/esp32/HardwareSerial.h +++ b/cores/esp32/HardwareSerial.h @@ -200,8 +200,6 @@ typedef enum { #define RX2 (gpio_num_t)4 #elif CONFIG_IDF_TARGET_ESP32S3 #define RX2 (gpio_num_t)19 -#elif CONFIG_IDF_TARGET_ESP32P4 -#define RX2 (gpio_num_t)15 #endif #endif @@ -210,8 +208,6 @@ typedef enum { #define TX2 (gpio_num_t)25 #elif CONFIG_IDF_TARGET_ESP32S3 #define TX2 (gpio_num_t)20 -#elif CONFIG_IDF_TARGET_ESP32P4 -#define TX2 (gpio_num_t)14 #endif #endif #endif /* SOC_UART_HP_NUM > 2 */ diff --git a/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index 706124c7451..b86c8a6d3c8 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -166,6 +166,7 @@ static bool _uartDetachBus_RX(void *busptr) { // sanity check - it should never happen assert(busptr && "_uartDetachBus_RX bus NULL pointer."); uart_t *bus = (uart_t *)busptr; + uart_set_loop_back(bus->num, false); // disable loopback return _uartDetachPins(bus->num, bus->_rxPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); } @@ -173,6 +174,7 @@ static bool _uartDetachBus_TX(void *busptr) { // sanity check - it should never happen assert(busptr && "_uartDetachBus_TX bus NULL pointer."); uart_t *bus = (uart_t *)busptr; + uart_set_loop_back(bus->num, false); // disable loopback return _uartDetachPins(bus->num, UART_PIN_NO_CHANGE, bus->_txPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); } From dc1a9f3c15374234e0ee45f42d000408c5f2fca4 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 24 Oct 2024 11:26:20 -0300 Subject: [PATCH 2/2] feat(uart): changes the uart loopback function to work with the P4 --- cores/esp32/HardwareSerial.cpp | 1 - cores/esp32/esp32-hal-uart.c | 6 +++--- cores/esp32/esp32-hal-uart.h | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index dcf82ae314a..7b13e3b9d30 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -435,7 +435,6 @@ void HardwareSerial::end() { if (uartGetDebug() == _uart_nr) { uartSetDebug(0); } - uart_set_loop_back(_uart_nr, false); // disable loopback mode, if previously enabled _rxFIFOFull = 0; uartEnd(_uart_nr); // fully detach all pins and delete the UART driver _destroyEventTask(); // when IDF uart driver is deleted, _eventTask must finish too diff --git a/cores/esp32/esp32-hal-uart.c b/cores/esp32/esp32-hal-uart.c index b86c8a6d3c8..7b2250c34fa 100644 --- a/cores/esp32/esp32-hal-uart.c +++ b/cores/esp32/esp32-hal-uart.c @@ -1166,11 +1166,11 @@ unsigned long uartDetectBaudrate(uart_t *uart) { This function internally binds defined UARTs TX signal with defined RX pin of any UART (same or different). This creates a loop that lets us receive anything we send on the UART without external wires. */ -void uart_internal_loopback(uint8_t uartNum, int8_t rxPin) { - if (uartNum > SOC_UART_HP_NUM - 1 || !GPIO_IS_VALID_GPIO(rxPin)) { +void uart_internal_loopback(uint8_t uartNum, bool loop_back_en) { + if (uartNum > SOC_UART_HP_NUM - 1) { return; } - esp_rom_gpio_connect_out_signal(rxPin, UART_TX_SIGNAL(uartNum), false, false); + uart_set_loop_back(uartNum, loop_back_en); } /* diff --git a/cores/esp32/esp32-hal-uart.h b/cores/esp32/esp32-hal-uart.h index 402b5785915..389193df9da 100644 --- a/cores/esp32/esp32-hal-uart.h +++ b/cores/esp32/esp32-hal-uart.h @@ -106,7 +106,7 @@ unsigned long uartDetectBaudrate(uart_t *uart); // Make sure UART's RX signal is connected to TX pin // This creates a loop that lets us receive anything we send on the UART -void uart_internal_loopback(uint8_t uartNum, int8_t rxPin); +void uart_internal_loopback(uint8_t uartNum, bool loop_back_en); // Routines that generate BREAK in the UART for testing purpose