• 已解决 73482 个问题
  • 已帮助 5993 位优秀工程师

求使用DMA进行串口接收的问题

有花堪折 2020-11-20 浏览量:457
应用:
STM32F405,
串口使用DMA接收,接收数据大小不定,

问题:我通过上位机给STM发送数据(数据大小不定,小于256),第一次发送时,接收正常
之后再发送时,能够进入串口中断,但没有数据,读到的dma_cnt一直是256,这个可能的原因是什么呢?

主要程序如下:      RECVBUFF_SIZE=256
void Debug_USART_Config(void)
{
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=DEBUG_PREE_PRIO;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = DEBUG_SUB_PRIO;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

        USART_Init(DEBUG_USART &USART_InitStructure);
USART_ITConfig(DEBUG_USART USART_IT_IDLE ENABLE);
USART_DMACmd(DEBUG_USARTUSART_DMAReq_RxENABLE);
        USART_Cmd(DEBUG_USART ENABLE);

        DMA_DeInit(DEBUG_USART_DMA_STREAM);
while (DMA_GetCmdStatus(DEBUG_USART_DMA_STREAM) != DISABLE){}
        DMA_InitStructure.DMA_Channel = DEBUG_USART_DMA_CHANNEL;  
        DMA_InitStructure.DMA_PeripheralbaseAddr = DEBUG_USART_DR_base;  
        DMA_InitStructure.DMA_Memory0baseAddr = (u32)RecvBuff;
        DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
        DMA_InitStructure.DMA_BufferSize = RECVBUFF_SIZE;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;  
  DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;   
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  DMA_Init(DEBUG_USART_DMA_STREAM &DMA_InitStructure);
  DMA_Cmd(DEBUG_USART_DMA_STREAM ENABLE);
while (DMA_GetCmdStatus(DEBUG_USART_DMA_STREAM) != ENABLE){}
}

void USART1_IRQHandler(void)
{
uint16_t dma_cnt;
    if(USART_GetITStatus(DEBUG_USART USART_IT_IDLE) != RESET)
    {
        USART_ReceiveData(DEBUG_USART);
dma_cnt = DMA_GetCurrDataCounter(DEBUG_USART_DMA_STREAM);
        uart1_rev_len =RECVBUFF_SIZE-dma_cnt;
        if(uart1_rev_len != 0)
        {
   uart_parse(RecvBuff);
            hw_memset(RecvBuff0sizeof(RecvBuff));
        }
        USART_ClearITPendingBit(DEBUG_USARTUSART_IT_IDLE);
        DMA_Cmd(DEBUG_USART_DMA_STREAM DISABLE );
while (DMA_GetCmdStatus(DEBUG_USART_DMA_STREAM) != DISABLE){}
       DMA_SetCurrDataCounter(DEBUG_USART_DMA_STREAMRECVBUFF_SIZE);
       DMA_Cmd(DEBUG_USART_DMA_STREAM ENABLE);
    }
}


uart_parse(RecvBuff)是解析收到的数据,
另外如果串口中断最后,加while (DMA_GetCmdStatus(DEBUG_USART_DMA_STREAM) != ENABLE){}的话
程序会卡死在while循环里
0 0 收起

我来回答

上传资料:
选择文件 文件大小不超过15M(格式支持:doc、ppt、xls、pdf、zip、rar、txt)
所有亮答 数量:0

相关问题

问题达人换一批

求使用DMA进行串口接收的问题