Select one of the symbols to view example projects that use it.
 
Outline
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/uart.h"
#include "esp_log.h"
TAG
#define EX_UART_NUM
#define PATTERN_CHR_NUM
#define BUF_SIZE
#define RD_BUF_SIZE
uart0_queue
uart_event_task(void *)
app_main()
Files
loading...
SourceVuESP-IDF Framework and Examplesuart_events samplemain/uart_events_example_main.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* UART Events Example This example code is in the Public Domain (or CC0 licensed, at your option.) Unless required by applicable law or agreed to in writing, this software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *//* ... */ #include <stdio.h> #include <string.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/queue.h" #include "driver/uart.h" #include "esp_log.h"7 includes static const char *TAG = "uart_events"; /** * This example shows how to use the UART driver to handle special UART events. * * It also reads data from UART0 directly, and echoes it to console. * * - Port: UART0 * - Receive (Rx) buffer: on * - Transmit (Tx) buffer: off * - Flow control: off * - Event queue: on * - Pin assignment: TxD (default), RxD (default) *//* ... */ #define EX_UART_NUM UART_NUM_0 #define PATTERN_CHR_NUM (3) /*!< Set the number of consecutive and identical characters received by receiver which defines a UART pattern*/ #define BUF_SIZE (1024) #define RD_BUF_SIZE (BUF_SIZE) static QueueHandle_t uart0_queue; static void uart_event_task(void *pvParameters) { uart_event_t event; size_t buffered_size; uint8_t* dtmp = (uint8_t*) malloc(RD_BUF_SIZE); for (;;) { //Waiting for UART event. if (xQueueReceive(uart0_queue, (void *)&event, (TickType_t)portMAX_DELAY)) { bzero(dtmp, RD_BUF_SIZE); ESP_LOGI(TAG, "uart[%d] event:", EX_UART_NUM); switch (event.type) { //Event of UART receiving data /*We'd better handler data event fast, there would be much more data events than other types of events. If we take too much time on data event, the queue might be full.*//* ... */ case UART_DATA: ESP_LOGI(TAG, "[UART DATA]: %d", event.size); uart_read_bytes(EX_UART_NUM, dtmp, event.size, portMAX_DELAY); ESP_LOGI(TAG, "[DATA EVT]:"); uart_write_bytes(EX_UART_NUM, (const char*) dtmp, event.size); break; //Event of HW FIFO overflow detected... case UART_FIFO_OVF: ESP_LOGI(TAG, "hw fifo overflow"); // If fifo overflow happened, you should consider adding flow control for your application. // The ISR has already reset the rx FIFO, // As an example, we directly flush the rx buffer here in order to read more data. uart_flush_input(EX_UART_NUM); xQueueReset(uart0_queue); break; //Event of UART ring buffer full... case UART_BUFFER_FULL: ESP_LOGI(TAG, "ring buffer full"); // If buffer full happened, you should consider increasing your buffer size // As an example, we directly flush the rx buffer here in order to read more data. uart_flush_input(EX_UART_NUM); xQueueReset(uart0_queue); break; //Event of UART RX break detected... case UART_BREAK: ESP_LOGI(TAG, "uart rx break"); break; //Event of UART parity check error... case UART_PARITY_ERR: ESP_LOGI(TAG, "uart parity error"); break; //Event of UART frame error... case UART_FRAME_ERR: ESP_LOGI(TAG, "uart frame error"); break; //UART_PATTERN_DET... case UART_PATTERN_DET: uart_get_buffered_data_len(EX_UART_NUM, &buffered_size); int pos = uart_pattern_pop_pos(EX_UART_NUM); ESP_LOGI(TAG, "[UART PATTERN DETECTED] pos: %d, buffered size: %d", pos, buffered_size); if (pos == -1) { // There used to be a UART_PATTERN_DET event, but the pattern position queue is full so that it can not // record the position. We should set a larger queue size. // As an example, we directly flush the rx buffer here. uart_flush_input(EX_UART_NUM); }{...} else { uart_read_bytes(EX_UART_NUM, dtmp, pos, 100 / portTICK_PERIOD_MS); uint8_t pat[PATTERN_CHR_NUM + 1]; memset(pat, 0, sizeof(pat)); uart_read_bytes(EX_UART_NUM, pat, PATTERN_CHR_NUM, 100 / portTICK_PERIOD_MS); ESP_LOGI(TAG, "read data: %s", dtmp); ESP_LOGI(TAG, "read pat : %s", pat); }{...} break; //Others... default: ESP_LOGI(TAG, "uart event type: %d", event.type); break;... }{...} }{...} }{...} free(dtmp); dtmp = NULL; vTaskDelete(NULL); }{ ... } void app_main(void) { esp_log_level_set(TAG, ESP_LOG_INFO); /* Configure parameters of an UART driver, * communication pins and install the driver *//* ... */ uart_config_t uart_config = { .baud_rate = 115200, .data_bits = UART_DATA_8_BITS, .parity = UART_PARITY_DISABLE, .stop_bits = UART_STOP_BITS_1, .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, .source_clk = UART_SCLK_DEFAULT, }{...}; //Install UART driver, and get the queue. uart_driver_install(EX_UART_NUM, BUF_SIZE * 2, BUF_SIZE * 2, 20, &uart0_queue, 0); uart_param_config(EX_UART_NUM, &uart_config); //Set UART log level esp_log_level_set(TAG, ESP_LOG_INFO); //Set UART pins (using UART0 default pins ie no changes.) uart_set_pin(EX_UART_NUM, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); //Set uart pattern detect function. uart_enable_pattern_det_baud_intr(EX_UART_NUM, '+', PATTERN_CHR_NUM, 9, 0, 0); //Reset the pattern queue length to record at most 20 pattern positions. uart_pattern_queue_reset(EX_UART_NUM, 20); //Create a task to handler UART event from ISR xTaskCreate(uart_event_task, "uart_event_task", 3072, NULL, 12, NULL); }{ ... }
Details