[driver][soft-spi] spi device driver framework adds pin init function function

为什么提交这份PR (why to submit this PR)
ps:在设备初始化阶段不应该存在对硬件的操作

你的解决方案是什么 (what is your solution)
ps:添加一个pin_init函数,在spi设备使用时调用该函数来完成硬件引脚的状态初始化,而不是在设备初始化阶段对硬件进行操作,已经在瑞萨HMI上通过验证
This commit is contained in:
Yuqiang Wang 2024-04-10 06:33:29 +08:00 committed by GitHub
parent 5bb902895c
commit d08ae0bfd1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 78 additions and 21 deletions

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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;

View File

@ -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);