@@ -106,9 +106,22 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
106
106
self -> timeout_ms = timeout * 1000 ;
107
107
108
108
self -> uart_num = UART_NUM_MAX ;
109
- for (uart_port_t num = 0 ; num < UART_NUM_MAX ; num ++ ) {
109
+
110
+ // ESP32-C6 and ESP32-P4 both have a single LP (low power) UART, which is
111
+ // limited in what it can do and which pins it can use. Ignore it for now.
112
+ // Its UART number is higher than the numbers for the regular ("HP", high-power) UARTs.
113
+
114
+ // SOC_UART_LP_NUM is not defined for chips without an LP UART.
115
+ #if defined(SOC_UART_LP_NUM ) && (SOC_UART_LP_NUM >= 1 )
116
+ #define UART_LIMIT LP_UART_NUM_0
117
+ #else
118
+ #define UART_LIMIT UART_NUM_MAX
119
+ #endif
120
+
121
+ for (uart_port_t num = 0 ; num < UART_LIMIT ; num ++ ) {
110
122
if (!uart_is_driver_installed (num )) {
111
123
self -> uart_num = num ;
124
+ break ;
112
125
}
113
126
}
114
127
if (self -> uart_num == UART_NUM_MAX ) {
@@ -224,6 +237,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
224
237
int rx_num = -1 ;
225
238
int rts_num = -1 ;
226
239
int cts_num = -1 ;
240
+
227
241
if (have_tx ) {
228
242
claim_pin (tx );
229
243
self -> tx_pin = tx ;
@@ -254,9 +268,13 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
254
268
self -> rts_pin = rs485_dir ;
255
269
rts_num = rs485_dir -> number ;
256
270
}
271
+
257
272
if (uart_set_pin (self -> uart_num , tx_num , rx_num , rts_num , cts_num ) != ESP_OK ) {
273
+ // Uninstall driver and clean up.
274
+ common_hal_busio_uart_deinit (self );
258
275
raise_ValueError_invalid_pins ();
259
276
}
277
+
260
278
if (have_rx ) {
261
279
// On ESP32-C3 and ESP32-S3 (at least), a junk byte with zero or more consecutive 1's can be
262
280
// generated, even if the pin is pulled high (normal UART resting state) to begin with.
0 commit comments