使用DMA通信的好处是,不占用单片机资源(不像普通串口中断,发送一个字节触发一次中断,发送100个字节触发100次中断;接收一个字节触发一次中断,接收200个字节触发200次中断),数据接收完毕触发一次DMA中断;发送数据完毕触发一次DMA中断。
下图是STM32F407单片机DMA通道关系图。
#define UART5_DMA_RX_BUFFER_MAX_LENGTH (255)
#define UART5_DMA_TX_BUFFER_MAX_LENGTH (255)
uint8_t UART5_DMA_RX_Buffer[UART5_DMA_RX_BUFFER_MAX_LENGTH];
uint8_t UART5_DMA_TX_Buffer[UART5_DMA_TX_BUFFER_MAX_LENGTH];
1、UART5 TX DMA初始化程序
void UART5_DMA_Tx_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1 , ENABLE); //DMA1时钟使能
DMA_DeInit(DMA1_Stream7);
while (DMA_GetCmdStatus(DMA1_Stream7) != DISABLE); //等待DMA可配置
DMA_InitStructure.DMA_Channel = DMA_Channel_4; //DMA通道配置
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&UART5->DR; //DMA外设地址
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)UART5_DMA_TX_Buffer; //发送缓存指针
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; //DMA传输方向:内存--->外设
DMA_InitStructure.DMA_BufferSize = UART5_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(DMA1_Stream7, &DMA_InitStructure); //初始化DMA Stream
DMA_Cmd(DMA1_Stream7, DISABLE); //关闭DMA传输
}
2、UART5 RX DMA初始化程序
void UART5_DMA_Rx_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1 , ENABLE); //DMA1时钟使能
DMA_DeInit(DMA1_Stream0);
while (DMA_GetCmdStatus(DMA1_Stream0) != DISABLE); //等待DMA可配置
DMA_InitStructure.DMA_Channel = DMA_Channel_4; //通道选择
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&UART5->DR; //DMA外设地址
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)UART5_DMA_RX_Buffer; //接收缓存指针
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory ; //DMA传输方向:外设到存储器模式:外设--->内存
DMA_InitStructure.DMA_BufferSize = UART5_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(DMA1_Stream0 , &DMA_InitStructure);
DMA_Cmd(DMA1_Stream0, ENABLE); //开启DMA传输
}
3、UART5 启动DMA发送初始化程序
void UART5_DMA_Begin_Send(uint8_t *send_buffer , uint16_t nSendCount)
{
if (nSendCount < UART5_DMA_TX_BUFFER_MAX_LENGTH)
{
memcpy(UART5_DMA_TX_Buffer , send_buffer , nSendCount);
DMA_Cmd(DMA1_Stream7 , DISABLE); //关闭DMA传输
while (DMA_GetCmdStatus(DMA1_Stream7) != DISABLE); //确保DMA可以被设置
DMA_SetCurrDataCounter(DMA1_Stream7 , nSendCount); //数据传输量
DMA_Cmd(DMA1_Stream7 , ENABLE); //开启DMA传输
}
}
4、UART5 DMA方式端口初始化程序(包含DMA配置)
void UART5_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_DeInit(UART5);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5 , ENABLE); //for USART2, USART3, UART4 or UART5.
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_UART5);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_UART5);
// Configure UART5 Tx (PC.12) as alternate function push-pull
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
// Configure UART5 Rx (PD.2) as input floating
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOD, &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(UART5, &USART_InitStructure);
USART_Cmd(UART5, ENABLE);
USART_ClearFlag(UART5, USART_FLAG_TC); //清除发送完成标志
while (USART_GetFlagStatus(UART5, USART_FLAG_TC) == RESET); //等待空闲帧发送完成后再清零发送完成标志(警告:如果不使能USART_Mode_Tx,会导致单片机在这里死机)
USART_ClearFlag(UART5, USART_FLAG_TC); //清除发送完成标志
USART_ClearFlag(UART5, USART_FLAG_TC); //清除发送完成标志
USART_ITConfig(UART5, USART_IT_RXNE, DISABLE); //禁止USART1接收不为空中断
USART_ITConfig(UART5, USART_IT_TXE, DISABLE); //禁止USART1发送空中断
USART_ITConfig(UART5, USART_IT_TC, ENABLE); //开启USART1传输完成中断
USART_ITConfig(UART5, USART_IT_IDLE, ENABLE); //开启USART1空闲中断
USART_DMACmd(UART5 , USART_DMAReq_Tx,ENABLE); //使能串口5的DMA发送
USART_DMACmd(UART5 , USART_DMAReq_Rx,ENABLE); //使能串口5的DMA接收
}
5、UART5 DMA中断接收和DMA中断发送
void UART5_IRQHandler(void)
{
uint16_t ch;
if (USART_GetITStatus(UART5 , USART_IT_IDLE) != RESET)
{
USART_ClearITPendingBit(UART5 , USART_IT_IDLE); //必须先清除总线空闲中断标识,然后读一下数据寄存器,DMA接收才会正确(先读SR,然后读DR才能清除空闲中断标识)注意:这句必须要,否则不能够清除中断标志位。
ch = USART_ReceiveData(UART5); //必须先清除总线空闲中断标识,然后读一下数据寄存器,DMA接收才会正确(先读SR,然后读DR才能清除空闲中断标识)注意:这句必须要,否则不能够清除中断标志位。
#ifdef __DEBUG_stm32f407__
__DEBUG_UART5_IT_IDLE++;
#endif
DMA_Cmd(DMA1_Stream0 , DISABLE); //关闭DMA,防止处理其间有数据
DMA_ClearFlag(DMA1_Stream0 , DMA_FLAG_TCIF0 | DMA_FLAG_FEIF0 | DMA_FLAG_DMEIF0 | DMA_FLAG_TEIF0 | DMA_FLAG_HTIF0);//清零标志位
ch = UART5_DMA_RX_BUFFER_MAX_LENGTH - DMA_GetCurrDataCounter(DMA1_Stream0);
if (ch > 0)
{
UART5_Timer_over = TRUE;
UART5_receCount = ch;
memcpy(UART5_mscomm_buffer , UART5_DMA_RX_Buffer , UART5_receCount);
#ifdef __DEBUG_stm32f407__
if (TCPIP_Connected_Status == TCPIP_CONNECTED_OK)
__DEBUG_UART5_DMA_TCP_recv_count++;
#endif
}
DMA_SetCurrDataCounter(DMA1_Stream0 , UART5_DMA_RX_BUFFER_MAX_LENGTH);
DMA_Cmd(DMA1_Stream0 , ENABLE);
}
else if (USART_GetITStatus(UART5 , USART_IT_TC) != RESET)
{
USART_ClearITPendingBit(UART5, USART_IT_TC);
#ifdef __DEBUG_stm32f407__
__DEBUG_UART5_IT_TC++;
#endif
DMA_ClearFlag(DMA1_Stream7 , DMA_FLAG_TCIF7 | DMA_FLAG_FEIF7 | DMA_FLAG_DMEIF7 | DMA_FLAG_TEIF7 | DMA_FLAG_HTIF7);//清零标志位
DMA_SetCurrDataCounter(DMA1_Stream7 , 0); //清除数据长度
}
}
6、主程序
void main(void)
{
UART5_Configuration();
UART5_DMA_Tx_Configuration();
UART5_DMA_Rx_Configuration();
while (1)
{
//在合适的时候调用UART5_DMA_Begin_Send(uint8_t *send_buffer , uint16_t nSendBytes)
// 通过DMA中断方式将数据发送出去
}
}
本程序经过多款产品验证OK