diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_spi.c b/bsp/stm32/libraries/HAL_Drivers/drv_spi.c index 9c3a2b49936966910b8f6977cd64fb9265e9163e..bf4fac3c10a1872ce8582e6a2e41b753382605ab 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 65f752fc8a23e51b242c03bcdf2235e042e464f7..bdb5e29ecd35acf99c0712655752cfc19a0310f2 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__ */