uart_events_example_main.c 4.5 KB

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