From a1923073e9e17003d5d6cc953ec464d0d8e918f8 Mon Sep 17 00:00:00 2001 From: liYony <941843540@qq.com> Date: Fri, 6 Jan 2023 13:42:09 +0800 Subject: [PATCH 1/3] =?UTF-8?q?[spi]attach=20=E7=89=87=E9=80=89=E5=BC=95?= =?UTF-8?q?=E8=84=9A=E4=BE=9D=E8=B5=96pin=E6=A1=86=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bsp/stm32/libraries/HAL_Drivers/drv_spi.c | 46 ++++++++++++----------- bsp/stm32/libraries/HAL_Drivers/drv_spi.h | 8 +--- components/drivers/Kconfig | 5 +++ components/drivers/include/drivers/spi.h | 4 ++ components/drivers/spi/spi_core.c | 3 ++ 5 files changed, 37 insertions(+), 29 deletions(-) diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_spi.c b/bsp/stm32/libraries/HAL_Drivers/drv_spi.c index 201c653dd15..992efb64ceb 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_spi.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_spi.c @@ -85,6 +85,11 @@ static rt_err_t stm32_spi_init(struct stm32_spi *spi_drv, struct rt_spi_configur RT_ASSERT(spi_drv != RT_NULL); RT_ASSERT(cfg != RT_NULL); +#ifdef RT_USING_SPI_SOFT_CS + rt_pin_mode(cfg->cs_pin, PIN_MODE_OUTPUT); + rt_pin_write(cfg->cs_pin, PIN_HIGH); +#endif + SPI_HandleTypeDef *spi_handle = &spi_drv->handle; if (cfg->mode & RT_SPI_SLAVE) @@ -291,20 +296,20 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message * RT_ASSERT(device != RT_NULL); RT_ASSERT(device->bus != RT_NULL); - RT_ASSERT(device->bus->parent.user_data != RT_NULL); RT_ASSERT(message != RT_NULL); struct stm32_spi *spi_drv = rt_container_of(device->bus, struct stm32_spi, spi_bus); SPI_HandleTypeDef *spi_handle = &spi_drv->handle; - struct stm32_hw_spi_cs *cs = device->parent.user_data; +#ifdef RT_USING_SPI_SOFT_CS if (message->cs_take && !(device->config.mode & RT_SPI_NO_CS)) { if (device->config.mode & RT_SPI_CS_HIGH) - HAL_GPIO_WritePin(cs->GPIOx, cs->GPIO_Pin, GPIO_PIN_SET); + rt_pin_write(device->config.cs_pin, PIN_HIGH); else - HAL_GPIO_WritePin(cs->GPIOx, cs->GPIO_Pin, GPIO_PIN_RESET); + rt_pin_write(device->config.cs_pin, PIN_LOW); } +#endif LOG_D("%s transfer prepare and start", spi_drv->config->bus_name); LOG_D("%s sendbuf: %X, recvbuf: %X, length: %d", @@ -432,13 +437,15 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message * #endif /* SOC_SERIES_STM32H7 || SOC_SERIES_STM32F7 */ } +#ifdef RT_USING_SPI_SOFT_CS if (message->cs_release && !(device->config.mode & RT_SPI_NO_CS)) { if (device->config.mode & RT_SPI_CS_HIGH) - HAL_GPIO_WritePin(cs->GPIOx, cs->GPIO_Pin, GPIO_PIN_RESET); + rt_pin_write(device->config.cs_pin, PIN_LOW); else - HAL_GPIO_WritePin(cs->GPIOx, cs->GPIO_Pin, GPIO_PIN_SET); + rt_pin_write(device->config.cs_pin, PIN_HIGH); } +#endif return message->length; } @@ -570,33 +577,28 @@ static int rt_hw_spi_bus_init(void) /** * Attach the spi device to SPI bus, this function must be used after initialization. */ -rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, GPIO_TypeDef *cs_gpiox, uint16_t cs_gpio_pin) +rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_base_t cs_pin) { RT_ASSERT(bus_name != RT_NULL); RT_ASSERT(device_name != RT_NULL); rt_err_t result; struct rt_spi_device *spi_device; - struct stm32_hw_spi_cs *cs_pin; - - /* initialize the cs pin && select the slave*/ - GPIO_InitTypeDef GPIO_Initure; - GPIO_Initure.Pin = cs_gpio_pin; - GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_Initure.Pull = GPIO_PULLUP; - GPIO_Initure.Speed = GPIO_SPEED_FREQ_HIGH; - HAL_GPIO_Init(cs_gpiox, &GPIO_Initure); - HAL_GPIO_WritePin(cs_gpiox, cs_gpio_pin, GPIO_PIN_SET); + struct rt_spi_configuration spi_cfg; /* attach the device to spi bus*/ spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device)); RT_ASSERT(spi_device != RT_NULL); - cs_pin = (struct stm32_hw_spi_cs *)rt_malloc(sizeof(struct stm32_hw_spi_cs)); - RT_ASSERT(cs_pin != RT_NULL); - cs_pin->GPIOx = cs_gpiox; - cs_pin->GPIO_Pin = cs_gpio_pin; - result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin); + result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, RT_NULL); + +#ifdef RT_USING_SPI_SOFT_CS + spi_cfg.cs_pin = cs_pin; + result = rt_spi_configure(spi_device, &spi_cfg); + rt_pin_mode(spi_cfg.cs_pin, PIN_MODE_OUTPUT); + rt_pin_write(spi_cfg.cs_pin, PIN_HIGH); +#endif + if (result != RT_EOK) { LOG_E("%s attach to %s faild, %d\n", device_name, bus_name, result); diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_spi.h b/bsp/stm32/libraries/HAL_Drivers/drv_spi.h index bdb5e29ecd3..7c8006080c2 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_spi.h +++ b/bsp/stm32/libraries/HAL_Drivers/drv_spi.h @@ -22,18 +22,12 @@ extern "C" { #endif -rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, GPIO_TypeDef* cs_gpiox, uint16_t cs_gpio_pin); +rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_base_t cs_pin); #ifdef __cplusplus } #endif -struct stm32_hw_spi_cs -{ - GPIO_TypeDef* GPIOx; - uint16_t GPIO_Pin; -}; - struct stm32_spi_config { SPI_TypeDef *Instance; diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index 52d735c9416..4be1e497cb7 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -276,6 +276,11 @@ config RT_USING_SPI default n if RT_USING_SPI + config RT_USING_SPI_SOFT_CS + select RT_USING_PIN + bool "Use GPIO to simulate SPI chip select" + default n + config RT_USING_SPI_BITOPS select RT_USING_PIN bool "Use GPIO to simulate SPI" diff --git a/components/drivers/include/drivers/spi.h b/components/drivers/include/drivers/spi.h index 0bcac3aac04..f2e9b7eb2a3 100644 --- a/components/drivers/include/drivers/spi.h +++ b/components/drivers/include/drivers/spi.h @@ -15,6 +15,7 @@ #include #include +#include #ifdef __cplusplus extern "C"{ @@ -78,6 +79,9 @@ struct rt_spi_configuration rt_uint8_t mode; rt_uint8_t data_width; rt_uint16_t reserved; +#ifdef RT_USING_SPI_SOFT_CS + rt_base_t cs_pin; +#endif rt_uint32_t max_hz; }; diff --git a/components/drivers/spi/spi_core.c b/components/drivers/spi/spi_core.c index 6bc875fccdc..f7a5dfc688c 100644 --- a/components/drivers/spi/spi_core.c +++ b/components/drivers/spi/spi_core.c @@ -80,6 +80,9 @@ rt_err_t rt_spi_configure(struct rt_spi_device *device, device->config.data_width = cfg->data_width; device->config.mode = cfg->mode & RT_SPI_MODE_MASK ; device->config.max_hz = cfg->max_hz ; +#ifdef RT_USING_SPI_SOFT_CS + device->config.cs_pin = cfg->cs_pin ; +#endif if (device->bus != RT_NULL) { From 7e406f8e2cd3cf14ad06d54177e5a771cf7f7d2d Mon Sep 17 00:00:00 2001 From: liYony <941843540@qq.com> Date: Fri, 6 Jan 2023 13:55:31 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=B8=80=E4=BA=9B=E5=B0=8F=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bsp/stm32/libraries/HAL_Drivers/drv_spi.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_spi.c b/bsp/stm32/libraries/HAL_Drivers/drv_spi.c index 992efb64ceb..df1c5c16804 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_spi.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_spi.c @@ -595,8 +595,6 @@ rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, #ifdef RT_USING_SPI_SOFT_CS spi_cfg.cs_pin = cs_pin; result = rt_spi_configure(spi_device, &spi_cfg); - rt_pin_mode(spi_cfg.cs_pin, PIN_MODE_OUTPUT); - rt_pin_write(spi_cfg.cs_pin, PIN_HIGH); #endif if (result != RT_EOK) From f7491375cf1d32f230b6804eb33a3850bcf2fea2 Mon Sep 17 00:00:00 2001 From: liYony <941843540@qq.com> Date: Sun, 8 Jan 2023 08:49:05 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9attach=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bsp/stm32/libraries/HAL_Drivers/drv_spi.c | 14 ++------------ bsp/stm32/libraries/HAL_Drivers/drv_spi.h | 2 +- components/drivers/Kconfig | 5 ----- components/drivers/include/drivers/spi.h | 2 -- components/drivers/spi/spi_core.c | 2 -- 5 files changed, 3 insertions(+), 22 deletions(-) diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_spi.c b/bsp/stm32/libraries/HAL_Drivers/drv_spi.c index df1c5c16804..c4b19801812 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_spi.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_spi.c @@ -85,10 +85,8 @@ static rt_err_t stm32_spi_init(struct stm32_spi *spi_drv, struct rt_spi_configur RT_ASSERT(spi_drv != RT_NULL); RT_ASSERT(cfg != RT_NULL); -#ifdef RT_USING_SPI_SOFT_CS rt_pin_mode(cfg->cs_pin, PIN_MODE_OUTPUT); rt_pin_write(cfg->cs_pin, PIN_HIGH); -#endif SPI_HandleTypeDef *spi_handle = &spi_drv->handle; @@ -301,7 +299,6 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message * struct stm32_spi *spi_drv = rt_container_of(device->bus, struct stm32_spi, spi_bus); SPI_HandleTypeDef *spi_handle = &spi_drv->handle; -#ifdef RT_USING_SPI_SOFT_CS if (message->cs_take && !(device->config.mode & RT_SPI_NO_CS)) { if (device->config.mode & RT_SPI_CS_HIGH) @@ -309,7 +306,6 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message * else rt_pin_write(device->config.cs_pin, PIN_LOW); } -#endif LOG_D("%s transfer prepare and start", spi_drv->config->bus_name); LOG_D("%s sendbuf: %X, recvbuf: %X, length: %d", @@ -437,7 +433,6 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message * #endif /* SOC_SERIES_STM32H7 || SOC_SERIES_STM32F7 */ } -#ifdef RT_USING_SPI_SOFT_CS if (message->cs_release && !(device->config.mode & RT_SPI_NO_CS)) { if (device->config.mode & RT_SPI_CS_HIGH) @@ -445,7 +440,6 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message * else rt_pin_write(device->config.cs_pin, PIN_HIGH); } -#endif return message->length; } @@ -577,14 +571,13 @@ static int rt_hw_spi_bus_init(void) /** * Attach the spi device to SPI bus, this function must be used after initialization. */ -rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_base_t cs_pin) +rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, struct rt_spi_configuration *cfg) { RT_ASSERT(bus_name != RT_NULL); RT_ASSERT(device_name != RT_NULL); rt_err_t result; struct rt_spi_device *spi_device; - struct rt_spi_configuration spi_cfg; /* attach the device to spi bus*/ spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device)); @@ -592,10 +585,7 @@ rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, RT_NULL); -#ifdef RT_USING_SPI_SOFT_CS - spi_cfg.cs_pin = cs_pin; - result = rt_spi_configure(spi_device, &spi_cfg); -#endif + result = rt_spi_configure(spi_device, cfg); if (result != RT_EOK) { diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_spi.h b/bsp/stm32/libraries/HAL_Drivers/drv_spi.h index 7c8006080c2..16976bb36ea 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_spi.h +++ b/bsp/stm32/libraries/HAL_Drivers/drv_spi.h @@ -22,7 +22,7 @@ extern "C" { #endif -rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_base_t cs_pin); +rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, struct rt_spi_configuration *cfg); #ifdef __cplusplus } diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index 4be1e497cb7..52d735c9416 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -276,11 +276,6 @@ config RT_USING_SPI default n if RT_USING_SPI - config RT_USING_SPI_SOFT_CS - select RT_USING_PIN - bool "Use GPIO to simulate SPI chip select" - default n - config RT_USING_SPI_BITOPS select RT_USING_PIN bool "Use GPIO to simulate SPI" diff --git a/components/drivers/include/drivers/spi.h b/components/drivers/include/drivers/spi.h index f2e9b7eb2a3..2cce830cd6f 100644 --- a/components/drivers/include/drivers/spi.h +++ b/components/drivers/include/drivers/spi.h @@ -79,9 +79,7 @@ struct rt_spi_configuration rt_uint8_t mode; rt_uint8_t data_width; rt_uint16_t reserved; -#ifdef RT_USING_SPI_SOFT_CS rt_base_t cs_pin; -#endif rt_uint32_t max_hz; }; diff --git a/components/drivers/spi/spi_core.c b/components/drivers/spi/spi_core.c index f7a5dfc688c..7aa09c9010f 100644 --- a/components/drivers/spi/spi_core.c +++ b/components/drivers/spi/spi_core.c @@ -80,9 +80,7 @@ rt_err_t rt_spi_configure(struct rt_spi_device *device, device->config.data_width = cfg->data_width; device->config.mode = cfg->mode & RT_SPI_MODE_MASK ; device->config.max_hz = cfg->max_hz ; -#ifdef RT_USING_SPI_SOFT_CS device->config.cs_pin = cfg->cs_pin ; -#endif if (device->bus != RT_NULL) {