summaryrefslogtreecommitdiff
path: root/components/drivers
diff options
context:
space:
mode:
authorwdfk-prog <[email protected]>2025-09-25 10:31:19 +0800
committerR b b666 <[email protected]>2025-09-25 11:41:58 +0800
commit7e5cd483600a5ad66d3d9d3dc761281469bec69e (patch)
tree0564e8c09981589bc0153485158d3b44b80cdfca /components/drivers
parentda99c507c49cac27ba5a5a8315b0b0c8ac00ca2a (diff)
feat[SPI]: Add SPI device detach function
Diffstat (limited to 'components/drivers')
-rw-r--r--components/drivers/include/drivers/dev_spi.h30
-rw-r--r--components/drivers/spi/dev_spi_core.c33
2 files changed, 62 insertions, 1 deletions
diff --git a/components/drivers/include/drivers/dev_spi.h b/components/drivers/include/drivers/dev_spi.h
index 2cee92748e..195ba4b6ee 100644
--- a/components/drivers/include/drivers/dev_spi.h
+++ b/components/drivers/include/drivers/dev_spi.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2024 RT-Thread Development Team
+ * Copyright (c) 2006-2025 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -349,6 +349,19 @@ rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device,
const char *bus_name,
void *user_data);
+/**
+ * @brief Detach a device from the SPI bus.
+ *
+ * This function serves as the high-level API to detach a SPI device from its bus.
+ * It unregisters the device from the device framework and ensures all associated
+ * resources, such as the chip select pin, are properly released by calling
+ * the underlying implementation.
+ *
+ * @param device The SPI device to be detached.
+ *
+ * @return rt_err_t The result of the operation. RT_EOK on success, otherwise an error code.
+ */
+rt_err_t rt_spi_bus_detach_device(struct rt_spi_device *device);
/**
* @brief attach a device on SPI bus with CS pin
@@ -368,6 +381,21 @@ rt_err_t rt_spi_bus_attach_device_cspin(struct rt_spi_device *device,
void *user_data);
/**
+ * @brief Detach a device from the SPI bus and release its CS pin.
+ *
+ * This function provides the low-level implementation for detaching a device
+ * from the SPI bus. It specifically handles the operations for the chip select (CS)
+ * pin, resetting it to input mode to release it. This function is typically
+ * called by the higher-level rt_spi_bus_detach_device() and should not be
+ * called directly by the user application.
+ *
+ * @param device The SPI device to be detached.
+ *
+ * @return rt_err_t The result of the operation. RT_EOK on success, otherwise an error code.
+ */
+rt_err_t rt_spi_bus_detach_device_cspin(struct rt_spi_device *device);
+
+/**
* @brief Reconfigure the SPI bus for the specified device.
*
* @param device: Pointer to the SPI device attached to the SPI bus.
diff --git a/components/drivers/spi/dev_spi_core.c b/components/drivers/spi/dev_spi_core.c
index 53bb7284fa..42f829447a 100644
--- a/components/drivers/spi/dev_spi_core.c
+++ b/components/drivers/spi/dev_spi_core.c
@@ -123,6 +123,34 @@ rt_err_t rt_spi_bus_attach_device_cspin(struct rt_spi_device *device,
return -RT_ERROR;
}
+rt_err_t rt_spi_bus_detach_device_cspin(struct rt_spi_device *device)
+{
+ rt_err_t result;
+
+ RT_ASSERT(device != RT_NULL);
+
+ result = rt_device_unregister(&device->parent);
+ if (result != RT_EOK)
+ {
+ LOG_E("Failed to unregister spi device, result: %d", result);
+ return result;
+ }
+
+ if (device->bus != RT_NULL && device->bus->owner == device)
+ {
+ device->bus->owner = RT_NULL;
+ }
+
+ if (device->cs_pin != PIN_NONE)
+ {
+ rt_pin_mode(device->cs_pin, PIN_MODE_INPUT);
+ }
+
+ device->bus = RT_NULL;
+
+ return RT_EOK;
+}
+
rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device,
const char *name,
const char *bus_name,
@@ -131,6 +159,11 @@ rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device,
return rt_spi_bus_attach_device_cspin(device, name, bus_name, PIN_NONE, user_data);
}
+rt_err_t rt_spi_bus_detach_device(struct rt_spi_device *device)
+{
+ return rt_spi_bus_detach_device_cspin(device);
+}
+
rt_err_t rt_spi_bus_configure(struct rt_spi_device *device)
{
rt_err_t result = -RT_ERROR;