From 3a9152c5fee95776ff29454b42bbf79d002edee7 Mon Sep 17 00:00:00 2001 From: BreederBai Date: Tue, 18 Oct 2022 03:52:03 +0800 Subject: [PATCH] =?UTF-8?q?[bsp/stm32]=20=E5=B0=86spi=20DMA=E4=BC=A0?= =?UTF-8?q?=E8=BE=93=E6=9B=B4=E6=94=B9=E4=B8=BA=E9=98=BB=E5=A1=9E=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B=E6=96=B9=E5=BC=8F=20(#6513)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [bsp/stm32] 将spi DMA传输更改为阻塞线程方式 * Update bsp/stm32/libraries/HAL_Drivers/drv_spi.h Co-authored-by: Man, Jianting (Meco) <920369182@qq.com> --- bsp/stm32/libraries/HAL_Drivers/drv_spi.c | 31 ++++++++++++++++++++++- bsp/stm32/libraries/HAL_Drivers/drv_spi.h | 3 +++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_spi.c b/bsp/stm32/libraries/HAL_Drivers/drv_spi.c index 9c3a2b4993..bf4fac3c10 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_spi.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_spi.c @@ -411,7 +411,15 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message * /* For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ - while (HAL_SPI_GetState(spi_handle) != HAL_SPI_STATE_READY); + if (spi_drv->spi_dma_flag & (SPI_USING_TX_DMA_FLAG | SPI_USING_RX_DMA_FLAG)) + { + /* blocking the thread,and the other tasks can run */ + rt_completion_wait(&spi_drv->cpt, RT_WAITING_FOREVER); + } + else + { + while (HAL_SPI_GetState(spi_handle) != HAL_SPI_STATE_READY); + } } if (message->cs_release && !(device->config.mode & RT_SPI_NO_CS)) @@ -537,6 +545,9 @@ static int rt_hw_spi_bus_init(void) } } + /* initialize completion object */ + rt_completion_init(&spi_bus_obj[i].cpt); + result = rt_spi_bus_register(&spi_bus_obj[i].spi_bus, spi_config[i].bus_name, &stm_spi_ops); RT_ASSERT(result == RT_EOK); @@ -938,6 +949,24 @@ static void stm32_get_dma_info(void) #endif } +void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) +{ + struct stm32_spi *spi_drv = rt_container_of(hspi, struct stm32_spi, handle); + rt_completion_done(&spi_drv->cpt); +} + +void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi) +{ + struct stm32_spi *spi_drv = rt_container_of(hspi, struct stm32_spi, handle); + rt_completion_done(&spi_drv->cpt); +} + +void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi) +{ + struct stm32_spi *spi_drv = rt_container_of(hspi, struct stm32_spi, handle); + rt_completion_done(&spi_drv->cpt); +} + #if defined(SOC_SERIES_STM32F0) void SPI1_DMA_RX_TX_IRQHandler(void) { diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_spi.h b/bsp/stm32/libraries/HAL_Drivers/drv_spi.h index 65f752fc8a..bdb5e29ecd 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_spi.h +++ b/bsp/stm32/libraries/HAL_Drivers/drv_spi.h @@ -16,6 +16,7 @@ #include #include #include "drv_dma.h" +#include #ifdef __cplusplus extern "C" { @@ -66,6 +67,8 @@ struct stm32_spi rt_uint8_t spi_dma_flag; struct rt_spi_bus spi_bus; + + struct rt_completion cpt; }; #endif /*__DRV_SPI_H__ */