Skip to content

Commit dd75181

Browse files
committed
feat(uart): deal with esp32-p4 pin setup and detach
1 parent 6dfd958 commit dd75181

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

cores/esp32/HardwareSerial.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,14 +340,24 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
340340
#if SOC_UART_HP_NUM > 2 // may save some flash bytes...
341341
case UART_NUM_2:
342342
if (rxPin < 0 && txPin < 0) {
343+
#ifdef RX2
343344
// do not change RX2/TX2 if it has already been set before
344345
rxPin = _rxPin < 0 ? (int8_t)RX2 : _rxPin;
346+
#endif
347+
#ifdef TX2
345348
txPin = _txPin < 0 ? (int8_t)TX2 : _txPin;
349+
#endif
346350
}
347351
break;
348352
#endif
349353
}
350354
}
355+
// if no RX/TX pins are defined, it will not start the UART driver
356+
if (rxPin < 0 && txPin < 0) {
357+
log_e("No RX/TX pins defined. Please set RX/TX pins.");
358+
HSERIAL_MUTEX_UNLOCK();
359+
return;
360+
}
351361

352362
// IDF UART driver keeps Pin setting on restarting. Negative Pin number will keep it unmodified.
353363
// it will detach previous UART attached pins
@@ -425,6 +435,7 @@ void HardwareSerial::end() {
425435
if (uartGetDebug() == _uart_nr) {
426436
uartSetDebug(0);
427437
}
438+
uart_set_loop_back(_uart_nr, false); // disable loopback mode, if previously enabled
428439
_rxFIFOFull = 0;
429440
uartEnd(_uart_nr); // fully detach all pins and delete the UART driver
430441
_destroyEventTask(); // when IDF uart driver is deleted, _eventTask must finish too

cores/esp32/HardwareSerial.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,6 @@ typedef enum {
200200
#define RX2 (gpio_num_t)4
201201
#elif CONFIG_IDF_TARGET_ESP32S3
202202
#define RX2 (gpio_num_t)19
203-
#elif CONFIG_IDF_TARGET_ESP32P4
204-
#define RX2 (gpio_num_t)15
205203
#endif
206204
#endif
207205

@@ -210,8 +208,6 @@ typedef enum {
210208
#define TX2 (gpio_num_t)25
211209
#elif CONFIG_IDF_TARGET_ESP32S3
212210
#define TX2 (gpio_num_t)20
213-
#elif CONFIG_IDF_TARGET_ESP32P4
214-
#define TX2 (gpio_num_t)14
215211
#endif
216212
#endif
217213
#endif /* SOC_UART_HP_NUM > 2 */

cores/esp32/esp32-hal-uart.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,15 @@ static bool _uartDetachBus_RX(void *busptr) {
166166
// sanity check - it should never happen
167167
assert(busptr && "_uartDetachBus_RX bus NULL pointer.");
168168
uart_t *bus = (uart_t *)busptr;
169+
uart_set_loop_back(bus->num, false); // disable loopback
169170
return _uartDetachPins(bus->num, bus->_rxPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
170171
}
171172

172173
static bool _uartDetachBus_TX(void *busptr) {
173174
// sanity check - it should never happen
174175
assert(busptr && "_uartDetachBus_TX bus NULL pointer.");
175176
uart_t *bus = (uart_t *)busptr;
177+
uart_set_loop_back(bus->num, false); // disable loopback
176178
return _uartDetachPins(bus->num, UART_PIN_NO_CHANGE, bus->_txPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
177179
}
178180

0 commit comments

Comments
 (0)