test_uart.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include <sys/param.h>
  8. #include "unity.h"
  9. #include "test_utils.h" // unity_send_signal
  10. #include "driver/uart.h" // for the uart driver access
  11. #include "esp_log.h"
  12. #include "esp_system.h" // for uint32_t esp_random()
  13. #include "esp_rom_gpio.h"
  14. #include "soc/uart_periph.h"
  15. #include "hal/uart_ll.h"
  16. #include "hal/uart_hal.h"
  17. #define UART_TAG "Uart"
  18. #define UART_NUM1 (UART_NUM_1)
  19. #define BUF_SIZE (100)
  20. #define UART1_RX_PIN (22)
  21. #define UART1_TX_PIN (23)
  22. #define UART_BAUD_11520 (11520)
  23. #define UART_BAUD_115200 (115200)
  24. #define TOLERANCE (0.02) //baud rate error tolerance 2%.
  25. #define UART1_CTS_PIN (13)
  26. // RTS for RS485 Half-Duplex Mode manages DE/~RE
  27. #define UART1_RTS_PIN (18)
  28. // Number of packets to be send during test
  29. #define PACKETS_NUMBER (10)
  30. // Wait timeout for uart driver
  31. #define PACKET_READ_TICS (1000 / portTICK_RATE_MS)
  32. #define TEST_DEFAULT_CLK UART_SCLK_APB
  33. static void uart_config(uint32_t baud_rate, uart_sclk_t source_clk)
  34. {
  35. uart_config_t uart_config = {
  36. .baud_rate = baud_rate,
  37. .source_clk = source_clk,
  38. .data_bits = UART_DATA_8_BITS,
  39. .parity = UART_PARITY_DISABLE,
  40. .stop_bits = UART_STOP_BITS_1,
  41. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  42. };
  43. uart_driver_install(UART_NUM1, BUF_SIZE * 2, BUF_SIZE * 2, 20, NULL, 0);
  44. uart_param_config(UART_NUM1, &uart_config);
  45. TEST_ESP_OK(uart_set_loop_back(UART_NUM1, true));
  46. }
  47. static volatile bool exit_flag;
  48. static void test_task(void *pvParameters)
  49. {
  50. xSemaphoreHandle *sema = (xSemaphoreHandle *) pvParameters;
  51. char* data = (char *) malloc(256);
  52. while (exit_flag == false) {
  53. uart_tx_chars(UART_NUM1, data, 256);
  54. // The uart_wait_tx_done() function does not block anything if ticks_to_wait = 0.
  55. uart_wait_tx_done(UART_NUM1, 0);
  56. }
  57. free(data);
  58. xSemaphoreGive(*sema);
  59. vTaskDelete(NULL);
  60. }
  61. static void test_task2(void *pvParameters)
  62. {
  63. while (exit_flag == false) {
  64. // This task obstruct a setting tx_done_sem semaphore in the UART interrupt.
  65. // It leads to waiting the ticks_to_wait time in uart_wait_tx_done() function.
  66. uart_disable_tx_intr(UART_NUM1);
  67. }
  68. vTaskDelete(NULL);
  69. }
  70. TEST_CASE("test uart_wait_tx_done is not blocked when ticks_to_wait=0", "[uart]")
  71. {
  72. uart_config(UART_BAUD_11520, TEST_DEFAULT_CLK);
  73. xSemaphoreHandle exit_sema = xSemaphoreCreateBinary();
  74. exit_flag = false;
  75. xTaskCreate(test_task, "tsk1", 2048, &exit_sema, 5, NULL);
  76. xTaskCreate(test_task2, "tsk2", 2048, NULL, 5, NULL);
  77. printf("Waiting for 5 sec\n");
  78. vTaskDelay(5000 / portTICK_PERIOD_MS);
  79. exit_flag = true;
  80. if (xSemaphoreTake(exit_sema, 1000 / portTICK_PERIOD_MS) == pdTRUE) {
  81. vSemaphoreDelete(exit_sema);
  82. } else {
  83. TEST_FAIL_MESSAGE("uart_wait_tx_done is blocked");
  84. }
  85. TEST_ESP_OK(uart_driver_delete(UART_NUM1));
  86. }
  87. TEST_CASE("test uart get baud-rate", "[uart]")
  88. {
  89. #if SOC_UART_SUPPORT_REF_TICK
  90. uint32_t baud_rate1 = 0;
  91. printf("init uart%d, use reftick, baud rate : %d\n", (int)UART_NUM1, (int)UART_BAUD_11520);
  92. uart_config(UART_BAUD_11520, UART_SCLK_REF_TICK);
  93. uart_get_baudrate(UART_NUM1, &baud_rate1);
  94. printf("get baud rate when use reftick: %d\n", (int)baud_rate1);
  95. TEST_ASSERT_UINT32_WITHIN(UART_BAUD_11520 * TOLERANCE, UART_BAUD_11520, baud_rate1);
  96. #endif
  97. uint32_t baud_rate2 = 0;
  98. printf("init uart%d, unuse reftick, baud rate : %d\n", (int)UART_NUM1, (int)UART_BAUD_115200);
  99. uart_config(UART_BAUD_115200, TEST_DEFAULT_CLK);
  100. uart_get_baudrate(UART_NUM1, &baud_rate2);
  101. printf("get baud rate when don't use reftick: %d\n", (int)baud_rate2);
  102. TEST_ASSERT_UINT32_WITHIN(UART_BAUD_115200 * TOLERANCE, UART_BAUD_115200, baud_rate2);
  103. uart_driver_delete(UART_NUM1);
  104. ESP_LOGI(UART_TAG, "get baud-rate test passed ....\n");
  105. }
  106. TEST_CASE("test uart tx data with break", "[uart]")
  107. {
  108. const int buf_len = 200;
  109. const int send_len = 128;
  110. const int brk_len = 10;
  111. char *psend = (char *)malloc(buf_len);
  112. TEST_ASSERT_NOT_NULL(psend);
  113. memset(psend, '0', buf_len);
  114. uart_config(UART_BAUD_115200, TEST_DEFAULT_CLK);
  115. printf("Uart%d send %d bytes with break\n", UART_NUM1, send_len);
  116. uart_write_bytes_with_break(UART_NUM1, (const char *)psend, send_len, brk_len);
  117. uart_wait_tx_done(UART_NUM1, (portTickType)portMAX_DELAY);
  118. //If the code is running here, it means the test passed, otherwise it will crash due to the interrupt wdt timeout.
  119. printf("Send data with break test passed\n");
  120. free(psend);
  121. uart_driver_delete(UART_NUM1);
  122. }
  123. static void uart_word_len_set_get_test(int uart_num)
  124. {
  125. printf("uart word len set and get test\n");
  126. uart_word_length_t word_length_set = 0;
  127. uart_word_length_t word_length_get = 0;
  128. for (int i = 0; i < UART_DATA_BITS_MAX; i++) {
  129. word_length_set = UART_DATA_5_BITS + i;
  130. TEST_ESP_OK(uart_set_word_length(uart_num, word_length_set));
  131. TEST_ESP_OK(uart_get_word_length(uart_num, &word_length_get));
  132. TEST_ASSERT_EQUAL(word_length_set, word_length_get);
  133. }
  134. }
  135. static void uart_stop_bit_set_get_test(int uart_num)
  136. {
  137. printf("uart stop bit set and get test\n");
  138. uart_stop_bits_t stop_bit_set = 0;
  139. uart_stop_bits_t stop_bit_get = 0;
  140. for (int i = UART_STOP_BITS_1; i < UART_STOP_BITS_MAX; i++) {
  141. stop_bit_set = i;
  142. TEST_ESP_OK(uart_set_stop_bits(uart_num, stop_bit_set));
  143. TEST_ESP_OK(uart_get_stop_bits(uart_num, &stop_bit_get));
  144. TEST_ASSERT_EQUAL(stop_bit_set, stop_bit_get);
  145. }
  146. }
  147. static void uart_parity_set_get_test(int uart_num)
  148. {
  149. printf("uart parity set and get test\n");
  150. uart_parity_t parity_set[3] = {
  151. UART_PARITY_DISABLE,
  152. UART_PARITY_EVEN,
  153. UART_PARITY_ODD,
  154. };
  155. uart_parity_t parity_get = 0;
  156. for (int i = 0; i < 3; i++) {
  157. TEST_ESP_OK(uart_set_parity(uart_num, parity_set[i]));
  158. TEST_ESP_OK(uart_get_parity(uart_num, &parity_get));
  159. TEST_ASSERT_EQUAL(parity_set[i], parity_get);
  160. }
  161. }
  162. static void uart_hw_flow_set_get_test(int uart_num)
  163. {
  164. printf("uart hw flow control set and get test\n");
  165. uart_hw_flowcontrol_t flowcontrol_set = 0;
  166. uart_hw_flowcontrol_t flowcontrol_get = 0;
  167. for (int i = 0; i < UART_HW_FLOWCTRL_DISABLE; i++) {
  168. TEST_ESP_OK(uart_set_hw_flow_ctrl(uart_num, flowcontrol_set, 20));
  169. TEST_ESP_OK(uart_get_hw_flow_ctrl(uart_num, &flowcontrol_get));
  170. TEST_ASSERT_EQUAL(flowcontrol_set, flowcontrol_get);
  171. }
  172. }
  173. static void uart_wakeup_set_get_test(int uart_num)
  174. {
  175. printf("uart wake up set and get test\n");
  176. int wake_up_set = 0;
  177. int wake_up_get = 0;
  178. for (int i = 3; i < 0x3ff; i++) {
  179. wake_up_set = i;
  180. TEST_ESP_OK(uart_set_wakeup_threshold(uart_num, wake_up_set));
  181. TEST_ESP_OK(uart_get_wakeup_threshold(uart_num, &wake_up_get));
  182. TEST_ASSERT_EQUAL(wake_up_set, wake_up_get);
  183. }
  184. }
  185. TEST_CASE("uart general API test", "[uart]")
  186. {
  187. const int uart_num = UART_NUM1;
  188. uart_config_t uart_config = {
  189. .baud_rate = 115200,
  190. .data_bits = UART_DATA_8_BITS,
  191. .parity = UART_PARITY_DISABLE,
  192. .stop_bits = UART_STOP_BITS_1,
  193. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  194. .source_clk = TEST_DEFAULT_CLK,
  195. };
  196. uart_param_config(uart_num, &uart_config);
  197. uart_word_len_set_get_test(uart_num);
  198. uart_stop_bit_set_get_test(uart_num);
  199. uart_parity_set_get_test(uart_num);
  200. uart_hw_flow_set_get_test(uart_num);
  201. uart_wakeup_set_get_test(uart_num);
  202. }
  203. static void uart_write_task(void *param)
  204. {
  205. int uart_num = (int)param;
  206. uint8_t *tx_buf = (uint8_t *)malloc(1024);
  207. if(tx_buf == NULL) {
  208. TEST_FAIL_MESSAGE("tx buffer malloc fail");
  209. }
  210. for(int i = 1; i < 1023; i++) {
  211. tx_buf[i] = (i & 0xff);
  212. }
  213. for(int i = 0; i < 1024; i++) {
  214. //d[0] and d[1023] are header
  215. tx_buf[0] = (i & 0xff);
  216. tx_buf[1023] = ((~i) & 0xff);
  217. uart_write_bytes(uart_num, (const char*)tx_buf, 1024);
  218. uart_wait_tx_done(uart_num, (TickType_t)portMAX_DELAY);
  219. }
  220. free(tx_buf);
  221. vTaskDelete(NULL);
  222. }
  223. /**
  224. * The following tests use loop back
  225. *
  226. * NOTE: In the following tests, because the internal loopback is enabled, the CTS signal is connected to
  227. * the RTS signal internally. However, On ESP32S3, they are not, and the CTS keeps the default level (which
  228. * is a high level). So the workaround is to map the CTS in_signal to a GPIO pin (here IO13 is used) and connect
  229. * the RTS output_signal to this IO.
  230. */
  231. TEST_CASE("uart read write test", "[uart]")
  232. {
  233. const int uart_num = UART_NUM1;
  234. uint8_t *rd_data = (uint8_t *)malloc(1024);
  235. if(rd_data == NULL) {
  236. TEST_FAIL_MESSAGE("rx buffer malloc fail");
  237. }
  238. uart_config_t uart_config = {
  239. .baud_rate = 2000000,
  240. .data_bits = UART_DATA_8_BITS,
  241. .parity = UART_PARITY_DISABLE,
  242. .stop_bits = UART_STOP_BITS_1,
  243. .flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS,
  244. .source_clk = TEST_DEFAULT_CLK,
  245. .rx_flow_ctrl_thresh = 120
  246. };
  247. TEST_ESP_OK(uart_driver_install(uart_num, BUF_SIZE * 2, 0, 20, NULL, 0));
  248. TEST_ESP_OK(uart_param_config(uart_num, &uart_config));
  249. TEST_ESP_OK(uart_set_loop_back(uart_num, true));
  250. TEST_ESP_OK(uart_set_pin(uart_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART1_CTS_PIN));
  251. //Connect the RTS out_signal to the CTS pin (which is mapped to CTS in_signal)
  252. esp_rom_gpio_connect_out_signal(UART1_CTS_PIN, UART_PERIPH_SIGNAL(uart_num, SOC_UART_RTS_PIN_IDX), 0, 0);
  253. TEST_ESP_OK(uart_wait_tx_done(uart_num, portMAX_DELAY));
  254. vTaskDelay(1 / portTICK_PERIOD_MS); // make sure last byte has flushed from TX FIFO
  255. TEST_ESP_OK(uart_flush_input(uart_num));
  256. xTaskCreate(uart_write_task, "uart_write_task", 2048 * 4, (void *)uart_num, UNITY_FREERTOS_PRIORITY - 1, NULL);
  257. for (int i = 0; i < 1024; i++) {
  258. int bytes_remaining = 1024;
  259. memset(rd_data, 0, 1024);
  260. while (bytes_remaining) {
  261. int bytes_received = uart_read_bytes(uart_num, rd_data + 1024 - bytes_remaining, bytes_remaining, (TickType_t)1000);
  262. if (bytes_received < 0) {
  263. TEST_FAIL_MESSAGE("read timeout, uart read write test fail");
  264. }
  265. bytes_remaining -= bytes_received;
  266. }
  267. int check_fail_cnt = 0;
  268. if (rd_data[0] != (i & 0xff)) {
  269. printf("packet %d index check error at offset 0, expected 0x%02x\n", i, i);
  270. ++check_fail_cnt;
  271. }
  272. if (rd_data[1023] != ((~i) & 0xff)) {
  273. printf("packet %d index check error at offset 1023, expected 0x%02x\n", i, ((~i) & 0xff));
  274. ++check_fail_cnt;
  275. }
  276. for (int j = 1; j < 1023; j++) {
  277. if (rd_data[j] != (j & 0xff)) {
  278. printf("data mismatch in packet %d offset %d, expected 0x%02x got 0x%02x\n", i, j, (j & 0xff), rd_data[j]);
  279. ++check_fail_cnt;
  280. }
  281. if (check_fail_cnt > 10) {
  282. printf("(further checks skipped)\n");
  283. break;
  284. }
  285. }
  286. if (check_fail_cnt > 0) {
  287. ESP_LOG_BUFFER_HEX("rd_data", rd_data, 1024);
  288. TEST_FAIL();
  289. }
  290. }
  291. uart_wait_tx_done(uart_num, (TickType_t)portMAX_DELAY);
  292. uart_driver_delete(uart_num);
  293. free(rd_data);
  294. }
  295. TEST_CASE("uart tx with ringbuffer test", "[uart]")
  296. {
  297. const int uart_num = UART_NUM1;
  298. uint8_t *rd_data = (uint8_t *)malloc(1024);
  299. uint8_t *wr_data = (uint8_t *)malloc(1024);
  300. if(rd_data == NULL || wr_data == NULL) {
  301. TEST_FAIL_MESSAGE("buffer malloc fail");
  302. }
  303. uart_config_t uart_config = {
  304. .baud_rate = 2000000,
  305. .data_bits = UART_DATA_8_BITS,
  306. .parity = UART_PARITY_DISABLE,
  307. .stop_bits = UART_STOP_BITS_1,
  308. .flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS,
  309. .rx_flow_ctrl_thresh = 120,
  310. .source_clk = TEST_DEFAULT_CLK,
  311. };
  312. uart_wait_tx_idle_polling(uart_num);
  313. TEST_ESP_OK(uart_param_config(uart_num, &uart_config));
  314. TEST_ESP_OK(uart_driver_install(uart_num, 1024 * 2, 1024 *2, 20, NULL, 0));
  315. TEST_ESP_OK(uart_set_loop_back(uart_num, true));
  316. TEST_ESP_OK(uart_set_pin(uart_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART1_CTS_PIN));
  317. //Connect the RTS out_signal to the CTS pin (which is mapped to CTS in_signal)
  318. esp_rom_gpio_connect_out_signal(UART1_CTS_PIN, UART_PERIPH_SIGNAL(uart_num, SOC_UART_RTS_PIN_IDX), 0, 0);
  319. for (int i = 0; i < 1024; i++) {
  320. wr_data[i] = i;
  321. rd_data[i] = 0;
  322. }
  323. uart_write_bytes(uart_num, (const char*)wr_data, 1024);
  324. uart_wait_tx_done(uart_num, (TickType_t)portMAX_DELAY);
  325. uart_read_bytes(uart_num, rd_data, 1024, (TickType_t)1000);
  326. TEST_ASSERT_EQUAL_HEX8_ARRAY(wr_data, rd_data, 1024);
  327. TEST_ESP_OK(uart_driver_delete(uart_num));
  328. free(rd_data);
  329. free(wr_data);
  330. }
  331. /* Global variable shared between the ISR and the test function */
  332. volatile uint32_t uart_isr_happened = 0;
  333. static void uart_custom_isr(void* arg) {
  334. (void) arg;
  335. /* Clear interrupt status and disable TX interrupt here in order to
  336. * prevent an infinite call loop. Use the LL function to prevent
  337. * entering a critical section from an interrupt. */
  338. uart_ll_disable_intr_mask(UART_LL_GET_HW(1), UART_INTR_TXFIFO_EMPTY);
  339. uart_clear_intr_status(UART_NUM_1, UART_INTR_TXFIFO_EMPTY);
  340. /* Mark the interrupt as serviced */
  341. uart_isr_happened = 1;
  342. }
  343. /**
  344. * This function shall always be executed by core 0.
  345. * This is required by `uart_isr_free`.
  346. */
  347. static void uart_test_custom_isr_core0(void* param) {
  348. /**
  349. * Setup the UART1 and make sure we can register and free a custom ISR
  350. */
  351. uart_config_t uart_config = {
  352. .baud_rate = 115200,
  353. .data_bits = UART_DATA_8_BITS,
  354. .parity = UART_PARITY_DISABLE,
  355. .stop_bits = UART_STOP_BITS_1,
  356. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  357. .source_clk = UART_SCLK_APB,
  358. };
  359. const uart_port_t uart_echo = UART_NUM_1;
  360. const int uart_tx = 4;
  361. const int uart_rx = 5;
  362. const int buf_size = 256;
  363. const int intr_alloc_flags = 0;
  364. const char msg[] = "hello world\n";
  365. uart_isr_handle_t handle = NULL;
  366. TEST_ESP_OK(uart_driver_install(uart_echo, buf_size * 2, 0, 0, NULL, intr_alloc_flags));
  367. TEST_ESP_OK(uart_param_config(uart_echo, &uart_config));
  368. TEST_ESP_OK(uart_set_pin(uart_echo, uart_tx, uart_rx, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
  369. /* Prevent the custom ISR handler from being called if UART_INTR_BRK_DET interrupt occurs.
  370. * It shall only be called for TX interrupts. */
  371. uart_disable_intr_mask(uart_echo, UART_INTR_BRK_DET);
  372. /* Unregister the default ISR setup by the function call above */
  373. TEST_ESP_OK(uart_isr_free(uart_echo));
  374. TEST_ESP_OK(uart_isr_register(uart_echo, uart_custom_isr, NULL, intr_alloc_flags, &handle));
  375. /* Set the TX FIFO empty threshold to the size of the message we are sending,
  376. * make sure it is never 0 in any case */
  377. TEST_ESP_OK(uart_enable_tx_intr(uart_echo, true, MAX(sizeof(msg), 1)));
  378. uart_write_bytes(uart_echo, msg, sizeof(msg));
  379. /* 10ms will be enough to receive the interrupt */
  380. vTaskDelay(10 / portTICK_PERIOD_MS);
  381. /* Make sure the ISR occured */
  382. TEST_ASSERT_EQUAL(uart_isr_happened, 1);
  383. esp_rom_printf("ISR happened: %d\n", uart_isr_happened);
  384. TEST_ESP_OK(uart_isr_free(uart_echo));
  385. TEST_ESP_OK(uart_driver_delete(uart_echo));
  386. #if !CONFIG_FREERTOS_UNICORE
  387. TaskHandle_t* parent_task = (TaskHandle_t*) param;
  388. esp_rom_printf("Notifying caller\n");
  389. TEST_ASSERT(xTaskNotify(*parent_task, 0, eNoAction));
  390. vTaskDelete(NULL);
  391. #else
  392. (void) param;
  393. #endif //!CONFIG_FREERTOS_UNICORE
  394. }
  395. TEST_CASE("uart can register and free custom ISRs", "[uart]")
  396. {
  397. #if !CONFIG_FREERTOS_UNICORE
  398. TaskHandle_t task_handle;
  399. TaskHandle_t current_handler = xTaskGetCurrentTaskHandle();
  400. /* Run the test on a determianted core, do not allow the core to be changed
  401. * as we will manipulate ISRs. */
  402. BaseType_t ret = xTaskCreatePinnedToCore(uart_test_custom_isr_core0,
  403. "uart_test_custom_isr_core0_task",
  404. 2048,
  405. &current_handler,
  406. 5,
  407. &task_handle,
  408. 0);
  409. TEST_ASSERT(ret);
  410. TEST_ASSERT(xTaskNotifyWait(0, 0, NULL, 1000 / portTICK_PERIOD_MS));
  411. (void) task_handle;
  412. #else
  413. uart_test_custom_isr_core0(NULL);
  414. #endif //!CONFIG_FREERTOS_UNICORE
  415. }
  416. TEST_CASE("uart int state restored after flush", "[uart]")
  417. {
  418. /**
  419. * The first goal of this test is to make sure that when our RX FIFO is full,
  420. * we can continue receiving back data after flushing
  421. * For more details, check IDF-4374
  422. */
  423. uart_config_t uart_config = {
  424. .baud_rate = 115200,
  425. .data_bits = UART_DATA_8_BITS,
  426. .parity = UART_PARITY_DISABLE,
  427. .stop_bits = UART_STOP_BITS_1,
  428. .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  429. .source_clk = UART_SCLK_APB,
  430. };
  431. const uart_port_t uart_echo = UART_NUM_1;
  432. const int uart_tx_signal = U1TXD_OUT_IDX;
  433. const int uart_tx = 4;
  434. const int uart_rx = 5;
  435. const int buf_size = 256;
  436. const int intr_alloc_flags = 0;
  437. TEST_ESP_OK(uart_driver_install(uart_echo, buf_size * 2, 0, 0, NULL, intr_alloc_flags));
  438. TEST_ESP_OK(uart_param_config(uart_echo, &uart_config));
  439. TEST_ESP_OK(uart_set_pin(uart_echo, uart_tx, uart_rx, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
  440. /* Make sure UART2's RX signal is connected to TX pin
  441. * This creates a loop that lets us receive anything we send on the UART */
  442. esp_rom_gpio_connect_out_signal(uart_rx, uart_tx_signal, false, false);
  443. uint8_t *data = (uint8_t *) malloc(buf_size);
  444. TEST_ASSERT_NOT_NULL(data);
  445. uart_write_bytes(uart_echo, (const char *) data, buf_size);
  446. /* As we set up a loopback, we can read them back on RX */
  447. int len = uart_read_bytes(uart_echo, data, buf_size, 1000 / portTICK_RATE_MS);
  448. TEST_ASSERT_EQUAL(len, buf_size);
  449. /* Fill the RX buffer, this should disable the RX interrupts */
  450. int written = uart_write_bytes(uart_echo, (const char *) data, buf_size);
  451. TEST_ASSERT_NOT_EQUAL(-1, written);
  452. written = uart_write_bytes(uart_echo, (const char *) data, buf_size);
  453. TEST_ASSERT_NOT_EQUAL(-1, written);
  454. written = uart_write_bytes(uart_echo, (const char *) data, buf_size);
  455. TEST_ASSERT_NOT_EQUAL(-1, written);
  456. /* Flush the input buffer, RX interrupts should be re-enabled */
  457. uart_flush_input(uart_echo);
  458. written = uart_write_bytes(uart_echo, (const char *) data, buf_size);
  459. TEST_ASSERT_NOT_EQUAL(-1, written);
  460. len = uart_read_bytes(uart_echo, data, buf_size, 1000 / portTICK_RATE_MS);
  461. /* len equals buf_size bytes if interrupts were indeed re-enabled */
  462. TEST_ASSERT_EQUAL(len, buf_size);
  463. /**
  464. * Second test, make sure that if we explicitly disable the RX interrupts,
  465. * they are NOT re-enabled after flushing
  466. * To do so, start by cleaning the RX FIFO, disable the RX interrupts,
  467. * flush again, send data to the UART and check that we haven't received
  468. * any of the bytes */
  469. uart_flush_input(uart_echo);
  470. uart_disable_rx_intr(uart_echo);
  471. uart_flush_input(uart_echo);
  472. written = uart_write_bytes(uart_echo, (const char *) data, buf_size);
  473. TEST_ASSERT_NOT_EQUAL(-1, written);
  474. len = uart_read_bytes(uart_echo, data, buf_size, 250 / portTICK_RATE_MS);
  475. TEST_ASSERT_EQUAL(len, 0);
  476. TEST_ESP_OK(uart_driver_delete(uart_echo));
  477. free(data);
  478. }