@@ -67,7 +67,7 @@ struct uart_struct_t {
67
67
68
68
static uart_t _uart_bus_array [] = {
69
69
{0 , false, 0 , NULL , -1 , -1 , -1 , -1 , 0 , 0 , 0 , 0 , false, 0 },
70
- #if SOC_UART_NUM > 1
70
+ #if SOC_UART_NUM > 1
71
71
{1 , false, 0 , NULL , -1 , -1 , -1 , -1 , 0 , 0 , 0 , 0 , false, 0 },
72
72
#endif
73
73
#if SOC_UART_NUM > 2
@@ -117,8 +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 (uint8_t uart_num , int8_t pin , rtc_gpio_mode_t direction , uint32_t idx )
121
- {
120
+ static bool lp_uart_config_io (uint8_t uart_num , int8_t pin , rtc_gpio_mode_t direction , uint32_t idx ) {
122
121
/* Skip configuration if the LP_IO is -1 */
123
122
if (pin < 0 ) {
124
123
return true;
@@ -138,21 +137,21 @@ static bool lp_uart_config_io(uint8_t uart_num, int8_t pin, rtc_gpio_mode_t dire
138
137
139
138
// Connect pins
140
139
const uart_periph_sig_t * upin = & uart_periph_signal [uart_num ].pins [idx ];
141
- #if !SOC_LP_GPIO_MATRIX_SUPPORTED // ESP32-C6/C61/C5
140
+ #if !SOC_LP_GPIO_MATRIX_SUPPORTED // ESP32-C6/C61/C5
142
141
// When LP_IO Matrix is not support, LP_IO Mux must be connected to the pins
143
142
if (rtc_gpio_iomux_func_sel (pin , upin -> iomux_func ) != ESP_OK ) {
144
143
log_e ("Failed to set LP_IO pin %d into Mux function" , pin );
145
144
return false;
146
145
}
147
- #else // So far, only ESP32-P4
146
+ #else // So far, only ESP32-P4
148
147
// If the configured pin is the default LP_IO Mux pin for LP UART, then set the LP_IO MUX function
149
148
if (upin -> default_gpio == pin ) {
150
149
if (rtc_gpio_iomux_func_sel (pin , upin -> iomux_func ) != ESP_OK ) {
151
150
log_e ("Failed to set LP_IO pin %d into Mux function" , pin );
152
151
return false;
153
152
}
154
153
} else {
155
- // Otherwise, set the LP_IO Matrix and select FUNC1
154
+ // Otherwise, set the LP_IO Matrix and select FUNC1
156
155
if (rtc_gpio_iomux_func_sel (pin , 1 ) != ESP_OK ) {
157
156
log_e ("Failed to set LP_IO pin %d into Mux function GPIO" , pin );
158
157
return false;
@@ -169,43 +168,43 @@ static bool lp_uart_config_io(uint8_t uart_num, int8_t pin, rtc_gpio_mode_t dire
169
168
return false;
170
169
}
171
170
}
172
- #endif // SOC_LP_GPIO_MATRIX_SUPPORTED
171
+ #endif // SOC_LP_GPIO_MATRIX_SUPPORTED
173
172
174
173
return true;
175
174
}
176
175
177
176
// When LP UART needs the RTC IO MUX to set the pin, it will always have fixed pins for RX, TX, CTS and RTS
178
177
static bool lpuartCheckPins (int8_t rxPin , int8_t txPin , int8_t ctsPin , int8_t rtsPin , uint8_t uart_nr ) {
179
- // check if LP UART is being used and if the pins are valid
180
- #if !SOC_LP_GPIO_MATRIX_SUPPORTED // ESP32-C6/C61/C5
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 );
185
- return false;
186
- }
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 );
190
- return false;
191
- }
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 );
195
- return false;
196
- }
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 );
200
- return false;
201
- }
178
+ // check if LP UART is being used and if the pins are valid
179
+ #if !SOC_LP_GPIO_MATRIX_SUPPORTED // ESP32-C6/C61/C5
180
+ uint16_t lp_uart_fixed_pin = uart_periph_signal [uart_nr ].pins [SOC_UART_RX_PIN_IDX ].default_gpio ;
181
+ if (uart_nr >= SOC_UART_HP_NUM ) { // it is a LP UART NUM
182
+ if (rxPin > 0 && rxPin != lp_uart_fixed_pin ) {
183
+ log_e ("UART%d LP UART requires RX pin to be set to %d." , uart_nr , lp_uart_fixed_pin );
184
+ return false;
185
+ }
186
+ lp_uart_fixed_pin = uart_periph_signal [uart_nr ].pins [SOC_UART_TX_PIN_IDX ].default_gpio ;
187
+ if (txPin > 0 && txPin != lp_uart_fixed_pin ) {
188
+ log_e ("UART%d LP UART requires TX pin to be set to %d." , uart_nr , lp_uart_fixed_pin );
189
+ return false;
190
+ }
191
+ lp_uart_fixed_pin = uart_periph_signal [uart_nr ].pins [SOC_UART_CTS_PIN_IDX ].default_gpio ;
192
+ if (ctsPin > 0 && ctsPin != lp_uart_fixed_pin ) {
193
+ log_e ("UART%d LP UART requires CTS pin to be set to %d." , uart_nr , lp_uart_fixed_pin );
194
+ return false;
195
+ }
196
+ lp_uart_fixed_pin = uart_periph_signal [uart_nr ].pins [SOC_UART_RTS_PIN_IDX ].default_gpio ;
197
+ if (rtsPin > 0 && rtsPin != lp_uart_fixed_pin ) {
198
+ log_e ("UART%d LP UART requires RTS pin to be set to %d." , uart_nr , lp_uart_fixed_pin );
199
+ return false;
200
+ }
202
201
}
203
202
return true;
204
- #else // ESP32-P4 can set any pin for LP UART
203
+ #else // ESP32-P4 can set any pin for LP UART
205
204
return true;
206
- #endif // SOC_LP_GPIO_MATRIX_SUPPORTED
205
+ #endif // SOC_LP_GPIO_MATRIX_SUPPORTED
207
206
}
208
- #endif // SOC_UART_LP_NUM >= 1
207
+ #endif // SOC_UART_LP_NUM >= 1
209
208
210
209
// Negative Pin Number will keep it unmodified, thus this function can detach individual pins
211
210
// This function will also unset the pins in the Peripheral Manager and set the pin to -1 after detaching
@@ -305,7 +304,7 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
305
304
uart_t * uart = & _uart_bus_array [uart_num ];
306
305
//log_v("attaching UART%d pins: prev,new RX(%d,%d) TX(%d,%d) CTS(%d,%d) RTS(%d,%d)", uart_num,
307
306
// uart->_rxPin, rxPin, uart->_txPin, txPin, uart->_ctsPin, ctsPin, uart->_rtsPin, rtsPin); vTaskDelay(10);
308
-
307
+
309
308
// IDF uart_set_pin() checks if the pin is used within LP UART and if it is a valid RTC IO pin
310
309
// No need for Arduino Layer to check it again
311
310
bool retCode = true;
@@ -317,7 +316,7 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
317
316
// connect RX Pad
318
317
bool ret = ESP_OK == uart_set_pin (uart -> num , UART_PIN_NO_CHANGE , rxPin , UART_PIN_NO_CHANGE , UART_PIN_NO_CHANGE );
319
318
#if SOC_UART_LP_NUM >= 1
320
- if (ret && uart_num >= SOC_UART_HP_NUM ) { // it is a LP UART NUM
319
+ if (ret && uart_num >= SOC_UART_HP_NUM ) { // it is a LP UART NUM
321
320
ret &= lp_uart_config_io (uart -> num , rxPin , RTC_GPIO_MODE_INPUT_ONLY , SOC_UART_RX_PIN_IDX );
322
321
}
323
322
#endif
@@ -340,7 +339,7 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
340
339
// connect TX Pad
341
340
bool ret = ESP_OK == uart_set_pin (uart -> num , txPin , UART_PIN_NO_CHANGE , UART_PIN_NO_CHANGE , UART_PIN_NO_CHANGE );
342
341
#if SOC_UART_LP_NUM >= 1
343
- if (ret && uart_num >= SOC_UART_HP_NUM ) { // it is a LP UART NUM
342
+ if (ret && uart_num >= SOC_UART_HP_NUM ) { // it is a LP UART NUM
344
343
ret &= lp_uart_config_io (uart -> num , txPin , RTC_GPIO_MODE_OUTPUT_ONLY , SOC_UART_TX_PIN_IDX );
345
344
}
346
345
#endif
@@ -363,7 +362,7 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
363
362
// connect CTS Pad
364
363
bool ret = ESP_OK == uart_set_pin (uart -> num , UART_PIN_NO_CHANGE , UART_PIN_NO_CHANGE , UART_PIN_NO_CHANGE , ctsPin );
365
364
#if SOC_UART_LP_NUM >= 1
366
- if (ret && uart_num >= SOC_UART_HP_NUM ) { // it is a LP UART NUM
365
+ if (ret && uart_num >= SOC_UART_HP_NUM ) { // it is a LP UART NUM
367
366
ret &= lp_uart_config_io (uart -> num , ctsPin , RTC_GPIO_MODE_INPUT_ONLY , SOC_UART_CTS_PIN_IDX );
368
367
}
369
368
#endif
@@ -386,7 +385,7 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
386
385
// connect RTS Pad
387
386
bool ret = ESP_OK == uart_set_pin (uart -> num , UART_PIN_NO_CHANGE , UART_PIN_NO_CHANGE , rtsPin , UART_PIN_NO_CHANGE );
388
387
#if SOC_UART_LP_NUM >= 1
389
- if (ret && uart_num >= SOC_UART_HP_NUM ) { // it is a LP UART NUM
388
+ if (ret && uart_num >= SOC_UART_HP_NUM ) { // it is a LP UART NUM
390
389
ret &= lp_uart_config_io (uart -> num , rtsPin , RTC_GPIO_MODE_OUTPUT_ONLY , SOC_UART_RTS_PIN_IDX );
391
390
}
392
391
#endif
@@ -551,7 +550,7 @@ uart_t *uartBegin(
551
550
}
552
551
uart_t * uart = & _uart_bus_array [uart_nr ];
553
552
log_v ("UART%d baud(%ld) Mode(%x) rxPin(%d) txPin(%d)" , uart_nr , baudrate , config , rxPin , txPin );
554
-
553
+
555
554
#if SOC_UART_LP_NUM >= 1
556
555
// check if LP UART is being used and if the pins are valid
557
556
if (!lpuartCheckPins (rxPin , txPin , UART_PIN_NO_CHANGE , UART_PIN_NO_CHANGE , uart_nr )) {
@@ -664,14 +663,17 @@ uart_t *uartBegin(
664
663
uart_config .stop_bits = (config & 0x30 ) >> 4 ;
665
664
uart_config .flow_ctrl = UART_HW_FLOWCTRL_DISABLE ;
666
665
uart_config .rx_flow_ctrl_thresh = rxfifo_full_thrhd >= UART_HW_FIFO_LEN (uart_nr ) ? UART_HW_FIFO_LEN (uart_nr ) - 6 : rxfifo_full_thrhd ;
667
- log_v ("UART%d RX FIFO full threshold set to %d (value requested: %d || FIFO Max = %d)" , uart_nr , uart_config .rx_flow_ctrl_thresh , rxfifo_full_thrhd , UART_HW_FIFO_LEN (uart_nr ));
668
- rxfifo_full_thrhd = uart_config .rx_flow_ctrl_thresh ; // makes sure that it will be set correctly in the struct
666
+ log_v (
667
+ "UART%d RX FIFO full threshold set to %d (value requested: %d || FIFO Max = %d)" , uart_nr , uart_config .rx_flow_ctrl_thresh , rxfifo_full_thrhd ,
668
+ UART_HW_FIFO_LEN (uart_nr )
669
+ );
670
+ rxfifo_full_thrhd = uart_config .rx_flow_ctrl_thresh ; // makes sure that it will be set correctly in the struct
669
671
uart_config .baud_rate = baudrate ;
670
672
#if SOC_UART_LP_NUM >= 1
671
- if (uart_nr >= SOC_UART_HP_NUM ) { // it is a LP UART NUM
673
+ if (uart_nr >= SOC_UART_HP_NUM ) { // it is a LP UART NUM
672
674
uart_config .lp_source_clk = LP_UART_SCLK_DEFAULT ; // use default LP clock
673
675
log_v ("Setting UART%d to use LP clock" , uart_nr );
674
- } else
676
+ } else
675
677
#endif
676
678
{
677
679
// there is an issue when returning from light sleep with the C6 and H2: the uart baud rate is not restored
@@ -982,11 +984,11 @@ void uartSetBaudRate(uart_t *uart, uint32_t baud_rate) {
982
984
newClkSrc = LP_UART_SCLK_DEFAULT ; // use default LP clock
983
985
}
984
986
#endif
985
- // ESP32-P4 demands an atomic operation for setting the clock source
986
- HP_UART_SRC_CLK_ATOMIC () {
987
- uart_ll_set_sclk (UART_LL_GET_HW (uart -> num ), newClkSrc );
988
- }
989
- #else // ESP32, ESP32-S2
987
+ // ESP32-P4 demands an atomic operation for setting the clock source
988
+ HP_UART_SRC_CLK_ATOMIC () {
989
+ uart_ll_set_sclk (UART_LL_GET_HW (uart -> num ), newClkSrc );
990
+ }
991
+ #else // ESP32, ESP32-S2
990
992
soc_module_clk_t newClkSrc = baud_rate <= REF_TICK_BAUDRATE_LIMIT ? SOC_MOD_CLK_REF_TICK : SOC_MOD_CLK_APB ;
991
993
uart_ll_set_sclk (UART_LL_GET_HW (uart -> num ), newClkSrc );
992
994
#endif
0 commit comments