@@ -73,7 +73,7 @@ static uart_t _uart_bus_array[] = {
73
73
74
74
// solves issue https://github.com/espressif/arduino-esp32/issues/6032
75
75
// baudrate must be multiplied when CPU Frequency is lower than APB 80MHz
76
- uint32_t _get_effective_baudrate (uint32_t baudrate )
76
+ uint32_t _get_effective_baudrate (uint32_t baudrate )
77
77
{
78
78
uint32_t Freq = getApbFrequency ()/1000000 ;
79
79
if (Freq < 80 ) {
@@ -143,7 +143,7 @@ static void uart_event_task(void *args)
143
143
}
144
144
145
145
146
- bool uartIsDriverInstalled (uart_t * uart )
146
+ bool uartIsDriverInstalled (uart_t * uart )
147
147
{
148
148
if (uart == NULL ) {
149
149
return 0 ;
@@ -155,15 +155,27 @@ bool uartIsDriverInstalled(uart_t* uart)
155
155
return false;
156
156
}
157
157
158
- void uartSetPins (uart_t * uart , uint8_t rxPin , uint8_t txPin )
158
+ // Valid pin UART_PIN_NO_CHANGE is defined to (-1)
159
+ // Negative Pin Number will keep it unmodified, thus this function can set individual pins
160
+ void uartSetPins (uart_t * uart , int8_t rxPin , int8_t txPin , int8_t ctsPin , int8_t rtsPin )
159
161
{
160
- if (uart == NULL || rxPin >= SOC_GPIO_PIN_COUNT || txPin >= SOC_GPIO_PIN_COUNT ) {
162
+ if (uart == NULL ) {
161
163
return ;
162
164
}
163
165
UART_MUTEX_LOCK ();
164
- ESP_ERROR_CHECK (uart_set_pin (uart -> num , txPin , rxPin , UART_PIN_NO_CHANGE , UART_PIN_NO_CHANGE ));
165
- UART_MUTEX_UNLOCK ();
166
+ // IDF uart_set_pin() will issue necessary Error Message and take care of all GPIO Number validation.
167
+ uart_set_pin (uart -> num , txPin , rxPin , ctsPin , rtsPin );
168
+ UART_MUTEX_UNLOCK ();
169
+ }
166
170
171
+ //
172
+ void uartSetHwFlowCtrlMode (uart_t * uart , uint8_t mode , uint8_t threshold ) {
173
+ if (uart == NULL ) {
174
+ return ;
175
+ }
176
+ // IDF will issue corresponding error message when mode or threshold are wrong and prevent crashing
177
+ // IDF will check (mode > HW_FLOWCTRL_CTS_RTS || threshold >= SOC_UART_FIFO_LEN)
178
+ uart_set_hw_flow_ctrl (uart -> num , (uart_hw_flowcontrol_t ) mode , threshold );
167
179
}
168
180
169
181
@@ -173,10 +185,6 @@ uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rx
173
185
return NULL ;
174
186
}
175
187
176
- if (rxPin == -1 && txPin == -1 ) {
177
- return NULL ;
178
- }
179
-
180
188
uart_t * uart = & _uart_bus_array [uart_nr ];
181
189
182
190
if (uart_is_driver_installed (uart_nr )) {
@@ -208,10 +216,10 @@ uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rx
208
216
ESP_ERROR_CHECK (uart_param_config (uart_nr , & uart_config ));
209
217
ESP_ERROR_CHECK (uart_set_pin (uart_nr , txPin , rxPin , UART_PIN_NO_CHANGE , UART_PIN_NO_CHANGE ));
210
218
211
- // Is it right or the idea is to swap rx and tx pins?
219
+ // Is it right or the idea is to swap rx and tx pins?
212
220
if (inverted ) {
213
221
// invert signal for both Rx and Tx
214
- ESP_ERROR_CHECK (uart_set_line_inverse (uart_nr , UART_SIGNAL_TXD_INV | UART_SIGNAL_RXD_INV ));
222
+ ESP_ERROR_CHECK (uart_set_line_inverse (uart_nr , UART_SIGNAL_TXD_INV | UART_SIGNAL_RXD_INV ));
215
223
}
216
224
217
225
// Creating UART event Task
@@ -231,7 +239,7 @@ void uartEnd(uart_t* uart)
231
239
if (uart == NULL ) {
232
240
return ;
233
241
}
234
-
242
+
235
243
UART_MUTEX_LOCK ();
236
244
uart_driver_delete (uart -> num );
237
245
if (uart -> envent_task ) {
@@ -248,13 +256,13 @@ void uartSetRxInvert(uart_t* uart, bool invert)
248
256
if (uart == NULL )
249
257
return ;
250
258
#if 0
251
- // POTENTIAL ISSUE :: original code only set/reset rxd_inv bit
259
+ // POTENTIAL ISSUE :: original code only set/reset rxd_inv bit
252
260
// IDF or LL set/reset the whole inv_mask!
253
261
if (invert )
254
262
ESP_ERROR_CHECK (uart_set_line_inverse (uart -> num , UART_SIGNAL_RXD_INV ));
255
263
else
256
264
ESP_ERROR_CHECK (uart_set_line_inverse (uart -> num , UART_SIGNAL_INV_DISABLE ));
257
-
265
+
258
266
#else
259
267
// this implementation is better over IDF API because it only affects RXD
260
268
// this is supported in ESP32, ESP32-S2 and ESP32-C3
@@ -263,7 +271,7 @@ void uartSetRxInvert(uart_t* uart, bool invert)
263
271
hw -> conf0 .rxd_inv = 1 ;
264
272
else
265
273
hw -> conf0 .rxd_inv = 0 ;
266
- #endif
274
+ #endif
267
275
}
268
276
269
277
@@ -289,7 +297,7 @@ uint32_t uartAvailableForWrite(uart_t* uart)
289
297
return 0 ;
290
298
}
291
299
UART_MUTEX_LOCK ();
292
- uint32_t available = uart_ll_get_txfifo_len (UART_LL_GET_HW (uart -> num ));
300
+ uint32_t available = uart_ll_get_txfifo_len (UART_LL_GET_HW (uart -> num ));
293
301
UART_MUTEX_UNLOCK ();
294
302
return available ;
295
303
}
@@ -373,7 +381,7 @@ void uartFlushTxOnly(uart_t* uart, bool txOnly)
373
381
if (uart == NULL ) {
374
382
return ;
375
383
}
376
-
384
+
377
385
UART_MUTEX_LOCK ();
378
386
while (!uart_ll_is_tx_idle (UART_LL_GET_HW (uart -> num )));
379
387
@@ -486,7 +494,7 @@ int log_printf(const char *format, ...)
486
494
xSemaphoreTake (_uart_bus_array [s_uart_debug_nr ].lock , portMAX_DELAY );
487
495
}
488
496
#endif
489
-
497
+
490
498
vsnprintf (temp , len + 1 , format , arg );
491
499
ets_printf ("%s" , temp );
492
500
@@ -563,22 +571,22 @@ unsigned long uartBaudrateDetect(uart_t *uart, bool flg)
563
571
* To start detection of baud rate with the uart the auto_baud.en bit needs to be cleared and set. The bit period is
564
572
* detected calling uartBadrateDetect(). The raw baudrate is computed using the UART_CLK_FREQ. The raw baudrate is
565
573
* rounded to the closed real baudrate.
566
- *
574
+ *
567
575
* ESP32-C3 reports wrong baud rate detection as shown below:
568
- *
576
+ *
569
577
* This will help in a future recall for the C3.
570
578
* Baud Sent: Baud Read:
571
579
* 300 --> 19536
572
580
* 2400 --> 19536
573
- * 4800 --> 19536
574
- * 9600 --> 28818
581
+ * 4800 --> 19536
582
+ * 9600 --> 28818
575
583
* 19200 --> 57678
576
584
* 38400 --> 115440
577
585
* 57600 --> 173535
578
586
* 115200 --> 347826
579
587
* 230400 --> 701754
580
- *
581
- *
588
+ *
589
+ *
582
590
*/
583
591
void uartStartDetectBaudrate (uart_t * uart ) {
584
592
if (uart == NULL ) {
@@ -588,10 +596,10 @@ void uartStartDetectBaudrate(uart_t *uart) {
588
596
uart_dev_t * hw = UART_LL_GET_HW (uart -> num );
589
597
590
598
#ifdef CONFIG_IDF_TARGET_ESP32C3
591
-
599
+
592
600
// ESP32-C3 requires further testing
593
- // Baud rate detection returns wrong values
594
-
601
+ // Baud rate detection returns wrong values
602
+
595
603
log_e ("ESP32-C3 baud rate detection is not supported." );
596
604
return ;
597
605
@@ -607,7 +615,7 @@ void uartStartDetectBaudrate(uart_t *uart) {
607
615
hw -> auto_baud .en = 1 ;
608
616
#endif
609
617
}
610
-
618
+
611
619
unsigned long
612
620
uartDetectBaudrate (uart_t * uart )
613
621
{
@@ -664,4 +672,4 @@ uartDetectBaudrate(uart_t *uart)
664
672
log_e ("ESP32-C3 baud rate detection is not supported." );
665
673
return 0 ;
666
674
#endif
667
- }
675
+ }
0 commit comments