Skip to content

Commit e1042d8

Browse files
committed
feat:[drivers][spi] rt_spi_configure 添加互斥保护
1 parent 4fcc03c commit e1042d8

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

components/drivers/include/drivers/dev_spi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2023, RT-Thread Development Team
2+
* Copyright (c) 2006-2024 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*

components/drivers/spi/dev_spi_core.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2023, RT-Thread Development Team
2+
* Copyright (c) 2006-2024 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -108,8 +108,8 @@ rt_err_t rt_spi_bus_configure(struct rt_spi_device *device)
108108
}
109109
else
110110
{
111-
/* RT_EBUSY is not an error condition and
112-
* the configuration will take effect once the device has the bus
111+
/* RT_EBUSY is not an error condition and
112+
* the configuration will take effect once the device has the bus
113113
*/
114114
return -RT_EBUSY;
115115
}
@@ -131,14 +131,24 @@ rt_err_t rt_spi_configure(struct rt_spi_device *device,
131131
{
132132
RT_ASSERT(device != RT_NULL);
133133
RT_ASSERT(cfg != RT_NULL);
134+
rt_err_t result = -RT_ERROR;
134135

135-
/* reset the CS pin */
136-
if (device->cs_pin != PIN_NONE)
136+
result = rt_mutex_take(&(device->bus->lock), RT_WAITING_FOREVER);
137+
if (result == RT_EOK)
137138
{
138-
if (cfg->mode & RT_SPI_CS_HIGH)
139-
rt_pin_write(device->cs_pin, PIN_LOW);
140-
else
141-
rt_pin_write(device->cs_pin, PIN_HIGH);
139+
/* reset the CS pin */
140+
if (device->cs_pin != PIN_NONE)
141+
{
142+
if (cfg->mode & RT_SPI_CS_HIGH)
143+
rt_pin_write(device->cs_pin, PIN_LOW);
144+
else
145+
rt_pin_write(device->cs_pin, PIN_HIGH);
146+
}
147+
rt_mutex_release(&(device->bus->lock));
148+
}
149+
else
150+
{
151+
return result;
142152
}
143153

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

0 commit comments

Comments
 (0)