@@ -67,7 +67,7 @@ struct uart_struct_t {
6767
6868static uart_t _uart_bus_array [] = {
6969 {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
7171 {1 , false, 0 , NULL , -1 , -1 , -1 , -1 , 0 , 0 , 0 , 0 , false, 0 },
7272#endif
7373#if SOC_UART_NUM > 2
@@ -117,8 +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 (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 ) {
122121 /* Skip configuration if the LP_IO is -1 */
123122 if (pin < 0 ) {
124123 return true;
@@ -138,21 +137,21 @@ static bool lp_uart_config_io(uint8_t uart_num, int8_t pin, rtc_gpio_mode_t dire
138137
139138 // Connect pins
140139 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
142141 // When LP_IO Matrix is not support, LP_IO Mux must be connected to the pins
143142 if (rtc_gpio_iomux_func_sel (pin , upin -> iomux_func ) != ESP_OK ) {
144143 log_e ("Failed to set LP_IO pin %d into Mux function" , pin );
145144 return false;
146145 }
147- #else // So far, only ESP32-P4
146+ #else // So far, only ESP32-P4
148147 // If the configured pin is the default LP_IO Mux pin for LP UART, then set the LP_IO MUX function
149148 if (upin -> default_gpio == pin ) {
150149 if (rtc_gpio_iomux_func_sel (pin , upin -> iomux_func ) != ESP_OK ) {
151150 log_e ("Failed to set LP_IO pin %d into Mux function" , pin );
152151 return false;
153152 }
154153 } else {
155- // Otherwise, set the LP_IO Matrix and select FUNC1
154+ // Otherwise, set the LP_IO Matrix and select FUNC1
156155 if (rtc_gpio_iomux_func_sel (pin , 1 ) != ESP_OK ) {
157156 log_e ("Failed to set LP_IO pin %d into Mux function GPIO" , pin );
158157 return false;
@@ -169,43 +168,43 @@ static bool lp_uart_config_io(uint8_t uart_num, int8_t pin, rtc_gpio_mode_t dire
169168 return false;
170169 }
171170 }
172- #endif // SOC_LP_GPIO_MATRIX_SUPPORTED
171+ #endif // SOC_LP_GPIO_MATRIX_SUPPORTED
173172
174173 return true;
175174}
176175
177176// When LP UART needs the RTC IO MUX to set the pin, it will always have fixed pins for RX, TX, CTS and RTS
178177static 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+ }
202201 }
203202 return true;
204- #else // ESP32-P4 can set any pin for LP UART
203+ #else // ESP32-P4 can set any pin for LP UART
205204 return true;
206- #endif // SOC_LP_GPIO_MATRIX_SUPPORTED
205+ #endif // SOC_LP_GPIO_MATRIX_SUPPORTED
207206}
208- #endif // SOC_UART_LP_NUM >= 1
207+ #endif // SOC_UART_LP_NUM >= 1
209208
210209// Negative Pin Number will keep it unmodified, thus this function can detach individual pins
211210// 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
305304 uart_t * uart = & _uart_bus_array [uart_num ];
306305 //log_v("attaching UART%d pins: prev,new RX(%d,%d) TX(%d,%d) CTS(%d,%d) RTS(%d,%d)", uart_num,
307306 // uart->_rxPin, rxPin, uart->_txPin, txPin, uart->_ctsPin, ctsPin, uart->_rtsPin, rtsPin); vTaskDelay(10);
308-
307+
309308 // IDF uart_set_pin() checks if the pin is used within LP UART and if it is a valid RTC IO pin
310309 // No need for Arduino Layer to check it again
311310 bool retCode = true;
@@ -317,7 +316,7 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
317316 // connect RX Pad
318317 bool ret = ESP_OK == uart_set_pin (uart -> num , UART_PIN_NO_CHANGE , rxPin , UART_PIN_NO_CHANGE , UART_PIN_NO_CHANGE );
319318#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
321320 ret &= lp_uart_config_io (uart -> num , rxPin , RTC_GPIO_MODE_INPUT_ONLY , SOC_UART_RX_PIN_IDX );
322321 }
323322#endif
@@ -340,7 +339,7 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
340339 // connect TX Pad
341340 bool ret = ESP_OK == uart_set_pin (uart -> num , txPin , UART_PIN_NO_CHANGE , UART_PIN_NO_CHANGE , UART_PIN_NO_CHANGE );
342341#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
344343 ret &= lp_uart_config_io (uart -> num , txPin , RTC_GPIO_MODE_OUTPUT_ONLY , SOC_UART_TX_PIN_IDX );
345344 }
346345#endif
@@ -363,7 +362,7 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
363362 // connect CTS Pad
364363 bool ret = ESP_OK == uart_set_pin (uart -> num , UART_PIN_NO_CHANGE , UART_PIN_NO_CHANGE , UART_PIN_NO_CHANGE , ctsPin );
365364#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
367366 ret &= lp_uart_config_io (uart -> num , ctsPin , RTC_GPIO_MODE_INPUT_ONLY , SOC_UART_CTS_PIN_IDX );
368367 }
369368#endif
@@ -386,7 +385,7 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
386385 // connect RTS Pad
387386 bool ret = ESP_OK == uart_set_pin (uart -> num , UART_PIN_NO_CHANGE , UART_PIN_NO_CHANGE , rtsPin , UART_PIN_NO_CHANGE );
388387#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
390389 ret &= lp_uart_config_io (uart -> num , rtsPin , RTC_GPIO_MODE_OUTPUT_ONLY , SOC_UART_RTS_PIN_IDX );
391390 }
392391#endif
@@ -551,7 +550,7 @@ uart_t *uartBegin(
551550 }
552551 uart_t * uart = & _uart_bus_array [uart_nr ];
553552 log_v ("UART%d baud(%ld) Mode(%x) rxPin(%d) txPin(%d)" , uart_nr , baudrate , config , rxPin , txPin );
554-
553+
555554#if SOC_UART_LP_NUM >= 1
556555 // check if LP UART is being used and if the pins are valid
557556 if (!lpuartCheckPins (rxPin , txPin , UART_PIN_NO_CHANGE , UART_PIN_NO_CHANGE , uart_nr )) {
@@ -664,14 +663,17 @@ uart_t *uartBegin(
664663 uart_config .stop_bits = (config & 0x30 ) >> 4 ;
665664 uart_config .flow_ctrl = UART_HW_FLOWCTRL_DISABLE ;
666665 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
669671 uart_config .baud_rate = baudrate ;
670672#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
672674 uart_config .lp_source_clk = LP_UART_SCLK_DEFAULT ; // use default LP clock
673675 log_v ("Setting UART%d to use LP clock" , uart_nr );
674- } else
676+ } else
675677#endif
676678 {
677679 // 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) {
982984 newClkSrc = LP_UART_SCLK_DEFAULT ; // use default LP clock
983985 }
984986#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
990992 soc_module_clk_t newClkSrc = baud_rate <= REF_TICK_BAUDRATE_LIMIT ? SOC_MOD_CLK_REF_TICK : SOC_MOD_CLK_APB ;
991993 uart_ll_set_sclk (UART_LL_GET_HW (uart -> num ), newClkSrc );
992994#endif
0 commit comments