一、前言
使用DMA通信的好处是,不占用单片机资源(不像普通串口中断,发送一个字节触发一次中断,发送100个字节触发100次中断;接收一个字节触发一次中断,接收200个字节触发200次中断),数据接收完毕触发一次DMA中断;发送数据完毕触发一次DMA中断。
下图是STM32F103单片机DMA通道关系图。
从上表可以观察到,串口使用DMA的对应关系如下:
USART1-RX使用DMA1的channel5
USART1-TX使用DMA1的channel4
USART2-RX使用DMA1的channel6
USART2-TX使用DMA1的channel7
USART3-RX使用DMA1的channel3
USART3-TX使用DMA1的channel2
UART4-RX使用DMA2的channel3
UART4-TX使用DMA2的channel5
#define UART4_DMA_RX_BUFFER_MAX_LENGTH (255)
#define UART4_DMA_TX_BUFFER_MAX_LENGTH (255)
uint8_t UART4_DMA_RX_Buffer[UART4_DMA_RX_BUFFER_MAX_LENGTH];
uint8_t UART4_DMA_TX_Buffer[UART4_DMA_TX_BUFFER_MAX_LENGTH];
1、UART4 TX DMA初始化程序
void UART4_DMA_Tx_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2 , ENABLE); //DMA2时钟使能
DMA_DeInit(DMA2_Channel5);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&UART4->DR; //DMA外设地址
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)UART4_DMA_TX_Buffer; //发送缓存指针
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //传输方向
DMA_InitStructure.DMA_BufferSize = UART4_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; //外设数据宽度:BYTE
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //内存数据宽度:BYTE
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //循环模式:否//(注:DMA_Mode_Normal为正常模式,DMA_Mode_Circular为循环模式)
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; //优先级:高
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //内存:内存(都)
DMA_Init(DMA2_Channel5 , &DMA_InitStructure); //初始化DMA2_Channel5
DMA_ClearFlag(DMA2_FLAG_GL5);
DMA_Cmd(DMA2_Channel5 , DISABLE); //开启DMA传输
}
2、UART4 RX DMA初始化程序
void UART4_DMA_Rx_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2 , ENABLE); //DMA2时钟使能
DMA_DeInit(DMA2_Channel3);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&UART4->DR);
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)UART4_DMA_RX_Buffer; //接收缓存指针
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = UART4_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;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //(注:DMA_Mode_Normal为正常模式,DMA_Mode_Circular为循环模式)
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA2_Channel3, &DMA_InitStructure);
DMA_ClearFlag(DMA2_FLAG_GL3);
DMA_Cmd(DMA2_Channel3 , ENABLE);
}
3、UART 启动DMA发送初始化程序
void UART4_DMA_Begin_Send(uint8_t *send_buffer , uint16_t nSendBytes)
{
if (nSendBytes < UART4_DMA_TX_BUFFER_MAX_LENGTH)
{
memcpy(UART4_DMA_TX_Buffer , send_buffer , nSendBytes);
DMA_Cmd(DMA2_Channel5 , DISABLE); //关闭DMA传输
DMA_SetCurrDataCounter(DMA2_Channel5 , nSendBytes); //数据传输量
DMA_Cmd(DMA2_Channel5 , ENABLE); //开启DMA传输
}
}
4、UART4 DMA方式端口初始化程序(包含DMA配置)
//
//DMA中断方式
//
void UART4_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
// config USART4 clock
RCC_APB1PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB1Periph_UART4, ENABLE);
// Configure USART4 Tx (PC.10) as alternate function push-pull
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure); // PC.10
// Configure USART4 Rx (PC.11) as input floating
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure); // PC.11
// USART4 mode config
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(UART4, &USART_InitStructure);
//CPU的小缺陷:串口配置好,如果直接Send,则第1个字节发送不出去如下语句解决第1个字节无法正确发送出去的问题
//USART_ClearFlag(UART4, USART_FLAG_TC); //清发送完成标志,Transmission Complete flag
//USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
//USART_Cmd(UART4, ENABLE);
USART_Cmd(UART4, ENABLE);
USART_ClearFlag(UART4, USART_FLAG_TC); //清除发送完成标志
while (USART_GetFlagStatus(UART4, USART_FLAG_TC) == RESET);//等待空闲帧发送完成后再清零发送完成标志(警告:如果不使能USART_Mode_Tx,会导致单片机在这里死机)
USART_ClearFlag(UART4, USART_FLAG_TC); //清除发送完成标志
USART_ITConfig(UART4, USART_IT_RXNE, DISABLE);
USART_ITConfig(UART4, USART_IT_TXE, DISABLE);
USART_ITConfig(UART4, USART_IT_IDLE, ENABLE);
USART_ITConfig(UART4, USART_IT_TC, ENABLE);
USART_DMACmd(UART4 , USART_DMAReq_Tx,ENABLE);
USART_DMACmd(UART4 , USART_DMAReq_Rx,ENABLE);
//错误:
//1、以下情况初始化STM32F013VC单片机串口4,则单片机会死机
// USART_ClearFlag(UART4, USART_FLAG_TC); //清除发送完成标志
// while (USART_GetFlagStatus(UART4, USART_FLAG_TC) == RESET); //等待空闲帧发送完成后再清零发送完成标志(警告:如果不使能USART_Mode_Tx,会导致单片机在这里死机)
// USART_ClearFlag(UART4, USART_FLAG_TC); //清除发送完成标志
// USART_Cmd(UART4, ENABLE);
//正确:
//2、以下情况初始化STM32F013VC单片机串口4,则正常运行
// USART_Cmd(UART4, ENABLE);
// USART_ClearFlag(UART4, USART_FLAG_TC); //清除发送完成标志
// while (USART_GetFlagStatus(UART4, USART_FLAG_TC) == RESET); //等待空闲帧发送完成后再清零发送完成标志(警告:如果不使能USART_Mode_Tx,会导致单片机在这里死机)
// USART_ClearFlag(UART4, USART_FLAG_TC); //清除发送完成标志
//总结:
//3、因此USART_Cmd(UART4, ENABLE);必须放在while (USART_GetFlagStatus(UART4, USART_FLAG_TC) == RESET);前面执行
}
5、UART4 DMA中断接收和DMA中断发送
//
//DMA中断方式
//
void UART4_IRQHandler(void)
{
uint16_t ch;
// uint8_t Buffer[10];
//BaseType_t xHigherPriorityTaskWoken = pdFALSE;
if (USART_GetITStatus(UART4,USART_IT_IDLE) != RESET)
{
USART_ClearITPendingBit(UART4 , USART_IT_IDLE); //必须先清除总线空闲中断标识,然后读一下数据寄存器,DMA接收才会正确(先读SR,然后读DR才能清除空闲中断标识)注意:这句必须要,否则不能够清除中断标志位。
ch = USART_ReceiveData(UART4); //必须先清除总线空闲中断标识,然后读一下数据寄存器,DMA接收才会正确(先读SR,然后读DR才能清除空闲中断标识)注意:这句必须要,否则不能够清除中断标志位。
//关闭DMA ,防止干扰
DMA_Cmd(DMA2_Channel3 , DISABLE); //关闭DMA,防止处理其间有数据
//清DMA标志位
DMA_ClearFlag(DMA2_FLAG_GL3 | DMA2_FLAG_TC3 | DMA2_FLAG_HT3 | DMA2_FLAG_TE3);
//获取接收到的数据长度 单位为字节
ch = UART4_DMA_RX_BUFFER_MAX_LENGTH - DMA_GetCurrDataCounter(DMA2_Channel3);
if (ch > 0)
{
MB_UART4.Outtime_mark = TRUE;
MB_UART4.receCount = ch;
memcpy(MB_UART4.mscomm_buffer , UART4_DMA_RX_Buffer , MB_UART4.receCount);
//MB_UART4.mscomm_buffer[ch] = '\0'; //自动添加结束符
//WriteBufferTo_ringBuffer(GPS_ring , UART4_DMA_RX_Buffer , ch);
}
//重新赋值计数值,必须大于等于最大可能接收到的数据帧数目
DMA_SetCurrDataCounter(DMA2_Channel3 , UART4_DMA_RX_BUFFER_MAX_LENGTH);
DMA_Cmd(DMA2_Channel3, ENABLE);
//-----------------------------------------------
//由于UART4使用DMA通信,所以不需要3.5字符静止时间
//-----------------------------------------------
//TIM_Cmd(TIM1, DISABLE);
//TIM_SetCounter(TIM1, 0x00);
//TIM_Cmd(TIM1, ENABLE);
//xSemaphoreGiveFromISR(xSemaphoreGPS, &xHigherPriorityTaskWoken);//发送同步信号
//如果xHigherPriorityTaskWoken = pdTRUE,那么退出中断后切到当前最高优先级任务执行
//portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
//Buffer[0] = 1;
//xQueueSendToBackFromISR(xQueue , Buffer , &xHigherPriorityTaskWoken);
//portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
else if (USART_GetITStatus(UART4,USART_IT_TC)!= RESET)
{
USART_ClearITPendingBit(UART4, USART_IT_TC);
DMA_ClearFlag(DMA2_FLAG_GL5 | DMA2_FLAG_TC5 | DMA2_FLAG_HT5 | DMA2_FLAG_TE5);
DMA_SetCurrDataCounter(DMA2_Channel5 , 0);
}
}
6、主程序
void main(void)
{
UART4_Configuration();
UART4_DMA_Tx_Configuration();
UART4_DMA_Rx_Configuration();
while (1)
{
//在合适的时候调用UART4_DMA_Begin_Send(uint8_t *send_buffer , uint16_t nSendBytes)
// 通过DMA中断方式将数据发送出去
}
}
本程序经过多款产品验证OK