我正在尝试使用 DMA 传输 SPI 中的数据,其中我的 Hal 状态是 HAL_SPI_STATUS_BUSY_TX。所需的状态是 HAL_SPI_STATE_READY。我想通过 SPI 发送一些批量数据和命令(单字节)。是否可以分别在DMA和非DMA模式之间切换?如图所示,它在 while 中循环。
hdma1_tx.Instance = DMA1_Stream7;
hdma1_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma1_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma1_tx.Init.MemBurst = DMA_MBURST_INC4;
hdma1_tx.Init.PeriphBurst = DMA_PBURST_INC4;
hdma1_tx.Init.Request = DMA_REQUEST_SPI1_TX;
hdma1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma1_tx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma1_tx.Init.MemInc = DMA_MINC_ENABLE;
hdma1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma1_tx.Init.Mode = DMA_NORMAL;
hdma1_tx.Init.Priority = DMA_PRIORITY_LOW;
if(HAL_DMA_Init(&hdma1_tx) != HAL_OK)
{
// Error
}
/* Associate the initialized DMA handle to the the SPI handle */
__HAL_LINKDMA(hspi, hdmatx, hdma1_tx);
/* Configure the DMA handler for Transmission process */
hdma_rx.Instance = DMA1_Stream1;
hdma_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma_rx.Init.MemBurst = DMA_MBURST_INC4;
hdma_rx.Init.PeriphBurst = DMA_PBURST_INC4;
hdma_rx.Init.Request = DMA_REQUEST_SPI1_RX;
hdma_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_rx.Init.MemInc = DMA_MINC_ENABLE;
hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_rx.Init.Mode = DMA_NORMAL;
hdma_rx.Init.Priority = DMA_PRIORITY_HIGH;
HAL_DMA_Init(&hdma_rx);
/* Associate the initialized DMA handle to the the SPI handle */
__HAL_LINKDMA(hspi, hdmarx, hdma_rx);
/*##-4- Configure the NVIC for DMA #########################################*/
/* NVIC configuration for DMA transfer complete interrupt (SPI1_TX) */
HAL_NVIC_SetPriority(DMA1_Stream7_IRQn, 1, 1);
HAL_NVIC_EnableIRQ(DMA1_Stream7_IRQn);
HAL_NVIC_SetPriority(SPI1_IRQn, 1, 0);
HAL_NVIC_EnableIRQ(SPI1_IRQn);
Run Code Online (Sandbox Code Playgroud)
HAL_STATUS 必须为 HAL_SPI_STATE_READY。
SPI 使能后 NDTR = 0x00
小智 1
可能导致此类问题的原因之一是存储要发送的数据的变量放置在错误的 RAM 区域中,请检查映射文件并修改链接器脚本
您可以在这里找到更多信息 https://community.st.com/s/article/FAQ-DMA-is-not-working-on-STM32H7-devices