test_uart.c 12 KB

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