Skip to content

Commit 7cca982

Browse files
committed
feat(lp_uart): supports any number of lp uart port for the future
1 parent a68810c commit 7cca982

File tree

2 files changed

+30
-26
lines changed

2 files changed

+30
-26
lines changed

Diff for: cores/esp32/HardwareSerial.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ void HardwareSerial::setDebugOutput(bool en) {
511511
return;
512512
}
513513
#if (SOC_UART_LP_NUM >= 1)
514-
if (_uart_nr == LP_UART_NUM_0) {
514+
if (_uart_nr >= SOC_UART_HP_NUM) {
515515
log_e("LP UART does not support Debug Output.");
516516
return;
517517
}

Diff for: cores/esp32/esp32-hal-uart.c

+29-25
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ static uart_t _uart_bus_array[] = {
117117

118118
#if SOC_UART_LP_NUM >= 1
119119
// 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)
121121
{
122122
/* Skip configuration if the LP_IO is -1 */
123123
if (pin < 0) {
@@ -137,7 +137,7 @@ static bool lp_uart_config_io(int8_t pin, rtc_gpio_mode_t direction, uint32_t id
137137
}
138138

139139
// 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];
141141
#if !SOC_LP_GPIO_MATRIX_SUPPORTED // ESP32-C6/C61/C5
142142
// When LP_IO Matrix is not support, LP_IO Mux must be connected to the pins
143143
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
160160
// Connect the LP_IO to the LP UART peripheral signal
161161
esp_err_t ret;
162162
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);
164164
} 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);
166166
}
167167
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);
169169
return false;
170170
}
171171
}
@@ -178,21 +178,25 @@ static bool lp_uart_config_io(int8_t pin, rtc_gpio_mode_t direction, uint32_t id
178178
static bool lpuartCheckPins(int8_t rxPin, int8_t txPin, int8_t ctsPin, int8_t rtsPin, uint8_t uart_nr) {
179179
// check if LP UART is being used and if the pins are valid
180180
#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);
184185
return false;
185186
}
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);
188190
return false;
189191
}
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);
192195
return false;
193196
}
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);
196200
return false;
197201
}
198202
}
@@ -313,8 +317,8 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
313317
// connect RX Pad
314318
bool ret = ESP_OK == uart_set_pin(uart->num, UART_PIN_NO_CHANGE, rxPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
315319
#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);
318322
}
319323
#endif
320324
if (ret) {
@@ -336,8 +340,8 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
336340
// connect TX Pad
337341
bool ret = ESP_OK == uart_set_pin(uart->num, txPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
338342
#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);
341345
}
342346
#endif
343347
if (ret) {
@@ -359,8 +363,8 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
359363
// connect CTS Pad
360364
bool ret = ESP_OK == uart_set_pin(uart->num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, ctsPin);
361365
#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);
364368
}
365369
#endif
366370
if (ret) {
@@ -382,8 +386,8 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
382386
// connect RTS Pad
383387
bool ret = ESP_OK == uart_set_pin(uart->num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, rtsPin, UART_PIN_NO_CHANGE);
384388
#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);
387391
}
388392
#endif
389393
if (ret) {
@@ -664,7 +668,7 @@ uart_t *uartBegin(
664668
rxfifo_full_thrhd = uart_config.rx_flow_ctrl_thresh; // makes sure that it will be set correctly in the struct
665669
uart_config.baud_rate = baudrate;
666670
#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
668672
uart_config.lp_source_clk = LP_UART_SCLK_DEFAULT; // use default LP clock
669673
log_v("Setting UART%d to use LP clock", uart_nr);
670674
} else
@@ -974,7 +978,7 @@ void uartSetBaudRate(uart_t *uart, uint32_t baud_rate) {
974978
#if SOC_UART_SUPPORT_XTAL_CLK // ESP32-S3, ESP32-C3, ESP32-C5, ESP32-C6, ESP32-H2 and ESP32-P4
975979
soc_module_clk_t newClkSrc = UART_SCLK_XTAL;
976980
#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
978982
newClkSrc = LP_UART_SCLK_DEFAULT; // use default LP clock
979983
}
980984
#endif
@@ -1350,7 +1354,7 @@ unsigned long uartDetectBaudrate(uart_t *uart) {
13501354
*/
13511355
void uart_internal_loopback(uint8_t uartNum, int8_t rxPin) {
13521356
// 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)) {
13541358
log_e("UART%d is not supported for loopback or RX pin %d is invalid.", uartNum, rxPin);
13551359
return;
13561360
}

0 commit comments

Comments
 (0)