@@ -117,7 +117,7 @@ static uart_t _uart_bus_array[] = {
117
117
118
118
#if SOC_UART_LP_NUM >= 1
119
119
// LP UART enable pins routine
120
- static bool lp_uart_config_io (int8_t pin , rtc_gpio_mode_t direction , uint32_t idx )
120
+ static bool lp_uart_config_io (uint8_t uart_num , int8_t pin , rtc_gpio_mode_t direction , uint32_t idx )
121
121
{
122
122
/* Skip configuration if the LP_IO is -1 */
123
123
if (pin < 0 ) {
@@ -137,7 +137,7 @@ static bool lp_uart_config_io(int8_t pin, rtc_gpio_mode_t direction, uint32_t id
137
137
}
138
138
139
139
// Connect pins
140
- const uart_periph_sig_t * upin = & uart_periph_signal [LP_UART_NUM_0 ].pins [idx ];
140
+ const uart_periph_sig_t * upin = & uart_periph_signal [uart_num ].pins [idx ];
141
141
#if !SOC_LP_GPIO_MATRIX_SUPPORTED // ESP32-C6/C61/C5
142
142
// When LP_IO Matrix is not support, LP_IO Mux must be connected to the pins
143
143
if (rtc_gpio_iomux_func_sel (pin , upin -> iomux_func ) != ESP_OK ) {
@@ -160,12 +160,12 @@ static bool lp_uart_config_io(int8_t pin, rtc_gpio_mode_t direction, uint32_t id
160
160
// Connect the LP_IO to the LP UART peripheral signal
161
161
esp_err_t ret ;
162
162
if (direction == RTC_GPIO_MODE_OUTPUT_ONLY ) {
163
- ret = lp_gpio_connect_out_signal (pin , UART_PERIPH_SIGNAL (LP_UART_NUM_0 , idx ), 0 , 0 );
163
+ ret = lp_gpio_connect_out_signal (pin , UART_PERIPH_SIGNAL (uart_num , idx ), 0 , 0 );
164
164
} else {
165
- ret = lp_gpio_connect_in_signal (pin , UART_PERIPH_SIGNAL (LP_UART_NUM_0 , idx ), 0 );
165
+ ret = lp_gpio_connect_in_signal (pin , UART_PERIPH_SIGNAL (uart_num , idx ), 0 );
166
166
}
167
167
if (ret != ESP_OK ) {
168
- log_e ("Failed to connect LP_IO pin %d to UART%d signal" , pin , LP_UART_NUM_0 );
168
+ log_e ("Failed to connect LP_IO pin %d to UART%d signal" , pin , uart_num );
169
169
return false;
170
170
}
171
171
}
@@ -178,21 +178,25 @@ static bool lp_uart_config_io(int8_t pin, rtc_gpio_mode_t direction, uint32_t id
178
178
static bool lpuartCheckPins (int8_t rxPin , int8_t txPin , int8_t ctsPin , int8_t rtsPin , uint8_t uart_nr ) {
179
179
// check if LP UART is being used and if the pins are valid
180
180
#if !SOC_LP_GPIO_MATRIX_SUPPORTED // ESP32-C6/C61/C5
181
- if (uart_nr == LP_UART_NUM_0 ) {
182
- if (rxPin > 0 && rxPin != LP_U0RXD_GPIO_NUM ) {
183
- log_e ("UART%d LP UART requires RX pin to be set to %d." , uart_nr , LP_U0RXD_GPIO_NUM );
181
+ uint16_t lp_uart_fixed_pin = uart_periph_signal [uart_nr ].pins [SOC_UART_RX_PIN_IDX ].default_gpio ;
182
+ if (uart_nr >= SOC_UART_HP_NUM ) { // it is a LP UART NUM
183
+ if (rxPin > 0 && rxPin != lp_uart_fixed_pin ) {
184
+ log_e ("UART%d LP UART requires RX pin to be set to %d." , uart_nr , lp_uart_fixed_pin );
184
185
return false;
185
186
}
186
- if (txPin > 0 && txPin != LP_U0TXD_GPIO_NUM ) {
187
- log_e ("UART%d LP UART requires TX pin to be set to %d." , uart_nr , LP_U0TXD_GPIO_NUM );
187
+ lp_uart_fixed_pin = uart_periph_signal [uart_nr ].pins [SOC_UART_TX_PIN_IDX ].default_gpio ;
188
+ if (txPin > 0 && txPin != lp_uart_fixed_pin ) {
189
+ log_e ("UART%d LP UART requires TX pin to be set to %d." , uart_nr , lp_uart_fixed_pin );
188
190
return false;
189
191
}
190
- if (ctsPin > 0 && ctsPin != LP_U0CTS_GPIO_NUM ) {
191
- log_e ("UART%d LP UART requires CTS pin to be set to %d." , uart_nr , LP_U0CTS_GPIO_NUM );
192
+ lp_uart_fixed_pin = uart_periph_signal [uart_nr ].pins [SOC_UART_CTS_PIN_IDX ].default_gpio ;
193
+ if (ctsPin > 0 && ctsPin != lp_uart_fixed_pin ) {
194
+ log_e ("UART%d LP UART requires CTS pin to be set to %d." , uart_nr , lp_uart_fixed_pin );
192
195
return false;
193
196
}
194
- if (rtsPin > 0 && rtsPin != LP_U0RTS_GPIO_NUM ) {
195
- log_e ("UART%d LP UART requires RTS pin to be set to %d." , uart_nr , LP_U0RTS_GPIO_NUM );
197
+ lp_uart_fixed_pin = uart_periph_signal [uart_nr ].pins [SOC_UART_RTS_PIN_IDX ].default_gpio ;
198
+ if (rtsPin > 0 && rtsPin != lp_uart_fixed_pin ) {
199
+ log_e ("UART%d LP UART requires RTS pin to be set to %d." , uart_nr , lp_uart_fixed_pin );
196
200
return false;
197
201
}
198
202
}
@@ -313,8 +317,8 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
313
317
// connect RX Pad
314
318
bool ret = ESP_OK == uart_set_pin (uart -> num , UART_PIN_NO_CHANGE , rxPin , UART_PIN_NO_CHANGE , UART_PIN_NO_CHANGE );
315
319
#if SOC_UART_LP_NUM >= 1
316
- if (ret && uart_num == LP_UART_NUM_0 ) {
317
- ret &= lp_uart_config_io (rxPin , RTC_GPIO_MODE_INPUT_ONLY , SOC_UART_RX_PIN_IDX );
320
+ if (ret && uart_num >= SOC_UART_HP_NUM ) { // it is a LP UART NUM
321
+ ret &= lp_uart_config_io (uart -> num , rxPin , RTC_GPIO_MODE_INPUT_ONLY , SOC_UART_RX_PIN_IDX );
318
322
}
319
323
#endif
320
324
if (ret ) {
@@ -336,8 +340,8 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
336
340
// connect TX Pad
337
341
bool ret = ESP_OK == uart_set_pin (uart -> num , txPin , UART_PIN_NO_CHANGE , UART_PIN_NO_CHANGE , UART_PIN_NO_CHANGE );
338
342
#if SOC_UART_LP_NUM >= 1
339
- if (ret && uart_num == LP_UART_NUM_0 ) {
340
- ret &= lp_uart_config_io (txPin , RTC_GPIO_MODE_OUTPUT_ONLY , SOC_UART_TX_PIN_IDX );
343
+ if (ret && uart_num >= SOC_UART_HP_NUM ) { // it is a LP UART NUM
344
+ ret &= lp_uart_config_io (uart -> num , txPin , RTC_GPIO_MODE_OUTPUT_ONLY , SOC_UART_TX_PIN_IDX );
341
345
}
342
346
#endif
343
347
if (ret ) {
@@ -359,8 +363,8 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
359
363
// connect CTS Pad
360
364
bool ret = ESP_OK == uart_set_pin (uart -> num , UART_PIN_NO_CHANGE , UART_PIN_NO_CHANGE , UART_PIN_NO_CHANGE , ctsPin );
361
365
#if SOC_UART_LP_NUM >= 1
362
- if (ret && uart_num == LP_UART_NUM_0 ) {
363
- ret &= lp_uart_config_io (ctsPin , RTC_GPIO_MODE_INPUT_ONLY , SOC_UART_CTS_PIN_IDX );
366
+ if (ret && uart_num >= SOC_UART_HP_NUM ) { // it is a LP UART NUM
367
+ ret &= lp_uart_config_io (uart -> num , ctsPin , RTC_GPIO_MODE_INPUT_ONLY , SOC_UART_CTS_PIN_IDX );
364
368
}
365
369
#endif
366
370
if (ret ) {
@@ -382,8 +386,8 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
382
386
// connect RTS Pad
383
387
bool ret = ESP_OK == uart_set_pin (uart -> num , UART_PIN_NO_CHANGE , UART_PIN_NO_CHANGE , rtsPin , UART_PIN_NO_CHANGE );
384
388
#if SOC_UART_LP_NUM >= 1
385
- if (ret && uart_num == LP_UART_NUM_0 ) {
386
- ret &= lp_uart_config_io (rtsPin , RTC_GPIO_MODE_OUTPUT_ONLY , SOC_UART_RTS_PIN_IDX );
389
+ if (ret && uart_num >= SOC_UART_HP_NUM ) { // it is a LP UART NUM
390
+ ret &= lp_uart_config_io (uart -> num , rtsPin , RTC_GPIO_MODE_OUTPUT_ONLY , SOC_UART_RTS_PIN_IDX );
387
391
}
388
392
#endif
389
393
if (ret ) {
@@ -664,7 +668,7 @@ uart_t *uartBegin(
664
668
rxfifo_full_thrhd = uart_config .rx_flow_ctrl_thresh ; // makes sure that it will be set correctly in the struct
665
669
uart_config .baud_rate = baudrate ;
666
670
#if SOC_UART_LP_NUM >= 1
667
- if (uart_nr == LP_UART_NUM_0 ) {
671
+ if (uart_nr >= SOC_UART_HP_NUM ) { // it is a LP UART NUM
668
672
uart_config .lp_source_clk = LP_UART_SCLK_DEFAULT ; // use default LP clock
669
673
log_v ("Setting UART%d to use LP clock" , uart_nr );
670
674
} else
@@ -974,7 +978,7 @@ void uartSetBaudRate(uart_t *uart, uint32_t baud_rate) {
974
978
#if SOC_UART_SUPPORT_XTAL_CLK // ESP32-S3, ESP32-C3, ESP32-C5, ESP32-C6, ESP32-H2 and ESP32-P4
975
979
soc_module_clk_t newClkSrc = UART_SCLK_XTAL ;
976
980
#if SOC_UART_LP_NUM >= 1
977
- if (uart -> num == LP_UART_NUM_0 ) {
981
+ if (uart -> num >= SOC_UART_HP_NUM ) { // it is a LP UART NUM
978
982
newClkSrc = LP_UART_SCLK_DEFAULT ; // use default LP clock
979
983
}
980
984
#endif
@@ -1350,7 +1354,7 @@ unsigned long uartDetectBaudrate(uart_t *uart) {
1350
1354
*/
1351
1355
void uart_internal_loopback (uint8_t uartNum , int8_t rxPin ) {
1352
1356
// LP UART is not supported for loopback
1353
- if (uartNum > SOC_UART_HP_NUM - 1 || !GPIO_IS_VALID_GPIO (rxPin )) {
1357
+ if (uartNum >= SOC_UART_HP_NUM || !GPIO_IS_VALID_GPIO (rxPin )) {
1354
1358
log_e ("UART%d is not supported for loopback or RX pin %d is invalid." , uartNum , rxPin );
1355
1359
return ;
1356
1360
}
0 commit comments