From 9aeb1ba548636070495c132f7e7e82df78387c34 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Fri, 25 Oct 2024 09:07:37 -0300 Subject: [PATCH] ESP32-P4 UART Pin Definitions (#10521) * 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. * feat(uart): removes the uart2 pin definitions - not existant * fix(uart): solves the case when uart has already been initialized * ci(pre-commit): Apply automatic fixes * fix(ci): uart definition for esp32-p4 uart2 rx,tx pins --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> --- cores/esp32/HardwareSerial.cpp | 42 ++++++++++++++++++++++++++++++++++ cores/esp32/HardwareSerial.h | 4 ---- tests/validation/uart/uart.ino | 8 +++++++ 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/cores/esp32/HardwareSerial.cpp b/cores/esp32/HardwareSerial.cpp index a6a7573f6e3..fb93dad1c47 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 @@ -341,14 +346,51 @@ 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; +#endif +#ifdef TX2 txPin = _txPin < 0 ? (int8_t)TX2 : _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; +#endif +#ifdef TX3 + txPin = _txPin < 0 ? (int8_t)TX3 : _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; +#endif +#ifdef TX4 + txPin = _txPin < 0 ? (int8_t)TX4 : _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 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/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 = "";