串口采用DMA方式收发数据有两种不同的方式,第一种方式:采用DMA传输完成中断进行发送和接收;第二种方式:采用串口总线空闲方式收发数据。这两种方式第二种方式更好一些,因为第二种方式可以收发不定长度的数据帧,然而第一种方式不能。但是第二种方式的逻辑复杂一些,收发过程之前都要判断总线是否是空闲。
在此,以USART2的DMA收发方式举例:
一、使用DMA传输完成中断收发
整体思路:上位机发送四个字节的数据,STM32接收完成后进入DMA中断中,发送下位机STM32定义好的数据给上位机并且清除DMA传输完成中断标志位,最后进入发送完成中断,关闭发送通道,清除DMA发送完成标志位。
在上述思路之前,要进行的自然是串口配置、DMA配置以及中断配置。此处的配置函数如下:
/******************************************
**函数名称:UpperUsart2Init
**函数参数:baudRate 波特率
**函数作用:初始化与上位机通讯的串口Usart2
**硬件引脚:TX--PD5 RX--PD6
******************************************/
void UpperUsart2Init(int baudRate)
/******************************************
**函数名称:UpperUsart2Init
**函数参数:baudRate 波特率
**函数作用:初始化与上位机通讯的串口Usart2
**硬件引脚:TX--PD5 RX--PD6
******************************************/
void UpperUsart2Init(int baudRate)
{
//开启串口时钟、DMA时钟以及相应GPIO时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
GPIO_PinAFConfig(GPIOD,GPIO_PinSource5,GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOD,GPIO_PinSource6,GPIO_AF_USART2);
//PD8(TX)设置成复用推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //| GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
//PD9(RX)设置成浮空输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD, &GPIO_InitStructure);
// USART2_DMA_RX DMA1_Stream5 DMA_Channel_4
DMA_DeInit(DMA1_Stream5);
DMA_InitStructure.DMA_Channel = DMA_Channel_4;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART2->DR);
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&upperRxBuffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = UPPERRBSIZE;
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_Circular;//如果是Normal只能接受一次,故采用循环模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream5, &DMA_InitStructure);
// USART2_DMA_TX DMA1_Stream6 DMA_Channel_4
DMA_DeInit(DMA1_Stream6);
DMA_InitStructure.DMA_Channel = DMA_Channel_4;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &(USART2->DR);
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&upperTxBuffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize = UPPERTBSIZE;
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_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream6, &DMA_InitStructure);
//USART2设置 115200 8 1 0 NONE
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_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART2,&USART_InitStructure);
// Configure one bit for preemption priority
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
// Enable DMA1_Stream5 Interrupt
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// Enable DMA1_Stream6 Interrupt
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// Enable USART2 Interrupt
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 5;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//开启串口、DMA和串口总线空闲中断
DMA_Cmd(DMA1_Stream5,ENABLE);
DMA_Cmd(DMA1_Stream6,DISABLE);
USART_DMACmd(USART2,USART_DMAReq_Tx,ENABLE);
USART_DMACmd(USART2,USART_DMAReq_Rx,ENABLE);
DMA_ITConfig(DMA1_Stream5, DMA_IT_TC, ENABLE);
DMA_ITConfig(DMA1_Stream6, DMA_IT_TC, ENABLE);
DMA_ClearITPendingBit(DMA1_Stream5, DMA_IT_TCIF5); //标志位设置为默认值
DMA_ClearITPendingBit(DMA1_Stream6, DMA_IT_TCIF6);
USART_ITConfig(USART2,USART_IT_IDLE,ENABLE);
// USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
}
上述程序段功能是配置串口、DMA以及相应中断。(一定要注意开启时钟)
在本程序中STM32发送给上位机的数据为uint8_t data[4] = {0x01,0x03,0x04,0x06};
下面这个中断是DMA接收完成中断,完成功能是:清除接收完成标志位,并且向上位机发送预定义数据。
/******************************************
**函数名称:DMA1_Stream5_IRQHandler
**函数参数:无
**函数作用:串口2 DMA接受完成时发送数据给上位机
******************************************/
void DMA1_Stream5_IRQHandler(void)
{
if(SET == DMA_GetITStatus(DMA1_Stream5, DMA_IT_TCIF5))
{
DMA_Cmd(DMA1_Stream5,DISABLE);
DMA_ClearFlag(DMA1_Stream5,DMA_FLAG_TCIF5);
DMA_Cmd(DMA1_Stream5,ENABLE);
DMA_SetCurrDataCounter(DMA1_Stream6,UPPERTBSIZE);
memcpy(upperTxBuffer,data,UPPERTBSIZE);
DMA_Cmd(DMA1_Stream6,ENABLE);
}
}
下面这个中断是DMA传输完成中断,完成功能是:清除发送完成标志位。
/******************************************
**函数名称:DMA1_Stream6_IRQHandler
**函数参数:无
**函数作用:串口2发送完成时中断入口函数,关闭传输通道并且清除标志
******************************************/
void DMA1_Stream6_IRQHandler(void) //UART2_TX
{
if(SET == DMA_GetITStatus(DMA1_Stream6,DMA_IT_TCIF6))
{
DMA_Cmd(DMA1_Stream6,DISABLE);
DMA_ClearFlag(DMA1_Stream6, DMA_FLAG_TCIF6);
}
}
上一篇:STM32 串口采用DMA方式接收数据
下一篇:STM32F429串口IDLE中断+DMA接收串口数据
推荐阅读最新更新时间:2024-03-16 16:05