Skip to content

Commit 7e5cd48

Browse files
wdfk-progRbb666
authored andcommitted
feat[SPI]: Add SPI device detach function
1 parent da99c50 commit 7e5cd48

File tree

4 files changed

+107
-4
lines changed

4 files changed

+107
-4
lines changed

bsp/stm32/libraries/HAL_Drivers/drivers/drv_spi.c

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2024, RT-Thread Development Team
2+
* Copyright (c) 2006-2025 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -25,7 +25,7 @@
2525
#include "drv_config.h"
2626
#include <string.h>
2727

28-
//#define DRV_DEBUG
28+
/*#define DRV_DEBUG*/
2929
#define LOG_TAG "drv.spi"
3030
#include <drv_log.h>
3131

@@ -683,6 +683,47 @@ rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name,
683683
return result;
684684
}
685685

686+
/**
687+
* Detach the spi device from SPI bus.
688+
*
689+
* @param device_name the name of the spi device to be detached.
690+
*/
691+
rt_err_t rt_hw_spi_device_detach(const char *device_name)
692+
{
693+
RT_ASSERT(device_name != RT_NULL);
694+
695+
rt_err_t result;
696+
struct rt_spi_device *spi_device;
697+
698+
rt_device_t device = rt_device_find(device_name);
699+
if (device == RT_NULL)
700+
{
701+
LOG_E("SPI device %s not found.", device_name);
702+
return -RT_ERROR;
703+
}
704+
705+
if (device->type != RT_Device_Class_SPIDevice)
706+
{
707+
LOG_E("%s is not an SPI device.", device_name);
708+
return -RT_ERROR;
709+
}
710+
711+
spi_device = (struct rt_spi_device *)device;
712+
713+
result = rt_spi_bus_detach_device_cspin(spi_device);
714+
if (result != RT_EOK)
715+
{
716+
LOG_E("Failed to detach %s from its bus, error code: %d", device_name, result);
717+
return result;
718+
}
719+
720+
rt_free(spi_device);
721+
722+
LOG_D("SPI device %s has been detached.", device_name);
723+
724+
return RT_EOK;
725+
}
726+
686727
#if defined(BSP_SPI1_TX_USING_DMA) || defined(BSP_SPI1_RX_USING_DMA)
687728
void SPI1_IRQHandler(void)
688729
{

bsp/stm32/libraries/HAL_Drivers/drivers/drv_spi.h

Lines changed: 2 additions & 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-2025 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -23,6 +23,7 @@ extern "C" {
2323
#endif
2424

2525
rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_base_t cs_pin);
26+
rt_err_t rt_hw_spi_device_detach(const char *device_name);
2627

2728
#ifdef __cplusplus
2829
}

components/drivers/include/drivers/dev_spi.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2024 RT-Thread Development Team
2+
* Copyright (c) 2006-2025 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -349,6 +349,19 @@ rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device,
349349
const char *bus_name,
350350
void *user_data);
351351

352+
/**
353+
* @brief Detach a device from the SPI bus.
354+
*
355+
* This function serves as the high-level API to detach a SPI device from its bus.
356+
* It unregisters the device from the device framework and ensures all associated
357+
* resources, such as the chip select pin, are properly released by calling
358+
* the underlying implementation.
359+
*
360+
* @param device The SPI device to be detached.
361+
*
362+
* @return rt_err_t The result of the operation. RT_EOK on success, otherwise an error code.
363+
*/
364+
rt_err_t rt_spi_bus_detach_device(struct rt_spi_device *device);
352365

353366
/**
354367
* @brief attach a device on SPI bus with CS pin
@@ -367,6 +380,21 @@ rt_err_t rt_spi_bus_attach_device_cspin(struct rt_spi_device *device,
367380
rt_base_t cs_pin,
368381
void *user_data);
369382

383+
/**
384+
* @brief Detach a device from the SPI bus and release its CS pin.
385+
*
386+
* This function provides the low-level implementation for detaching a device
387+
* from the SPI bus. It specifically handles the operations for the chip select (CS)
388+
* pin, resetting it to input mode to release it. This function is typically
389+
* called by the higher-level rt_spi_bus_detach_device() and should not be
390+
* called directly by the user application.
391+
*
392+
* @param device The SPI device to be detached.
393+
*
394+
* @return rt_err_t The result of the operation. RT_EOK on success, otherwise an error code.
395+
*/
396+
rt_err_t rt_spi_bus_detach_device_cspin(struct rt_spi_device *device);
397+
370398
/**
371399
* @brief Reconfigure the SPI bus for the specified device.
372400
*

components/drivers/spi/dev_spi_core.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,34 @@ rt_err_t rt_spi_bus_attach_device_cspin(struct rt_spi_device *device,
123123
return -RT_ERROR;
124124
}
125125

126+
rt_err_t rt_spi_bus_detach_device_cspin(struct rt_spi_device *device)
127+
{
128+
rt_err_t result;
129+
130+
RT_ASSERT(device != RT_NULL);
131+
132+
result = rt_device_unregister(&device->parent);
133+
if (result != RT_EOK)
134+
{
135+
LOG_E("Failed to unregister spi device, result: %d", result);
136+
return result;
137+
}
138+
139+
if (device->bus != RT_NULL && device->bus->owner == device)
140+
{
141+
device->bus->owner = RT_NULL;
142+
}
143+
144+
if (device->cs_pin != PIN_NONE)
145+
{
146+
rt_pin_mode(device->cs_pin, PIN_MODE_INPUT);
147+
}
148+
149+
device->bus = RT_NULL;
150+
151+
return RT_EOK;
152+
}
153+
126154
rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device,
127155
const char *name,
128156
const char *bus_name,
@@ -131,6 +159,11 @@ rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device,
131159
return rt_spi_bus_attach_device_cspin(device, name, bus_name, PIN_NONE, user_data);
132160
}
133161

162+
rt_err_t rt_spi_bus_detach_device(struct rt_spi_device *device)
163+
{
164+
return rt_spi_bus_detach_device_cspin(device);
165+
}
166+
134167
rt_err_t rt_spi_bus_configure(struct rt_spi_device *device)
135168
{
136169
rt_err_t result = -RT_ERROR;

0 commit comments

Comments
 (0)