uart_events_example_main.c 5.9 KB

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