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
This commit is contained in:
Chen Moushun authored and GitHub committed 2026-11-08 14:48:35 +08:00
1 parent 2b0b1044c0
commit 37d8b8d1d0
6 files changed
+154 -1

No files matched your search

+10
View File
@@ -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"
+2
View File
@@ -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":"" }
@@ -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:
+37
View File
@@ -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
+5 -1
View File
@@ -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']
@@ -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 <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <drivers/dev_spi.h>
#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 */