最近几天在写HAL库串口空闲中断+DMA双缓冲,一直不成功,感觉配置都对但就是不行,能进空闲中断,但切换不了缓冲的目标数组,而且接受的数也不对,希望有写过HAL库串口空闲中断+DMA双缓冲的人能提供一些帮助,十分感谢!我写的配置如下
void DMA_Init()
{
RCC->AHB1ENR|=RCC_AHB1ENR_DMA2EN;
hdma_usart1_rx.Instance = DMA2_Stream2;
hdma_usart1_rx.Init.Channel = DMA_CHANNEL_4;
hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE;
hdma_usart1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_usart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_usart1_rx.Init.Mode = DMA_CIRCULAR;
hdma_usart1_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma_usart1_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma_usart1_rx.Init.MemBurst = DMA_MBURST_SINGLE;
hdma_usart1_rx.Init.PeriphBurst = DMA_PBURST_SINGLE;
HAL_DMA_DeInit(&hdma_usart1_rx);
HAL_DMA_Init(&hdma_usart1_rx);
__HAL_LINKDMA(&huart1,hdmarx,hdma_usart1_rx);
HAL_DMAEx_MultiBufferStart(&hdma_usart1_rx, (uint32_t)(&USART1->DR),(uint32_t)(& usart_dma_bf[0][0]),(uint32_t)(& usart_dma_bf[1][0]), BUFLEN);
DMA2_Stream2->CR|=(uint32_t)4<<25;
DMA2_Stream2->CR|=0<<19;
DMA2_Stream2->CR|=1<<18;
DMA2_Stream2->CR|=1<<0;
__HAL_DMA_DISABLE(&hdma_usart1_rx);
HAL_Delay(1);
__HAL_UART_CLEAR_IDLEFLAG(&huart1);
__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
__HAL_DMA_ENABLE(&hdma_usart1_rx);
}
接收是这样的
if((__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE) != RESET) && __HAL_UART_GET_IT_SOURCE(&huart1, UART_IT_IDLE) != RESET)
{
// printf("456
");
__HAL_UART_CLEAR_IDLEFLAG(&huart1);
(void)huart1.Instance->SR;
(void)huart1.Instance->DR;
if((DMA2_Stream2->CR & DMA_SxCR_CT) != 0)
{
printf("0000
");
temp = DMA2_Stream2->NDTR;
rx1_len = BUFLEN - temp;
printf("rx1_len = %d
", rx1_len);
HAL_UART_Receive_DMA(&huart1, Usart1_Rece_Buf0, BUFLEN);
}
else
{
printf("1111
");
temp = DMA2_Stream2->NDTR;
rx1_len = BUFLEN - temp;
printf("rx1_len = %d
", rx1_len);
HAL_UART_Receive_DMA(&huart1, Usart1_Rece_Buf0, BUFLEN);
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>