Browse Source

[bsp/stm32] 将spi DMA传输更改为阻塞线程方式 (#6513)

* [bsp/stm32] 将spi DMA传输更改为阻塞线程方式

* Update bsp/stm32/libraries/HAL_Drivers/drv_spi.h

Co-authored-by: Man, Jianting (Meco) <920369182@qq.com>
BreederBai 2 years ago
parent
commit
3a9152c5fe

+ 30 - 1
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)
 {

+ 3 - 0
bsp/stm32/libraries/HAL_Drivers/drv_spi.h

@@ -16,6 +16,7 @@
 #include <rthw.h>
 #include <drv_common.h>
 #include "drv_dma.h"
+#include <ipc/completion.h>
 
 #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__ */