test_uart.c 13 KB

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