@@ -106,9 +106,22 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
106106 self -> timeout_ms = timeout * 1000 ;
107107
108108 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 ++ ) {
110122 if (!uart_is_driver_installed (num )) {
111123 self -> uart_num = num ;
124+ break ;
112125 }
113126 }
114127 if (self -> uart_num == UART_NUM_MAX ) {
@@ -224,6 +237,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
224237 int rx_num = -1 ;
225238 int rts_num = -1 ;
226239 int cts_num = -1 ;
240+
227241 if (have_tx ) {
228242 claim_pin (tx );
229243 self -> tx_pin = tx ;
@@ -254,9 +268,13 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
254268 self -> rts_pin = rs485_dir ;
255269 rts_num = rs485_dir -> number ;
256270 }
271+
257272 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 );
258275 raise_ValueError_invalid_pins ();
259276 }
277+
260278 if (have_rx ) {
261279 // On ESP32-C3 and ESP32-S3 (at least), a junk byte with zero or more consecutive 1's can be
262280 // generated, even if the pin is pulled high (normal UART resting state) to begin with.
0 commit comments