Skip to content

Commit 5355f19

Browse files
authored
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.
1 parent a3ee376 commit 5355f19

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

cores/esp32/HardwareSerial.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -341,14 +341,63 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
341341
case UART_NUM_2:
342342
if (rxPin < 0 && txPin < 0) {
343343
// do not change RX2/TX2 if it has already been set before
344+
#ifdef RX2
344345
rxPin = _rxPin < 0 ? (int8_t)RX2 : _rxPin;
346+
#else
347+
rxPin = _rxPin;
348+
#endif
349+
#ifdef TX2
345350
txPin = _txPin < 0 ? (int8_t)TX2 : _txPin;
351+
#else
352+
txPin = _txPin;
353+
#endif
354+
}
355+
break;
356+
#endif
357+
#if SOC_UART_HP_NUM > 3 // may save some flash bytes...
358+
case UART_NUM_3:
359+
if (rxPin < 0 && txPin < 0) {
360+
// do not change RX2/TX2 if it has already been set before
361+
#ifdef RX3
362+
rxPin = _rxPin < 0 ? (int8_t)RX3 : _rxPin;
363+
#else
364+
rxPin = _rxPin;
365+
#endif
366+
#ifdef TX3
367+
txPin = _txPin < 0 ? (int8_t)TX3 : _txPin;
368+
#else
369+
txPin = _txPin;
370+
#endif
371+
}
372+
break;
373+
#endif
374+
#if SOC_UART_HP_NUM > 4 // may save some flash bytes...
375+
case UART_NUM_4:
376+
if (rxPin < 0 && txPin < 0) {
377+
// do not change RX2/TX2 if it has already been set before
378+
#ifdef RX4
379+
rxPin = _rxPin < 0 ? (int8_t)RX4 : _rxPin;
380+
#else
381+
rxPin = _rxPin;
382+
#endif
383+
#ifdef TX4
384+
txPin = _txPin < 0 ? (int8_t)TX4 : _txPin;
385+
#else
386+
txPin = _txPin;
387+
#endif
346388
}
347389
break;
348390
#endif
349391
}
350392
}
351393

394+
// if no RX/TX pins are defined, it will not start the UART driver
395+
if (rxPin < 0 && txPin < 0) {
396+
log_e("No RX/TX pins defined. Please set RX/TX pins.");
397+
HSERIAL_MUTEX_UNLOCK();
398+
return;
399+
}
400+
352401
// IDF UART driver keeps Pin setting on restarting. Negative Pin number will keep it unmodified.
353402
// it will detach previous UART attached pins
354403

0 commit comments

Comments
 (0)