[spi]添加rt_spi_bus_attach_device_cspin 函数 (#6916)

这个函数实现了原先的挂载功能,并且调用pin框架,初始化cs引脚,是一个功能相对完善的挂载函数。用户也能更简单的使用。
该函数取代rt_spi_bus_attach_device,后续rt_spi_bus_attach_device会被逐渐淘汰掉。新的BSP以及软件包应当使用rt_spi_bus_attach_device_cspin新函数。

STM32L475潘多拉测试通过。
This commit is contained in:
liYangYang 2023-02-12 11:14:54 +08:00 committed by GitHub
parent e4edf7e182
commit 1eec660b12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 33 deletions

View File

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

View File

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

View File

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

View File

@ -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.
*

View File

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