@@ -93,15 +93,27 @@ bool uartIsDriverInstalled(uart_t* uart)
9393 return false;
9494}
9595
96- void uartSetPins (uart_t * uart , uint8_t rxPin , uint8_t txPin )
96+ // Valid pin UART_PIN_NO_CHANGE is defined to (-1)
97+ // Negative Pin Number will keep it unmodified, thus this function can set individual pins
98+ void uartSetPins (uart_t * uart , int8_t rxPin , int8_t txPin , int8_t ctsPin , int8_t rtsPin )
9799{
98- if (uart == NULL || rxPin >= SOC_GPIO_PIN_COUNT || txPin >= SOC_GPIO_PIN_COUNT ) {
100+ if (uart == NULL ) {
99101 return ;
100102 }
101103 UART_MUTEX_LOCK ();
102- ESP_ERROR_CHECK (uart_set_pin (uart -> num , txPin , rxPin , UART_PIN_NO_CHANGE , UART_PIN_NO_CHANGE ));
103- UART_MUTEX_UNLOCK ();
104+ // IDF uart_set_pin() will issue necessary Error Message and take care of all GPIO Number validation.
105+ uart_set_pin (uart -> num , txPin , rxPin , ctsPin , rtsPin );
106+ UART_MUTEX_UNLOCK ();
107+ }
104108
109+ //
110+ void uartSetHwFlowCtrlMode (uart_t * uart , uint8_t mode , uint8_t threshold ) {
111+ if (uart == NULL ) {
112+ return ;
113+ }
114+ // IDF will issue corresponding error message when mode or threshold are wrong and prevent crashing
115+ // IDF will check (mode > HW_FLOWCTRL_CTS_RTS || threshold >= SOC_UART_FIFO_LEN)
116+ uart_set_hw_flow_ctrl (uart -> num , (uart_hw_flowcontrol_t ) mode , threshold );
105117}
106118
107119
@@ -111,10 +123,6 @@ uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rx
111123 return NULL ;
112124 }
113125
114- if (rxPin == -1 && txPin == -1 ) {
115- return NULL ;
116- }
117-
118126 uart_t * uart = & _uart_bus_array [uart_nr ];
119127
120128 if (uart_is_driver_installed (uart_nr )) {
@@ -175,13 +183,13 @@ void uartSetRxInvert(uart_t* uart, bool invert)
175183 if (uart == NULL )
176184 return ;
177185#if 0
178- // POTENTIAL ISSUE :: original code only set/reset rxd_inv bit
186+ // POTENTIAL ISSUE :: original code only set/reset rxd_inv bit
179187 // IDF or LL set/reset the whole inv_mask!
180188 if (invert )
181189 ESP_ERROR_CHECK (uart_set_line_inverse (uart -> num , UART_SIGNAL_RXD_INV ));
182190 else
183191 ESP_ERROR_CHECK (uart_set_line_inverse (uart -> num , UART_SIGNAL_INV_DISABLE ));
184-
192+
185193#else
186194 // this implementation is better over IDF API because it only affects RXD
187195 // this is supported in ESP32, ESP32-S2 and ESP32-C3
@@ -466,7 +474,6 @@ void log_print_buf(const uint8_t *b, size_t len){
466474 */
467475unsigned long uartBaudrateDetect (uart_t * uart , bool flg )
468476{
469- #ifndef CONFIG_IDF_TARGET_ESP32S3
470477 if (uart == NULL ) {
471478 return 0 ;
472479 }
@@ -484,32 +491,29 @@ unsigned long uartBaudrateDetect(uart_t *uart, bool flg)
484491 UART_MUTEX_UNLOCK ();
485492
486493 return ret ;
487- #else
488- return 0 ;
489- #endif
490494}
491495
492496
493497/*
494498 * To start detection of baud rate with the uart the auto_baud.en bit needs to be cleared and set. The bit period is
495499 * detected calling uartBadrateDetect(). The raw baudrate is computed using the UART_CLK_FREQ. The raw baudrate is
496500 * rounded to the closed real baudrate.
497- *
501+ *
498502 * ESP32-C3 reports wrong baud rate detection as shown below:
499- *
503+ *
500504 * This will help in a future recall for the C3.
501505 * Baud Sent: Baud Read:
502506 * 300 --> 19536
503507 * 2400 --> 19536
504- * 4800 --> 19536
505- * 9600 --> 28818
508+ * 4800 --> 19536
509+ * 9600 --> 28818
506510 * 19200 --> 57678
507511 * 38400 --> 115440
508512 * 57600 --> 173535
509513 * 115200 --> 347826
510514 * 230400 --> 701754
511- *
512- *
515+ *
516+ *
513517*/
514518void uartStartDetectBaudrate (uart_t * uart ) {
515519 if (uart == NULL ) {
@@ -531,7 +535,7 @@ void uartStartDetectBaudrate(uart_t *uart) {
531535 //hw->rx_filt.glitch_filt_en = 1;
532536 //hw->conf0.autobaud_en = 0;
533537 //hw->conf0.autobaud_en = 1;
534- #elif CONFIG_IDF_TARGET_ESP32S3
538+
535539#else
536540 hw -> auto_baud .glitch_filt = 0x08 ;
537541 hw -> auto_baud .en = 0 ;
@@ -568,7 +572,6 @@ uartDetectBaudrate(uart_t *uart)
568572
569573#ifdef CONFIG_IDF_TARGET_ESP32C3
570574 //hw->conf0.autobaud_en = 0;
571- #elif CONFIG_IDF_TARGET_ESP32S3
572575#else
573576 hw -> auto_baud .en = 0 ;
574577#endif
0 commit comments