test_uart.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* Copyright (c) 2023, Canaan Bright Sight Co., Ltd
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions are met:
  5. * 1. Redistributions of source code must retain the above copyright
  6. * notice, this list of conditions and the following disclaimer.
  7. * 2. Redistributions in binary form must reproduce the above copyright
  8. * notice, this list of conditions and the following disclaimer in the
  9. * documentation and/or other materials provided with the distribution.
  10. *
  11. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  12. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  13. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  16. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  18. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  21. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /*
  26. * Copyright (c) 2006-2025 RT-Thread Development Team
  27. *
  28. * SPDX-License-Identifier: Apache-2.0
  29. */
  30. #include <rtdevice.h>
  31. #include <rtdbg.h>
  32. #include <utest.h>
  33. #include "drv_uart.h"
  34. #include <string.h>
  35. /*
  36. * 测试 UART0 在 DMA 模式下的数据发送功能,以及 UART0 在中断模式下的数据接收功能
  37. *
  38. * 功能说明:
  39. * 1. 发送测试(uart_tx_demo):
  40. * - 查找名为 "uart0" 的串口设备;
  41. * - 打开设备,并配置为 DMA 发送(DMA_TX)+ 流式传输(STREAM)模式;
  42. * - 设置串口参数:
  43. * - 波特率:115200;
  44. * - 数据位:8 位;
  45. * - 停止位:1 位;
  46. * - 校验位:无;
  47. * - 动态分配一段 2000 字节的内存作为发送缓冲区:
  48. * - 填充 1999 个字符 '[';
  49. * - 最后添加 '\0' 作为字符串结尾;
  50. * - 调用 `rt_device_write` 接口,将缓冲区数据通过 UART0 DMA 方式发送出去;
  51. * - 发送完成后关闭 UART0 设备并释放发送缓冲区内存。
  52. *
  53. * 2. 接收测试(uart_rx_demo):
  54. * - 查找名为 "uart0" 的串口设备;
  55. * - 打开设备,并配置为中断接收(INT_RX)+ 流式传输(STREAM)模式;
  56. * - 设置串口参数(波特率 115200,8N1,无校验);
  57. * - 在 5 秒超时范围内循环读取 UART0 接收到的数据:
  58. * - 如果有数据,则立即打印接收到的内容;
  59. * - 如果没有数据,每隔 2.5 秒检查一次;
  60. * - 超时或接收后关闭 UART0 设备。
  61. *
  62. * 硬件说明:
  63. * - 本测试基于 K230 平台;
  64. * - uart_tx_demo 用于发送测试,可在串口调试工具上观察 1999 个 '[' 输出;
  65. * - uart_rx_demo 用于接收测试,在 5 秒内通过外部串口助手发送数据,可在日志中看到接收结果;
  66. *
  67. */
  68. #define UART0_DEV_NAME "uart0"
  69. #define TEXT_LENGTH 2000
  70. #define TEXT_TIME 5
  71. #define RX_TEXT_PERIOD 2500
  72. static void uart_tx_demo(void)
  73. {
  74. rt_device_t uart_dev;
  75. char *msg = rt_malloc(TEXT_LENGTH);
  76. for (int i = 0; i < TEXT_LENGTH - 1; i++)
  77. {
  78. msg[i] = '[';
  79. }
  80. msg[TEXT_LENGTH-1]='\0';
  81. rt_err_t ret;
  82. uart_dev = rt_device_find(UART0_DEV_NAME);
  83. uassert_not_null(uart_dev);
  84. ret = rt_device_open(uart_dev, RT_DEVICE_FLAG_DMA_TX | RT_DEVICE_FLAG_STREAM);
  85. uassert_int_equal(ret, RT_EOK);
  86. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  87. config.baud_rate = 115200;
  88. config.data_bits = DATA_BITS_8;
  89. config.stop_bits = STOP_BITS_1;
  90. config.parity = PARITY_NONE;
  91. ret = rt_device_control(uart_dev, RT_DEVICE_CTRL_CONFIG, &config);
  92. uassert_int_equal(ret, RT_EOK);
  93. size_t len = TEXT_LENGTH;
  94. ret = rt_device_write(uart_dev, 0, msg, len);
  95. uassert_int_equal(ret, len);
  96. ret = rt_device_close(uart_dev);
  97. uassert_int_equal(ret, RT_EOK);
  98. rt_free(msg);
  99. }
  100. static void uart_rx_demo(void)
  101. {
  102. rt_device_t uart_dev;
  103. char rx_buf[32];
  104. rt_size_t rx_len;
  105. uart_dev = rt_device_find(UART0_DEV_NAME);
  106. uassert_not_null(uart_dev);
  107. rt_err_t ret = rt_device_open(uart_dev, RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM);
  108. uassert_int_equal(ret, RT_EOK);
  109. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  110. config.baud_rate = 115200;
  111. config.data_bits = DATA_BITS_8;
  112. config.stop_bits = STOP_BITS_1;
  113. config.parity = PARITY_NONE;
  114. ret = rt_device_control(uart_dev, RT_DEVICE_CTRL_CONFIG, &config);
  115. uassert_int_equal(ret, RT_EOK);
  116. LOG_I("UART RX demo: please send data to uart0 within 5s...\n");
  117. rt_tick_t timeout = rt_tick_get() + RT_TICK_PER_SECOND * TEXT_TIME;
  118. while (rt_tick_get() < timeout)
  119. {
  120. rx_len = rt_device_read(uart_dev, 0, rx_buf, sizeof(rx_buf) - 1);
  121. if (rx_len > 0)
  122. {
  123. rx_buf[rx_len] = '\0';
  124. LOG_I("UART RX got %d bytes: %s\n", rx_len, rx_buf);
  125. break;
  126. }
  127. rt_thread_mdelay(RX_TEXT_PERIOD);
  128. }
  129. ret = rt_device_close(uart_dev);
  130. uassert_int_equal(ret, RT_EOK);
  131. }
  132. static void uart_testcase(void)
  133. {
  134. UTEST_UNIT_RUN(uart_tx_demo);
  135. UTEST_UNIT_RUN(uart_rx_demo);
  136. }
  137. static rt_err_t utest_tc_init(void)
  138. {
  139. return RT_EOK;
  140. }
  141. static rt_err_t utest_tc_cleanup(void)
  142. {
  143. return RT_EOK;
  144. }
  145. UTEST_TC_EXPORT(uart_testcase, "bsp.k230.drivers.uart", utest_tc_init, utest_tc_cleanup, 10);