STM32F407 USART1串口使用DMA接收不定长数据和DMA中断发送

一、前言      

       

        使用DMA通信的好处是,不占用单片机资源(不像普通串口中断,发送一个字节触发一次中断,发送100个字节触发100次中断;接收一个字节触发一次中断,接收200个字节触发200次中断),数据接收完毕触发一次DMA中断;发送数据完毕触发一次DMA中断。

        下图是STM32F407单片机DMA通道关系图。
STM32F407 USART1串口使用DMA接收不定长数据和DMA中断发送_第1张图片

STM32F407 USART1串口使用DMA接收不定长数据和DMA中断发送_第2张图片 

 

#define USART1_DMA_RX_BUFFER_MAX_LENGTH		(255)
#define USART1_DMA_TX_BUFFER_MAX_LENGTH		(255)
uint8_t USART1_DMA_RX_Buffer[USART1_DMA_RX_BUFFER_MAX_LENGTH];
uint8_t USART1_DMA_TX_Buffer[USART1_DMA_TX_BUFFER_MAX_LENGTH];

 1、USART1  TX DMA初始化程序

void USART1_DMA_Tx_Configuration(void)
{
	DMA_InitTypeDef  DMA_InitStructure;
	
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 , ENABLE);					//DMA2时钟使能
	DMA_DeInit(DMA2_Stream7);
	while (DMA_GetCmdStatus(DMA2_Stream7) != DISABLE);						//等待DMA可配置
	DMA_InitStructure.DMA_Channel = DMA_Channel_4; 							//DMA通道配置
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART1->DR;		//DMA外设地址
	DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)USART1_DMA_TX_Buffer;	//发送缓存指针
	DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;					//DMA传输方向:内存--->外设
	DMA_InitStructure.DMA_BufferSize = USART1_DMA_TX_BUFFER_MAX_LENGTH;		//数据传输字节数量
	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_Normal;							//使用普通模式 
	DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;					//中等优先级 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 Stream
	DMA_Cmd(DMA2_Stream7, DISABLE); 										//开启DMA传输
}

2、USART1  RX DMA初始化程序

void USART1_DMA_Rx_Configuration(void)
{
	DMA_InitTypeDef  DMA_InitStructure;

	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 , ENABLE);					//DMA2时钟使能
	DMA_DeInit(DMA2_Stream5);
	while (DMA_GetCmdStatus(DMA2_Stream5) != DISABLE);						//等待DMA可配置  
	DMA_InitStructure.DMA_Channel = DMA_Channel_4;  						//通道选择
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART1->DR;		//DMA外设地址
	DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)USART1_DMA_RX_Buffer;	//接收缓存指针
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory ;				//DMA传输方向:外设到存储器模式:外设--->内存
	DMA_InitStructure.DMA_BufferSize = USART1_DMA_RX_BUFFER_MAX_LENGTH;		//缓冲大小 
	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_Normal;							//使用普通模式 
	DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;					//中等优先级 DMA_Priority_VeryHigh
	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);							//初始化DMA_Stream5	
	DMA_Cmd(DMA2_Stream5, ENABLE);  										//开启DMA传输
}

 3、USART1  启动DMA发送初始化程序


void USART1_DMA_Begin_Send(uint8_t *send_buffer , uint16_t nSendCount)
{		
	if (nSendCount < USART1_DMA_TX_BUFFER_MAX_LENGTH)
	{
		memcpy(USART1_DMA_TX_Buffer , send_buffer , nSendCount);
		DMA_Cmd(DMA2_Stream7 , DISABLE);                    //关闭DMA传输
		while (DMA_GetCmdStatus(DMA2_Stream7) != DISABLE);	//确保DMA可以被设置
		DMA_SetCurrDataCounter(DMA2_Stream7 , nSendCount);  //数据传输量
		DMA_Cmd(DMA2_Stream7 , ENABLE);               		//开启DMA传输
	}
}

4、USART1  DMA方式端口初始化程序(包含DMA配置)

void USART1_Configuration(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	
	USART_DeInit(USART1);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);		//for USART1 and USART6 
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
		
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);     	

	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_Init(GPIOA, &GPIO_InitStructure);	

	USART_InitStructure.USART_BaudRate = 9600;
	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_Cmd(USART1, ENABLE);


	USART_ClearFlag(USART1, USART_FLAG_TC); //清除发送完成标志	
	while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);	//等待空闲帧发送完成后再清零发送完成标志(警告:如果不使能USART_Mode_Tx,会导致单片机在这里死机)
	USART_ClearFlag(USART1, USART_FLAG_TC);	//清除发送完成标志

    USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);				//禁止USART1接收不为空中断
	USART_ITConfig(USART1, USART_IT_TXE, DISABLE);				//禁止USART1发送空中断
	USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);				//开启USART1空闲中断
	USART_ITConfig(USART1, USART_IT_TC, ENABLE);				//开启USART1传输完成中断
	
	USART_DMACmd(USART1 ,   USART_DMAReq_Tx,ENABLE);  			//使能串口的DMA发送
	USART_DMACmd(USART1 ,   USART_DMAReq_Rx,ENABLE);  			//使能串口的DMA接收
}

5、USART1   DMA中断接收和DMA中断发送

void USART1_IRQHandler(void)
{
	uint16_t ch;

	
	if (USART_GetITStatus(USART1,USART_IT_IDLE) != RESET)
	{		
		USART_ClearITPendingBit(USART1 , USART_IT_IDLE);	//必须先清除总线空闲中断标识,然后读一下数据寄存器,DMA接收才会正确(先读SR,然后读DR才能清除空闲中断标识)注意:这句必须要,否则不能够清除中断标志位。
		ch =  USART_ReceiveData(USART1);					//必须先清除总线空闲中断标识,然后读一下数据寄存器,DMA接收才会正确(先读SR,然后读DR才能清除空闲中断标识)注意:这句必须要,否则不能够清除中断标志位。
		
		#ifdef __DEBUG_stm32f407__
			__DEBUG_USART1_IT_IDLE++;
		#endif
		
		DMA_Cmd(DMA2_Stream5 , DISABLE); 					//关闭DMA,防止处理其间有数据
		DMA_ClearFlag(DMA2_Stream5 , DMA_FLAG_TCIF5 | DMA_FLAG_FEIF5 | DMA_FLAG_DMEIF5 | DMA_FLAG_TEIF5 | DMA_FLAG_HTIF5);//清零标志位
		ch = USART1_DMA_RX_BUFFER_MAX_LENGTH - DMA_GetCurrDataCounter(DMA2_Stream5);
		if (ch > 0)
		{
			if (MB_USART1.MBHosts_Query_isr == enum_MODBUS_STATUS_SEND_OVER)
			{
				MB_USART1.Outtime_mark = TRUE;
				MB_USART1.receCount = ch;
				memcpy(MB_USART1.mscomm_buffer , USART1_DMA_RX_Buffer , MB_USART1.receCount);
			}
		}
		DMA_SetCurrDataCounter(DMA2_Stream5 , USART1_DMA_RX_BUFFER_MAX_LENGTH);
		DMA_Cmd(DMA2_Stream5, ENABLE);
	}
	
	else if (USART_GetITStatus(USART1,USART_IT_TC)!= RESET) 
	{
		USART_ClearITPendingBit(USART1, USART_IT_TC);
		
		#ifdef __DEBUG_stm32f407__
			__DEBUG_USART1_IT_TC++;
		#endif
		
		//DMA_Cmd(DMA2_Stream7 , DISABLE);//这条语句必须屏蔽,否则485通信时会出现异常情况,2018.10.18
		DMA_ClearFlag(DMA2_Stream7 , DMA_FLAG_TCIF7 | DMA_FLAG_FEIF7 | DMA_FLAG_DMEIF7 | DMA_FLAG_TEIF7 | DMA_FLAG_HTIF7);//清零标志位
		DMA_SetCurrDataCounter(DMA2_Stream7 , 0);	//清除数据长度
		
		if (MB_USART1.MBHosts_Query_isr == enum_MODBUS_STATUS_SEND_BEGINING)
		{
			MB_USART1.MBHosts_Query_isr = enum_MODBUS_STATUS_SEND_OVER;
			TIM_Cmd(TIM3, DISABLE);	
			TIM_SetCounter(TIM3,0x0000);
			TIM_Cmd(TIM3, ENABLE);	
		}		
		GPIO_USART1_RS485_RECIVE_enable();
	}	
} 

6、主程序

void main(void)
{
    USART1_Configuration();
	USART1_DMA_Tx_Configuration();
	USART1_DMA_Rx_Configuration();
    
    while (1)
    {
          //在合适的时候调用USART1_DMA_Begin_Send(uint8_t *send_buffer , uint16_t nSendBytes)
          // 通过DMA中断方式将数据发送出去
    }
} 

你可能感兴趣的:(STM32F407,STM32F407,USART1串口,DMA接收不定长数据,DMA中断发送)