test_spi.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 <rtthread.h>
  31. #include <rtdevice.h>
  32. #include <rtdbg.h>
  33. #include <utest.h>
  34. #include "drv_spi.h"
  35. #include <string.h>
  36. #include "drv_pinctrl.h"
  37. #include "drv_gpio.h"
  38. /*
  39. * 测试 SPI0 在标准SPI模式下的数据发送功能
  40. *
  41. * 功能说明:
  42. * - 查找名为 "spi0" 的SPI总线设备;
  43. * - 挂载SPI设备到总线;
  44. * - 配置SPI设备参数:
  45. * - 模式:标准SPI模式0 (RT_SPI_MODE_0)
  46. * - 数据位:8位
  47. * - 最大频率:1MHz
  48. * - 数据线宽度:1(标准SPI)
  49. * - 准备测试数据(递增序列);
  50. * - 创建SPI消息并发送16字节数据;
  51. * - 发送完成接收从机的16字节数据;
  52. * - 发送完成后卸载SPI设备。
  53. *
  54. * 硬件说明:
  55. * - 本测试基于 K230 平台;
  56. * - 测试SPI0(OSPI)的标准SPI模式TX和RX功能,使用硬件CS;
  57. * - 对应的引脚配置为:
  58. * - CS: GPIO14
  59. * - CLK: GPIO15
  60. * - D0: GPIO16
  61. * - D1: GPIO17
  62. * - 需要连接SPI从设备(如SPI调试器等)来验证数据传输,本测试文件使用一块stm32制作的SPI调试器;
  63. * - 如果没有实际从设备,可以使用逻辑分析仪或示波器观察SPI波形(但是只能验证TX功能,接收到会是16bit的0xff);
  64. */
  65. #define SPI0_BUS_NAME "spi0"
  66. #define SPI0_DEV_NAME0 "spi00"
  67. #define TEST_DATA_LENGTH 16
  68. #define SPI0_CS_PIN 14
  69. #define SPI0_CLK_PIN 15
  70. #define SPI0_D0_PIN 16
  71. #define SPI0_D1_PIN 17
  72. #define SPI0_CS_PIN_AF IOMUX_FUNC2
  73. #define SPI0_CLK_PIN_AF IOMUX_FUNC2
  74. #define SPI0_D0_PIN_AF IOMUX_FUNC2
  75. #define SPI0_D1_PIN_AF IOMUX_FUNC2
  76. static void spi_gpio_init(void)
  77. {
  78. LOG_I("SPI demo: initializing SPI0 GPIO...");
  79. k230_pinctrl_set_function(SPI0_CS_PIN, SPI0_CS_PIN_AF);
  80. k230_pinctrl_set_function(SPI0_CLK_PIN, SPI0_CLK_PIN_AF);
  81. k230_pinctrl_set_function(SPI0_D0_PIN, SPI0_D0_PIN_AF);
  82. k230_pinctrl_set_function(SPI0_D1_PIN, SPI0_D1_PIN_AF);
  83. k230_pinctrl_set_oe(SPI0_CS_PIN, 1);
  84. k230_pinctrl_set_oe(SPI0_CLK_PIN, 1);
  85. k230_pinctrl_set_oe(SPI0_D0_PIN, 1);
  86. k230_pinctrl_set_oe(SPI0_D1_PIN, 1);
  87. k230_pinctrl_set_ie(SPI0_CS_PIN, 1);
  88. k230_pinctrl_set_ie(SPI0_CLK_PIN, 1);
  89. k230_pinctrl_set_ie(SPI0_D0_PIN, 1);
  90. k230_pinctrl_set_ie(SPI0_D1_PIN, 1);
  91. }
  92. static void spi_device_demo(void)
  93. {
  94. struct rt_qspi_device *qspi_dev;
  95. LOG_I("Using rt_qspi_device to transmit");
  96. rt_err_t ret;
  97. uint8_t tx_data[TEST_DATA_LENGTH];
  98. uint8_t rx_data[TEST_DATA_LENGTH];
  99. for (int i = 0; i < TEST_DATA_LENGTH; i++)
  100. {
  101. tx_data[i] = i;
  102. }
  103. rt_memset(rx_data, 0, sizeof(rx_data));
  104. /* Find QSPI Bus */
  105. struct rt_spi_bus *spi_bus = (struct rt_spi_bus *)rt_device_find(SPI0_BUS_NAME);
  106. if (!spi_bus)
  107. {
  108. LOG_E("Failed to find SPI bus: %s", SPI0_BUS_NAME);
  109. return;
  110. }
  111. LOG_I("Success to find SPI bus: %s", SPI0_BUS_NAME);
  112. qspi_dev = (struct rt_qspi_device *)rt_malloc(sizeof(struct rt_qspi_device));
  113. if (!qspi_dev)
  114. {
  115. LOG_E("Failed to allocate SPI device memory");
  116. return;
  117. }
  118. LOG_I("Success to allocate QSPI device memory");
  119. /* Attach SPI Device */
  120. ret = rt_spi_bus_attach_device(&(qspi_dev->parent), SPI0_DEV_NAME0, SPI0_BUS_NAME, RT_NULL);
  121. if (ret != RT_EOK)
  122. {
  123. LOG_E("Failed to attach SPI device: %d", ret);
  124. rt_free(qspi_dev);
  125. return;
  126. }
  127. LOG_I("SPI device attached successfully");
  128. /* SPI Device Config*/
  129. struct rt_qspi_configuration qspi_cfg;
  130. qspi_cfg.parent.mode = RT_SPI_MODE_0 | RT_SPI_MSB;
  131. qspi_cfg.parent.data_width = 8;
  132. qspi_cfg.parent.max_hz = 1000000;
  133. qspi_cfg.parent.reserved = 0;
  134. qspi_cfg.qspi_dl_width = 1;
  135. qspi_cfg.medium_size = 0;
  136. qspi_cfg.ddr_mode = 0;
  137. ret = rt_qspi_configure(qspi_dev, &qspi_cfg);
  138. if (ret != RT_EOK)
  139. {
  140. LOG_E("SPI configuration failed: %d", ret);
  141. rt_free(qspi_dev);
  142. return;
  143. }
  144. LOG_I("SPI configuration: Standard SPI, mode=0, data_width=8, max_hz=%d, data_lines=%d",
  145. qspi_cfg.parent.max_hz, qspi_cfg.qspi_dl_width);
  146. LOG_I("Sending test data (length=%d):", TEST_DATA_LENGTH);
  147. for (int i = 0; i < TEST_DATA_LENGTH; i++)
  148. {
  149. rt_kprintf("%02X ", tx_data[i]);
  150. }
  151. rt_kprintf("\n");
  152. /* Create SPI Message */
  153. struct rt_qspi_message msg;
  154. rt_memset(&msg, 0, sizeof(msg));
  155. /*Using Standard SPI*/
  156. msg.instruction.content = 0;
  157. msg.instruction.qspi_lines = 1;
  158. msg.address.content = 0;
  159. msg.address.size = 0;
  160. msg.address.qspi_lines = 1;
  161. msg.qspi_data_lines = 1;
  162. msg.dummy_cycles = 0;
  163. /* SPI Message Config */
  164. msg.parent.send_buf = tx_data;
  165. msg.parent.recv_buf = rx_data;
  166. msg.parent.length = TEST_DATA_LENGTH;
  167. msg.parent.cs_take = 1;
  168. msg.parent.cs_release = 1;
  169. msg.parent.next = RT_NULL;
  170. /* Transfer Data */
  171. ret = rt_qspi_transfer_message(qspi_dev, &msg);
  172. if (ret != TEST_DATA_LENGTH)
  173. {
  174. LOG_E("SPI transfer failed, returned: %d", ret);
  175. }
  176. uassert_int_equal(ret, TEST_DATA_LENGTH);
  177. LOG_I("SPI TX demo: sent %d bytes successfully", ret);
  178. LOG_I("Received data from slave (length=%d):", TEST_DATA_LENGTH);
  179. for (int i = 0; i < TEST_DATA_LENGTH; i++)
  180. {
  181. rt_kprintf("%02X ", rx_data[i]);
  182. }
  183. rt_kprintf("\n");
  184. /* Detach SPI Device */
  185. ret = rt_spi_bus_detach_device(&(qspi_dev->parent));
  186. uassert_int_equal(ret, RT_EOK);
  187. rt_free(qspi_dev);
  188. }
  189. static void testcase(void)
  190. {
  191. UTEST_UNIT_RUN(spi_gpio_init);
  192. UTEST_UNIT_RUN(spi_device_demo);
  193. }
  194. static rt_err_t utest_tc_init(void)
  195. {
  196. LOG_I("SPI test case initialization");
  197. return RT_EOK;
  198. }
  199. static rt_err_t utest_tc_cleanup(void)
  200. {
  201. LOG_I("SPI test case cleanup");
  202. return RT_EOK;
  203. }
  204. UTEST_TC_EXPORT(testcase, "bsp.k230.drivers.spi", utest_tc_init, utest_tc_cleanup, 10);