From 22fb7152d960ced65d42e122f10f569dc4f6fb19 Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Thu, 1 Jun 2023 22:43:07 -0400 Subject: [PATCH] =?UTF-8?q?[stm32][spi-dma]=20=E5=8F=AA=E6=9C=89=E4=BC=A0?= =?UTF-8?q?=E8=BE=93=E6=95=B0=E6=8D=AE=E8=B6=85=E8=BF=87=E4=B8=80=E5=AE=9A?= =?UTF-8?q?=E9=95=BF=E5=BA=A6=E6=89=8D=E4=BC=9A=E4=BD=BF=E7=94=A8DMA?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=20=E5=9C=A8=E4=BC=A0=E8=BE=93=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E4=B8=BA=E7=9F=AD=E5=B0=8F=E6=8A=A5=E6=96=87=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E4=BD=BF=E7=94=A8DMA=E6=A8=A1=E5=BC=8F=E5=8F=8D?= =?UTF-8?q?=E5=80=92=E4=BC=9A=E6=85=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bsp/stm32/libraries/HAL_Drivers/drv_spi.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_spi.c b/bsp/stm32/libraries/HAL_Drivers/drv_spi.c index fc2690ac97..fe0028c06d 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_spi.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_spi.c @@ -283,6 +283,8 @@ static rt_err_t stm32_spi_init(struct stm32_spi *spi_drv, struct rt_spi_configur static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message) { + #define DMA_TRANS_MIN_LEN 10 /* only buffer length >= DMA_TRANS_MIN_LEN will use DMA mode */ + HAL_StatusTypeDef state = HAL_OK; rt_size_t message_length, already_send_length; rt_uint16_t send_length; @@ -341,7 +343,7 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m #if defined(SOC_SERIES_STM32H7) || defined(SOC_SERIES_STM32F7) rt_uint32_t* dma_buf = RT_NULL; - if ((spi_drv->spi_dma_flag & SPI_USING_TX_DMA_FLAG) && (spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG)) + if ((spi_drv->spi_dma_flag & SPI_USING_TX_DMA_FLAG) && (spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG) && (send_length >= DMA_TRANS_MIN_LEN)) { dma_buf = (rt_uint32_t *)rt_malloc_align(send_length,32); if(send_buf) @@ -360,16 +362,16 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m /* start once data exchange in DMA mode */ if (message->send_buf && message->recv_buf) { - if ((spi_drv->spi_dma_flag & SPI_USING_TX_DMA_FLAG) && (spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG)) + if ((spi_drv->spi_dma_flag & SPI_USING_TX_DMA_FLAG) && (spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG) && (send_length >= DMA_TRANS_MIN_LEN)) { state = HAL_SPI_TransmitReceive_DMA(spi_handle, (uint8_t *)send_buf, (uint8_t *)recv_buf, send_length); } - else if ((spi_drv->spi_dma_flag & SPI_USING_TX_DMA_FLAG)) + else if ((spi_drv->spi_dma_flag & SPI_USING_TX_DMA_FLAG) && (send_length >= DMA_TRANS_MIN_LEN)) { /* same as Tx ONLY. It will not receive SPI data any more. */ state = HAL_SPI_Transmit_DMA(spi_handle, (uint8_t *)send_buf, send_length); } - else if ((spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG)) + else if ((spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG) && (send_length >= DMA_TRANS_MIN_LEN)) { state = HAL_ERROR; LOG_E("It shoule be enabled both BSP_SPIx_TX_USING_DMA and BSP_SPIx_TX_USING_DMA flag, if wants to use SPI DMA Rx singly."); @@ -382,7 +384,7 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m } else if (message->send_buf) { - if (spi_drv->spi_dma_flag & SPI_USING_TX_DMA_FLAG) + if ((spi_drv->spi_dma_flag & SPI_USING_TX_DMA_FLAG) && (send_length >= DMA_TRANS_MIN_LEN)) { state = HAL_SPI_Transmit_DMA(spi_handle, (uint8_t *)send_buf, send_length); } @@ -400,7 +402,7 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m else if(message->recv_buf) { rt_memset((uint8_t *)recv_buf, 0xff, send_length); - if (spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG) + if ((spi_drv->spi_dma_flag & SPI_USING_RX_DMA_FLAG) && (send_length >= DMA_TRANS_MIN_LEN)) { state = HAL_SPI_Receive_DMA(spi_handle, (uint8_t *)recv_buf, send_length); } @@ -432,7 +434,7 @@ static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *m /* 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. */ - if (spi_drv->spi_dma_flag & (SPI_USING_TX_DMA_FLAG | SPI_USING_RX_DMA_FLAG)) + if ((spi_drv->spi_dma_flag & (SPI_USING_TX_DMA_FLAG | SPI_USING_RX_DMA_FLAG)) && (send_length >= DMA_TRANS_MIN_LEN)) { /* blocking the thread,and the other tasks can run */ if (rt_completion_wait(&spi_drv->cpt, 1000) != RT_EOK)