@@ -34,7 +34,9 @@ struct uart_struct_t {
34
34
uint8_t num ;
35
35
bool has_peek ;
36
36
uint8_t peek_byte ;
37
-
37
+ QueueHandle_t uart_event_queue ;
38
+ void (* onReceive )(void );
39
+ TaskHandle_t envent_task ;
38
40
};
39
41
40
42
#if CONFIG_DISABLE_HAL_LOCKS
@@ -43,12 +45,12 @@ struct uart_struct_t {
43
45
#define UART_MUTEX_UNLOCK ()
44
46
45
47
static uart_t _uart_bus_array [] = {
46
- {0 , false, 0 },
48
+ {0 , false, 0 , NULL , NULL , NULL },
47
49
#if SOC_UART_NUM > 1
48
- {1 , false, 0 },
50
+ {1 , false, 0 , NULL , NULL , NULL },
49
51
#endif
50
52
#if SOC_UART_NUM > 2
51
- {2 , false, 0 },
53
+ {2 , false, 0 , NULL , NULL , NULL },
52
54
#endif
53
55
};
54
56
@@ -58,20 +60,20 @@ static uart_t _uart_bus_array[] = {
58
60
#define UART_MUTEX_UNLOCK () xSemaphoreGive(uart->lock)
59
61
60
62
static uart_t _uart_bus_array [] = {
61
- {NULL , 0 , false, 0 },
63
+ {NULL , 0 , false, 0 , NULL , NULL , NULL },
62
64
#if SOC_UART_NUM > 1
63
- {NULL , 1 , false, 0 },
65
+ {NULL , 1 , false, 0 , NULL , NULL , NULL },
64
66
#endif
65
67
#if SOC_UART_NUM > 2
66
- {NULL , 2 , false, 0 },
68
+ {NULL , 2 , false, 0 , NULL , NULL , NULL },
67
69
#endif
68
70
};
69
71
70
72
#endif
71
73
72
74
// solves issue https://github.com/espressif/arduino-esp32/issues/6032
73
75
// baudrate must be multiplied when CPU Frequency is lower than APB 80MHz
74
- uint32_t _get_effective_baudrate (uint32_t baudrate )
76
+ uint32_t _get_effective_baudrate (uint32_t baudrate )
75
77
{
76
78
uint32_t Freq = getApbFrequency ()/1000000 ;
77
79
if (Freq < 80 ) {
@@ -82,7 +84,68 @@ uint32_t _get_effective_baudrate(uint32_t baudrate)
82
84
}
83
85
}
84
86
85
- bool uartIsDriverInstalled (uart_t * uart )
87
+
88
+ void uartOnReceive (uart_t * uart , void (* function )(void ))
89
+ {
90
+ if (uart == NULL || function == NULL ) {
91
+ return ;
92
+ }
93
+ UART_MUTEX_LOCK ();
94
+ uart -> onReceive = function ;
95
+ UART_MUTEX_UNLOCK ();
96
+ }
97
+
98
+
99
+ static void uart_event_task (void * args )
100
+ {
101
+ uart_t * uart = (uart_t * )args ;
102
+ uart_event_t event ;
103
+ for (;;) {
104
+ //Waiting for UART event.
105
+ if (xQueueReceive (uart -> uart_event_queue , (void * )& event , (portTickType )portMAX_DELAY )) {
106
+ switch (event .type ) {
107
+ //Event of UART receving data
108
+ case UART_DATA :
109
+ UART_MUTEX_LOCK ();
110
+ if (uart -> onReceive ) uart -> onReceive ();
111
+ UART_MUTEX_UNLOCK ();
112
+ break ;
113
+ //Event of HW FIFO overflow detected
114
+ case UART_FIFO_OVF :
115
+ log_w ("UART%d FIFO Overflow. Flushing data. Consider adding Flow Control to your Application." , uart -> num );
116
+ uart_flush_input (uart -> num );
117
+ xQueueReset (uart -> uart_event_queue );
118
+ break ;
119
+ //Event of UART ring buffer full
120
+ case UART_BUFFER_FULL :
121
+ log_w ("UART%d Buffer Full. Flushing data. Consider encreasing your buffer size of your Application." , uart -> num );
122
+ uart_flush_input (uart -> num );
123
+ xQueueReset (uart -> uart_event_queue );
124
+ break ;
125
+ //Event of UART RX break detected
126
+ case UART_BREAK :
127
+ log_w ("UART%d RX break." , uart -> num );
128
+ break ;
129
+ //Event of UART parity check error
130
+ case UART_PARITY_ERR :
131
+ log_w ("UART%d parity error." , uart -> num );
132
+ break ;
133
+ //Event of UART frame error
134
+ case UART_FRAME_ERR :
135
+ log_w ("UART%d frame error." , uart -> num );
136
+ break ;
137
+ //Others
138
+ default :
139
+ log_w ("UART%d unknown event type %d." , uart -> num , event .type );
140
+ break ;
141
+ }
142
+ }
143
+ }
144
+ vTaskDelete (NULL );
145
+ }
146
+
147
+
148
+ bool uartIsDriverInstalled (uart_t * uart )
86
149
{
87
150
if (uart == NULL ) {
88
151
return 0 ;
@@ -100,9 +163,9 @@ void uartSetPins(uart_t* uart, uint8_t rxPin, uint8_t txPin)
100
163
return ;
101
164
}
102
165
UART_MUTEX_LOCK ();
103
- ESP_ERROR_CHECK (uart_set_pin (uart -> num , txPin , rxPin , UART_PIN_NO_CHANGE , UART_PIN_NO_CHANGE ));
166
+ ESP_ERROR_CHECK (uart_set_pin (uart -> num , txPin , rxPin , UART_PIN_NO_CHANGE , UART_PIN_NO_CHANGE ));
104
167
UART_MUTEX_UNLOCK ();
105
-
168
+
106
169
}
107
170
108
171
@@ -143,14 +206,20 @@ uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rx
143
206
uart_config .source_clk = UART_SCLK_APB ;
144
207
145
208
146
- ESP_ERROR_CHECK (uart_driver_install (uart_nr , 2 * queueLen , 0 , 0 , NULL , 0 ));
209
+ ESP_ERROR_CHECK (uart_driver_install (uart_nr , 2 * queueLen , 0 , 20 , & ( uart -> uart_event_queue ) , 0 ));
147
210
ESP_ERROR_CHECK (uart_param_config (uart_nr , & uart_config ));
148
211
ESP_ERROR_CHECK (uart_set_pin (uart_nr , txPin , rxPin , UART_PIN_NO_CHANGE , UART_PIN_NO_CHANGE ));
149
212
150
- // Is it right or the idea is to swap rx and tx pins?
213
+ // Is it right or the idea is to swap rx and tx pins?
151
214
if (inverted ) {
152
215
// invert signal for both Rx and Tx
153
- ESP_ERROR_CHECK (uart_set_line_inverse (uart_nr , UART_SIGNAL_TXD_INV | UART_SIGNAL_RXD_INV ));
216
+ ESP_ERROR_CHECK (uart_set_line_inverse (uart_nr , UART_SIGNAL_TXD_INV | UART_SIGNAL_RXD_INV ));
217
+ }
218
+
219
+ // Creating UART event Task
220
+ xTaskCreate (uart_event_task , "uart_event_task" , 2048 , uart , configMAX_PRIORITIES - 1 , & (uart -> envent_task ));
221
+ if (!uart -> envent_task ) {
222
+ log_e (" -- UART%d Event Task not Created!" , uart_nr );
154
223
}
155
224
156
225
UART_MUTEX_UNLOCK ();
@@ -164,9 +233,14 @@ void uartEnd(uart_t* uart)
164
233
if (uart == NULL ) {
165
234
return ;
166
235
}
167
-
236
+
168
237
UART_MUTEX_LOCK ();
169
238
uart_driver_delete (uart -> num );
239
+ if (uart -> envent_task ) {
240
+ vTaskDelete (uart -> envent_task );
241
+ uart -> envent_task = NULL ;
242
+ uart -> onReceive = NULL ;
243
+ }
170
244
UART_MUTEX_UNLOCK ();
171
245
}
172
246
@@ -176,13 +250,13 @@ void uartSetRxInvert(uart_t* uart, bool invert)
176
250
if (uart == NULL )
177
251
return ;
178
252
#if 0
179
- // POTENTIAL ISSUE :: original code only set/reset rxd_inv bit
253
+ // POTENTIAL ISSUE :: original code only set/reset rxd_inv bit
180
254
// IDF or LL set/reset the whole inv_mask!
181
255
if (invert )
182
256
ESP_ERROR_CHECK (uart_set_line_inverse (uart -> num , UART_SIGNAL_RXD_INV ));
183
257
else
184
258
ESP_ERROR_CHECK (uart_set_line_inverse (uart -> num , UART_SIGNAL_INV_DISABLE ));
185
-
259
+
186
260
#else
187
261
// this implementation is better over IDF API because it only affects RXD
188
262
// this is supported in ESP32, ESP32-S2 and ESP32-C3
@@ -191,7 +265,7 @@ void uartSetRxInvert(uart_t* uart, bool invert)
191
265
hw -> conf0 .rxd_inv = 1 ;
192
266
else
193
267
hw -> conf0 .rxd_inv = 0 ;
194
- #endif
268
+ #endif
195
269
}
196
270
197
271
@@ -217,7 +291,7 @@ uint32_t uartAvailableForWrite(uart_t* uart)
217
291
return 0 ;
218
292
}
219
293
UART_MUTEX_LOCK ();
220
- uint32_t available = uart_ll_get_txfifo_len (UART_LL_GET_HW (uart -> num ));
294
+ uint32_t available = uart_ll_get_txfifo_len (UART_LL_GET_HW (uart -> num ));
221
295
UART_MUTEX_UNLOCK ();
222
296
return available ;
223
297
}
@@ -301,7 +375,7 @@ void uartFlushTxOnly(uart_t* uart, bool txOnly)
301
375
if (uart == NULL ) {
302
376
return ;
303
377
}
304
-
378
+
305
379
UART_MUTEX_LOCK ();
306
380
while (!uart_ll_is_tx_idle (UART_LL_GET_HW (uart -> num )));
307
381
@@ -414,7 +488,7 @@ int log_printf(const char *format, ...)
414
488
xSemaphoreTake (_uart_bus_array [s_uart_debug_nr ].lock , portMAX_DELAY );
415
489
}
416
490
#endif
417
-
491
+
418
492
vsnprintf (temp , len + 1 , format , arg );
419
493
ets_printf ("%s" , temp );
420
494
@@ -491,22 +565,22 @@ unsigned long uartBaudrateDetect(uart_t *uart, bool flg)
491
565
* To start detection of baud rate with the uart the auto_baud.en bit needs to be cleared and set. The bit period is
492
566
* detected calling uartBadrateDetect(). The raw baudrate is computed using the UART_CLK_FREQ. The raw baudrate is
493
567
* rounded to the closed real baudrate.
494
- *
568
+ *
495
569
* ESP32-C3 reports wrong baud rate detection as shown below:
496
- *
570
+ *
497
571
* This will help in a future recall for the C3.
498
572
* Baud Sent: Baud Read:
499
573
* 300 --> 19536
500
574
* 2400 --> 19536
501
- * 4800 --> 19536
502
- * 9600 --> 28818
575
+ * 4800 --> 19536
576
+ * 9600 --> 28818
503
577
* 19200 --> 57678
504
578
* 38400 --> 115440
505
579
* 57600 --> 173535
506
580
* 115200 --> 347826
507
581
* 230400 --> 701754
508
- *
509
- *
582
+ *
583
+ *
510
584
*/
511
585
void uartStartDetectBaudrate (uart_t * uart ) {
512
586
if (uart == NULL ) {
@@ -516,10 +590,10 @@ void uartStartDetectBaudrate(uart_t *uart) {
516
590
uart_dev_t * hw = UART_LL_GET_HW (uart -> num );
517
591
518
592
#ifdef CONFIG_IDF_TARGET_ESP32C3
519
-
593
+
520
594
// ESP32-C3 requires further testing
521
- // Baud rate detection returns wrong values
522
-
595
+ // Baud rate detection returns wrong values
596
+
523
597
log_e ("ESP32-C3 baud rate detection is not supported." );
524
598
return ;
525
599
@@ -535,7 +609,7 @@ void uartStartDetectBaudrate(uart_t *uart) {
535
609
hw -> auto_baud .en = 1 ;
536
610
#endif
537
611
}
538
-
612
+
539
613
unsigned long
540
614
uartDetectBaudrate (uart_t * uart )
541
615
{
0 commit comments