uart_events_example_main.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* Uart 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 <stdlib.h>
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/task.h"
  12. #include "esp_system.h"
  13. #include "nvs_flash.h"
  14. #include "driver/uart.h"
  15. #include "freertos/queue.h"
  16. #include "esp_log.h"
  17. #include "soc/uart_struct.h"
  18. static const char *TAG = "uart_events";
  19. /**
  20. * This example shows how to use the UART driver to handle special UART events.
  21. *
  22. * It also reads data from UART0 directly, and echoes it to console.
  23. *
  24. * - port: UART0
  25. * - rx buffer: on
  26. * - tx buffer: on
  27. * - flow control: off
  28. * - event queue: on
  29. * - pin assignment: txd(default), rxd(default)
  30. */
  31. #define EX_UART_NUM UART_NUM_0
  32. #define BUF_SIZE (1024)
  33. static QueueHandle_t uart0_queue;
  34. static void uart_event_task(void *pvParameters)
  35. {
  36. uart_event_t event;
  37. size_t buffered_size;
  38. uint8_t* dtmp = (uint8_t*) malloc(BUF_SIZE);
  39. for(;;) {
  40. //Waiting for UART event.
  41. if(xQueueReceive(uart0_queue, (void * )&event, (portTickType)portMAX_DELAY)) {
  42. ESP_LOGI(TAG, "uart[%d] event:", EX_UART_NUM);
  43. switch(event.type) {
  44. //Event of UART receving data
  45. /*We'd better handler data event fast, there would be much more data events than
  46. other types of events. If we take too much time on data event, the queue might
  47. be full.
  48. in this example, we don't process data in event, but read data outside.*/
  49. case UART_DATA:
  50. uart_get_buffered_data_len(EX_UART_NUM, &buffered_size);
  51. ESP_LOGI(TAG, "data, len: %d; buffered len: %d", event.size, buffered_size);
  52. break;
  53. //Event of HW FIFO overflow detected
  54. case UART_FIFO_OVF:
  55. ESP_LOGI(TAG, "hw fifo overflow\n");
  56. //If fifo overflow happened, you should consider adding flow control for your application.
  57. //We can read data out out the buffer, or directly flush the rx buffer.
  58. uart_flush(EX_UART_NUM);
  59. break;
  60. //Event of UART ring buffer full
  61. case UART_BUFFER_FULL:
  62. ESP_LOGI(TAG, "ring buffer full\n");
  63. //If buffer full happened, you should consider encreasing your buffer size
  64. //We can read data out out the buffer, or directly flush the rx buffer.
  65. uart_flush(EX_UART_NUM);
  66. break;
  67. //Event of UART RX break detected
  68. case UART_BREAK:
  69. ESP_LOGI(TAG, "uart rx break\n");
  70. break;
  71. //Event of UART parity check error
  72. case UART_PARITY_ERR:
  73. ESP_LOGI(TAG, "uart parity error\n");
  74. break;
  75. //Event of UART frame error
  76. case UART_FRAME_ERR:
  77. ESP_LOGI(TAG, "uart frame error\n");
  78. break;
  79. //UART_PATTERN_DET
  80. case UART_PATTERN_DET:
  81. ESP_LOGI(TAG, "uart pattern detected\n");
  82. break;
  83. //Others
  84. default:
  85. ESP_LOGI(TAG, "uart event type: %d\n", event.type);
  86. break;
  87. }
  88. }
  89. }
  90. free(dtmp);
  91. dtmp = NULL;
  92. vTaskDelete(NULL);
  93. }
  94. /* Configure the UART events example */
  95. void app_main()
  96. {
  97. uart_config_t uart_config = {
  98. .baud_rate = 115200,
  99. .data_bits = UART_DATA_8_BITS,
  100. .parity = UART_PARITY_DISABLE,
  101. .stop_bits = UART_STOP_BITS_1,
  102. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  103. .rx_flow_ctrl_thresh = 122,
  104. };
  105. //Set UART parameters
  106. uart_param_config(EX_UART_NUM, &uart_config);
  107. //Set UART log level
  108. esp_log_level_set(TAG, ESP_LOG_INFO);
  109. //Install UART driver, and get the queue.
  110. uart_driver_install(EX_UART_NUM, BUF_SIZE * 2, BUF_SIZE * 2, 10, &uart0_queue, 0);
  111. //Set UART pins (using UART0 default pins ie no changes.)
  112. uart_set_pin(EX_UART_NUM, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
  113. //Set uart pattern detect function.
  114. uart_enable_pattern_det_intr(EX_UART_NUM, '+', 3, 10000, 10, 10);
  115. //Create a task to handler UART event from ISR
  116. xTaskCreate(uart_event_task, "uart_event_task", 2048, NULL, 12, NULL);
  117. //process data
  118. uint8_t* data = (uint8_t*) malloc(BUF_SIZE);
  119. do {
  120. int len = uart_read_bytes(EX_UART_NUM, data, BUF_SIZE, 100 / portTICK_RATE_MS);
  121. if(len > 0) {
  122. ESP_LOGI(TAG, "uart read : %d", len);
  123. uart_write_bytes(EX_UART_NUM, (const char*)data, len);
  124. }
  125. } while(1);
  126. }