zoukankan      html  css  js  c++  java
  • stm32串口定长DMA接收 + 数据错位纠正

    串口dma接收配置

    • 使用的芯片为stm32F407
    • 接收串口为串口六
    __IO uint8_t Rx_data[12]={0};   //dma数据存放数组
    
    void Initial_UART6(unsigned long baudrate)
    {
     	GPIO_InitTypeDef GPIO_InitStructure;
            DMA_InitTypeDef DMA_InitStructure;
    	USART_InitTypeDef USART_InitStructure;
    	NVIC_InitTypeDef NVIC_InitStructure;
    	
    	
            //时钟使能
    	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG,ENABLE);
    	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6,ENABLE);
            RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);//DMA2时钟使能 
    
            /********GPIO 配置**********/
    	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_14;
    	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    	GPIO_Init(GPIOG, &GPIO_InitStructure);
    	GPIO_PinAFConfig(GPIOG,GPIO_PinSource9,GPIO_AF_USART6); //GPIOG9复用为USART6
    	GPIO_PinAFConfig(GPIOG,GPIO_PinSource14,GPIO_AF_USART6); //GPIOG14复用为USART6
    	
    	/**********DMA配置*************/
    	DMA_DeInit(DMA2_Stream2);
    	while (DMA_GetCmdStatus(DMA2_Stream2) != DISABLE){}//等待DMA可配置 
    	DMA_InitStructure.DMA_Channel=DMA_Channel_5;
    	DMA_InitStructure.DMA_PeripheralBaseAddr=(u32)&USART6->DR;
    	DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&Rx_data[0];
    	DMA_InitStructure.DMA_BufferSize = 12;//数据传输量 
    	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设非增量模式
    	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//存储器增量模式
    	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//外设数据长度:8位
    	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//存储器数据长度:8位
    	DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //循环接收模式
    	DMA_InitStructure.DMA_Priority = DMA_Priority_High;//高优先级
    	DMA_Init(DMA2_Stream2, &DMA_InitStructure);//初始化DMA Stream
        
    
            /**********串口配置*************/
    	USART_InitStructure.USART_BaudRate = baudrate;
    	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(USART6, &USART_InitStructure); 
    	USART_DMACmd(USART6,USART_DMAReq_Rx,ENABLE);  //使能串口6的DMA接收
    
    
    	/********dma传输完成中断配置***************/
            NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream2_IRQn;//串口1中断通道
    	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//抢占优先级1
    	NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;		//子优先级3
    	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ通道使能
    	NVIC_Init(&NVIC_InitStructure);	//根据指定的参数初始化VIC寄存器、
    
    	USART_Cmd(USART6, ENABLE);      //使能串口
    	DMA_ITConfig(DMA2_Stream2,DMA_IT_TC,ENABLE); //开启dma接收完成中断
            DMA_Cmd(DMA2_Stream2, ENABLE);   //开启dma传输
    }
    

    数据错位纠正

    这里我们主要使用校验的方式判断数据是否错位,可选的方法有CRC校验以及帧头帧尾校验
    为了方便起见并且减少计算量,我选择了帧头帧尾校验

    DMA传输完成中断函数
    void DMA2_Stream2_IRQHandler(void)
    {
    	if(DMA_GetFlagStatus(DMA2_Stream2,DMA_IT_TCIF2) == SET)
    	{
                    //校验不通过重启dma接收
    		if(Rx_data[0]!=0XAA || Rx_data[11]!=0xA0)  //这里我选择了0xaa作为帧头,0xa0作为帧尾。此处校验不通过
    		{
    			//printf("RX ERROR!!!
    ");     
    			DMA_Cmd(DMA2_Stream2,DISABLE);    
    			DMA_SetCurrDataCounter(DMA2_Stream2,12); //重设传输量
    			delay_us(500);                  //此处延时根据实际情况修改
    			DMA_Cmd(DMA2_Stream2,ENABLE);
    		}
    		else
    		{
    			printf("RX SUCCESSFUL!!!
    ");
    		}
    	}
    	DMA_ClearFlag(DMA2_Stream2, DMA_FLAG_TCIF2);  //清楚标志位
    }
    
  • 相关阅读:
    Identity Server 4 原理和实战(完结)_建立Angular 客户端
    Identity Server 4 原理和实战(完结)_为 MVC 客户端刷新 Token
    Identity Server 4 原理和实战(完结)_Authorization Code Flow 实例
    Identity Server 4 原理和实战(完结)_Resource Owner Password Credentials 授权实例
    ASP.NET Core会议管理平台实战_2、基本概念的理解
    ASP.NET Core会议管理平台实战_汇总贴
    ASP.NET Core会议管理平台实战_1、开篇介绍
    Identity Server 4 原理和实战(完结)_建立Identity Server 4项目,Client Credentials 授权实例
    Identity Server 4 原理和实战(完结)_----选看 OpenId Connect 简介
    Identity Server 4 原理和实战(完结)_----选看 OAuth 2.0 简介(下)
  • 原文地址:https://www.cnblogs.com/chilkings/p/13617202.html
Copyright © 2011-2022 走看看