test_uart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #include <string.h>
  2. #include <sys/param.h>
  3. #include "unity.h"
  4. #include "test_utils.h" // unity_send_signal
  5. #include "driver/uart.h" // for the uart driver access
  6. #include "esp_log.h"
  7. #include "esp_system.h" // for uint32_t esp_random()
  8. #define UART_TAG "Uart"
  9. #define UART_NUM1 (UART_NUM_1)
  10. #define BUF_SIZE (100)
  11. #define UART1_RX_PIN (22)
  12. #define UART1_TX_PIN (23)
  13. #define UART_BAUD_11520 (11520)
  14. #define UART_BAUD_115200 (115200)
  15. #define TOLERANCE (0.02) //baud rate error tolerance 2%.
  16. // RTS for RS485 Half-Duplex Mode manages DE/~RE
  17. #define UART1_RTS_PIN (18)
  18. // Number of packets to be send during test
  19. #define PACKETS_NUMBER (10)
  20. // Wait timeout for uart driver
  21. #define PACKET_READ_TICS (1000 / portTICK_RATE_MS)
  22. static void uart_config(uint32_t baud_rate, bool use_ref_tick)
  23. {
  24. uart_config_t uart_config = {
  25. .baud_rate = baud_rate,
  26. .data_bits = UART_DATA_8_BITS,
  27. .parity = UART_PARITY_DISABLE,
  28. .stop_bits = UART_STOP_BITS_1,
  29. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  30. };
  31. uart_config.source_clk = use_ref_tick ? UART_SCLK_REF_TICK : UART_SCLK_APB;
  32. uart_driver_install(UART_NUM1, BUF_SIZE * 2, BUF_SIZE * 2, 20, NULL, 0);
  33. uart_param_config(UART_NUM1, &uart_config);
  34. TEST_ESP_OK(uart_set_loop_back(UART_NUM1, true));
  35. }
  36. static volatile bool exit_flag;
  37. static void test_task(void *pvParameters)
  38. {
  39. xSemaphoreHandle *sema = (xSemaphoreHandle *) pvParameters;
  40. char* data = (char *) malloc(256);
  41. while (exit_flag == false) {
  42. uart_tx_chars(UART_NUM1, data, 256);
  43. // The uart_wait_tx_done() function does not block anything if ticks_to_wait = 0.
  44. uart_wait_tx_done(UART_NUM1, 0);
  45. }
  46. free(data);
  47. xSemaphoreGive(*sema);
  48. vTaskDelete(NULL);
  49. }
  50. static void test_task2(void *pvParameters)
  51. {
  52. while (exit_flag == false) {
  53. // This task obstruct a setting tx_done_sem semaphore in the UART interrupt.
  54. // It leads to waiting the ticks_to_wait time in uart_wait_tx_done() function.
  55. uart_disable_tx_intr(UART_NUM1);
  56. }
  57. vTaskDelete(NULL);
  58. }
  59. TEST_CASE("test uart_wait_tx_done is not blocked when ticks_to_wait=0", "[uart]")
  60. {
  61. uart_config(UART_BAUD_11520, false);
  62. xSemaphoreHandle exit_sema = xSemaphoreCreateBinary();
  63. exit_flag = false;
  64. xTaskCreate(test_task, "tsk1", 2048, &exit_sema, 5, NULL);
  65. xTaskCreate(test_task2, "tsk2", 2048, NULL, 5, NULL);
  66. printf("Waiting for 5 sec\n");
  67. vTaskDelay(5000 / portTICK_PERIOD_MS);
  68. exit_flag = true;
  69. if (xSemaphoreTake(exit_sema, 1000 / portTICK_PERIOD_MS) == pdTRUE) {
  70. vSemaphoreDelete(exit_sema);
  71. } else {
  72. TEST_FAIL_MESSAGE("uart_wait_tx_done is blocked");
  73. }
  74. TEST_ESP_OK(uart_driver_delete(UART_NUM1));
  75. }
  76. TEST_CASE("test uart get baud-rate", "[uart]")
  77. {
  78. uint32_t baud_rate1 = 0;
  79. uint32_t baud_rate2 = 0;
  80. printf("init uart%d, use reftick, baud rate : %d\n", (int)UART_NUM1, (int)UART_BAUD_11520);
  81. uart_config(UART_BAUD_11520, true);
  82. uart_get_baudrate(UART_NUM1, &baud_rate1);
  83. printf("init uart%d, unuse reftick, baud rate : %d\n", (int)UART_NUM1, (int)UART_BAUD_115200);
  84. uart_config(UART_BAUD_115200, false);
  85. uart_get_baudrate(UART_NUM1, &baud_rate2);
  86. printf("get baud rate when use reftick: %d\n", (int)baud_rate1);
  87. printf("get baud rate when don't use reftick: %d\n", (int)baud_rate2);
  88. uart_driver_delete(UART_NUM1);
  89. TEST_ASSERT_UINT32_WITHIN(UART_BAUD_11520 * TOLERANCE, UART_BAUD_11520, baud_rate1);
  90. TEST_ASSERT_UINT32_WITHIN(UART_BAUD_115200 * TOLERANCE, UART_BAUD_115200, baud_rate2);
  91. ESP_LOGI(UART_TAG, "get baud-rate test passed ....\n");
  92. }
  93. TEST_CASE("test uart tx data with break", "[uart]")
  94. {
  95. const int buf_len = 200;
  96. const int send_len = 128;
  97. const int brk_len = 10;
  98. char *psend = (char *)malloc(buf_len);
  99. TEST_ASSERT_NOT_NULL(psend);
  100. memset(psend, '0', buf_len);
  101. uart_config(UART_BAUD_115200, false);
  102. printf("Uart%d send %d bytes with break\n", UART_NUM1, send_len);
  103. uart_write_bytes_with_break(UART_NUM1, (const char *)psend, send_len, brk_len);
  104. uart_wait_tx_done(UART_NUM1, (portTickType)portMAX_DELAY);
  105. //If the code is running here, it means the test passed, otherwise it will crash due to the interrupt wdt timeout.
  106. printf("Send data with break test passed\n");
  107. free(psend);
  108. uart_driver_delete(UART_NUM1);
  109. }
  110. static void uart_word_len_set_get_test(int uart_num)
  111. {
  112. printf("uart word len set and get test\n");
  113. uart_word_length_t word_length_set = 0;
  114. uart_word_length_t word_length_get = 0;
  115. for (int i = 0; i < UART_DATA_BITS_MAX; i++) {
  116. word_length_set = UART_DATA_5_BITS + i;
  117. TEST_ESP_OK(uart_set_word_length(uart_num, word_length_set));
  118. TEST_ESP_OK(uart_get_word_length(uart_num, &word_length_get));
  119. TEST_ASSERT_EQUAL(word_length_set, word_length_get);
  120. }
  121. }
  122. static void uart_stop_bit_set_get_test(int uart_num)
  123. {
  124. printf("uart stop bit set and get test\n");
  125. uart_stop_bits_t stop_bit_set = 0;
  126. uart_stop_bits_t stop_bit_get = 0;
  127. for (int i = UART_STOP_BITS_1; i < UART_STOP_BITS_MAX; i++) {
  128. stop_bit_set = i;
  129. TEST_ESP_OK(uart_set_stop_bits(uart_num, stop_bit_set));
  130. TEST_ESP_OK(uart_get_stop_bits(uart_num, &stop_bit_get));
  131. TEST_ASSERT_EQUAL(stop_bit_set, stop_bit_get);
  132. }
  133. }
  134. static void uart_parity_set_get_test(int uart_num)
  135. {
  136. printf("uart parity set and get test\n");
  137. uart_parity_t parity_set[3] = {
  138. UART_PARITY_DISABLE,
  139. UART_PARITY_EVEN,
  140. UART_PARITY_ODD,
  141. };
  142. uart_parity_t parity_get = 0;
  143. for (int i = 0; i < 3; i++) {
  144. TEST_ESP_OK(uart_set_parity(uart_num, parity_set[i]));
  145. TEST_ESP_OK(uart_get_parity(uart_num, &parity_get));
  146. TEST_ASSERT_EQUAL(parity_set[i], parity_get);
  147. }
  148. }
  149. static void uart_hw_flow_set_get_test(int uart_num)
  150. {
  151. printf("uart hw flow control set and get test\n");
  152. uart_hw_flowcontrol_t flowcontrol_set = 0;
  153. uart_hw_flowcontrol_t flowcontrol_get = 0;
  154. for (int i = 0; i < UART_HW_FLOWCTRL_DISABLE; i++) {
  155. TEST_ESP_OK(uart_set_hw_flow_ctrl(uart_num, flowcontrol_set, 20));
  156. TEST_ESP_OK(uart_get_hw_flow_ctrl(uart_num, &flowcontrol_get));
  157. TEST_ASSERT_EQUAL(flowcontrol_set, flowcontrol_get);
  158. }
  159. }
  160. static void uart_wakeup_set_get_test(int uart_num)
  161. {
  162. printf("uart wake up set and get test\n");
  163. int wake_up_set = 0;
  164. int wake_up_get = 0;
  165. for (int i = 3; i < 0x3ff; i++) {
  166. wake_up_set = i;
  167. TEST_ESP_OK(uart_set_wakeup_threshold(uart_num, wake_up_set));
  168. TEST_ESP_OK(uart_get_wakeup_threshold(uart_num, &wake_up_get));
  169. TEST_ASSERT_EQUAL(wake_up_set, wake_up_get);
  170. }
  171. }
  172. TEST_CASE("uart general API test", "[uart]")
  173. {
  174. const int uart_num = UART_NUM1;
  175. uart_config_t uart_config = {
  176. .baud_rate = 115200,
  177. .data_bits = UART_DATA_8_BITS,
  178. .parity = UART_PARITY_DISABLE,
  179. .stop_bits = UART_STOP_BITS_1,
  180. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  181. .source_clk = UART_SCLK_APB,
  182. };
  183. uart_param_config(uart_num, &uart_config);
  184. uart_word_len_set_get_test(uart_num);
  185. uart_stop_bit_set_get_test(uart_num);
  186. uart_parity_set_get_test(uart_num);
  187. uart_hw_flow_set_get_test(uart_num);
  188. uart_wakeup_set_get_test(uart_num);
  189. }
  190. static void uart_write_task(void *param)
  191. {
  192. int uart_num = (int)param;
  193. uint8_t *tx_buf = (uint8_t *)malloc(1024);
  194. if(tx_buf == NULL) {
  195. TEST_FAIL_MESSAGE("tx buffer malloc fail");
  196. }
  197. for(int i = 1; i < 1023; i++) {
  198. tx_buf[i] = (i & 0xff);
  199. }
  200. for(int i = 0; i < 1024; i++) {
  201. //d[0] and d[1023] are header
  202. tx_buf[0] = (i & 0xff);
  203. tx_buf[1023] = ((~i) & 0xff);
  204. uart_write_bytes(uart_num, (const char*)tx_buf, 1024);
  205. uart_wait_tx_done(uart_num, (TickType_t)portMAX_DELAY);
  206. }
  207. free(tx_buf);
  208. vTaskDelete(NULL);
  209. }
  210. TEST_CASE("uart read write test", "[uart]")
  211. {
  212. const int uart_num = UART_NUM1;
  213. uint8_t *rd_data = (uint8_t *)malloc(1024);
  214. if(rd_data == NULL) {
  215. TEST_FAIL_MESSAGE("rx buffer malloc fail");
  216. }
  217. uart_config_t uart_config = {
  218. .baud_rate = 2000000,
  219. .data_bits = UART_DATA_8_BITS,
  220. .parity = UART_PARITY_DISABLE,
  221. .stop_bits = UART_STOP_BITS_1,
  222. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  223. .source_clk = UART_SCLK_APB,
  224. };
  225. TEST_ESP_OK(uart_driver_install(uart_num, BUF_SIZE * 2, 0, 20, NULL, 0));
  226. TEST_ESP_OK(uart_param_config(uart_num, &uart_config));
  227. TEST_ESP_OK(uart_set_loop_back(uart_num, true));
  228. TEST_ESP_OK(uart_wait_tx_done(uart_num, portMAX_DELAY));
  229. vTaskDelay(1 / portTICK_PERIOD_MS); // make sure last byte has flushed from TX FIFO
  230. TEST_ESP_OK(uart_flush_input(uart_num));
  231. xTaskCreate(uart_write_task, "uart_write_task", 2048 * 4, (void *)uart_num, UNITY_FREERTOS_PRIORITY - 1, NULL);
  232. int len_tmp = 0;
  233. int rd_len = 1024;
  234. for (int i = 0; i < 1024; i++) {
  235. rd_len = 1024;
  236. memset(rd_data, 0, 1024);
  237. while (rd_len) {
  238. len_tmp = uart_read_bytes(uart_num, rd_data + 1024 - rd_len, rd_len, (TickType_t)1000);
  239. if (len_tmp < 0) {
  240. TEST_FAIL_MESSAGE("read timeout, uart read write test fail");
  241. }
  242. rd_len -= len_tmp;
  243. }
  244. TEST_ASSERT_EQUAL_HEX8_MESSAGE((i & 0xff), rd_data[0], "uart data header check error index 0");
  245. TEST_ASSERT_EQUAL_HEX8_MESSAGE((~i) & 0xff, rd_data[1023], "uart data header check error index 1023");
  246. for (int j = 1; j < 1023; j++) {
  247. TEST_ASSERT_EQUAL_HEX8_MESSAGE(j & 0xff, rd_data[j], "uart data check error");
  248. }
  249. }
  250. uart_wait_tx_done(uart_num, (TickType_t)portMAX_DELAY);
  251. uart_driver_delete(uart_num);
  252. free(rd_data);
  253. }
  254. TEST_CASE("uart tx with ringbuffer test", "[uart]")
  255. {
  256. const int uart_num = UART_NUM1;
  257. uint8_t *rd_data = (uint8_t *)malloc(1024);
  258. uint8_t *wr_data = (uint8_t *)malloc(1024);
  259. if(rd_data == NULL || wr_data == NULL) {
  260. TEST_FAIL_MESSAGE("buffer malloc fail");
  261. }
  262. uart_config_t uart_config = {
  263. .baud_rate = 2000000,
  264. .data_bits = UART_DATA_8_BITS,
  265. .parity = UART_PARITY_DISABLE,
  266. .stop_bits = UART_STOP_BITS_1,
  267. .flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS,
  268. .rx_flow_ctrl_thresh = 120,
  269. .source_clk = UART_SCLK_APB,
  270. };
  271. TEST_ESP_OK(uart_param_config(uart_num, &uart_config));
  272. TEST_ESP_OK(uart_driver_install(uart_num, 1024 * 2, 1024 *2, 20, NULL, 0));
  273. TEST_ESP_OK(uart_set_loop_back(uart_num, true));
  274. for (int i = 0; i < 1024; i++) {
  275. wr_data[i] = i;
  276. rd_data[i] = 0;
  277. }
  278. uart_write_bytes(uart_num, (const char*)wr_data, 1024);
  279. uart_wait_tx_done(uart_num, (TickType_t)portMAX_DELAY);
  280. uart_read_bytes(uart_num, rd_data, 1024, (TickType_t)1000);
  281. TEST_ASSERT_EQUAL_HEX8_ARRAY(wr_data, rd_data, 1024);
  282. TEST_ESP_OK(uart_driver_delete(uart_num));
  283. free(rd_data);
  284. free(wr_data);
  285. }