uart_nonblocking_rx.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 Non-Blocking Receive Echo Test
  12. *
  13. * Test Objectives:
  14. * - Demonstrate non-blocking receive behavior paired with blocking transmit output
  15. * - Verify APIs: rt_device_find, rt_device_open with RT_DEVICE_FLAG_RX_NON_BLOCKING,
  16. * rt_device_read (non-blocking), rt_device_write, rt_device_close
  17. *
  18. * Test Scenarios:
  19. * - **Scenario 1 (Interactive Echo / uart_test_nonblocking_rx):**
  20. * 1. Restart UART in RX non-blocking / TX blocking mode.
  21. * 2. Prompt user (or loopback peer) to send data while issuing countdown markers to MSH.
  22. * 3. Perform successive non-blocking reads of varying sizes (256/128 bytes), echoing captured data back and logging totals.
  23. *
  24. * Verification Metrics:
  25. * - Each read returns immediately with available data (may be zero); totals accumulate without overflow.
  26. * - Function returns RT_TRUE after final log, indicating no failures during sequence.
  27. *
  28. * Dependencies:
  29. * - Requires `RT_UTEST_SERIAL_V2` with loopback or user-provided input on `RT_SERIAL_TC_DEVICE_NAME`.
  30. * - UART driver must support non-blocking reads and reversible open/close.
  31. *
  32. * Expected Results:
  33. * - No assertions triggered; terminal displays countdown prompts and mirrored input.
  34. * - Utest harness prints `[ PASSED ] [ result ] testcase (components.drivers.serial.v2.uart_nonblocking_rx)`.
  35. */
  36. #include <rtthread.h>
  37. #include <rtdevice.h>
  38. #include "utest.h"
  39. #ifdef RT_UTEST_SERIAL_V2
  40. static rt_bool_t nonblock_read(rt_device_t uart_dev)
  41. {
  42. rt_size_t total_length, recv_length;
  43. char uart_read_buffer[1024], log_buffer[64];
  44. /* make sure device is closed and reopen it */
  45. while (rt_device_close(uart_dev) != -RT_ERROR);
  46. rt_device_open(uart_dev, RT_DEVICE_FLAG_TX_BLOCKING | RT_DEVICE_FLAG_RX_NON_BLOCKING);
  47. rt_sprintf(log_buffer, "\nNONBLOCKING READ BEGIN, PLEASE SEND SOME DATAS\n");
  48. rt_device_write(uart_dev, 0, log_buffer, rt_strlen(log_buffer));
  49. total_length = 0;
  50. rt_device_write(uart_dev, 0, "5\n", 2);
  51. recv_length = 0;
  52. recv_length = rt_device_read(uart_dev, -1, uart_read_buffer, 256);
  53. rt_device_write(uart_dev, 0, uart_read_buffer, 256);
  54. total_length += recv_length;
  55. rt_sprintf(log_buffer, "\nnonblock : %d bytes read, total: %d \n", recv_length, total_length);
  56. rt_device_write(uart_dev, 0, log_buffer, rt_strlen(log_buffer));
  57. rt_device_write(uart_dev, 0, "4\n", 2);
  58. recv_length = 0;
  59. recv_length = rt_device_read(uart_dev, -1, uart_read_buffer, 256);
  60. rt_device_write(uart_dev, 0, uart_read_buffer, 256);
  61. total_length += recv_length;
  62. rt_sprintf(log_buffer, "\nnonblock : %d bytes read , total: %d \n", recv_length, total_length);
  63. rt_device_write(uart_dev, 0, log_buffer, rt_strlen(log_buffer));
  64. rt_device_write(uart_dev, 0, "3\n", 2);
  65. recv_length = 0;
  66. recv_length = rt_device_read(uart_dev, -1, uart_read_buffer, 256);
  67. rt_device_write(uart_dev, 0, uart_read_buffer, 256);
  68. total_length += recv_length;
  69. rt_sprintf(log_buffer, "\nnonblock : %d bytes read, total: %d \n", recv_length, total_length);
  70. rt_device_write(uart_dev, 0, log_buffer, rt_strlen(log_buffer));
  71. rt_device_write(uart_dev, 0, "2\n", 2);
  72. recv_length = 0;
  73. recv_length = rt_device_read(uart_dev, -1, uart_read_buffer, 128);
  74. rt_device_write(uart_dev, 0, uart_read_buffer, 128);
  75. total_length += recv_length;
  76. rt_sprintf(log_buffer, "\nnonblock : %d bytes read , total: %d \n", recv_length, total_length);
  77. rt_device_write(uart_dev, 0, log_buffer, rt_strlen(log_buffer));
  78. rt_device_write(uart_dev, 0, "1\n", 2);
  79. recv_length = 0;
  80. recv_length = rt_device_read(uart_dev, -1, uart_read_buffer, 128);
  81. rt_device_write(uart_dev, 0, uart_read_buffer, 128);
  82. total_length += recv_length;
  83. rt_sprintf(log_buffer, "\nnonblock : %d bytes read , total: %d \n", recv_length, total_length);
  84. rt_device_write(uart_dev, 0, log_buffer, rt_strlen(log_buffer));
  85. rt_sprintf(log_buffer, "BLOCKING READ END");
  86. rt_device_write(uart_dev, 0, log_buffer, rt_strlen(log_buffer));
  87. return RT_TRUE;
  88. }
  89. static void uart_test_nonblocking_rx(void)
  90. {
  91. rt_device_t uart_dev;
  92. uart_dev = rt_device_find(RT_SERIAL_TC_DEVICE_NAME);
  93. uassert_not_null(uart_dev);
  94. uassert_true(nonblock_read(uart_dev));
  95. }
  96. static rt_err_t utest_tc_init(void)
  97. {
  98. return RT_EOK;
  99. }
  100. static rt_err_t utest_tc_cleanup(void)
  101. {
  102. rt_device_t uart_dev = rt_device_find(RT_SERIAL_TC_DEVICE_NAME);
  103. while (rt_device_close(uart_dev) != -RT_ERROR);
  104. return RT_EOK;
  105. }
  106. static void testcase(void)
  107. {
  108. UTEST_UNIT_RUN(uart_test_nonblocking_rx);
  109. }
  110. UTEST_TC_EXPORT(testcase, "components.drivers.serial.v2.uart_nonblocking_rx", utest_tc_init, utest_tc_cleanup, 10);
  111. #endif