summaryrefslogtreecommitdiff
path: root/bsp/stm32
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 /bsp/stm32
parentda99c507c49cac27ba5a5a8315b0b0c8ac00ca2a (diff)
feat[SPI]: Add SPI device detach function
Diffstat (limited to 'bsp/stm32')
-rw-r--r--bsp/stm32/libraries/HAL_Drivers/drivers/drv_spi.c45
-rw-r--r--bsp/stm32/libraries/HAL_Drivers/drivers/drv_spi.h3
2 files changed, 45 insertions, 3 deletions
diff --git a/bsp/stm32/libraries/HAL_Drivers/drivers/drv_spi.c b/bsp/stm32/libraries/HAL_Drivers/drivers/drv_spi.c
index 3a7bf68125..07be86130d 100644
--- a/bsp/stm32/libraries/HAL_Drivers/drivers/drv_spi.c
+++ b/bsp/stm32/libraries/HAL_Drivers/drivers/drv_spi.c
@@ -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
*
@@ -25,7 +25,7 @@
#include "drv_config.h"
#include <string.h>
-//#define DRV_DEBUG
+/*#define DRV_DEBUG*/
#define LOG_TAG "drv.spi"
#include <drv_log.h>
@@ -683,6 +683,47 @@ rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name,
return result;
}
+/**
+ * Detach the spi device from SPI bus.
+ *
+ * @param device_name the name of the spi device to be detached.
+ */
+rt_err_t rt_hw_spi_device_detach(const char *device_name)
+{
+ RT_ASSERT(device_name != RT_NULL);
+
+ rt_err_t result;
+ struct rt_spi_device *spi_device;
+
+ rt_device_t device = rt_device_find(device_name);
+ if (device == RT_NULL)
+ {
+ LOG_E("SPI device %s not found.", device_name);
+ return -RT_ERROR;
+ }
+
+ if (device->type != RT_Device_Class_SPIDevice)
+ {
+ LOG_E("%s is not an SPI device.", device_name);
+ return -RT_ERROR;
+ }
+
+ spi_device = (struct rt_spi_device *)device;
+
+ result = rt_spi_bus_detach_device_cspin(spi_device);
+ if (result != RT_EOK)
+ {
+ LOG_E("Failed to detach %s from its bus, error code: %d", device_name, result);
+ return result;
+ }
+
+ rt_free(spi_device);
+
+ LOG_D("SPI device %s has been detached.", device_name);
+
+ return RT_EOK;
+}
+
#if defined(BSP_SPI1_TX_USING_DMA) || defined(BSP_SPI1_RX_USING_DMA)
void SPI1_IRQHandler(void)
{
diff --git a/bsp/stm32/libraries/HAL_Drivers/drivers/drv_spi.h b/bsp/stm32/libraries/HAL_Drivers/drivers/drv_spi.h
index 10b94fbbe8..746809cad9 100644
--- a/bsp/stm32/libraries/HAL_Drivers/drivers/drv_spi.h
+++ b/bsp/stm32/libraries/HAL_Drivers/drivers/drv_spi.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2006-2023, RT-Thread Development Team
+ * Copyright (c) 2006-2025 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -23,6 +23,7 @@ extern "C" {
#endif
rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_base_t cs_pin);
+rt_err_t rt_hw_spi_device_detach(const char *device_name);
#ifdef __cplusplus
}