From 5355f193b91c38ac1dd828543856b0b76a9615bc Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Fri, 25 Oct 2024 06:50:01 -0300 Subject: [PATCH 1/5] feat(uart): allow pins_arduino.h to define esp32-p4 uart pins ESP32-P4 has UART default pins only for UART0 and UART1. This PR allows the board definition from pins_arduino.h to define RX2 ... RX4 and TX2 ... TX4 if necessary. It also solves the issue of begin(baud) with no pins for UART2...4 by just sending a error message and returning. --- cores/esp32/HardwareSerial.cpp | 49 ++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index a6a7573f6e3..cda842b6547 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -341,14 +341,63 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in case UART_NUM_2: if (rxPin < 0 && txPin < 0) { // do not change RX2/TX2 if it has already been set before +#ifdef RX2 rxPin = _rxPin < 0 ? (int8_t)RX2 : _rxPin; +#else + rxPin = _rxPin; +#endif +#ifdef TX2 txPin = _txPin < 0 ? (int8_t)TX2 : _txPin; +#else + txPin = _txPin; +#endif + } + break; +#endif +#if SOC_UART_HP_NUM > 3 // may save some flash bytes... + case UART_NUM_3: + if (rxPin < 0 && txPin < 0) { + // do not change RX2/TX2 if it has already been set before +#ifdef RX3 + rxPin = _rxPin < 0 ? (int8_t)RX3 : _rxPin; +#else + rxPin = _rxPin; +#endif +#ifdef TX3 + txPin = _txPin < 0 ? (int8_t)TX3 : _txPin; +#else + txPin = _txPin; +#endif + } + break; +#endif +#if SOC_UART_HP_NUM > 4 // may save some flash bytes... + case UART_NUM_4: + if (rxPin < 0 && txPin < 0) { + // do not change RX2/TX2 if it has already been set before +#ifdef RX4 + rxPin = _rxPin < 0 ? (int8_t)RX4 : _rxPin; +#else + rxPin = _rxPin; +#endif +#ifdef TX4 + txPin = _txPin < 0 ? (int8_t)TX4 : _txPin; +#else + txPin = _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 From 0beaa5e43a412747bbfe51b9413a3b5a235b1824 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Fri, 25 Oct 2024 06:51:26 -0300 Subject: [PATCH 2/5] feat(uart): removes the uart2 pin definitions - not existant --- cores/esp32/HardwareSerial.h | 4 ---- 1 file changed, 4 deletions(-) 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 */ From 0931a62f5db0285ca8c3370d7789ce24cc949efc Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Fri, 25 Oct 2024 07:21:00 -0300 Subject: [PATCH 3/5] fix(uart): solves the case when uart has already been initialized --- cores/esp32/HardwareSerial.cpp | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index cda842b6547..0c880d6b08b 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -313,6 +313,11 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in // map logical pins to GPIO numbers rxPin = digitalPinToGPIONumber(rxPin); txPin = digitalPinToGPIONumber(txPin); + int8_t _rxPin = uart_get_RxPin(_uart_nr); + int8_t _txPin = uart_get_TxPin(_uart_nr); + + rxPin = rxPin < 0 ? _rxPin : rxPin; + txPin = txPin < 0 ? _txPin : txPin; HSERIAL_MUTEX_LOCK(); // First Time or after end() --> set default Pins @@ -343,13 +348,9 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in // do not change RX2/TX2 if it has already been set before #ifdef RX2 rxPin = _rxPin < 0 ? (int8_t)RX2 : _rxPin; -#else - rxPin = _rxPin; #endif #ifdef TX2 txPin = _txPin < 0 ? (int8_t)TX2 : _txPin; -#else - txPin = _txPin; #endif } break; @@ -360,13 +361,9 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in // do not change RX2/TX2 if it has already been set before #ifdef RX3 rxPin = _rxPin < 0 ? (int8_t)RX3 : _rxPin; -#else - rxPin = _rxPin; #endif #ifdef TX3 txPin = _txPin < 0 ? (int8_t)TX3 : _txPin; -#else - txPin = _txPin; #endif } break; @@ -377,13 +374,9 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in // do not change RX2/TX2 if it has already been set before #ifdef RX4 rxPin = _rxPin < 0 ? (int8_t)RX4 : _rxPin; -#else - rxPin = _rxPin; #endif #ifdef TX4 txPin = _txPin < 0 ? (int8_t)TX4 : _txPin; -#else - txPin = _txPin; #endif } break; From 2bce7bb0f34b6ef823e3a32dccc6b165df8531cf Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Fri, 25 Oct 2024 10:27:17 +0000 Subject: [PATCH 4/5] ci(pre-commit): Apply automatic fixes --- cores/esp32/HardwareSerial.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index 0c880d6b08b..fb93dad1c47 100644 --- a/cores/esp32/HardwareSerial.cpp +++ b/cores/esp32/HardwareSerial.cpp @@ -390,7 +390,7 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in 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 From 02c359c27a20d2fda13354db7b8b8ff77b1550b0 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Fri, 25 Oct 2024 08:20:31 -0300 Subject: [PATCH 5/5] fix(ci): uart definition for esp32-p4 uart2 rx,tx pins --- tests/validation/uart/uart.ino | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/validation/uart/uart.ino b/tests/validation/uart/uart.ino index e5fa0a8285f..01c449867db 100644 --- a/tests/validation/uart/uart.ino +++ b/tests/validation/uart/uart.ino @@ -52,6 +52,14 @@ #define NEW_TX1 10 #endif +// ESP32-P4 has no UART pin definition for RX2, TX2, RX3, TX3, RX4, TX4 +#ifndef RX2 +#define RX2 RX1 +#endif +#ifndef TX2 +#define TX2 RX1 +#endif + /* Utility global variables */ static String recv_msg = "";