bypass_lower_run.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2006-2024 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-11-20 zhujiale the first version
  9. * 2025-11-24 ChuanN-sudo add standardized utest documentation block
  10. */
  11. /**
  12. * Test Case Name: Serial Bypass Lower Run Test
  13. *
  14. * Test Objectives:
  15. * - Validate serial device bypass lower layer registration and data processing mechanisms.
  16. * - Verify correct character reception and callback execution in bypass mode.
  17. * - Test core APIs: rt_bypass_lower_register(), rt_bypass_lower_unregister(), rt_hw_serial_isr()
  18. *
  19. * Test Scenarios:
  20. * - Register bypass lower layer callback to intercept serial RX data before normal processing.
  21. * - Simulate hardware interrupt with custom getc function returning predefined character sequences.
  22. * - Verify repeated character reception, counting mechanism and sequential character reception with incremental validation.
  23. * - Temporarily replace serial device operations with mock implementations for controlled testing.
  24. *
  25. * Verification Metrics:
  26. * - Bypass callback executes for each received character with correct character value.
  27. * - Character validation assertions pass: bypass_lower_001 validates 10 'a' characters, bypass_lower_002 validates sequential characters 'b' to 'u'.
  28. * - Mock getc function returns -1 after 10 iterations (bypass_lower_001) or 20 iterations (bypass_lower_002) to terminate reception.
  29. * - Serial device operations restore to original state after test completion.
  30. * - Bypass lower layer unregisters successfully without resource leaks.
  31. *
  32. * Dependencies:
  33. * - Hardware requirements: Platform with serial console device (UART) support.
  34. * - Software configuration:
  35. * - RT_USING_UTESTCASES must be enabled (select "RT-Thread Utestcases" in menuconfig).
  36. * - RT_UTEST_SERIAL_BYPASS must be enabled (enable via: RT-Thread Utestcases -> Kernel Components -> Drivers -> Serial Test -> Serial Bypass Test).
  37. * - Environmental Assumptions: Serial device initialized and operational before test execution.
  38. *
  39. * Expected Results:
  40. * - Final output: "[ PASSED ] [ result ] testcase (components.drivers.serial.bypass_lower)"
  41. * - Character validation assertions pass for all test iterations.
  42. */
  43. #include <rtthread.h>
  44. #include <rtdevice.h>
  45. #include "utest.h"
  46. static struct rt_serial_device* _serial0;
  47. static int cnt = 0;
  48. #define __REG32(x) (*((volatile unsigned int*)((rt_ubase_t)x)))
  49. #define UART_FR(base) __REG32(base + 0x18)
  50. #define UART_DR(base) __REG32(base + 0x00)
  51. #define UARTFR_TXFF 0x20
  52. struct hw_uart_device
  53. {
  54. rt_size_t hw_base;
  55. rt_size_t irqno;
  56. };
  57. static int uart_putc(struct rt_serial_device* serial, char c)
  58. {
  59. struct hw_uart_device* uart;
  60. RT_ASSERT(serial != RT_NULL);
  61. uart = (struct hw_uart_device*)serial->parent.user_data;
  62. while (UART_FR(uart->hw_base) & UARTFR_TXFF);
  63. UART_DR(uart->hw_base) = c;
  64. return 1;
  65. }
  66. static rt_err_t utest_lower_run_test2(struct rt_serial_device* serial, char ch, void* data)
  67. {
  68. static rt_uint8_t num = 0;
  69. num++;
  70. uassert_true(ch == ('a' + num));
  71. return RT_EOK;
  72. }
  73. static int utest_getc_2(struct rt_serial_device* serial)
  74. {
  75. static rt_uint8_t num = 0;
  76. if (num == 20)
  77. return -1;
  78. num++;
  79. return 'a' + num;
  80. }
  81. static const struct rt_uart_ops _utest_ops2 =
  82. {
  83. RT_NULL,
  84. RT_NULL,
  85. uart_putc,
  86. utest_getc_2,
  87. };
  88. static rt_err_t utest_lower_run(struct rt_serial_device* serial, char ch, void* data)
  89. {
  90. uassert_true(ch == 'a');
  91. cnt++;
  92. return RT_EOK;
  93. }
  94. static int utest_getc(struct rt_serial_device* serial)
  95. {
  96. static rt_uint8_t num = 0;
  97. if (num == 10)
  98. return -1;
  99. num++;
  100. return 'a';
  101. }
  102. static const struct rt_uart_ops _utest_ops =
  103. {
  104. RT_NULL,
  105. RT_NULL,
  106. uart_putc,
  107. utest_getc,
  108. };
  109. static void bypass_lower_001(void)
  110. {
  111. const struct rt_uart_ops* tmp = _serial0->ops;
  112. _serial0->ops = &_utest_ops;
  113. rt_bypass_lower_register(_serial0, "utest", RT_BYPASS_MAX_LEVEL, utest_lower_run, RT_NULL);
  114. rt_hw_serial_isr(_serial0, RT_SERIAL_EVENT_RX_IND);
  115. rt_thread_mdelay(100);
  116. uassert_true(cnt == 10);
  117. _serial0->ops = tmp;
  118. rt_bypass_lower_unregister(_serial0, RT_BYPASS_MAX_LEVEL);
  119. }
  120. static void bypass_lower_002(void)
  121. {
  122. const struct rt_uart_ops* tmp = _serial0->ops;
  123. _serial0->ops = &_utest_ops2;
  124. rt_bypass_lower_register(_serial0, "utest", RT_BYPASS_MAX_LEVEL, utest_lower_run_test2, RT_NULL);
  125. rt_hw_serial_isr(_serial0, RT_SERIAL_EVENT_RX_IND);
  126. rt_thread_mdelay(100);
  127. uassert_true(cnt == 10);
  128. _serial0->ops = tmp;
  129. rt_bypass_lower_unregister(_serial0, RT_BYPASS_MAX_LEVEL);
  130. }
  131. static rt_err_t utest_tc_init(void)
  132. {
  133. _serial0 = (struct rt_serial_device*)rt_console_get_device();
  134. return RT_EOK;
  135. }
  136. static rt_err_t utest_tc_cleanup(void)
  137. {
  138. return RT_EOK;
  139. }
  140. static void _testcase(void)
  141. {
  142. UTEST_UNIT_RUN(bypass_lower_001);
  143. UTEST_UNIT_RUN(bypass_lower_002);
  144. }
  145. UTEST_TC_EXPORT(_testcase, "components.drivers.serial.bypass_lower", utest_tc_init, utest_tc_cleanup, 10);