Skip to content

feat:[drivers][spi] rt_spi_configure 添加互斥保护 && rt_spi_bus_configure 添加 -RT_EBUSY 返回值 #9501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions components/drivers/include/drivers/dev_spi.h
Original file line number Diff line number Diff line change
@@ -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
*
Expand Down Expand Up @@ -184,7 +184,13 @@ rt_err_t rt_spi_bus_attach_device_cspin(struct rt_spi_device *device,
rt_base_t cs_pin,
void *user_data);

/* re-configure SPI bus */
/**
* @brief This function can re-configure SPI bus.
* @param device: the SPI device attached to SPI bus
* @retval RT_EOK on release SPI device successfully.
* RT_EBUSY is not an error condition and the configuration will take effect once the device has the bus
* others on taken SPI bus failed.
*/
rt_err_t rt_spi_bus_configure(struct rt_spi_device *device);

/**
Expand Down Expand Up @@ -223,7 +229,14 @@ rt_err_t rt_spi_take(struct rt_spi_device *device);
*/
rt_err_t rt_spi_release(struct rt_spi_device *device);

/* set configuration on SPI device */
/**
* @brief This function can set configuration on SPI device.
* @param device: the SPI device attached to SPI bus
* @param cfg: the configuration pointer.
* @retval RT_EOK on release SPI device successfully.
* RT_EBUSY is not an error condition and the configuration will take effect once the device has the bus
* others on taken SPI bus failed.
*/
rt_err_t rt_spi_configure(struct rt_spi_device *device,
struct rt_spi_configuration *cfg);

Expand Down
31 changes: 24 additions & 7 deletions components/drivers/spi/dev_spi_core.c
Original file line number Diff line number Diff line change
@@ -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
*
Expand Down Expand Up @@ -106,6 +106,13 @@ rt_err_t rt_spi_bus_configure(struct rt_spi_device *device)
LOG_E("SPI device %s configuration failed", device->parent.parent.name);
}
}
else
{
/* RT_EBUSY is not an error condition and
* the configuration will take effect once the device has the bus
*/
return -RT_EBUSY;
}

/* release lock */
rt_mutex_release(&(device->bus->lock));
Expand All @@ -124,14 +131,24 @@ rt_err_t rt_spi_configure(struct rt_spi_device *device,
{
RT_ASSERT(device != RT_NULL);
RT_ASSERT(cfg != RT_NULL);
rt_err_t result = -RT_ERROR;

/* reset the CS pin */
if (device->cs_pin != PIN_NONE)
result = rt_mutex_take(&(device->bus->lock), RT_WAITING_FOREVER);
if (result == RT_EOK)
{
if (cfg->mode & RT_SPI_CS_HIGH)
rt_pin_write(device->cs_pin, PIN_LOW);
else
rt_pin_write(device->cs_pin, PIN_HIGH);
/* reset the CS pin */
if (device->cs_pin != PIN_NONE)
{
if (cfg->mode & RT_SPI_CS_HIGH)
rt_pin_write(device->cs_pin, PIN_LOW);
else
rt_pin_write(device->cs_pin, PIN_HIGH);
}
rt_mutex_release(&(device->bus->lock));
}
else
{
return result;
}

/* If the configurations are the same, we don't need to set again. */
Expand Down
Loading