uart_rxnb_txb.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright (c) 2006-2025 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-06-16 KyleChan the first version
  9. * 2025-11-13 CYFS Add standardized utest documentation block
  10. */
  11. /**
  12. * Test Case Name: UART Non-Blocking RX & Blocking TX Integration Test
  13. *
  14. * Test Objectives:
  15. * - Validate RX interrupt/callback based reception paired with blocking transmit path
  16. * - Verify APIs: rt_device_find, rt_device_control(RT_DEVICE_CTRL_CONFIG / RT_SERIAL_CTRL_SET_RX_TIMEOUT),
  17. * rt_device_open with RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING,
  18. * rt_device_set_rx_indicate, rt_device_read, rt_device_write, rt_sem APIs
  19. *
  20. * Test Scenarios:
  21. * - **Scenario 1 (Callback-Driven Reception / tc_uart_api):**
  22. * 1. Configure UART buffers (optional DMA ping buffer) and create RX semaphore.
  23. * 2. Register `uart_rx_indicate` callback to signal semaphore whenever new bytes arrive.
  24. * 3. For a sweep of deterministic and random lengths, launch sender/receiver threads.
  25. * 4. Receiver waits on semaphore, drains available bytes, and enforces monotonic data ordering until quota met.
  26. *
  27. * Verification Metrics:
  28. * - Received data remains sequential; `uart_result` stays `RT_TRUE`.
  29. * - Semaphore take operations succeed; `uart_over_flag` flips upon completion.
  30. * - UART open/close and callback registration succeed without leaks; resources cleaned in teardown.
  31. *
  32. * Dependencies:
  33. * - Requires `RT_UTEST_SERIAL_V2` with loopback for `RT_SERIAL_TC_DEVICE_NAME`.
  34. * - Non-blocking RX must support callback indication.
  35. * - Heap must provide buffers and semaphore; two 1 KB thread stacks required.
  36. *
  37. * Expected Results:
  38. * - Test executes without assertion failures; logs show iteration counts and lengths.
  39. * - Utest harness prints `[ PASSED ] [ result ] testcase (components.drivers.serial.v2.uart_rxnb_txb)`.
  40. */
  41. #include <rtthread.h>
  42. #include "utest.h"
  43. #include <rtdevice.h>
  44. #include <stdlib.h>
  45. #ifdef RT_UTEST_SERIAL_V2
  46. static struct rt_serial_device *serial;
  47. static rt_sem_t rx_sem;
  48. static rt_uint8_t uart_over_flag;
  49. static rt_bool_t uart_result = RT_TRUE;
  50. static rt_err_t uart_find(void)
  51. {
  52. serial = (struct rt_serial_device *)rt_device_find(RT_SERIAL_TC_DEVICE_NAME);
  53. if (serial == RT_NULL)
  54. {
  55. LOG_E("find %s device failed!\n", RT_SERIAL_TC_DEVICE_NAME);
  56. return -RT_ERROR;
  57. }
  58. return RT_EOK;
  59. }
  60. static rt_err_t uart_rx_indicate(rt_device_t device, rt_size_t size)
  61. {
  62. rt_sem_release(rx_sem);
  63. return RT_EOK;
  64. }
  65. static void uart_send_entry(void *parameter)
  66. {
  67. rt_uint8_t *uart_write_buffer;
  68. rt_uint16_t send_len;
  69. rt_uint32_t i = 0;
  70. send_len = *(rt_uint16_t *)parameter;
  71. /* assign send buffer */
  72. uart_write_buffer = (rt_uint8_t *)rt_malloc(send_len);
  73. if (uart_write_buffer == RT_NULL)
  74. {
  75. LOG_E("Without spare memory for uart dma!");
  76. uart_result = RT_FALSE;
  77. return;
  78. }
  79. rt_memset(uart_write_buffer, 0, send_len);
  80. for (i = 0; i < send_len; i++)
  81. {
  82. uart_write_buffer[i] = (rt_uint8_t)i;
  83. }
  84. /* send buffer */
  85. if (rt_device_write(&serial->parent, 0, uart_write_buffer, send_len) != send_len)
  86. {
  87. LOG_E("device write failed\r\n");
  88. }
  89. rt_free(uart_write_buffer);
  90. }
  91. static void uart_rec_entry(void *parameter)
  92. {
  93. rt_uint16_t rev_len;
  94. rev_len = *(rt_uint16_t *)parameter;
  95. rt_uint8_t *uart_write_buffer;
  96. uart_write_buffer = (rt_uint8_t *)rt_calloc(1, rev_len + 1);
  97. rt_int32_t cnt, i;
  98. rt_uint8_t last_old_data;
  99. rt_bool_t fisrt_flag = RT_TRUE;
  100. rt_uint32_t all_receive_length = 0;
  101. while (1)
  102. {
  103. rt_err_t result;
  104. result = rt_sem_take(rx_sem, RT_WAITING_FOREVER);
  105. if (result != RT_EOK)
  106. {
  107. LOG_E("take sem err in recv.");
  108. }
  109. cnt = rt_device_read(&serial->parent, 0, (void *)uart_write_buffer, rev_len);
  110. if (cnt == 0)
  111. {
  112. continue;
  113. }
  114. if (fisrt_flag != RT_TRUE)
  115. {
  116. if ((rt_uint8_t)(last_old_data + 1) != uart_write_buffer[0])
  117. {
  118. LOG_E("_Read Different data -> former data: %x, current data: %x.", last_old_data, uart_write_buffer[0]);
  119. uart_result = RT_FALSE;
  120. rt_free(uart_write_buffer);
  121. return;
  122. }
  123. }
  124. else
  125. {
  126. fisrt_flag = RT_FALSE;
  127. }
  128. for (i = 0; i < cnt - 1; i++)
  129. {
  130. if ((rt_uint8_t)(uart_write_buffer[i] + 1) != uart_write_buffer[i + 1])
  131. {
  132. LOG_E("Read Different data -> former data: %x, current data: %x.", uart_write_buffer[i], uart_write_buffer[i + 1]);
  133. uart_result = RT_FALSE;
  134. rt_free(uart_write_buffer);
  135. return;
  136. }
  137. }
  138. all_receive_length += cnt;
  139. if (all_receive_length >= rev_len)
  140. break;
  141. else
  142. last_old_data = uart_write_buffer[cnt - 1];
  143. }
  144. rt_free(uart_write_buffer);
  145. uart_over_flag = RT_TRUE;
  146. }
  147. static rt_err_t uart_api(rt_uint16_t test_buf)
  148. {
  149. rt_thread_t thread_send = RT_NULL;
  150. rt_thread_t thread_recv = RT_NULL;
  151. rt_err_t result = RT_EOK;
  152. result = uart_find();
  153. if (result != RT_EOK)
  154. {
  155. return -RT_ERROR;
  156. }
  157. rx_sem = rt_sem_create("rx_sem", 0, RT_IPC_FLAG_PRIO);
  158. if (rx_sem == RT_NULL)
  159. {
  160. LOG_E("Init sem failed.");
  161. uart_result = RT_FALSE;
  162. return -RT_ERROR;
  163. }
  164. /* reinitialize */
  165. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  166. config.baud_rate = BAUD_RATE_115200;
  167. config.rx_bufsz = RT_SERIAL_TC_RXBUF_SIZE;
  168. config.tx_bufsz = RT_SERIAL_TC_TXBUF_SIZE;
  169. #ifdef RT_SERIAL_USING_DMA
  170. config.dma_ping_bufsz = RT_SERIAL_TC_RXBUF_SIZE / 2;
  171. #endif
  172. rt_device_control(&serial->parent, RT_DEVICE_CTRL_CONFIG, &config);
  173. result = rt_device_open(&serial->parent, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);
  174. if (result != RT_EOK)
  175. {
  176. LOG_E("Open uart device failed.");
  177. uart_result = RT_FALSE;
  178. rt_sem_delete(rx_sem);
  179. return -RT_ERROR;
  180. }
  181. rt_int32_t timeout = 5000;
  182. rt_device_control(&serial->parent, RT_SERIAL_CTRL_SET_RX_TIMEOUT, (void *)&timeout);
  183. /* set receive callback function */
  184. result = rt_device_set_rx_indicate(&serial->parent, uart_rx_indicate);
  185. if (result != RT_EOK)
  186. {
  187. goto __exit;
  188. }
  189. thread_recv = rt_thread_create("uart_recv", uart_rec_entry, &test_buf, 1024, RT_THREAD_PRIORITY_MAX - 5, 10);
  190. thread_send = rt_thread_create("uart_send", uart_send_entry, &test_buf, 1024, RT_THREAD_PRIORITY_MAX - 4, 10);
  191. if (thread_send != RT_NULL && thread_recv != RT_NULL)
  192. {
  193. rt_thread_startup(thread_recv);
  194. rt_thread_startup(thread_send);
  195. }
  196. else
  197. {
  198. result = -RT_ERROR;
  199. goto __exit;
  200. }
  201. while (1)
  202. {
  203. if (uart_result != RT_TRUE)
  204. {
  205. LOG_E("The test for uart dma is failure.");
  206. result = -RT_ERROR;
  207. goto __exit;
  208. }
  209. if (uart_over_flag == RT_TRUE)
  210. {
  211. goto __exit;
  212. }
  213. /* waiting for test over */
  214. rt_thread_mdelay(5);
  215. }
  216. __exit:
  217. if (rx_sem)
  218. rt_sem_delete(rx_sem);
  219. rt_device_close(&serial->parent);
  220. rt_thread_mdelay(5);
  221. uart_over_flag = RT_FALSE;
  222. return result;
  223. }
  224. static void tc_uart_api(void)
  225. {
  226. rt_uint32_t count = 0;
  227. rt_uint16_t num = 0;
  228. rt_uint32_t i = 0;
  229. for (i = 1; i < 10; i++)
  230. {
  231. if (uart_api(RT_SERIAL_TC_TXBUF_SIZE * i + i % 2) == RT_EOK)
  232. LOG_I("data_lens [%4d], it is correct to read and write data. [%d] count testing.", RT_SERIAL_TC_TXBUF_SIZE * i + i % 2, ++count);
  233. else
  234. {
  235. LOG_E("uart test error");
  236. goto __exit;
  237. }
  238. }
  239. for (i = 1; i < 10; i++)
  240. {
  241. if (uart_api(RT_SERIAL_TC_RXBUF_SIZE * i + i % 2) == RT_EOK)
  242. LOG_I("data_lens [%4d], it is correct to read and write data. [%d] count testing.", RT_SERIAL_TC_RXBUF_SIZE * i + i % 2, ++count);
  243. else
  244. {
  245. LOG_E("uart test error");
  246. goto __exit;
  247. }
  248. }
  249. srand(rt_tick_get());
  250. while (RT_SERIAL_TC_SEND_ITERATIONS - count)
  251. {
  252. num = (rand() % 1000) + 1;
  253. if (uart_api(num) == RT_EOK)
  254. LOG_I("data_lens [%4d], it is correct to read and write data. [%d] count testing.", num, ++count);
  255. else
  256. {
  257. LOG_E("uart test error");
  258. break;
  259. }
  260. }
  261. __exit:
  262. uassert_true(uart_result == RT_TRUE);
  263. }
  264. static rt_err_t utest_tc_init(void)
  265. {
  266. LOG_I("UART TEST: Please connect Tx and Rx directly for self testing.");
  267. return RT_EOK;
  268. }
  269. static rt_err_t utest_tc_cleanup(void)
  270. {
  271. rx_sem = RT_NULL;
  272. uart_result = RT_TRUE;
  273. uart_over_flag = RT_FALSE;
  274. rt_device_t uart_dev = rt_device_find(RT_SERIAL_TC_DEVICE_NAME);
  275. while (rt_device_close(uart_dev) != -RT_ERROR);
  276. return RT_EOK;
  277. }
  278. static void testcase(void)
  279. {
  280. UTEST_UNIT_RUN(tc_uart_api);
  281. }
  282. UTEST_TC_EXPORT(testcase, "components.drivers.serial.v2.uart_rxnb_txb", utest_tc_init, utest_tc_cleanup, 30);
  283. #endif /* TC_UART_USING_TC */