From e165fcca9481b76b1d7bb356f7298d3b52f084d0 Mon Sep 17 00:00:00 2001 From: GuEe-GUI <2991707448@qq.com> Date: Tue, 8 Jul 2025 18:22:44 +0800 Subject: [PATCH 1/4] Make a priv data read API for probed SPI device Signed-off-by: GuEe-GUI <2991707448@qq.com> --- components/drivers/include/drivers/dev_spi.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/components/drivers/include/drivers/dev_spi.h b/components/drivers/include/drivers/dev_spi.h index c5de588d7c4..11d58c08be7 100644 --- a/components/drivers/include/drivers/dev_spi.h +++ b/components/drivers/include/drivers/dev_spi.h @@ -313,6 +313,11 @@ rt_err_t rt_spi_driver_register(struct rt_spi_driver *driver); rt_err_t rt_spi_device_register(struct rt_spi_device *device); #define RT_SPI_DRIVER_EXPORT(driver) RT_DRIVER_EXPORT(driver, spi, BUILIN) + +rt_inline const void *rt_spi_device_id_data(struct rt_spi_device *device) +{ + return device->id ? device->id->data : (device->ofw_id ? device->ofw_id->data : RT_NULL); +} #endif /* RT_USING_DM */ /** From 7735d130e46dc8c4980ffad2e2fb8ea9d3487455 Mon Sep 17 00:00:00 2001 From: GuEe-GUI <2991707448@qq.com> Date: Tue, 8 Jul 2025 18:24:26 +0800 Subject: [PATCH 2/4] Fixup the SPI device pre-alloc May the bus is a QSPI, we should check the mode before alloc the SPI device. The raw API the SPI mode default but the SPI DM bus probe run before the SPI mode confirm, we should move the SPI register API alone, then confirm the bus mode before register. Signed-off-by: GuEe-GUI <2991707448@qq.com> --- components/drivers/spi/dev_qspi_core.c | 16 +++++++--------- components/drivers/spi/dev_spi_bus.c | 14 +++++++++++++- components/drivers/spi/dev_spi_core.c | 18 +++++++++++++----- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/components/drivers/spi/dev_qspi_core.c b/components/drivers/spi/dev_qspi_core.c index f076945b643..f90834b7c51 100644 --- a/components/drivers/spi/dev_qspi_core.c +++ b/components/drivers/spi/dev_qspi_core.c @@ -10,6 +10,10 @@ #include "drivers/dev_spi.h" +extern rt_err_t spi_bus_register(struct rt_spi_bus *bus, + const char *name, + const struct rt_spi_ops *ops); + rt_err_t rt_qspi_configure(struct rt_qspi_device *device, struct rt_qspi_configuration *cfg) { RT_ASSERT(device != RT_NULL); @@ -67,16 +71,10 @@ rt_err_t rt_qspi_configure(struct rt_qspi_device *device, struct rt_qspi_configu rt_err_t rt_qspi_bus_register(struct rt_spi_bus *bus, const char *name, const struct rt_spi_ops *ops) { - rt_err_t result = RT_EOK; + /* set SPI bus to qspi modes */ + bus->mode = RT_SPI_BUS_MODE_QSPI; - result = rt_spi_bus_register(bus, name, ops); - if(result == RT_EOK) - { - /* set SPI bus to qspi modes */ - bus->mode = RT_SPI_BUS_MODE_QSPI; - } - - return result; + return spi_bus_register(bus, name, ops); } rt_size_t rt_qspi_transfer_message(struct rt_qspi_device *device, struct rt_qspi_message *message) diff --git a/components/drivers/spi/dev_spi_bus.c b/components/drivers/spi/dev_spi_bus.c index cb40de9b8dc..ed1f29eac8a 100644 --- a/components/drivers/spi/dev_spi_bus.c +++ b/components/drivers/spi/dev_spi_bus.c @@ -34,7 +34,19 @@ void spi_bus_scan_devices(struct rt_spi_bus *bus) continue; } - spi_dev = rt_calloc(1, sizeof(*spi_dev)); + if ((bus->mode & RT_SPI_BUS_MODE_SPI) == RT_SPI_BUS_MODE_SPI) + { + spi_dev = rt_calloc(1, sizeof(struct rt_spi_device)); + } + else if ((bus->mode & RT_SPI_BUS_MODE_QSPI) == RT_SPI_BUS_MODE_QSPI) + { + spi_dev = rt_calloc(1, sizeof(struct rt_qspi_device)); + } + else + { + LOG_E("Unknown bus mode = %x", bus->mode); + RT_ASSERT(0); + } if (!spi_dev) { diff --git a/components/drivers/spi/dev_spi_core.c b/components/drivers/spi/dev_spi_core.c index d830989947b..53bb7284faa 100644 --- a/components/drivers/spi/dev_spi_core.c +++ b/components/drivers/spi/dev_spi_core.c @@ -26,9 +26,9 @@ extern rt_err_t rt_spi_bus_device_init(struct rt_spi_bus *bus, const char *name); extern rt_err_t rt_spidev_device_init(struct rt_spi_device *dev, const char *name); -rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus, - const char *name, - const struct rt_spi_ops *ops) +rt_err_t spi_bus_register(struct rt_spi_bus *bus, + const char *name, + const struct rt_spi_ops *ops) { rt_err_t result; @@ -42,8 +42,6 @@ rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus, bus->ops = ops; /* initialize owner */ bus->owner = RT_NULL; - /* set bus mode */ - bus->mode = RT_SPI_BUS_MODE_SPI; #ifdef RT_USING_DM if (!bus->slave) @@ -77,6 +75,16 @@ rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus, return RT_EOK; } +rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus, + const char *name, + const struct rt_spi_ops *ops) +{ + /* set bus mode */ + bus->mode = RT_SPI_BUS_MODE_SPI; + + return spi_bus_register(bus, name, ops); +} + rt_err_t rt_spi_bus_attach_device_cspin(struct rt_spi_device *device, const char *name, const char *bus_name, From b14468ab6f68eb36d2aad13acf7704f93d5bac19 Mon Sep 17 00:00:00 2001 From: GuEe-GUI <2991707448@qq.com> Date: Tue, 8 Jul 2025 18:27:39 +0800 Subject: [PATCH 3/4] Fixup the QSPI transfer result check The result could < 0 (with error number) now. Signed-off-by: GuEe-GUI <2991707448@qq.com> --- components/drivers/include/drivers/dev_spi.h | 6 +++--- components/drivers/spi/dev_qspi_core.c | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/components/drivers/include/drivers/dev_spi.h b/components/drivers/include/drivers/dev_spi.h index 11d58c08be7..8aeab22b86f 100644 --- a/components/drivers/include/drivers/dev_spi.h +++ b/components/drivers/include/drivers/dev_spi.h @@ -603,7 +603,7 @@ rt_err_t rt_qspi_bus_register(struct rt_spi_bus *bus, const char *name, const st * * @return the actual length of transmitted. */ -rt_size_t rt_qspi_transfer_message(struct rt_qspi_device *device, struct rt_qspi_message *message); +rt_ssize_t rt_qspi_transfer_message(struct rt_qspi_device *device, struct rt_qspi_message *message); /** * @brief This function can send data then receive data from QSPI device @@ -616,7 +616,7 @@ rt_size_t rt_qspi_transfer_message(struct rt_qspi_device *device, struct rt_qsp * * @return the status of transmit. */ -rt_err_t rt_qspi_send_then_recv(struct rt_qspi_device *device, const void *send_buf, rt_size_t send_length,void *recv_buf, rt_size_t recv_length); +rt_ssize_t rt_qspi_send_then_recv(struct rt_qspi_device *device, const void *send_buf, rt_size_t send_length,void *recv_buf, rt_size_t recv_length); /** * @brief This function can send data to QSPI device @@ -627,7 +627,7 @@ rt_err_t rt_qspi_send_then_recv(struct rt_qspi_device *device, const void *send_ * * @return the status of transmit. */ -rt_err_t rt_qspi_send(struct rt_qspi_device *device, const void *send_buf, rt_size_t length); +rt_ssize_t rt_qspi_send(struct rt_qspi_device *device, const void *send_buf, rt_size_t length); #ifdef __cplusplus } diff --git a/components/drivers/spi/dev_qspi_core.c b/components/drivers/spi/dev_qspi_core.c index f90834b7c51..aa536af4dfd 100644 --- a/components/drivers/spi/dev_qspi_core.c +++ b/components/drivers/spi/dev_qspi_core.c @@ -77,9 +77,9 @@ rt_err_t rt_qspi_bus_register(struct rt_spi_bus *bus, const char *name, const st return spi_bus_register(bus, name, ops); } -rt_size_t rt_qspi_transfer_message(struct rt_qspi_device *device, struct rt_qspi_message *message) +rt_ssize_t rt_qspi_transfer_message(struct rt_qspi_device *device, struct rt_qspi_message *message) { - rt_err_t result; + rt_ssize_t result; RT_ASSERT(device != RT_NULL); RT_ASSERT(message != RT_NULL); @@ -128,7 +128,7 @@ rt_size_t rt_qspi_transfer_message(struct rt_qspi_device *device, struct rt_qsp return result; } -rt_err_t rt_qspi_send_then_recv(struct rt_qspi_device *device, const void *send_buf, rt_size_t send_length, void *recv_buf, rt_size_t recv_length) +rt_ssize_t rt_qspi_send_then_recv(struct rt_qspi_device *device, const void *send_buf, rt_size_t send_length, void *recv_buf, rt_size_t recv_length) { RT_ASSERT(send_buf); RT_ASSERT(recv_buf); @@ -137,7 +137,7 @@ rt_err_t rt_qspi_send_then_recv(struct rt_qspi_device *device, const void *send_ struct rt_qspi_message message; unsigned char *ptr = (unsigned char *)send_buf; rt_size_t count = 0; - rt_err_t result = 0; + rt_ssize_t result = 0; message.instruction.content = ptr[0]; message.instruction.qspi_lines = 1; @@ -206,7 +206,7 @@ rt_err_t rt_qspi_send_then_recv(struct rt_qspi_device *device, const void *send_ { result = -RT_EIO; } - else + else if (result > 0) { result = recv_length; } @@ -214,7 +214,7 @@ rt_err_t rt_qspi_send_then_recv(struct rt_qspi_device *device, const void *send_ return result; } -rt_err_t rt_qspi_send(struct rt_qspi_device *device, const void *send_buf, rt_size_t length) +rt_ssize_t rt_qspi_send(struct rt_qspi_device *device, const void *send_buf, rt_size_t length) { RT_ASSERT(send_buf); RT_ASSERT(length != 0); @@ -222,7 +222,7 @@ rt_err_t rt_qspi_send(struct rt_qspi_device *device, const void *send_buf, rt_si struct rt_qspi_message message; unsigned char *ptr = (unsigned char *)send_buf; rt_size_t count = 0; - rt_err_t result = 0; + rt_ssize_t result = 0; message.instruction.content = ptr[0]; message.instruction.qspi_lines = 1; @@ -292,7 +292,7 @@ rt_err_t rt_qspi_send(struct rt_qspi_device *device, const void *send_buf, rt_si { result = -RT_EIO; } - else + else if (result > 0) { result = length; } From 39d390d01f7adc6cdc25a640516505907a2533b7 Mon Sep 17 00:00:00 2001 From: GuEe-GUI <2991707448@qq.com> Date: Tue, 8 Jul 2025 18:32:05 +0800 Subject: [PATCH 4/4] Update the SPI Kconfig for DM Signed-off-by: GuEe-GUI <2991707448@qq.com> --- components/drivers/spi/Kconfig | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/components/drivers/spi/Kconfig b/components/drivers/spi/Kconfig index 4b95a18bfed..4a61ee8a8fc 100644 --- a/components/drivers/spi/Kconfig +++ b/components/drivers/spi/Kconfig @@ -1,4 +1,4 @@ -config RT_USING_SPI +menuconfig RT_USING_SPI bool "Using SPI Bus/Device device drivers" default n @@ -235,3 +235,7 @@ config RT_USING_SPI select RT_USING_LWIP default n endif + +if RT_USING_DM && RT_USING_SPI + osource "$(SOC_DM_SPI_DIR)/Kconfig" +endif