STM32F1的串口使用DMA已经很多例程可见。有个项目使用到了STM32F4的DMA,我从F1的代码移植过来做了一些修改,以适配F4对应函数的一些差异。
本次代码的原理:
1.使用F4的串口1 和其他设备进行1发1收的异步通信,即发——收——发——收这种形式的通信。
2.发送使用原库函数,未使用DMA,接收过程为了减少对中断的调用,使用了DMA向缓冲区写数据,并在接收时使用空闲中断来判断接收帧的结束
以下为代码:
串口引脚及硬件的配置
void Uart_Init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* System Clocks Configuration */
//= System Clocks Configuration ====================================================================//
/* Enable GPIO UART1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE ); // 开启时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_DMA2, ENABLE);
//=NVIC_Configuration==============================================================================//
/* Configure the NVIC Preemption Priority Bits */
//NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; // 串口中断配置
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//=GPIO_Configuration==============================================================================//
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 ;
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
// //USART1配置
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;
/* Configure USART1 */
USART_InitStructure.USART_BaudRate = 115200; // 波特率设置
USART_Init(USART1, &USART_InitStructure);
/* Enable USART1 Receive and Transmit interrupts */
//USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // 不开启 串口接收字符中断
USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); //开启串口空闲中断
/* Enable the USART1 */
USART_Cmd(USART1, ENABLE); // 开启串口
USART_GetFlagStatus(USART1, USART_FLAG_TC);
}
DMA的配置
#define BUFLEN 32
uint8_t buf[BUFLEN];
void USART_DMA_Init(void)
{DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA2_Stream5);
DMA_InitStructure.DMA_Channel = DMA_Channel_4;
DMA_InitStructure.DMA_Memory0BaseAddr = (u32)&buf;
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART1->DR;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = sizeof(buf);
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_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_Mode = 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(DMA2_Stream5, &DMA_InitStructure);
DMA_Cmd(DMA2_Stream5, ENABLE); //使能DMA通道
USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
}
//开启一次DMA传输
void USARTDMA_Enable(void)
{
DMA_Cmd(DMA2_Stream5, DISABLE );
DMA_SetCurrDataCounter(DMA2_Stream5,sizeof(buf));
DMA_Cmd(DMA2_Stream5, ENABLE);
}
中断处理及调用:
//串口中断
void USART1_IRQHandler(void) //串口1中断服务程序
{
uint16_t ch;
//即使开启了接收字符中断,这里也不会再产生中断,被DMA劫持了
// if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) {
// buf[0]= USART1->DR;
// }
if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET) //接收中断
{
//在这里处理buf的内容
ch = USART1->SR;
ch = USART1->DR;//清除IDLE中断
}
}
//usart 发送
void USART1_SendBuffer( uint8_t * buf,uint8_t size){
uint8_t i=0;
while(i<size){
USART_SendData(USART1 , buf[i]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
i++;
}
}
//调用
void InitEQ(void){
Uart_Init();
USART_DMA_Init();
}
由于发——收——发——收的方式一般都是不定长的数据,所以使用DMA的定长接收并不合适。对于有些应用比如GPS数据,接收字符的超时IDLE中断可能会导致接收不完整,这种情况一般使用计数器或定时器控制超时时间,上述方法也不一定适用。上述方法适用于,接收的字符在发送方能确保不会超时的场合,比如不使用操作系统的单片机等。