Skip to content

Commit 2bbee8b

Browse files
committed
feat[stm32][spi]: enable interrupt-safe operations
1 parent d0859e7 commit 2bbee8b

File tree

1 file changed

+64
-15
lines changed
  • bsp/stm32/libraries/HAL_Drivers/drivers

1 file changed

+64
-15
lines changed

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

Lines changed: 64 additions & 15 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
*
@@ -26,7 +26,7 @@
2626
#include "drv_config.h"
2727
#include <string.h>
2828

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

@@ -360,9 +360,14 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m
360360
rt_uint8_t *aligned_send_buf = RT_NULL;
361361
rt_uint8_t *aligned_recv_buf = RT_NULL;
362362

363-
const rt_bool_t dma_eligible = (send_length >= DMA_TRANS_MIN_LEN);
364-
const rt_bool_t use_tx_dma = dma_eligible && (spi_drv->spi_dma_flag & SPI_USING_TX_DMA_FLAG);
365-
const rt_bool_t use_rx_dma = dma_eligible && (spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG);
363+
rt_bool_t dma_eligible = (send_length >= DMA_TRANS_MIN_LEN);
364+
rt_bool_t use_tx_dma = dma_eligible && (spi_drv->spi_dma_flag & SPI_USING_TX_DMA_FLAG);
365+
rt_bool_t use_rx_dma = dma_eligible && (spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG);
366+
367+
if (!rt_scheduler_is_available())
368+
{
369+
use_rx_dma = use_tx_dma = dma_eligible = RT_FALSE;
370+
}
366371

367372
if (dma_eligible)
368373
{
@@ -478,18 +483,21 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m
478483
}
479484
else
480485
{
481-
rt_uint32_t timeout = timeout_ms;
482-
while (HAL_SPI_GetState(spi_handle) != HAL_SPI_STATE_READY)
486+
if (rt_scheduler_is_available())
483487
{
484-
if(timeout-- > 0)
485-
{
486-
rt_thread_mdelay(1);
487-
}
488-
else
488+
rt_uint32_t timeout = timeout_ms;
489+
while (HAL_SPI_GetState(spi_handle) != HAL_SPI_STATE_READY)
489490
{
490-
LOG_E("timeout! SPI state did not become READY.");
491-
state = HAL_TIMEOUT;
492-
break;
491+
if(timeout-- > 0)
492+
{
493+
rt_thread_mdelay(1);
494+
}
495+
else
496+
{
497+
LOG_E("timeout! SPI state did not become READY.");
498+
state = HAL_TIMEOUT;
499+
break;
500+
}
493501
}
494502
}
495503
}
@@ -682,6 +690,47 @@ rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name,
682690
return result;
683691
}
684692

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

0 commit comments

Comments
 (0)