Skip to content

Commit

Permalink
ESP32-P4 UART Pin Definitions (#10521)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
SuGlider and pre-commit-ci-lite[bot] authored Oct 25, 2024
1 parent 38a4c29 commit 9aeb1ba
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
42 changes: 42 additions & 0 deletions cores/esp32/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
4 changes: 0 additions & 4 deletions cores/esp32/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 */
Expand Down
8 changes: 8 additions & 0 deletions tests/validation/uart/uart.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down

0 comments on commit 9aeb1ba

Please sign in to comment.