1 /*
2 This example code is in the Public Domain (or CC0 licensed, at your option.)
3
4 Unless required by applicable law or agreed to in writing, this
5 software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
6 CONDITIONS OF ANY KIND, either express or implied.
7 */
8
9 #include "driver/uart.h"
10 #include "freertos/xtensa_api.h"
11 #include "freertos/FreeRTOS.h"
12 #include "freertos/task.h"
13 #include "freertos/queue.h"
14 #include "esp_log.h"
15 #include "console_uart.h"
16 #include "app_spp_msg_prs.h"
17
18 #define CONSOLE_UART_NUM UART_NUM_0
19
20 static QueueHandle_t uart_queue;
21 static spp_msg_prs_cb_t spp_msg_parser;
22
23 static const uart_config_t uart_cfg = {
24 .baud_rate = 115200, //1.5M
25 .data_bits = UART_DATA_8_BITS,
26 .parity = UART_PARITY_DISABLE,
27 .stop_bits = UART_STOP_BITS_1,
28 .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
29 .rx_flow_ctrl_thresh = 127,
30 };
31
32 extern void spp_msg_args_parser(char *buf, int len);
33
spp_msg_handler(char * buf,int len)34 void spp_msg_handler(char *buf, int len)
35 {
36 ESP_LOGE(TAG_CNSL, "Command [%s]", buf);
37 spp_msg_args_parser(buf, len);
38 }
39
console_uart_task(void * pvParameters)40 static void console_uart_task(void *pvParameters)
41 {
42 int len;
43 uart_event_t event;
44 spp_msg_prs_cb_t *parser = &spp_msg_parser;
45 spp_msg_parser_reset_state(parser);
46 spp_msg_parser_register_callback(parser, spp_msg_handler);
47 spp_msg_show_usage();
48 #define TMP_BUF_LEN 128
49 uint8_t tmp_buf[128] = {0};
50
51 for (;;) {
52 //Waiting for UART event.
53 if (xQueueReceive(uart_queue, (void * )&event, (portTickType)portMAX_DELAY)) {
54 switch (event.type) {
55 //Event of UART receving data
56 case UART_DATA:
57 {
58 len = uart_read_bytes(CONSOLE_UART_NUM, tmp_buf, TMP_BUF_LEN, 0);
59 for (int i = 0; i < len; i++) {
60 spp_msg_parse(tmp_buf[i], parser);
61 }
62 break;
63 }
64 //Event of HW FIFO overflow detected
65 case UART_FIFO_OVF:
66 {
67 ESP_LOGI(TAG_CNSL, "hw fifo overflow");
68 break;
69 }
70 //Event of UART ring buffer full
71 case UART_BUFFER_FULL:
72 {
73 ESP_LOGI(TAG_CNSL, "ring buffer full");
74 break;
75 }
76 //Event of UART RX break detected
77 case UART_BREAK:
78 {
79 ESP_LOGI(TAG_CNSL, "uart rx break");
80 break;
81 }
82 //Event of UART parity check error
83 case UART_PARITY_ERR:
84 {
85 ESP_LOGI(TAG_CNSL, "uart parity error");
86 break;
87 }
88 //Event of UART frame error
89 case UART_FRAME_ERR:
90 {
91 ESP_LOGI(TAG_CNSL, "uart frame error");
92 break;
93 }
94 //Others
95 default:
96 break;
97 }
98 }
99 }
100 vTaskDelete(NULL);
101 }
102
103
console_uart_init(void)104 esp_err_t console_uart_init(void)
105 {
106 esp_err_t ret;
107
108 ret = uart_param_config(CONSOLE_UART_NUM, &uart_cfg);
109 if (ret != ESP_OK) {
110 ESP_LOGE(TAG_CNSL, "Uart %d initialize err %04x", CONSOLE_UART_NUM, ret);
111 return ret;
112 }
113
114 uart_set_pin(CONSOLE_UART_NUM, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
115 uart_driver_install(CONSOLE_UART_NUM, 1024, 1024, 8, &uart_queue, 0);
116 xTaskCreate(console_uart_task, "uTask", 2048, NULL, 8, NULL);
117
118 return ESP_OK;
119 }
120