From 4d57b60cc8d04f78d3ee475bb0d8df89cd8fa06d Mon Sep 17 00:00:00 2001 From: Yuqiang Wang Date: Wed, 10 Apr 2024 06:33:29 +0800 Subject: [PATCH] [driver][soft-spi] spi device driver framework adds pin init function function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为什么提交这份PR (why to submit this PR) ps:在设备初始化阶段不应该存在对硬件的操作 你的解决方案是什么 (what is your solution) ps:添加一个pin_init函数,在spi设备使用时调用该函数来完成硬件引脚的状态初始化,而不是在设备初始化阶段对硬件进行操作,已经在瑞萨HMI上通过验证 --- .../lpc55sxx/Libraries/drivers/drv_soft_spi.c | 18 ++++++++++++---- bsp/raspberry-pico/drivers/drv_soft_spi.c | 21 ++++++++++++++----- .../libraries/HAL_Drivers/drv_soft_spi.c | 18 ++++++++++++---- .../HAL_Drivers/drivers/drv_soft_spi.c | 18 ++++++++++++---- .../Libraries/ch32_drivers/drv_soft_spi.c | 18 ++++++++++++---- components/drivers/spi/spi-bit-ops.c | 5 +++++ components/drivers/spi/spi-bit-ops.h | 1 + 7 files changed, 78 insertions(+), 21 deletions(-) diff --git a/bsp/nxp/lpc/lpc55sxx/Libraries/drivers/drv_soft_spi.c b/bsp/nxp/lpc/lpc55sxx/Libraries/drivers/drv_soft_spi.c index 9a4d37fbcb..e80385a1a2 100644 --- a/bsp/nxp/lpc/lpc55sxx/Libraries/drivers/drv_soft_spi.c +++ b/bsp/nxp/lpc/lpc55sxx/Libraries/drivers/drv_soft_spi.c @@ -25,6 +25,8 @@ static struct lpc_soft_spi_config soft_spi_config[] = #endif }; +static struct lpc_soft_spi spi_obj[sizeof(soft_spi_config) / sizeof(soft_spi_config[0])]; + /** * Attach the spi device to soft SPI bus, this function must be used after initialization. */ @@ -179,9 +181,20 @@ static void lpc_udelay(rt_uint32_t us) } } +static void lpc_pin_init(void) +{ + rt_size_t obj_num = sizeof(spi_obj) / sizeof(struct lpc_soft_spi); + + for(rt_size_t i; i < obj_num; i++) + { + lpc_spi_gpio_init(&spi_obj[i]); + } +} + static struct rt_spi_bit_ops lpc_soft_spi_ops = { .data = RT_NULL, + .pin_init = lpc_pin_init, .tog_sclk = lpc_tog_sclk, .set_sclk = lpc_set_sclk, .set_mosi = lpc_set_mosi, @@ -195,20 +208,17 @@ static struct rt_spi_bit_ops lpc_soft_spi_ops = .delay_us = 1, }; -static struct lpc_soft_spi spi_obj[sizeof(soft_spi_config) / sizeof(soft_spi_config[0])]; - /* Soft SPI initialization function */ int rt_hw_softspi_init(void) { rt_size_t obj_num = sizeof(spi_obj) / sizeof(struct lpc_soft_spi); rt_err_t result; - for (int i = 0; i < obj_num; i++) + for (rt_size_t i = 0; i < obj_num; i++) { lpc_soft_spi_ops.data = (void *)&soft_spi_config[i]; spi_obj[i].spi.ops = &lpc_soft_spi_ops; spi_obj[i].cfg = (void *)&soft_spi_config[i]; - lpc_spi_gpio_init(&spi_obj[i]); result = rt_spi_bit_add_bus(&spi_obj[i].spi, soft_spi_config[i].bus_name, &lpc_soft_spi_ops); RT_ASSERT(result == RT_EOK); } diff --git a/bsp/raspberry-pico/drivers/drv_soft_spi.c b/bsp/raspberry-pico/drivers/drv_soft_spi.c index e1addb2d90..11ac0db7af 100644 --- a/bsp/raspberry-pico/drivers/drv_soft_spi.c +++ b/bsp/raspberry-pico/drivers/drv_soft_spi.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2023, RT-Thread Development Team + * Copyright (c) 2006-2024, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * @@ -24,6 +24,8 @@ static struct pico_soft_spi_config soft_spi_config[] = #endif }; +static struct pico_soft_spi spi_obj[sizeof(soft_spi_config) / sizeof(soft_spi_config[0])]; + /** * Attach the spi device to soft SPI bus, this function must be used after initialization. */ @@ -153,9 +155,20 @@ static void pico_udelay(rt_uint32_t us) busy_wait_us_32(us); } +static void pico_pin_init(void) +{ + rt_size_t obj_num = sizeof(spi_obj) / sizeof(struct pico_soft_spi); + + for(rt_size_t i; i < obj_num; i++) + { + pico_spi_gpio_init(&spi_obj[i]); + } +} + static struct rt_spi_bit_ops pico_soft_spi_ops = { .data = RT_NULL, + .pin_init = pico_pin_init, .tog_sclk = pico_tog_sclk, .set_sclk = pico_set_sclk, .set_mosi = pico_set_mosi, @@ -169,20 +182,18 @@ static struct rt_spi_bit_ops pico_soft_spi_ops = .delay_us = 1, }; -static struct pico_soft_spi spi_obj[sizeof(soft_spi_config) / sizeof(soft_spi_config[0])]; - /* Soft SPI initialization function */ int rt_hw_softspi_init(void) { rt_size_t obj_num = sizeof(spi_obj) / sizeof(struct pico_soft_spi); rt_err_t result; - for (int i = 0; i < obj_num; i++) + for (rt_size_t i = 0; i < obj_num; i++) { pico_soft_spi_ops.data = (void *)&soft_spi_config[i]; spi_obj[i].spi.ops = &pico_soft_spi_ops; spi_obj[i].cfg = (void *)&soft_spi_config[i]; - pico_spi_gpio_init(&spi_obj[i]); + result = rt_spi_bit_add_bus(&spi_obj[i].spi, soft_spi_config[i].bus_name, &pico_soft_spi_ops); RT_ASSERT(result == RT_EOK); } diff --git a/bsp/renesas/libraries/HAL_Drivers/drv_soft_spi.c b/bsp/renesas/libraries/HAL_Drivers/drv_soft_spi.c index 47535f0f06..a89bf9f92d 100644 --- a/bsp/renesas/libraries/HAL_Drivers/drv_soft_spi.c +++ b/bsp/renesas/libraries/HAL_Drivers/drv_soft_spi.c @@ -28,6 +28,8 @@ static struct ra_soft_spi_config soft_spi_config[] = #endif }; +static struct ra_soft_spi spi_obj[sizeof(soft_spi_config) / sizeof(soft_spi_config[0])]; + /** * Attach the spi device to soft SPI bus, this function must be used after initialization. */ @@ -183,9 +185,20 @@ static void ra_udelay(rt_uint32_t us) } } +static void ra_pin_init(void) +{ + rt_size_t obj_num = sizeof(spi_obj) / sizeof(struct ra_soft_spi); + + for(rt_size_t i; i < obj_num; i++) + { + ra_spi_gpio_init(&spi_obj[i]); + } +} + static struct rt_spi_bit_ops ra_soft_spi_ops = { .data = RT_NULL, + .pin_init = ra_pin_init, .tog_sclk = ra_tog_sclk, .set_sclk = ra_set_sclk, .set_mosi = ra_set_mosi, @@ -199,21 +212,18 @@ static struct rt_spi_bit_ops ra_soft_spi_ops = .delay_us = 1, }; -static struct ra_soft_spi spi_obj[sizeof(soft_spi_config) / sizeof(soft_spi_config[0])]; - /* Soft SPI initialization function */ int rt_hw_softspi_init(void) { rt_size_t obj_num = sizeof(spi_obj) / sizeof(struct ra_soft_spi); rt_err_t result; - for (int i = 0; i < obj_num; i++) + for (rt_size_t i = 0; i < obj_num; i++) { memcpy(&spi_obj[i].ops, &ra_soft_spi_ops, sizeof(struct rt_spi_bit_ops)); spi_obj[i].ops.data = (void *)&soft_spi_config[i]; spi_obj[i].spi.ops = &ra_soft_spi_ops; spi_obj[i].cfg = (void *)&soft_spi_config[i]; - ra_spi_gpio_init(&spi_obj[i]); result = rt_spi_bit_add_bus(&spi_obj[i].spi, soft_spi_config[i].bus_name, &spi_obj[i].ops); RT_ASSERT(result == RT_EOK); } diff --git a/bsp/stm32/libraries/HAL_Drivers/drivers/drv_soft_spi.c b/bsp/stm32/libraries/HAL_Drivers/drivers/drv_soft_spi.c index ad186e3bcd..6f36d5ad8e 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drivers/drv_soft_spi.c +++ b/bsp/stm32/libraries/HAL_Drivers/drivers/drv_soft_spi.c @@ -28,6 +28,8 @@ static struct stm32_soft_spi_config soft_spi_config[] = #endif }; +static struct stm32_soft_spi spi_obj[sizeof(soft_spi_config) / sizeof(soft_spi_config[0])]; + /** * Attach the spi device to soft SPI bus, this function must be used after initialization. */ @@ -184,9 +186,20 @@ static void stm32_udelay(rt_uint32_t us) } } +static void stm32_pin_init(void) +{ + rt_size_t obj_num = sizeof(spi_obj) / sizeof(struct stm32_soft_spi); + + for(rt_size_t i; i < obj_num; i++) + { + stm32_spi_gpio_init(&spi_obj[i]); + } +} + static struct rt_spi_bit_ops stm32_soft_spi_ops = { .data = RT_NULL, + .pin_init = stm32_pin_init, .tog_sclk = stm32_tog_sclk, .set_sclk = stm32_set_sclk, .set_mosi = stm32_set_mosi, @@ -200,21 +213,18 @@ static struct rt_spi_bit_ops stm32_soft_spi_ops = .delay_us = 1, }; -static struct stm32_soft_spi spi_obj[sizeof(soft_spi_config) / sizeof(soft_spi_config[0])]; - /* Soft SPI initialization function */ int rt_hw_softspi_init(void) { rt_size_t obj_num = sizeof(spi_obj) / sizeof(struct stm32_soft_spi); rt_err_t result; - for (int i = 0; i < obj_num; i++) + for (rt_size_t i = 0; i < obj_num; i++) { memcpy(&spi_obj[i].ops, &stm32_soft_spi_ops, sizeof(struct rt_spi_bit_ops)); spi_obj[i].ops.data = (void *)&soft_spi_config[i]; spi_obj[i].spi.ops = &stm32_soft_spi_ops; spi_obj[i].cfg = (void *)&soft_spi_config[i]; - stm32_spi_gpio_init(&spi_obj[i]); result = rt_spi_bit_add_bus(&spi_obj[i].spi, soft_spi_config[i].bus_name, &spi_obj[i].ops); RT_ASSERT(result == RT_EOK); } diff --git a/bsp/wch/risc-v/Libraries/ch32_drivers/drv_soft_spi.c b/bsp/wch/risc-v/Libraries/ch32_drivers/drv_soft_spi.c index eb123a4686..4b1a6301f0 100644 --- a/bsp/wch/risc-v/Libraries/ch32_drivers/drv_soft_spi.c +++ b/bsp/wch/risc-v/Libraries/ch32_drivers/drv_soft_spi.c @@ -26,6 +26,8 @@ static struct ch32_soft_spi_config soft_spi_config[] = #endif }; +static struct ch32_soft_spi spi_obj[sizeof(soft_spi_config) / sizeof(soft_spi_config[0])]; + /** * Attach the spi device to soft SPI bus, this function must be used after initialization. */ @@ -187,9 +189,20 @@ static void ch32_udelay(rt_uint32_t us) } } +static void ch32_pin_init(void) +{ + rt_size_t obj_num = sizeof(spi_obj) / sizeof(struct ch32_soft_spi); + + for(rt_size_t i; i < obj_num; i++) + { + ch32_spi_gpio_init(&spi_obj[i]); + } +} + static struct rt_spi_bit_ops ch32_soft_spi_ops = { .data = RT_NULL, + .pin_init = ch32_pin_init, .tog_sclk = ch32_tog_sclk, .set_sclk = ch32_set_sclk, .set_mosi = ch32_set_mosi, @@ -203,20 +216,17 @@ static struct rt_spi_bit_ops ch32_soft_spi_ops = .delay_us = 1, }; -static struct ch32_soft_spi spi_obj[sizeof(soft_spi_config) / sizeof(soft_spi_config[0])]; - /* Soft SPI initialization function */ int rt_soft_spi_init(void) { rt_size_t obj_num = sizeof(spi_obj) / sizeof(struct ch32_soft_spi); rt_err_t result; - for (int i = 0; i < obj_num; i++) + for (rt_size_t i = 0; i < obj_num; i++) { ch32_soft_spi_ops.data = (void *)&soft_spi_config[i]; spi_obj[i].spi.ops = &ch32_soft_spi_ops; spi_obj[i].cfg = (void *)&soft_spi_config[i]; - ch32_spi_gpio_init(&spi_obj[i]); result = rt_spi_bit_add_bus(&spi_obj[i].spi, soft_spi_config[i].bus_name, &ch32_soft_spi_ops); RT_ASSERT(result == RT_EOK); } diff --git a/components/drivers/spi/spi-bit-ops.c b/components/drivers/spi/spi-bit-ops.c index d03e5007cf..f551741873 100644 --- a/components/drivers/spi/spi-bit-ops.c +++ b/components/drivers/spi/spi-bit-ops.c @@ -384,6 +384,11 @@ rt_err_t spi_bit_configure(struct rt_spi_device *device, struct rt_spi_configura RT_ASSERT(device != RT_NULL); RT_ASSERT(configuration != RT_NULL); + if(ops->pin_init != RT_NULL) + { + ops->pin_init(); + } + if (configuration->mode & RT_SPI_SLAVE) { return -RT_EIO; diff --git a/components/drivers/spi/spi-bit-ops.h b/components/drivers/spi/spi-bit-ops.h index b536b3d81e..d1e83cdedd 100644 --- a/components/drivers/spi/spi-bit-ops.h +++ b/components/drivers/spi/spi-bit-ops.h @@ -21,6 +21,7 @@ extern "C" { struct rt_spi_bit_ops { void *data; /* private data for lowlevel routines */ + void (*const pin_init)(void); void (*const tog_sclk)(void *data); void (*const set_sclk)(void *data, rt_int32_t state); void (*const set_mosi)(void *data, rt_int32_t state);