stm32hal串口dma空闲中断
时间: 2023-05-27 13:06:54 浏览: 232
STM32串口空闲中断
在使用STM32HAL库时,可以通过以下步骤实现串口DMA空闲中断:
1. 配置串口DMA传输,使其使用循环模式和使用空闲中断。
```c
/* Configure the DMA handler for Transmission process */
hdma_tx.Instance = USARTx_TX_DMA_STREAM;
hdma_tx.Init.Channel = USARTx_TX_DMA_CHANNEL;
hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_tx.Init.MemInc = DMA_MINC_ENABLE;
hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_tx.Init.Mode = DMA_NORMAL;
hdma_tx.Init.Priority = DMA_PRIORITY_LOW;
hdma_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma_tx.Init.MemBurst = DMA_MBURST_INC4;
hdma_tx.Init.PeriphBurst = DMA_PBURST_INC4;
/* Initialize DMA handle */
if(HAL_DMA_Init(&hdma_tx) != HAL_OK)
{
Error_Handler();
}
/* Associate the initialized DMA handle to the the UART handle */
__HAL_LINKDMA(huart, hdmatx, hdma_tx);
/* Configure the DMA handler for reception process */
hdma_rx.Instance = USARTx_RX_DMA_STREAM;
hdma_rx.Init.Channel = USARTx_RX_DMA_CHANNEL;
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_CIRCULAR;
hdma_rx.Init.Priority = DMA_PRIORITY_HIGH;
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;
/* Initialize DMA handle */
if(HAL_DMA_Init(&hdma_rx) != HAL_OK)
{
Error_Handler();
}
/* Associate the initialized DMA handle to the the UART handle */
__HAL_LINKDMA(huart, hdmarx, hdma_rx);
/* Configure the DMA handler callbacks */
HAL_DMA_RegisterCallback(&hdma_tx, HAL_DMA_XFER_CPLT_CB_ID, DMA_TxCpltCallback);
HAL_DMA_RegisterCallback(&hdma_rx, HAL_DMA_XFER_CPLT_CB_ID, DMA_RxCpltCallback);
HAL_DMA_RegisterCallback(&hdma_rx, HAL_DMA_XFER_ERROR_CB_ID, DMA_RxErrorCallback);
/* Configure DMA transfer interrupts */
__HAL_DMA_ENABLE_IT(&hdma_rx, DMA_IT_TC);
__HAL_DMA_DISABLE_IT(&hdma_rx, DMA_IT_HT);
__HAL_DMA_DISABLE_IT(&hdma_rx, DMA_IT_FE);
__HAL_DMA_DISABLE_IT(&hdma_rx, DMA_IT_TE);
```
2. 实现DMA传输完成回调函数。
```c
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
/* Transmission complete callback */
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
/* Reception complete callback */
}
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
{
/* UART error callback */
}
```
3. 实现DMA空闲中断回调函数。
```c
void HAL_UART_IDLE_IRQHandler(UART_HandleTypeDef *huart)
{
/* Disable DMA transfer on reception */
hdma_rx.Instance->CR &= ~DMA_SxCR_EN;
/* Clear IDLE flag */
__HAL_UART_CLEAR_IDLEFLAG(huart);
/* Call user callback */
HAL_UART_RxCpltCallback(huart);
/* Enable DMA transfer on reception */
hdma_rx.Instance->CR |= DMA_SxCR_EN;
}
```
4. 配置空闲中断使能。
```c
/* Enable the UART Idle Interrupt */
__HAL_UART_ENABLE_IT(&huart, UART_IT_IDLE);
```
通过以上步骤,就可以实现串口DMA空闲中断。
阅读全文