diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_qspi.c b/bsp/stm32/libraries/HAL_Drivers/drv_qspi.c index 900da17abf..3efaf2a4a6 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_qspi.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_qspi.c @@ -324,17 +324,12 @@ rt_err_t rt_hw_qspi_device_attach(const char *bus_name, const char *device_name, qspi_device->enter_qspi_mode = enter_qspi_mode; qspi_device->exit_qspi_mode = exit_qspi_mode; qspi_device->config.qspi_dl_width = data_line_width; - qspi_device->parent.cs_pin = cs_pin; #ifdef BSP_QSPI_USING_SOFTCS - if(cs_pin != PIN_NONE) - { - rt_pin_mode(cs_pin, PIN_MODE_OUTPUT); - rt_pin_write(cs_pin, PIN_HIGH); - } -#endif - - result = rt_spi_bus_attach_device(&qspi_device->parent, device_name, bus_name, RT_NULL); + result = rt_spi_bus_attach_device_cspin(&qspi_device->parent, device_name, bus_name, cs_pin, RT_NULL); +#else + result = rt_spi_bus_attach_device_cspin(&qspi_device->parent, device_name, bus_name, PIN_NONE, RT_NULL); +#endif /* BSP_QSPI_USING_SOFTCS */ __exit: if (result != RT_EOK) diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_soft_spi.c b/bsp/stm32/libraries/HAL_Drivers/drv_soft_spi.c index 7e32c24d60..eadf5eb4f3 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_soft_spi.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_soft_spi.c @@ -36,19 +36,11 @@ rt_err_t rt_hw_softspi_device_attach(const char *bus_name, const char *device_na rt_err_t result; struct rt_spi_device *spi_device; - /* initialize the cs pin && select the slave*/ - if(cs_pin != PIN_NONE) - { - rt_pin_mode(cs_pin,PIN_MODE_OUTPUT); - rt_pin_write(cs_pin,PIN_HIGH); - } - /* attach the device to soft spi bus*/ spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device)); RT_ASSERT(spi_device != RT_NULL); - spi_device->cs_pin = cs_pin; - result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, RT_NULL); + result = rt_spi_bus_attach_device_cspin(spi_device, device_name, bus_name, cs_pin, RT_NULL); return result; } diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_spi.c b/bsp/stm32/libraries/HAL_Drivers/drv_spi.c index 7ef30d2b19..12536c7952 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_spi.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_spi.c @@ -580,15 +580,7 @@ rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device)); RT_ASSERT(spi_device != RT_NULL); - spi_device->cs_pin = cs_pin; - if(cs_pin != PIN_NONE) - { - rt_pin_mode(cs_pin, PIN_MODE_OUTPUT); - rt_pin_write(cs_pin, PIN_HIGH); - } - - result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, RT_NULL); - + result = rt_spi_bus_attach_device_cspin(spi_device, device_name, bus_name, cs_pin, RT_NULL); if (result != RT_EOK) { LOG_E("%s attach to %s faild, %d\n", device_name, bus_name, result); diff --git a/components/drivers/include/drivers/spi.h b/components/drivers/include/drivers/spi.h index cd5ebe2db6..7e3842d76e 100644 --- a/components/drivers/include/drivers/spi.h +++ b/components/drivers/include/drivers/spi.h @@ -177,6 +177,13 @@ rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device, const char *bus_name, void *user_data); +/* attach a device on SPI bus with CS pin */ +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, + void *user_data); + /** * This function takes SPI bus. * diff --git a/components/drivers/spi/spi_core.c b/components/drivers/spi/spi_core.c index 6bc875fccd..dd805337f5 100644 --- a/components/drivers/spi/spi_core.c +++ b/components/drivers/spi/spi_core.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2021, RT-Thread Development Team + * Copyright (c) 2006-2023, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * @@ -40,10 +40,11 @@ rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus, return RT_EOK; } -rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device, - const char *name, - const char *bus_name, - void *user_data) +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, + void *user_data) { rt_err_t result; rt_device_t bus; @@ -59,9 +60,15 @@ rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device, if (result != RT_EOK) return result; + if(cs_pin != PIN_NONE) + { + rt_pin_mode(cs_pin, PIN_MODE_OUTPUT); + rt_pin_write(cs_pin, PIN_HIGH); + } + rt_memset(&device->config, 0, sizeof(device->config)); device->parent.user_data = user_data; - + device->cs_pin = cs_pin; return RT_EOK; } @@ -69,6 +76,14 @@ rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device, return -RT_ERROR; } +rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device, + const char *name, + const char *bus_name, + void *user_data) +{ + 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) {