From 02eaf76d7b9c6ee0f968465cd8837fe5dfa58a83 Mon Sep 17 00:00:00 2001 From: yangpeng Date: Tue, 6 Feb 2024 13:33:07 +0800 Subject: [PATCH] =?UTF-8?q?[qspi]=E4=BF=AE=E5=A4=8Dqspi=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=9C=AA=E7=94=9F=E6=95=88=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/include/drivers/spi.h | 5 ++- components/drivers/spi/qspi_core.c | 30 +++++++++++------- components/drivers/spi/spi_core.c | 40 ++++++++++++++---------- 3 files changed, 45 insertions(+), 30 deletions(-) diff --git a/components/drivers/include/drivers/spi.h b/components/drivers/include/drivers/spi.h index 8ba269f94b..b35c7b30b3 100644 --- a/components/drivers/include/drivers/spi.h +++ b/components/drivers/include/drivers/spi.h @@ -181,9 +181,12 @@ rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device, rt_err_t rt_spi_bus_attach_device_cspin(struct rt_spi_device *device, const char *name, const char *bus_name, - rt_base_t cs_pin, + rt_base_t cs_pin, void *user_data); +/* re-configure SPI bus */ +rt_err_t rt_spi_bus_configure(struct rt_spi_device *device); + /** * This function takes SPI bus. * diff --git a/components/drivers/spi/qspi_core.c b/components/drivers/spi/qspi_core.c index 010b427f5f..5d17a085e0 100644 --- a/components/drivers/spi/qspi_core.c +++ b/components/drivers/spi/qspi_core.c @@ -15,21 +15,27 @@ rt_err_t rt_qspi_configure(struct rt_qspi_device *device, struct rt_qspi_configu RT_ASSERT(device != RT_NULL); RT_ASSERT(cfg != RT_NULL); - struct rt_qspi_device *qspi_device = (struct rt_qspi_device *)device; - rt_err_t result = RT_EOK; + /* If the configurations are the same, we don't need to set again. */ + if (device->config.medium_size == cfg->medium_size && + device->config.ddr_mode == cfg->ddr_mode && + device->config.qspi_dl_width == cfg->qspi_dl_width && + device->config.parent.data_width == cfg->parent.data_width && + device->config.parent.mode == (cfg->parent.mode & RT_SPI_MODE_MASK) && + device->config.parent.max_hz == cfg->parent.max_hz) + { + return RT_EOK; + } /* copy configuration items */ - qspi_device->config.parent.mode = cfg->parent.mode; - qspi_device->config.parent.max_hz = cfg->parent.max_hz; - qspi_device->config.parent.data_width = cfg->parent.data_width; - qspi_device->config.parent.reserved = cfg->parent.reserved; - qspi_device->config.medium_size = cfg->medium_size; - qspi_device->config.ddr_mode = cfg->ddr_mode; - qspi_device->config.qspi_dl_width = cfg->qspi_dl_width; + device->config.parent.mode = cfg->parent.mode; + device->config.parent.max_hz = cfg->parent.max_hz; + device->config.parent.data_width = cfg->parent.data_width; + device->config.parent.reserved = cfg->parent.reserved; + device->config.medium_size = cfg->medium_size; + device->config.ddr_mode = cfg->ddr_mode; + device->config.qspi_dl_width = cfg->qspi_dl_width; - result = rt_spi_configure(&device->parent, &cfg->parent); - - return result; + return rt_spi_bus_configure(&device->parent); } rt_err_t rt_qspi_bus_register(struct rt_spi_bus *bus, const char *name, const struct rt_spi_ops *ops) diff --git a/components/drivers/spi/spi_core.c b/components/drivers/spi/spi_core.c index 3209a54db9..295b7f0296 100644 --- a/components/drivers/spi/spi_core.c +++ b/components/drivers/spi/spi_core.c @@ -87,26 +87,10 @@ rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device, return rt_spi_bus_attach_device_cspin(device, name, bus_name, PIN_NONE, user_data); } -rt_err_t rt_spi_configure(struct rt_spi_device *device, - struct rt_spi_configuration *cfg) +rt_err_t rt_spi_bus_configure(struct rt_spi_device *device) { rt_err_t result = -RT_ERROR; - RT_ASSERT(device != RT_NULL); - - /* If the configurations are the same, we don't need to set again. */ - if(device->config.data_width == cfg->data_width && - device->config.mode == (cfg->mode & RT_SPI_MODE_MASK) && - device->config.max_hz == cfg->max_hz) - { - return RT_EOK; - } - - /* set configuration */ - device->config.data_width = cfg->data_width; - device->config.mode = cfg->mode & RT_SPI_MODE_MASK; - device->config.max_hz = cfg->max_hz; - /* reset the CS pin */ if (device->cs_pin != PIN_NONE) { @@ -144,6 +128,28 @@ rt_err_t rt_spi_configure(struct rt_spi_device *device, return result; } +rt_err_t rt_spi_configure(struct rt_spi_device *device, + struct rt_spi_configuration *cfg) +{ + RT_ASSERT(device != RT_NULL); + RT_ASSERT(cfg != RT_NULL); + + /* If the configurations are the same, we don't need to set again. */ + if (device->config.data_width == cfg->data_width && + device->config.mode == (cfg->mode & RT_SPI_MODE_MASK) && + device->config.max_hz == cfg->max_hz) + { + return RT_EOK; + } + + /* set configuration */ + device->config.data_width = cfg->data_width; + device->config.mode = cfg->mode & RT_SPI_MODE_MASK; + device->config.max_hz = cfg->max_hz; + + return rt_spi_bus_configure(device); +} + rt_err_t rt_spi_send_then_send(struct rt_spi_device *device, const void *send_buf1, rt_size_t send_length1,