一直在论坛学习感谢大家无私奉献,上传一份STM32F4 UART1 DMA发送和接收不定长度数据的代码STM32F4 串口收发使用DMA还是很方便的。但是配置DMA时需要配置数据长度,这一点对于发送来说可以预估计自己发送的长度来配置DMA发送数据长度,但是对于接收不是很好解决,因为如果使用DMA接收中断是要配置的数据长度减到0才能出发中断。但是我们无法判断接受数据的长度,导致无法判断数据接收完成。网上有提出的解决方法是用定时器固定周期的读DMA接收的长度来判断是否接收完成,也有使用UART的空闲中断来处理的。在这里我使用UART的空闲中断来处理接收不定长数据。当然也要打开DMA接收完成中断,处理数据接收超过DMA配置的长度导致的DMA接收中断。1.使用DMA发送时每次发送数据前需要配置发送的数据长度,此时要注意应先关闭DMA,然后配置数据长度,最后开启DMA发送,同时在DMA发送中断里面不要忘记清除相应的中断标志位。2.DMA接收长度达到配置长度后会导致接收中断,此时在中断处理函数内要先关闭DMA,然后读出数据长度,清掉相应的中断标志位,最后重新配置DMA接收长度并打开DMA接收。在这里的DMA中断指示为了防护一次性接收数据超过DMA配置长度。3.UART空闲中断,利用空闲中断可以很好地判断DMA接收不定长度的数据是否完成。初始化UART时打开空闲中断。当数据接收完成后会触发UART空闲中断。在中断内首先关闭DMA,读出DMA接收到的数据长度,清除DMA标志,重新配置DMA接收长度,清除空闲中断标志IDLE。这里要注意清除IDLE要由软件序列清除即依次读取USART1->SR;和USART1->DR;[mw_shl_code=c,true]
u8 ReceiveBuff[RECEIVE_BUF_SIZE]; //接收缓冲
u8 SendBuff[SEND_BUF_SIZE]; //发送数据缓冲区
u16 UART1_ReceiveSize = 0;
//初始化IO 串口1
//bound:波特率
void uart_init(u32 bound)
{
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //使能GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//使能USART1时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);//DMA2时钟使能
//串口1对应引脚复用映射
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); //GPIOA9复用为USART1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); //GPIOA10复用为USART1
//USART1端口配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; //GPIOA9与GPIOA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA9,PA10
//USART1 初始化设置
USART_InitStructure.USART_BaudRate = bound;//波特率设置
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
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); //初始化串口1
USART_Cmd(USART1, ENABLE); //使能串口1
//USART_ClearFlag(USART1, USART_FLAG_TC);
USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);//开启相关中断
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//串口1中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE); //使能串口1的DMA发送
USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE); //使能串口1的DMA接收
//****************************配置UART1发送
DMA_DeInit(DMA2_Stream7);
while (DMA_GetCmdStatus(DMA2_Stream7) != DISABLE);//等待DMA可配置
/* 配置 DMA Stream */
DMA_InitStructure.DMA_Channel = DMA_Channel_4; //通道选择
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART1->DR;//DMA外设地址
DMA_InitStructure.DMA_Memory0BaseAddr = (u32)SendBuff;//DMA 存储器0地址
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;//存储器到外设模式
DMA_InitStructure.DMA_BufferSize = SEND_BUF_SIZE;//数据传输量
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_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 NVIC
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream7_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_ITConfig(DMA2_Stream7,DMA_IT_TC,ENABLE);
//****************************配置UART1接收
DMA_DeInit(DMA2_Stream5);
while (DMA_GetCmdStatus(DMA2_Stream5) != DISABLE);//等待DMA可配置
/* 配置 DMA Stream */
DMA_InitStructure.DMA_Channel = DMA_Channel_4; //通道选择
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART1->DR;//DMA外设地址
DMA_InitStructure.DMA_Memory0BaseAddr = (u32)ReceiveBuff;//DMA 存储器0地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory ;//外设到存储器模式
DMA_InitStructure.DMA_BufferSize = RECEIVE_BUF_SIZE;//数据传输量
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_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 Stream
//DMA NVIC
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_Cmd(DMA2_Stream5, ENABLE); //开启DMA传输
DMA_ITConfig(DMA2_Stream5,DMA_IT_TC,ENABLE);
}
//开启一次DMA传输
//DMA_StreamxMA数据流,DMA1_Stream0~7/DMA2_Stream0~7
//ndtr:数据传输量
void SP0_DmaSendDataProc(DMA_Stream_TypeDef *DMA_Streamx,u16 ndtr)
{
DMA_Cmd(DMA_Streamx, DISABLE); //关闭DMA传输
while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE){} //确保DMA可以被设置
DMA_SetCurrDataCounter(DMA_Streamx,ndtr); //数据传输量
DMA_Cmd(DMA_Streamx, ENABLE); //开启DMA传输
}
///////////////////////////////////////////////////////////////////////////
//串口1
//发送单字节
void SP0_SendByteInfoProc(u8 nSendInfo)
{
u8 *pBuf = NULL;
/*/???
if (g_nSysUsart1SendOver_Flag != 0)
return;
*/
//指向发送缓冲区
pBuf = SendBuff;
*pBuf++ = nSendInfo;
SP0_DmaSendDataProc(DMA2_Stream7,1); //开始一次DMA传输!
}
//发送多字节
void SP0_SendBytesInfoProc(u8* pSendInfo, u16 nSendCount)
{
u16 i = 0;
u8 *pBuf = NULL;
/*/???
if (g_nSysUsart1SendOver_Flag != 0)
return;
*/
//指向发送缓冲区
pBuf = SendBuff;
for (i=0; i<nSendCount; i++)
{
*pBuf++ = pSendInfo;
}
//DMA发送方式
SP0_DmaSendDataProc(DMA2_Stream7,nSendCount); //开始一次DMA传输!
}
//发送完成中断
void DMA2_Stream7_IRQHandler(void)
{
//清除标志
if(DMA_GetFlagStatus(DMA2_Stream7,DMA_FLAG_TCIF7)!=RESET)//等待DMA2_Steam7传输完成
{
DMA_ClearFlag(DMA2_Stream7,DMA_FLAG_TCIF7);//清除DMA2_Steam7传输完成标志
}
}
//接收完成中断
void DMA2_Stream5_IRQHandler(void)
{
//清除标志
if(DMA_GetFlagStatus(DMA2_Stream5,DMA_FLAG_TCIF5)!=RESET)//等待DMA2_Steam7传输完成
{
DMA_Cmd(DMA2_Stream5, DISABLE); //关闭DMA,防止处理其间有数据
UART1_ReceiveSize =RECEIVE_BUF_SIZE - DMA_GetCurrDataCounter(DMA2_Stream5);
if(UART1_ReceiveSize !=0)
{
OSSemPost(DMAReceiveSize_Sem);
}
DMA_ClearFlag(DMA2_Stream5,DMA_FLAG_TCIF5 | DMA_FLAG_FEIF5 | DMA_FLAG_DMEIF5 | DMA_FLAG_TEIF5 | DMA_FLAG_HTIF5);//清除DMA2_Steam7传输完成标志
DMA_SetCurrDataCounter(DMA2_Stream5, RECEIVE_BUF_SIZE);
DMA_Cmd(DMA2_Stream5, ENABLE); //打开DMA
}
}
//空闲中断
void USART1_IRQHandler(void)
{
u16 data;
if(USART_GetITStatus(USART1,USART_IT_IDLE) != RESET)
{
DMA_Cmd(DMA2_Stream5, DISABLE); //关闭DMA,防止处理其间有数据
data = USART1->SR;
data = USART1->DR;
//USART_ClearFlag(USART1,USART_IT_IDLE);
UART1_ReceiveSize =RECEIVE_BUF_SIZE - DMA_GetCurrDataCounter(DMA2_Stream5);
if(UART1_ReceiveSize !=0)
{
OSSemPost(DMAReceiveSize_Sem);
}
DMA_ClearFlag(DMA2_Stream5,DMA_FLAG_TCIF5 | DMA_FLAG_FEIF5 | DMA_FLAG_DMEIF5 | DMA_FLAG_TEIF5 | DMA_FLAG_HTIF5);//清除DMA2_Steam7传输完成标志
DMA_SetCurrDataCounter(DMA2_Stream5, RECEIVE_BUF_SIZE);
DMA_Cmd(DMA2_Stream5, ENABLE); //打开DMA,
}
}
[/mw_shl_code]
补充内容 (2017-6-28 09:18):
上传一下代码,需要的朋友可以测试下。另外解决问题的可以帮忙回复下
一周热门 更多>