uart_qemu_echo.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. * 2025-11-13 CYFS Add standardized utest documentation block
  9. */
  10. /**
  11. * Test Case Name: UART QEMU Echo Loopback Test
  12. *
  13. * Test Objectives:
  14. * - Validate dual-UART echo behavior under QEMU by cross-linking uart1 and uart2
  15. * - Verify APIs: rt_device_find, rt_device_open, rt_device_write, rt_device_read,
  16. * rt_device_control(RT_SERIAL_CTRL_GET_UNREAD_BYTES_COUNT), rt_thread_create/startup
  17. *
  18. * Test Scenarios:
  19. * - **Scenario 1 (Cross-Echo Stress / uart_test_nonblocking_tx):**
  20. * 1. Open uart1/uart2 in blocking mode and spawn threads to mirror RX→TX on uart2 while recording statistics.
  21. * 2. Simultaneously read uart1 in a dedicated thread to monitor inbound bytes.
  22. * 3. Send random-length payloads up to 1 KB for 1000 iterations, periodically comparing TX/RX counters across both devices.
  23. * 4. Signal threads to exit once validation completes and ensure device handles close cleanly.
  24. *
  25. * Verification Metrics:
  26. * - u1/u2 TX and RX counters remain equal; send total matches aggregated transmit length.
  27. * - No allocation failures; `echo_test()` returns RT_TRUE when counters align.
  28. *
  29. * Dependencies:
  30. * - Requires `RT_UTEST_SERIAL_V2` running under QEMU with uart1↔uart2 interconnected.
  31. * - UART driver must support unread-bytes query and blocking modes.
  32. * - Threads need 2 KB stacks; dynamic buffers sized at 1 KB per UART.
  33. *
  34. * Expected Results:
  35. * - Test completes without assertions; logs show synchronized counter updates.
  36. * - Utest harness prints `[ PASSED ] [ result ] testcase (components.drivers.serial.v2.uart_qemu_echo)`.
  37. */
  38. #include <rtthread.h>
  39. #include <rtdevice.h>
  40. #include "utest.h"
  41. #define UART_SEND_TIMES 100
  42. #define UART_TEST_NUMBER 6
  43. #ifdef RT_UTEST_SERIAL_V2
  44. #define echo_test_buffer_size (1024)
  45. static rt_device_t u1serial;
  46. static rt_device_t u2serial;
  47. static rt_uint32_t u2rx_length = 0;
  48. static rt_uint32_t u2tx_length = 0;
  49. static rt_uint32_t u1rx_length = 0;
  50. static rt_uint32_t u1tx_length = 0;
  51. static rt_uint8_t uart_over_flag = RT_FALSE;
  52. static void echo_test_u2_thread_entry(void *parameter)
  53. {
  54. char *uart_name = "uart2";
  55. u2serial = rt_device_find(uart_name);
  56. if (!u2serial)
  57. {
  58. LOG_I("find %s failed!\n", uart_name);
  59. return;
  60. }
  61. rt_uint8_t *rx_buffer = rt_malloc(echo_test_buffer_size);
  62. rt_device_open(u2serial, RT_DEVICE_FLAG_RX_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);
  63. rt_ssize_t buf_datalen = 0;
  64. while (1)
  65. {
  66. rt_device_control(u2serial, RT_SERIAL_CTRL_GET_UNREAD_BYTES_COUNT, (void *)&buf_datalen);
  67. int32_t recbLen = rt_device_read(u2serial, 0, rx_buffer, buf_datalen > 0 ? buf_datalen : 1);
  68. if (recbLen > 0)
  69. {
  70. u2rx_length += recbLen;
  71. u2tx_length += rt_device_write(u2serial, 0, rx_buffer, recbLen);
  72. if (uart_over_flag)
  73. break;
  74. }
  75. }
  76. rt_free(rx_buffer);
  77. }
  78. static void echo_test_u1_thread_entry(void *parameter)
  79. {
  80. rt_uint8_t *rx_buffer = rt_malloc(echo_test_buffer_size);
  81. rt_ssize_t buf_datalen = 0;
  82. while (1)
  83. {
  84. rt_device_control(u1serial, RT_SERIAL_CTRL_GET_UNREAD_BYTES_COUNT, (void *)&buf_datalen);
  85. int32_t recbLen = rt_device_read(u1serial, 0, rx_buffer, buf_datalen > 0 ? buf_datalen : 1);
  86. if (recbLen > 0)
  87. {
  88. u1rx_length += recbLen;
  89. if (uart_over_flag)
  90. break;
  91. }
  92. }
  93. rt_free(rx_buffer);
  94. }
  95. static rt_bool_t echo_test()
  96. {
  97. rt_bool_t result = RT_TRUE;
  98. char *uart_name = "uart1";
  99. u1serial = rt_device_find(uart_name);
  100. if (!u1serial)
  101. {
  102. LOG_I("find %s failed!\n", uart_name);
  103. return RT_FALSE;
  104. }
  105. rt_uint8_t *tx_buffer = rt_malloc(echo_test_buffer_size);
  106. rt_device_open(u1serial, RT_DEVICE_FLAG_RX_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);
  107. rt_thread_startup(rt_thread_create("serial2", echo_test_u2_thread_entry, RT_NULL, 2048, RT_THREAD_PRIORITY_MAX - 4, 5));
  108. rt_thread_startup(rt_thread_create("serial1", echo_test_u1_thread_entry, RT_NULL, 2048, RT_THREAD_PRIORITY_MAX - 5, 5));
  109. uint32_t sendTotalCount = 0;
  110. srand(rt_tick_get());
  111. for (uint32_t count = 0; count < 1000; count++)
  112. {
  113. // Indefinite length of data is sent
  114. uint32_t sendCount = rand() % echo_test_buffer_size;
  115. u1tx_length += rt_device_write(u1serial, 0, tx_buffer, sendCount);
  116. sendTotalCount += sendCount;
  117. // Wait for the cross-send to complete
  118. rt_thread_mdelay(UART_SEND_TIMES);
  119. if (count % 50 == 0)
  120. {
  121. LOG_I("echo, uart2: tx: %ld, rx: %ld", u2tx_length, u2rx_length);
  122. LOG_I("echo, uart1: tx: %ld, rx: %ld", u1tx_length, u1rx_length);
  123. if (u2tx_length != u2rx_length || u1tx_length != u1rx_length || u2tx_length != u1tx_length)
  124. {
  125. LOG_I("echo test error!!!");
  126. result = RT_FALSE;
  127. break;
  128. }
  129. if (u2tx_length != sendTotalCount)
  130. {
  131. LOG_I("u2tx_length != sendTotalCount echo test error!!!");
  132. result = RT_FALSE;
  133. break;
  134. }
  135. }
  136. }
  137. uart_over_flag = RT_TRUE;
  138. // Notify the thread to exit
  139. rt_device_write(u1serial, 0, tx_buffer, echo_test_buffer_size);
  140. rt_thread_mdelay(30);
  141. {
  142. rt_device_t uart_dev = rt_device_find("uart2");
  143. while (rt_device_close(uart_dev) != -RT_ERROR);
  144. }
  145. {
  146. rt_device_t uart_dev = rt_device_find("uart1");
  147. while (rt_device_close(uart_dev) != -RT_ERROR);
  148. }
  149. rt_free(tx_buffer);
  150. return result;
  151. }
  152. static void uart_test_nonblocking_tx(void)
  153. {
  154. uassert_true(echo_test());
  155. }
  156. static rt_err_t utest_tc_init(void)
  157. {
  158. return RT_EOK;
  159. }
  160. static rt_err_t utest_tc_cleanup(void)
  161. {
  162. u1serial = RT_NULL;
  163. u2serial = RT_NULL;
  164. u2rx_length = 0;
  165. u2tx_length = 0;
  166. u1rx_length = 0;
  167. u1tx_length = 0;
  168. uart_over_flag = RT_FALSE;
  169. return RT_EOK;
  170. }
  171. static void testcase(void)
  172. {
  173. UTEST_UNIT_RUN(uart_test_nonblocking_tx);
  174. }
  175. UTEST_TC_EXPORT(testcase, "components.drivers.serial.v2.uart_qemu_echo", utest_tc_init, utest_tc_cleanup, 10);
  176. #endif