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

一、前言    

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

        下图是STM32F407单片机DMA通道关系图。

  

#define USART3_DMA_RX_BUFFER_MAX_LENGTH		(255)
#define USART3_DMA_TX_BUFFER_MAX_LENGTH		(255)
uint8_t USART3_DMA_RX_Buffer[USART3_DMA_RX_BUFFER_MAX_LENGTH];
uint8_t USART3_DMA_TX_Buffer[USART3_DMA_TX_BUFFER_MAX_LENGTH];

1、USART3  TX DMA初始化程序

void USART3_DMA_Tx_Configuration(void)
{
	DMA_InitTypeDef  DMA_InitStructure;
	
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1 , ENABLE);					//DMA2时钟使能
	DMA_DeInit(DMA1_Stream3);
	while (DMA_GetCmdStatus(DMA1_Stream3) != DISABLE);						//等待DMA可配置
	DMA_InitStructure.DMA_Channel = DMA_Channel_4; 							//DMA通道配置
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART3->DR;		//DMA外设地址
	DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)USART3_DMA_TX_Buffer;	//发送缓存指针
	DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;					//DMA传输方向:内存--->外设
	DMA_InitStructure.DMA_BufferSize = USART3_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(DMA1_Stream3, &DMA_InitStructure);								//初始化DMA Stream
	DMA_Cmd(DMA1_Stream3, DISABLE); 										//开启DMA传输
}

2、USART3  RX DMA初始化程序

void USART3_DMA_Rx_Configuration(void)
{
	DMA_InitTypeDef  DMA_InitStructure;

	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1 , ENABLE);					//DMA2时钟使能
	DMA_DeInit(DMA1_Stream1);
	while (DMA_GetCmdStatus(DMA1_Stream1) != DISABLE);						//等待DMA可配置  
	DMA_InitStructure.DMA_Channel = DMA_Channel_4;  						//通道选择
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART3->DR;		//DMA外设地址
	DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)USART3_DMA_RX_Buffer;	//接收缓存指针
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory ;				//DMA传输方向:外设到存储器模式:外设--->内存
	DMA_InitStructure.DMA_BufferSize = USART3_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(DMA1_Stream1 , &DMA_InitStructure);							//初始化DMA_Stream	
	DMA_Cmd(DMA1_Stream1, ENABLE);  										//开启DMA传输
}

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

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

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

void USART3_Configuration(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
		
 
	USART_DeInit(USART3);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3 , ENABLE); 	//for USART2, USART3, UART4 or UART5.	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
	
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);     	

	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_10;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
	GPIO_Init(GPIOB, &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(USART3, &USART_InitStructure);
	USART_Cmd(USART3, ENABLE);

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

    USART_ITConfig(USART3, USART_IT_RXNE, DISABLE);				//禁止USART3接收不为空中断
	USART_ITConfig(USART3, USART_IT_TXE, DISABLE);				//禁止USART3发送空中断
	USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);				//开启USART3空闲中断 
	USART_ITConfig(USART3, USART_IT_TC, ENABLE);				//开启USART3传输完成中断 

	USART_DMACmd(USART3 ,   USART_DMAReq_Tx,ENABLE);  			//使能串口的DMA发送
	USART_DMACmd(USART3 ,   USART_DMAReq_Rx,ENABLE);  			//使能串口的DMA接收
}

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

void USART3_IRQHandler(void)
{
	int16_t ch;

	
	if (USART_GetITStatus(USART3,USART_IT_IDLE) != RESET)
	{		
		USART_ClearITPendingBit(USART3 , USART_IT_IDLE);	//必须先清除总线空闲中断标识,然后读一下数据寄存器,DMA接收才会正确(先读SR,然后读DR才能清除空闲中断标识)注意:这句必须要,否则不能够清除中断标志位。
		ch =  USART_ReceiveData(USART3);					//必须先清除总线空闲中断标识,然后读一下数据寄存器,DMA接收才会正确(先读SR,然后读DR才能清除空闲中断标识)注意:这句必须要,否则不能够清除中断标志位。
		
		#ifdef __DEBUG_stm32f407__
			__DEBUG_USART3_IT_IDLE++;
		#endif
		
		DMA_Cmd(DMA1_Stream1 , DISABLE); 			//关闭DMA,防止处理其间有数据
		DMA_ClearFlag(DMA1_Stream1 , DMA_FLAG_TCIF1 | DMA_FLAG_FEIF1 | DMA_FLAG_DMEIF1 | DMA_FLAG_TEIF1 | DMA_FLAG_HTIF1);
		ch = USART3_DMA_RX_BUFFER_MAX_LENGTH - DMA_GetCurrDataCounter(DMA1_Stream1);
		if (ch > 0)
		{
			USART3_Outtime_mark = TRUE;
			USART3_receCount = ch;
			memcpy(USART3_mscomm_buffer , USART3_DMA_RX_Buffer , USART3_receCount);
		}
		DMA_SetCurrDataCounter(DMA1_Stream1 , USART3_DMA_RX_BUFFER_MAX_LENGTH);
		DMA_Cmd(DMA1_Stream1, ENABLE);
	}
	
	else if (USART_GetITStatus(USART3,USART_IT_TC)!= RESET) 
	{
		USART_ClearITPendingBit(USART3, USART_IT_TC);
		
		#ifdef __DEBUG_stm32f407__
			__DEBUG_USART3_IT_TC++;
		#endif
		

		DMA_ClearFlag(DMA1_Stream3 , DMA_FLAG_TCIF3 | DMA_FLAG_FEIF3 | DMA_FLAG_DMEIF3 | DMA_FLAG_TEIF3 | DMA_FLAG_HTIF3);
		DMA_SetCurrDataCounter(DMA1_Stream3 , 0);	//清除数据长度
		GPIO_USART3_RS485_RECIVE_enable();
	}	
} 

6、主程序

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

本程序经过多款产品验证OK

  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个比较常见的应用场景。我可以给你提供一些思路和代码片段来实现这个功能。 1. 配置USART2串口 首先,你需要使用标准库函数来配置USART2串口。你需要设置波特率、数据位、停止位、校验位等参数。下面是一个示例代码: ```c #include "stm32f4xx.h" // 配置USART2串口 void USART2_Init(void) { USART_InitTypeDef USART_InitStruct; RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); USART_InitStruct.USART_BaudRate = 115200; USART_InitStruct.USART_WordLength = USART_WordLength_8b; USART_InitStruct.USART_StopBits = USART_StopBits_1; USART_InitStruct.USART_Parity = USART_Parity_No; USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART2, &USART_InitStruct); USART_Cmd(USART2, ENABLE); } ``` 2. 配置DMA 接下来,你需要配置DMADMA可以帮助你实现高效的数据传输。你需要配置DMA的通道、数据传输方向、数据缓冲区地址等参数。下面是一个示例代码: ```c #include "stm32f4xx.h" // 配置DMA void DMA_Init(void) { DMA_InitTypeDef DMA_InitStruct; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); DMA_InitStruct.DMA_Channel = DMA_Channel_4; DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&USART2->DR; DMA_InitStruct.DMA_Memory0BaseAddr = (uint32_t)buffer1; DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStruct.DMA_BufferSize = BUFFER_SIZE; DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStruct.DMA_Mode = DMA_Mode_Circular; DMA_InitStruct.DMA_Priority = DMA_Priority_High; DMA_InitStruct.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA1_Stream5, &DMA_InitStruct); DMA_Cmd(DMA1_Stream5, ENABLE); } ``` 在这个示例中,我们使用DMA1的Stream5通道,将数据从USART2的DR寄存器传输到缓冲区buffer1中。我们设置了循环模式,这样当缓冲区满了之后就会自动从缓冲区开头重新开始存储数据。 3. 配置乒乓缓冲区 为了避免在数据传输期间出现数据冲突,你需要使用乒乓缓冲区来处理数据。乒乓缓冲区就是在两个缓冲区之间进行切换,当一个缓冲区被DMA传输完毕后,另一个缓冲区就会被用来存储新的数据。下面是一个示例代码: ```c #include "stm32f4xx.h" #define BUFFER_SIZE 1024 uint8_t buffer1[BUFFER_SIZE]; uint8_t buffer2[BUFFER_SIZE]; volatile uint8_t *pRX = buffer1; volatile uint8_t *pTX = buffer2; // USART2 DMA发送数据 void USART2_DMA_Send(uint8_t *pData, uint16_t Length) { while(DMA_GetFlagStatus(DMA1_Stream6, DMA_FLAG_TCIF6) == RESET); DMA_ClearFlag(DMA1_Stream6, DMA_FLAG_TCIF6); DMA_Cmd(DMA1_Stream6, DISABLE); DMA_SetCurrDataCounter(DMA1_Stream6, Length); DMA_MemoryTargetConfig(DMA1_Stream6, (uint32_t)pData, DMA_Memory_0); DMA_Cmd(DMA1_Stream6, ENABLE); } // USART2 DMA接收数据 void USART2_DMA_Receive(void) { if(DMA_GetFlagStatus(DMA1_Stream5, DMA_FLAG_TCIF5) != RESET) { DMA_ClearFlag(DMA1_Stream5, DMA_FLAG_TCIF5); // 处理接收到的数据 // ... if(pRX == buffer1) { // 切换到buffer2 DMA_MemoryTargetConfig(DMA1_Stream5, (uint32_t)pTX, DMA_Memory_0); pRX = buffer2; pTX = buffer1; } else { // 切换到buffer1 DMA_MemoryTargetConfig(DMA1_Stream5, (uint32_t)pTX, DMA_Memory_0); pRX = buffer1; pTX = buffer2; } // 重新启动DMA传输 DMA_Cmd(DMA1_Stream5, ENABLE); } } ``` 在这个示例中,我们使用了两个缓冲区buffer1和buffer2,并且定义了两个指针pRX和pTX来指向当前使用的缓冲区。当DMA传输完成后,我们会检查当前使用的缓冲区,并且切换到另一个缓冲区来存储新的数据。然后重新启动DMA传输,以便继续接收数据。 4. 发送接收数据 最后,你需要编写代码来发送接收数据。你可以使用USART_SendData函数来发送数据使用USART_ReceiveData函数来接收数据。下面是一个示例代码: ```c #include "stm32f4xx.h" #define BUFFER_SIZE 1024 uint8_t buffer1[BUFFER_SIZE]; uint8_t buffer2[BUFFER_SIZE]; volatile uint8_t *pRX = buffer1; volatile uint8_t *pTX = buffer2; // 配置USART2串口 void USART2_Init(void) { USART_InitTypeDef USART_InitStruct; RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); USART_InitStruct.USART_BaudRate = 115200; USART_InitStruct.USART_WordLength = USART_WordLength_8b; USART_InitStruct.USART_StopBits = USART_StopBits_1; USART_InitStruct.USART_Parity = USART_Parity_No; USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART2, &USART_InitStruct); USART_Cmd(USART2, ENABLE); } // 配置DMA void DMA_Init(void) { DMA_InitTypeDef DMA_InitStruct; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); DMA_InitStruct.DMA_Channel = DMA_Channel_4; DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&USART2->DR; DMA_InitStruct.DMA_Memory0BaseAddr = (uint32_t)buffer1; DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStruct.DMA_BufferSize = BUFFER_SIZE; DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStruct.DMA_Mode = DMA_Mode_Circular; DMA_InitStruct.DMA_Priority = DMA_Priority_High; DMA_InitStruct.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA1_Stream5, &DMA_InitStruct); DMA_Cmd(DMA1_Stream5, ENABLE); } // USART2 DMA发送数据 void USART2_DMA_Send(uint8_t *pData, uint16_t Length) { while(DMA_GetFlagStatus(DMA1_Stream6, DMA_FLAG_TCIF6) == RESET); DMA_ClearFlag(DMA1_Stream6, DMA_FLAG_TCIF6); DMA_Cmd(DMA1_Stream6, DISABLE); DMA_SetCurrDataCounter(DMA1_Stream6, Length); DMA_MemoryTargetConfig(DMA1_Stream6, (uint32_t)pData, DMA_Memory_0); DMA_Cmd(DMA1_Stream6, ENABLE); } // USART2 DMA接收数据 void USART2_DMA_Receive(void) { if(DMA_GetFlagStatus(DMA1_Stream5, DMA_FLAG_TCIF5) != RESET) { DMA_ClearFlag(DMA1_Stream5, DMA_FLAG_TCIF5); // 处理接收到的数据 // ... if(pRX == buffer1) { // 切换到buffer2 DMA_MemoryTargetConfig(DMA1_Stream5, (uint32_t)pTX, DMA_Memory_0); pRX = buffer2; pTX = buffer1; } else { // 切换到buffer1 DMA_MemoryTargetConfig(DMA1_Stream5, (uint32_t)pTX, DMA_Memory_0); pRX = buffer1; pTX = buffer2; } // 重新启动DMA传输 DMA_Cmd(DMA1_Stream5, ENABLE); } } int main(void) { USART2_Init(); DMA_Init(); while(1) { // 发送数据 uint8_t data[] = "Hello World!"; USART2_DMA_Send(data, sizeof(data)); // 接收数据 USART2_DMA_Receive(); } } ``` 这个示例代码中,我们在主循环中不断发送接收数据。当需要发送数据时,我们调用USART2_DMA_Send函数,并传入要发送数据数据度。当需要接收数据时,我们调用USART2_DMA_Receive函数。在接收数据后,我们会对数据进行处理,并且切换到另一个缓冲区来存储新的数据
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值