From 37d8b8d1d0b8b39d9238cd86dac2a1ae79d190c1 Mon Sep 17 00:00:00 2001 From: Chen Moushun Date: Sun, 8 Nov 2026 15:48:35 +0900 Subject: [PATCH] drivers/spi: QEMU virtual SPI loopback bus for utest (#11972) (#11979) * drivers/spi: add QEMU virtual SPI loopback bus for utest (#11972) Register a pure-software SPI bus (default name spi2) with memcpy loopback xfer so components.drivers.spi.loopback passes on qemu-vexpress-a9 without a PL022 model. Fix utest include/CPPPATH and align Kconfig/rtconfig bus name. Verified: scons + qemu utest_run components.drivers.spi.loopback PASSED. Fixes #11972 (SPI/QEMU checklist item). * drivers/spi: fix clang-format violations in spi_virt_bus.c Fix the 'Check File Format and License' CI job failure: - put the spi_virt_ops initializer brace on the assignment line - add the missing newline at end of file No functional change. * drivers/spi: separate virt bus Kconfig from loopback utest (#11972) Add a dedicated, default-off RT_UTEST_SPI_VIRT_BUS switch (default name spi_virt) so enabling RT_UTEST_SPI_LOOPBACK no longer compiles or registers a software SPI bus. Hardware loopback now always runs on the real controller named by RT_UTEST_SPI_LOOPBACK_BUS_NAME, while QEMU/CI targets opt in to the virtual bus explicitly. Also move the utest enablement out of the qemu-vexpress-a9 BSP defaults and into ci.attachconfig as peripheral.SPI_VIRT_LOOPBACK. * cla: empty commit to retrigger license/cla check (#11979) * ci(utest): add A9 SPI loopback virt bus config (#11979) * [drivers][spi] gate virtual SPI bus utest on !RT_USING_DM RT_UTEST_SPI_VIRT_BUS compiles spi_virt_bus.c, which registers a software loopback bus with rt_spi_bus_register(). That path needs no device tree node, so on a BSP that enables RT_USING_DM and RT_USING_OFW the registration fails with -RT_EINVAL when no matching DT node exists. Add "depends on !RT_USING_DM" to the option and document in help that it is only available without the device model and must stay disabled on DM based BSPs. spi_virt_bus.c is already fully wrapped in #ifdef RT_UTEST_SPI_VIRT_BUS, so no C change is needed. The A9 utest config (bsp/qemu-vexpress-a9) has RT_USING_DM disabled, and .github/utest/configs/components/spi.cfg still enables both the virt bus and the loopback test with bus name "spi_virt", so CI coverage is kept. Refs #11972 --- .github/utest/configs/components/spi.cfg | 10 ++ .github/workflows/utest_auto_run.yml | 2 + .../.ci/attachconfig/ci.attachconfig.yml | 8 ++ components/drivers/spi/utest/Kconfig | 37 ++++++++ components/drivers/spi/utest/SConscript | 6 +- components/drivers/spi/utest/spi_virt_bus.c | 92 +++++++++++++++++++ 6 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 .github/utest/configs/components/spi.cfg create mode 100644 components/drivers/spi/utest/spi_virt_bus.c diff --git a/.github/utest/configs/components/spi.cfg b/.github/utest/configs/components/spi.cfg new file mode 100644 index 0000000000..f974f39afd --- /dev/null +++ b/.github/utest/configs/components/spi.cfg @@ -0,0 +1,10 @@ +# dependencies +CONFIG_RT_CONSOLEBUF_SIZE=1024 +CONFIG_RT_USING_CI_ACTION=y +CONFIG_RT_USING_UTEST=y +CONFIG_RT_USING_UTESTCASES=y + +CONFIG_RT_UTEST_SPI_LOOPBACK=y +CONFIG_RT_UTEST_SPI_LOOPBACK_BUS_NAME="spi_virt" +CONFIG_RT_UTEST_SPI_VIRT_BUS=y +CONFIG_RT_UTEST_SPI_VIRT_BUS_NAME="spi_virt" diff --git a/.github/workflows/utest_auto_run.yml b/.github/workflows/utest_auto_run.yml index 94a66dd070..00dba488b7 100644 --- a/.github/workflows/utest_auto_run.yml +++ b/.github/workflows/utest_auto_run.yml @@ -152,6 +152,8 @@ jobs: config_file: "components/dfs.cfg" - platform: { UTEST: "A9", RTT_BSP: "bsp/qemu-vexpress-a9", QEMU_ARCH: "arm", QEMU_MACHINE: "vexpress-a9", SD_FILE: "sd.bin", KERNEL: "standard", "SMP_RUN":"" } config_file: "components/libc.cfg" + - platform: { UTEST: "A9", RTT_BSP: "bsp/qemu-vexpress-a9", QEMU_ARCH: "arm", QEMU_MACHINE: "vexpress-a9", SD_FILE: "sd.bin", KERNEL: "standard", "SMP_RUN":"" } + config_file: "components/spi.cfg" - platform: { UTEST: "A9", RTT_BSP: "bsp/qemu-vexpress-a9", QEMU_ARCH: "arm", QEMU_MACHINE: "vexpress-a9", SD_FILE: "sd.bin", KERNEL: "standard", "SMP_RUN":"" } config_file: "components/dac.cfg" - platform: { UTEST: "A9", RTT_BSP: "bsp/qemu-vexpress-a9", QEMU_ARCH: "arm", QEMU_MACHINE: "vexpress-a9", SD_FILE: "sd.bin", KERNEL: "standard", "SMP_RUN":"" } diff --git a/bsp/qemu-vexpress-a9/.ci/attachconfig/ci.attachconfig.yml b/bsp/qemu-vexpress-a9/.ci/attachconfig/ci.attachconfig.yml index ee74be04f3..3761744320 100644 --- a/bsp/qemu-vexpress-a9/.ci/attachconfig/ci.attachconfig.yml +++ b/bsp/qemu-vexpress-a9/.ci/attachconfig/ci.attachconfig.yml @@ -24,6 +24,14 @@ peripheral.LVGL: - CONFIG_RT_USING_POSIX_SOCKET=y - ONFIG_RT_USING_LWIP=y - CONFIG_BSP_USING_LVGL=y +peripheral.SPI_VIRT_LOOPBACK: + kconfig: + - CONFIG_RT_USING_UTEST=y + - CONFIG_RT_USING_UTESTCASES=y + - CONFIG_RT_UTEST_SPI_LOOPBACK=y + - CONFIG_RT_UTEST_SPI_LOOPBACK_BUS_NAME="spi_virt" + - CONFIG_RT_UTEST_SPI_VIRT_BUS=y + - CONFIG_RT_UTEST_SPI_VIRT_BUS_NAME="spi_virt" # ------ online-packages CI ------ online-packages.tools.coremark: diff --git a/components/drivers/spi/utest/Kconfig b/components/drivers/spi/utest/Kconfig index 1638916ab5..c4653c7e4c 100644 --- a/components/drivers/spi/utest/Kconfig +++ b/components/drivers/spi/utest/Kconfig @@ -15,6 +15,11 @@ if RT_UTEST_SPI_LOOPBACK config RT_UTEST_SPI_LOOPBACK_BUS_NAME string "SPI bus name for loopback test" default "spi2" + help + The real SPI bus the loopback test attaches to. On boards with a + wired MOSI/MISO loopback this must be the hardware controller name. + BSPs without a real SPI controller can instead enable + RT_UTEST_SPI_VIRT_BUS and point this name at the virtual bus. config RT_UTEST_SPI_LOOPBACK_DEV_NAME string "Temporary SPI device name for loopback test" @@ -48,6 +53,38 @@ config RT_UTEST_SPI_LOOPBACK_TIMEOUT endif +config RT_UTEST_SPI_VIRT_BUS + bool "SPI virtual loopback bus (software)" + depends on !RT_USING_DM + select RT_USING_SPI + default n + help + Compile and register a pure software SPI bus that feeds MOSI data + back to MISO. It is meant for BSPs without a real SPI controller, + such as QEMU targets, or for CI runs that have no wired loopback. + Keep it disabled on boards with a hardware SPI controller: enabling + it would add a second bus next to the real one. + + This option is only available without the device model + (RT_USING_DM). spi_virt_bus.c registers the bus with + rt_spi_bus_register(), which needs no device tree node; on a DM + and OFW based BSP without a matching DT node that registration + fails with -RT_EINVAL. Do not enable this option on BSPs that + enable RT_USING_DM. + +if RT_UTEST_SPI_VIRT_BUS + +config RT_UTEST_SPI_VIRT_BUS_NAME + string "SPI virtual bus device name" + default "spi_virt" + help + Name of the software loopback bus registered by spi_virt_bus.c. + Use a dedicated name that does not collide with any real SPI + controller of the board, and set RT_UTEST_SPI_LOOPBACK_BUS_NAME + to the same value so the test attaches to it. + +endif + config RT_UTEST_SPI_FLASH bool "SPI Flash SFUD Read/Write Test" select RT_USING_SPI diff --git a/components/drivers/spi/utest/SConscript b/components/drivers/spi/utest/SConscript index 8b7a049e5e..45cc980141 100644 --- a/components/drivers/spi/utest/SConscript +++ b/components/drivers/spi/utest/SConscript @@ -3,11 +3,15 @@ from building import * cwd = GetCurrentDir() src = [] -CPPPATH = [cwd, cwd + '/..', cwd + '/../sfud/inc'] +CPPPATH = [cwd, cwd + '/..', cwd + '/../../include', cwd + '/../sfud/inc', + cwd + '/../../../utilities/utest'] if GetDepend(['RT_UTEST_SPI_LOOPBACK']): src += ['spi_loopback_tc.c'] +if GetDepend(['RT_UTEST_SPI_VIRT_BUS']): + src += ['spi_virt_bus.c'] + if GetDepend(['RT_UTEST_SPI_FLASH']): src += ['spi_flash_tc.c'] diff --git a/components/drivers/spi/utest/spi_virt_bus.c b/components/drivers/spi/utest/spi_virt_bus.c new file mode 100644 index 0000000000..39796095a3 --- /dev/null +++ b/components/drivers/spi/utest/spi_virt_bus.c @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2006-2026, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2026-11-02 add SPI virtual loopback bus for utest + * + * A pure-software SPI bus that feeds MOSI data back to MISO. It lets the + * SPI loopback utest run on BSPs (such as qemu-vexpress-a9) which have no + * real SPI controller wired to external pins. + */ + +#include +#include +#include +#include + +#ifdef RT_UTEST_SPI_VIRT_BUS + +#ifndef RT_UTEST_SPI_VIRT_BUS_NAME +#define RT_UTEST_SPI_VIRT_BUS_NAME "spi_virt" +#endif + +#define SPI_VIRT_BUS_NAME RT_UTEST_SPI_VIRT_BUS_NAME +#define SPI_VIRT_BUS_MAX_HZ 10000000 +#define SPI_VIRT_BUS_DEFAULT_HZ 1000000 + +static struct rt_spi_bus spi_virt_bus; + +static rt_err_t spi_virt_configure(struct rt_spi_device *device, struct rt_spi_configuration *configuration) +{ + if (configuration->max_hz == 0) + { + configuration->max_hz = SPI_VIRT_BUS_DEFAULT_HZ; + } + + if (configuration->max_hz > SPI_VIRT_BUS_MAX_HZ) + { + configuration->max_hz = SPI_VIRT_BUS_MAX_HZ; + } + + device->config = *configuration; + device->config.usage_freq = configuration->max_hz; + + return RT_EOK; +} + +static rt_ssize_t spi_virt_xfer(struct rt_spi_device *device, struct rt_spi_message *message) +{ + rt_size_t i; + + if (message == RT_NULL) + { + return 0; + } + + if (message->send_buf != RT_NULL && message->recv_buf != RT_NULL) + { + rt_memcpy(message->recv_buf, message->send_buf, message->length); + } + else if (message->send_buf != RT_NULL) + { + /* TX-only: drop the data, nothing to verify. */ + } + else if (message->recv_buf != RT_NULL) + { + /* RX-only: fill with a deterministic pattern. */ + rt_uint8_t *rx = (rt_uint8_t *)message->recv_buf; + + for (i = 0; i < message->length; i++) + { + rx[i] = (rt_uint8_t)(0xA5 + i); + } + } + + return (rt_ssize_t)message->length; +} + +static const struct rt_spi_ops spi_virt_ops = { + .configure = spi_virt_configure, + .xfer = spi_virt_xfer, +}; + +static int spi_virt_bus_init(void) +{ + return rt_spi_bus_register(&spi_virt_bus, SPI_VIRT_BUS_NAME, &spi_virt_ops); +} +INIT_BOARD_EXPORT(spi_virt_bus_init); + +#endif /* RT_UTEST_SPI_VIRT_BUS */