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

一、前言    

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

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

 

#define USART6_DMA_RX_BUFFER_MAX_LENGTH		(255)
#define USART6_DMA_TX_BUFFER_MAX_LENGTH		(255)
uint8_t USART6_DMA_RX_Buffer[USART6_DMA_RX_BUFFER_MAX_LENGTH];
uint8_t USART6_DMA_TX_Buffer[USART6_DMA_TX_BUFFER_MAX_LENGTH];

 1、USART6  TX DMA初始化程序


//是否可以这么认为,USART6如果DMA的TX使用DMA2_Stream6,则USART6如果DMA的RX必须使用DMA2_Stream1
//                 USART6如果DMA的TX使用DMA2_Stream7,则USART6如果DMA的RX必须使用DMA2_Stream2

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

2、USART6  RX DMA初始化程序

void USART6_DMA_Rx_Configuration(void)
{
	DMA_InitTypeDef  DMA_InitStructure;

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

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


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

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


void USART6_Configuration(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	
		
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);		//for USART1 and USART6 

	GPIO_PinAFConfig(GPIOG, GPIO_PinSource14, GPIO_AF_USART6);
	GPIO_PinAFConfig(GPIOG, GPIO_PinSource9, GPIO_AF_USART6);     	

	// 配置 PG14/USART6_TX 为复用功能 
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;	//输出类型为推挽 
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;	//内部上拉电阻使能 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;	//复用模式 
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOG, &GPIO_InitStructure);

	// 配置 PG9/USART6_RX 为复用功能 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_Init(GPIOG, &GPIO_InitStructure);

	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(USART6, &USART_InitStructure);
	USART_Cmd(USART6, ENABLE);
		
	USART_ClearFlag(USART6, USART_FLAG_TC); //清除发送完成标志	
	while (USART_GetFlagStatus(USART6, USART_FLAG_TC) == RESET);	//等待空闲帧发送完成后再清零发送完成标志(警告:如果不使能USART_Mode_Tx,会导致单片机在这里死机)
	USART_ClearFlag(USART6, USART_FLAG_TC);	//清除发送完成标志

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

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


void USART6_IRQHandler(void)
{
	int16_t ch;

	
	if (USART_GetITStatus(USART6 , USART_IT_IDLE) != RESET)
	{		
		USART_ClearITPendingBit(USART6 , USART_IT_IDLE);	//必须先清除总线空闲中断标识,然后读一下数据寄存器,DMA接收才会正确(先读SR,然后读DR才能清除空闲中断标识)注意:这句必须要,否则不能够清除中断标志位。
		ch =  USART_ReceiveData(USART6);					//必须先清除总线空闲中断标识,然后读一下数据寄存器,DMA接收才会正确(先读SR,然后读DR才能清除空闲中断标识)注意:这句必须要,否则不能够清除中断标志位。
		
		#ifdef __DEBUG_stm32f407__
			__DEBUG_USART6_IT_IDLE++;
		#endif
		
		DMA_Cmd(DMA2_Stream1 , DISABLE);
		DMA_ClearFlag(DMA2_Stream1 , DMA_FLAG_TCIF1 | DMA_FLAG_FEIF1 | DMA_FLAG_DMEIF1 | DMA_FLAG_TEIF1 | DMA_FLAG_HTIF1);
		ch = USART6_DMA_RX_BUFFER_MAX_LENGTH - DMA_GetCurrDataCounter(DMA2_Stream1);
		if (ch > 0)
		{
			//USART6_Timer_over = TRUE;
			USART6_receCount = ch;			
			WriteBufferTo_ringBuffer(camera_ring , USART6_DMA_RX_Buffer , USART6_receCount);			
		}
		DMA_SetCurrDataCounter(DMA2_Stream1 , USART6_DMA_RX_BUFFER_MAX_LENGTH);
		DMA_Cmd(DMA2_Stream1 , ENABLE);
	}
	
	else if (USART_GetITStatus(USART6 , USART_IT_TC)!= RESET) 
	{
		USART_ClearITPendingBit(USART6 , USART_IT_TC);
		
		#ifdef __DEBUG_stm32f407__
			__DEBUG_USART6_IT_TC++;
		#endif
		

		DMA_ClearFlag(DMA2_Stream6 , DMA_FLAG_TCIF6 | DMA_FLAG_FEIF6 | DMA_FLAG_DMEIF6 | DMA_FLAG_TEIF6 | DMA_FLAG_HTIF6);
		DMA_SetCurrDataCounter(DMA2_Stream6 , 0);
		GPIO_USART6_RS485_RECIVE_enable();
	}	
} 

6、主程序

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

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

  • 5
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
以下是基于STM32F103的RS485通信代码,其中包括初始化和发送数据的函数: ```c #include "stm32f10x.h" #include "stm32f10x_gpio.h" #include "stm32f10x_usart.h" #include "misc.h" GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; void USART1_Init(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 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); } void USART1_SendByte(uint8_t byte) { USART_SendData(USART1, byte); while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); } void USART1_SendString(const char* str) { while(*str) { USART1_SendByte(*str++); } } void RS485_SendData(uint8_t* data, uint8_t len) { GPIO_SetBits(GPIOA, GPIO_Pin_8); // set to transmit mode for(int i = 0; i < len; i++) { USART1_SendByte(*data++); } while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); GPIO_ResetBits(GPIOA, GPIO_Pin_8); // set to receive mode } int main(void) { USART1_Init(); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); while(1) { uint8_t data[] = {0x01, 0x02, 0x03, 0x04}; RS485_SendData(data, sizeof(data)); } } ``` 在上述代码中: 1. `USART1_Init` 函数初始化 USART1 端口,配置波特率为 9600,数据位宽为 8 位,停止位为 1,无校验位,同时设置为收发模式。 2. `USART1_SendByte` 函数向 USART1 端口发送一个字节的数据。 3. `USART1_SendString` 函数向 USART1 端口发送一个字符串。 4. `RS485_SendData` 函数将 RS485 的模式设置为发送模式,然后通过 USART1 端口发送数据,最后将 RS485 的模式设置为接收模式。 5. 在 `main` 函数中,先初始化 USART1 端口和 GPIO 端口,然后循环发送数据。 请注意,此代码仅供参考,实际应用中需要根据具体的需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值