uart_test.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_example";
  19. /**
  20. * Test code brief
  21. * This example shows how to configure uart settings and install uart driver.
  22. *
  23. * uart_evt_test() is an example that read and write data on UART0, and handler some of the special events.
  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. * uart_echo_test() is an example that read and write data on UART1, with hardware flow control turning on.
  32. * - port: UART1
  33. * - rx buffer: on
  34. * - tx buffer: off
  35. * - flow control: on
  36. * - event queue: off
  37. * - pin assignment: txd(io4), rxd(io5), rts(18), cts(19)
  38. */
  39. #define BUF_SIZE (1024)
  40. #define ECHO_TEST_TXD (4)
  41. #define ECHO_TEST_RXD (5)
  42. #define ECHO_TEST_RTS (18)
  43. #define ECHO_TEST_CTS (19)
  44. QueueHandle_t uart0_queue;
  45. void uart_task(void *pvParameters)
  46. {
  47. int uart_num = (int) pvParameters;
  48. uart_event_t event;
  49. size_t buffered_size;
  50. uint8_t* dtmp = (uint8_t*) malloc(BUF_SIZE);
  51. for(;;) {
  52. //Waiting for UART event.
  53. if(xQueueReceive(uart0_queue, (void * )&event, (portTickType)portMAX_DELAY)) {
  54. ESP_LOGI(TAG, "uart[%d] event:", uart_num);
  55. switch(event.type) {
  56. //Event of UART receving data
  57. /*We'd better handler data event fast, there would be much more data events than
  58. other types of events. If we take too much time on data event, the queue might
  59. be full.
  60. in this example, we don't process data in event, but read data outside.*/
  61. case UART_DATA:
  62. uart_get_buffered_data_len(uart_num, &buffered_size);
  63. ESP_LOGI(TAG, "data, len: %d; buffered len: %d", event.size, buffered_size);
  64. break;
  65. //Event of HW FIFO overflow detected
  66. case UART_FIFO_OVF:
  67. ESP_LOGI(TAG, "hw fifo overflow\n");
  68. //If fifo overflow happened, you should consider adding flow control for your application.
  69. //We can read data out out the buffer, or directly flush the rx buffer.
  70. uart_flush(uart_num);
  71. break;
  72. //Event of UART ring buffer full
  73. case UART_BUFFER_FULL:
  74. ESP_LOGI(TAG, "ring buffer full\n");
  75. //If buffer full happened, you should consider encreasing your buffer size
  76. //We can read data out out the buffer, or directly flush the rx buffer.
  77. uart_flush(uart_num);
  78. break;
  79. //Event of UART RX break detected
  80. case UART_BREAK:
  81. ESP_LOGI(TAG, "uart rx break\n");
  82. break;
  83. //Event of UART parity check error
  84. case UART_PARITY_ERR:
  85. ESP_LOGI(TAG, "uart parity error\n");
  86. break;
  87. //Event of UART frame error
  88. case UART_FRAME_ERR:
  89. ESP_LOGI(TAG, "uart frame error\n");
  90. break;
  91. //UART_PATTERN_DET
  92. case UART_PATTERN_DET:
  93. ESP_LOGI(TAG, "uart pattern detected\n");
  94. break;
  95. //Others
  96. default:
  97. ESP_LOGI(TAG, "uart event type: %d\n", event.type);
  98. break;
  99. }
  100. }
  101. }
  102. free(dtmp);
  103. dtmp = NULL;
  104. vTaskDelete(NULL);
  105. }
  106. void uart_evt_test()
  107. {
  108. int uart_num = UART_NUM_0;
  109. uart_config_t uart_config = {
  110. .baud_rate = 115200,
  111. .data_bits = UART_DATA_8_BITS,
  112. .parity = UART_PARITY_DISABLE,
  113. .stop_bits = UART_STOP_BITS_1,
  114. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  115. .rx_flow_ctrl_thresh = 122,
  116. };
  117. //Set UART parameters
  118. uart_param_config(uart_num, &uart_config);
  119. //Set UART log level
  120. esp_log_level_set(TAG, ESP_LOG_INFO);
  121. //Install UART driver, and get the queue.
  122. uart_driver_install(uart_num, BUF_SIZE * 2, BUF_SIZE * 2, 10, &uart0_queue, 0);
  123. //Set UART pins,(-1: default pin, no change.)
  124. //For UART0, we can just use the default pins.
  125. //uart_set_pin(uart_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
  126. //Set uart pattern detect function.
  127. uart_enable_pattern_det_intr(uart_num, '+', 3, 10000, 10, 10);
  128. //Create a task to handler UART event from ISR
  129. xTaskCreate(uart_task, "uart_task", 2048, (void*)uart_num, 12, NULL);
  130. //process data
  131. uint8_t* data = (uint8_t*) malloc(BUF_SIZE);
  132. do {
  133. int len = uart_read_bytes(uart_num, data, BUF_SIZE, 100 / portTICK_RATE_MS);
  134. if(len > 0) {
  135. ESP_LOGI(TAG, "uart read : %d", len);
  136. uart_write_bytes(uart_num, (const char*)data, len);
  137. }
  138. } while(1);
  139. }
  140. //an example of echo test with hardware flow control on UART1
  141. void uart_echo_test()
  142. {
  143. int uart_num = UART_NUM_1;
  144. uart_config_t uart_config = {
  145. .baud_rate = 115200,
  146. .data_bits = UART_DATA_8_BITS,
  147. .parity = UART_PARITY_DISABLE,
  148. .stop_bits = UART_STOP_BITS_1,
  149. .flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS,
  150. .rx_flow_ctrl_thresh = 122,
  151. };
  152. //Configure UART1 parameters
  153. uart_param_config(uart_num, &uart_config);
  154. //Set UART1 pins(TX: IO4, RX: I05, RTS: IO18, CTS: IO19)
  155. uart_set_pin(uart_num, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS);
  156. //Install UART driver( We don't need an event queue here)
  157. //In this example we don't even use a buffer for sending data.
  158. uart_driver_install(uart_num, BUF_SIZE * 2, 0, 0, NULL, 0);
  159. uint8_t* data = (uint8_t*) malloc(BUF_SIZE);
  160. while(1) {
  161. //Read data from UART
  162. int len = uart_read_bytes(uart_num, data, BUF_SIZE, 20 / portTICK_RATE_MS);
  163. //Write data back to UART
  164. uart_write_bytes(uart_num, (const char*) data, len);
  165. }
  166. }
  167. void app_main()
  168. {
  169. //A uart read/write example without event queue;
  170. xTaskCreate(uart_echo_test, "uart_echo_test", 1024, NULL, 10, NULL);
  171. //A uart example with event queue.
  172. uart_evt_test();
  173. }