利用USART的IDLE中断通过DMA接收数据后再通过DMA发送出来,数据有错误和丢失

2019-07-20 02:31发布

硬件:探索者开发板
上位机通过串口调试软件XCOM发送36B的数据给USART1口,开发板接收后再通过USART1发回给上位机。通过USART3口打印一些信息供测试。如果手动发送,XCOM软件收到的数据与发送的数据一致。如果按10ms定时发送,会出现收到的数据为40B(串口接收发送缓冲区均为40B)、37B,好像是存在空闲中断未检测到的现象,发送的36B中会有部分数据丢失。

收到的40B为 :发送的36B+ BD DB 04 00(数据包的前4B)  感觉像是该USART空闲中断时没中断
37B为: 00+ 发送的36B   前边多了个0
USART1收到的字节数比发送的少了31B    2个出错的数据包多收到的字节数+31B刚好是一个完整的数据包  

main函数代码:

u32 totalpacket,rightpacket,errpacket; //记录接收到的数据包总数和正确个数 错误个数
 float rightrate=0;

USART1_Configuration();
 DMA_Configuration();
 NVIC_Configuration();
USART3_Init(115200);
 
 rightpacket=0;
 totalpacket=0;
 errpacket=0;

while(1)
{
     if(USART1_RX_Finish==1)  //接收到1个数据包  数据包正常情况下应该是36B
     {
         totalpacket++;   //数据包总数+1
         USART1_RX_Finish=0;
        //根据包头(前2B:0xBD 0xDB)判断是不是完整的数据包,
        //根据第2B Status(0x04)判断是否有效 
        //计算校验和,与第33Byte比较,判断包中数据是否存在错误
         if(USART1_RECEIVE_DATA[0]==0xBD && USART1_RECEIVE_DATA[1]==0xDB && USART1_RECEIVE_DATA[2]==0x04)
         {
             rightpacket++;  //正确的数据包计数+1
             rightrate=((float)rightpacket/totalpacket) * 100;
             printf("totalpacket:%d ",totalpacket); //打印数据包总数
             printf("rightpacket:%d ",rightpacket); //打印数据包正确数
             printf("rightpacket rate:%f  ",rightrate ); //打印数据包正确率
         }
        else
        {
             errpacket++;
             rightrate=((float)rightpacket/totalpacket) * 100;
             printf("totalpacket0:%d ",totalpacket); //打印数据包总数
             printf("rightpacket0 rate:%f  ",rightrate ); //打印数据包正确率
             printf("errorpacket0:%d ",errpacket); //打印数据包错误数
        }
    }
}

串口1配置
void USART1_Configuration(void)
{
     USART_InitTypeDef USART_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
 
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //使能GPIOA时钟
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//使能USART1时钟
 
  
 //USART1端口配置
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; //GPIOA9与GPIOA10
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz
     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽
     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
     GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA9,PA10 
  
 //串口1对应引脚复用映射
     GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); //GPIOA9复用为USART1
     GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); //GPIOA10复用为USART1

      USART_InitStructure.USART_BaudRate = 115200;
      USART_InitStructure.USART_WordLength = USART_WordLength_8b;
      USART_InitStructure.USART_StopBits = USART_StopBits_1;
      USART_InitStructure.USART_Parity = USART_Parity_No;
      USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
      USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
      USART_Init(USART1, &USART_InitStructure);
 //空闲中断
      USART_ITConfig(USART1, USART_IT_IDLE , ENABLE);
 /* 使能 USART, 配置完毕 */
      USART_Cmd(USART1, ENABLE);
 /* CPU的小缺陷:串口配置好,如果直接Send,则第1个字节发送不出去  如下语句解决第1个字节无法正确发送出去的问题 */
      USART_ClearFlag(USART1, USART_FLAG_TC); /* 清发送完成标志,Transmission Complete flag */
}

串口1中断服务函数
void USART1_IRQHandler(void)
{
     u16 DATA_LEN;
      u16 i;
  
 //接收完成中断处理
      if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)//如果为空闲总线中断,即一帧数据接收完成 (检测到停止位后,一定时间内未接收到数据则进入空闲中断)
      {    
            DMA_Cmd(DMA2_Stream5, DISABLE);//关闭DMA,防止处理其间有数据
            DMA_ClearFlag(DMA2_Stream5,DMA_FLAG_TCIF5);  //接收完成中断中必须要清楚标志位,且必须在关闭DMA之后
            USART1_RX_Finish=1;  
            DATA_LEN=RX_BUF_SIZE - DMA_GetCurrDataCounter(DMA2_Stream5);  //已传输的数据量  RX_BUF_SIZE为40
            if(DATA_LEN!=36)
            {
                  printf("Data Packet Length:%d ",DATA_LEN); 
            }
        if(DATA_LEN > 0 ) 
            { 
                  while(USART1_TX_Finish==0)//等待数据发送完成
                  { ;}
        //将接收到的数据拷贝到DMA发送缓冲区地址
                  for(i=0;i<DATA_LEN;i++ )
                  {
                        USART1_SEND_DATA=USART1_RECEIVE_DATA;
                  }
               USART1_TX_Finish=0;//DMA传输开始标志量
             MYDMA_Enable(DMA2_Stream7,DATA_LEN);   
       }
  
       DMA_ClearFlag(DMA2_Stream5,DMA_FLAG_DMEIF5 | DMA_FLAG_TCIF5| DMA_FLAG_TEIF5);//清标志(直接模式错误、传输完成错误、传输错误)        
       MYDMA_Enable(DMA2_Stream5,RX_BUF_SIZE);
  //读SR后读DR清除Idle
       USART1->SR;
       USART1->DR;  
 }
 //USART_IT_PE 奇偶校验
 //USART_IT_FE 帧错误
 //USART_IT_NE 
      if(USART_GetITStatus(USART1, USART_IT_PE | USART_IT_FE | USART_IT_NE) != RESET)//出错
      {
            USART_ClearITPendingBit(USART1, USART_IT_PE | USART_IT_FE | USART_IT_NE);
      }
      USART_ClearITPendingBit(USART1, USART_IT_TC);
      USART_ClearITPendingBit(USART1, USART_IT_IDLE);
}

DMA配置代码:
void DMA_Configuration(void)
{
     DMA_InitTypeDef DMA_InitStructure;
 /* DMA clock enable */
       RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);//DMA2
 /* DMA2 Stream7 Channel4 (triggered by USART1 Tx event) Config */
      DMA_DeInit(DMA2_Stream7);  
      while (DMA_GetCmdStatus(DMA2_Stream7) != DISABLE){}//等待DMA可配置  
  
      DMA_InitStructure.DMA_Channel = DMA_Channel_4;  //通道选择 
      DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART1->DR;
      DMA_InitStructure.DMA_Memory0BaseAddr = (u32)USART1_SEND_DATA;
      DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
      DMA_InitStructure.DMA_BufferSize = TX_BUF_SIZE;  //TX_BUF_SIZE=40
      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_High;
      DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;         
      DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
      DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;//存储器突发单次传输
      DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;//外设突发单次传输
 
      DMA_Init(DMA2_Stream7, &DMA_InitStructure);
    //使能中断
      DMA_ITConfig(DMA2_Stream7, DMA_IT_TC, ENABLE);
      DMA_ITConfig(DMA2_Stream7, DMA_IT_TE, ENABLE);
 /* Enable USART1 DMA TX request */
      USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
      DMA_Cmd(DMA2_Stream7, DISABLE);  //未启动DMA发送
 
 
 /* DMA2 Stream5 Channel4 (triggered by USART1 Rx event) Config */
      DMA_DeInit(DMA2_Stream5); 
      while (DMA_GetCmdStatus(DMA2_Stream5) != DISABLE){}//等待DMA可配置  
      DMA_InitStructure.DMA_Channel = DMA_Channel_4;  //通道选择 
      DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART1->DR;
      DMA_InitStructure.DMA_Memory0BaseAddr = (u32)USART1_RECEIVE_DATA;
      DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
      DMA_InitStructure.DMA_BufferSize = RX_BUF_SIZE;  //其值最好超过串口缓冲区的值 RX_BUF_SIZE=40
      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_Mode_Circular;
      DMA_InitStructure.DMA_Priority = DMA_Priority_High;
      DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;         
      DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
      DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;//存储器突发单次传输
      DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;//外设突发单次传输
      DMA_Init(DMA2_Stream5, &DMA_InitStructure);
  
 /* Enable USART1 DMA RX request */
      USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);
      DMA_Cmd(DMA2_Stream5, ENABLE); //启动DMA接收
 
}



void MYDMA_Enable(DMA_Stream_TypeDef *DMA_Streamx,u16 ndtr)

     DMA_Cmd(DMA_Streamx, DISABLE);                      //关闭DMA传输  
      while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE){} //确保DMA可以被设置    
      DMA_SetCurrDataCounter(DMA_Streamx,ndtr);          //数据传输量  
      DMA_Cmd(DMA_Streamx, ENABLE);                      //开启DMA传输
}     //DMA2_Stream7_中断服务函数
//USART1使用DMA发数据中断服务程序
void DMA2_Stream7_IRQHandler(void)
{
      if(DMA_GetITStatus(DMA2_Stream7,DMA_IT_TCIF7)!=RESET)
      {  
             DMA_ClearITPendingBit(DMA2_Stream7,DMA_FLAG_TCIF7);
            DMA_Cmd(DMA2_Stream7, DISABLE);//关闭DMA
            USART1_TX_Finish=1;//置DMA传输完成    
      }
      if(DMA_GetITStatus(DMA2_Stream7,DMA_IT_TEIF7)!=RESET)
      {
            DMA_ClearITPendingBit(DMA2_Stream7,DMA_FLAG_TEIF7);
      } 
}

void NVIC_Configuration(void)
{
     NVIC_InitTypeDef NVIC_InitStructure;
 /* Configure one bit for preemption priority */
      NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
 /* Enable the USART1 Interrupt */
      NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
      NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
      NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
      NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
      NVIC_Init(&NVIC_InitStructure);
 
 //Enable DMA Stream7 Interrupt 发送
      NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream7_IRQn;
      NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
      NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
      NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
      NVIC_Init(&NVIC_InitStructure);
}

请高手指点!!!!!!!!!!!!!!!!!!!!!!!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。