uart_flush_txb.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 TX Blocking Flush Test
  12. *
  13. * Test Objectives:
  14. * - Validate TX flush completion timing in blocking mode and ensure subsequent RX integrity
  15. * - Verify APIs: rt_device_find, rt_device_control(RT_DEVICE_CTRL_CONFIG / RT_SERIAL_CTRL_TX_FLUSH / _SET_TX_TIMEOUT / RT_SERIAL_CTRL_RX_FLUSH),
  16. * rt_device_open with RT_DEVICE_FLAG_RX_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING,
  17. * rt_device_write, rt_device_read
  18. *
  19. * Test Scenarios:
  20. * - **Scenario 1 (Flush Timing & Data Integrity / tc_uart_api):**
  21. * 1. Configure enlarged TX buffer and set TX timeout guard.
  22. * 2. Perform multiple iterations with varied payload sizes (aligned and unaligned), recording ticks needed to flush queued bytes and verifying they fall within `[expect_time, expect_time + 10]`.
  23. * 3. After each flush, resend small samples and confirm received data matches transmitted pattern.
  24. *
  25. * Verification Metrics:
  26. * - `rt_device_write` returns full payload length; measured tick difference respects expected window.
  27. * - Post-flush RX comparisons succeed for all sample sizes.
  28. *
  29. * Dependencies:
  30. * - Requires `RT_UTEST_SERIAL_V2` with hardware loopback and TX flush capability on `RT_SERIAL_TC_DEVICE_NAME`.
  31. * - Optional DMA ping buffer configuration supported.
  32. *
  33. * Expected Results:
  34. * - Test completes without assertions; logs show flush durations per payload.
  35. * - Utest harness prints `[ PASSED ] [ result ] testcase (components.drivers.serial.v2.uart_flush_txb)`.
  36. */
  37. #include <rtthread.h>
  38. #include "utest.h"
  39. #include <rtdevice.h>
  40. #include <stdlib.h>
  41. #ifdef RT_UTEST_SERIAL_V2
  42. static struct rt_serial_device *serial;
  43. static rt_err_t uart_find(void)
  44. {
  45. serial = (struct rt_serial_device *)rt_device_find(RT_SERIAL_TC_DEVICE_NAME);
  46. if (serial == RT_NULL)
  47. {
  48. LOG_E("find %s device failed!\n", RT_SERIAL_TC_DEVICE_NAME);
  49. return -RT_ERROR;
  50. }
  51. return RT_EOK;
  52. }
  53. static rt_err_t test_item(rt_uint8_t *uart_write_buffer, rt_uint32_t send_size)
  54. {
  55. rt_uint32_t old_tick;
  56. rt_tick_t tick_diff;
  57. rt_tick_t expect_time = send_size * 0.0868;
  58. rt_uint8_t readBuf[16] = {0};
  59. rt_uint32_t readSize = 0;
  60. if (send_size >= sizeof(readBuf))
  61. {
  62. readSize = sizeof(readBuf);
  63. }
  64. else
  65. {
  66. readSize = send_size;
  67. }
  68. /* In interrupt mode, ticks may be inaccurate; compensation should be applied*/
  69. if (send_size > 384)
  70. {
  71. expect_time -= send_size / 384;
  72. }
  73. old_tick = rt_tick_get();
  74. rt_ssize_t size = rt_device_write(&serial->parent, 0, uart_write_buffer, send_size);
  75. if (size != send_size)
  76. {
  77. LOG_E("size [%4d], send_size [%4d]", size, send_size);
  78. return -RT_ERROR;
  79. }
  80. rt_device_control(&serial->parent, RT_SERIAL_CTRL_TX_FLUSH, RT_NULL);
  81. tick_diff = rt_tick_get() - old_tick;
  82. if (tick_diff < expect_time || tick_diff > (expect_time + 10))
  83. {
  84. LOG_E("send_size [%4d], time required for TXB mode transmission to complete [%3d], expect_time [%3d]", send_size, tick_diff, expect_time);
  85. return -RT_ERROR;
  86. }
  87. else
  88. {
  89. LOG_I("send_size [%4d], time required for TXB mode transmission to complete [%3d], expect_time [%3d]", send_size, tick_diff, expect_time);
  90. }
  91. /* Resend the data and check for any discrepancies upon reception */
  92. if (readSize > 0)
  93. {
  94. rt_device_control(&serial->parent, RT_SERIAL_CTRL_RX_FLUSH, RT_NULL);
  95. rt_device_write(&serial->parent, 0, uart_write_buffer, readSize);
  96. rt_device_read(&serial->parent, 0, readBuf, readSize);
  97. for (rt_uint32_t i = 0; i < readSize; i++)
  98. {
  99. if (readBuf[i] != uart_write_buffer[i])
  100. {
  101. LOG_E("index: %d, Read Different data -> former data: %x, current data: %x.", i, uart_write_buffer[i], readBuf[i]);
  102. return -RT_ERROR;
  103. }
  104. }
  105. }
  106. return RT_EOK;
  107. }
  108. static rt_bool_t uart_api()
  109. {
  110. rt_err_t result = RT_EOK;
  111. result = uart_find();
  112. if (result != RT_EOK)
  113. {
  114. return RT_FALSE;
  115. }
  116. /* Reinitialize */
  117. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  118. config.baud_rate = BAUD_RATE_115200;
  119. config.rx_bufsz = RT_SERIAL_TC_RXBUF_SIZE;
  120. config.tx_bufsz = RT_SERIAL_TC_TXBUF_SIZE * 5 + 10;
  121. #ifdef RT_SERIAL_USING_DMA
  122. config.dma_ping_bufsz = RT_SERIAL_TC_RXBUF_SIZE / 2;
  123. #endif
  124. rt_device_control(&serial->parent, RT_DEVICE_CTRL_CONFIG, &config);
  125. result = rt_device_open(&serial->parent, RT_DEVICE_FLAG_RX_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);
  126. if (result != RT_EOK)
  127. {
  128. LOG_E("Open uart device failed.");
  129. return RT_FALSE;
  130. }
  131. rt_uint8_t *uart_write_buffer;
  132. rt_uint32_t i;
  133. rt_int32_t tx_timeout = 10 * 1000;
  134. uart_write_buffer = (rt_uint8_t *)rt_malloc(RT_SERIAL_TC_TXBUF_SIZE * 5 + 10);
  135. for (rt_uint32_t count = 0; count < (RT_SERIAL_TC_TXBUF_SIZE * 5 + 10); count++)
  136. {
  137. uart_write_buffer[count] = count;
  138. }
  139. rt_device_control(&serial->parent, RT_SERIAL_CTRL_SET_TX_TIMEOUT, (void *)&tx_timeout);
  140. srand(rt_tick_get());
  141. for (i = 0; i < RT_SERIAL_TC_SEND_ITERATIONS; i++)
  142. {
  143. if (RT_EOK != test_item(uart_write_buffer, RT_SERIAL_TC_TXBUF_SIZE * (rand() % 6)))
  144. {
  145. result = -RT_ERROR;
  146. goto __exit;
  147. }
  148. if (RT_EOK != test_item(uart_write_buffer, RT_SERIAL_TC_TXBUF_SIZE * (rand() % 6) + 1))
  149. {
  150. result = -RT_ERROR;
  151. goto __exit;
  152. }
  153. if (RT_EOK != test_item(uart_write_buffer, rand() % (RT_SERIAL_TC_TXBUF_SIZE * 5)))
  154. {
  155. result = -RT_ERROR;
  156. goto __exit;
  157. }
  158. }
  159. __exit:
  160. rt_free(uart_write_buffer);
  161. rt_device_close(&serial->parent);
  162. rt_thread_mdelay(5);
  163. return result == RT_EOK ? RT_TRUE : RT_FALSE;
  164. }
  165. static void tc_uart_api(void)
  166. {
  167. uassert_true(uart_api() == RT_TRUE);
  168. }
  169. static rt_err_t utest_tc_init(void)
  170. {
  171. return RT_EOK;
  172. }
  173. static rt_err_t utest_tc_cleanup(void)
  174. {
  175. rt_device_t uart_dev = rt_device_find(RT_SERIAL_TC_DEVICE_NAME);
  176. while (rt_device_close(uart_dev) != -RT_ERROR);
  177. return RT_EOK;
  178. }
  179. static void testcase(void)
  180. {
  181. UTEST_UNIT_RUN(tc_uart_api);
  182. }
  183. UTEST_TC_EXPORT(testcase, "components.drivers.serial.v2.uart_flush_txb", utest_tc_init, utest_tc_cleanup, 30);
  184. #endif /* TC_UART_USING_TC */