【发布时间】:2019-10-01 13:31:57
【问题描述】:
我正在尝试使用 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);
HAL_STATUS 必须是 HAL_SPI_STATE_READY。
启用 SPI 后 NDTR = 0x00
【问题讨论】:
-
我在不同的芯片(STM32G4)上遇到了非常相似的问题;原来是配置问题。我将留下一个指向 EESE 答案的链接,我在其中按问题和解决方案描述,以防其他人发现这个问题:electronics.stackexchange.com/a/606613/194340