本文以STM32F1xx的串口1为例,简单呈现下使用DMA中断连续发送的代码
串口DMA配置通常可以分为2个部分:
1.串口配置
2.DMA配置
void UART1_Init(u32 bound){
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE); //使能USART1,GPIOA时钟
//USART1_TX GPIOA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART1_RX GPIOA.10初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
//USART 初始化设置
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_ITConfig(USART1,USART_IT_IDLE,ENABLE); //使能空闲中断
USART_Cmd(USART1, ENABLE); //使能串口1
}
2.DMA配置
本代码封装了下DMA配置
void DMA_Config(DMA_Channel_TypeDef*DMA_CHx,u32 cpar,u32 cmar,u32 dma_dir,u32 priority,u16 bufsize)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //使能DMA传输
DMA_DeInit(DMA_CHx); //将DMA的通道1寄存器重设为缺省值
DMA_InitStructure.DMA_PeripheralBaseAddr = cpar; //DMA外设ADC基地址
DMA_InitStructure.DMA_MemoryBaseAddr = cmar; //DMA内存基地址
DMA_InitStructure.DMA_DIR = dma_dir; //数据传输方向,从内存读取发送到外设
DMA_InitStructure.DMA_BufferSize = bufsize; //DMA通道的DMA缓存的大小
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 = priority; //DMA通道 x拥有中优先级
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //DMA通道x没有设置为内存到内存传输
DMA_Init(DMA_CHx, &DMA_InitStructure);
}
void UART_DMA_Config{
NVIC_InitTypeDef NVIC_InitStructure;
//配置串口1发送DMA
DMA_Config(DMA1_Channel4,(u32)&USART1->DR,(u32)USART1_TX_BUF,DMA_DIR_PeripheralDST,DMA_Priority_Medium,0);//DMA1通道4,外设为串口1Tx,存储器为USART_TX_BUF
//配置串口1接收DMA
DMA_Config(DMA1_Channel5,(u32)&USART1->DR,(u32)USART1_RX_BUF,DMA_DIR_PeripheralSRC,DMA_Priority_Medium,0);//DMA1通道5,外设为串口1Rx,存储器为USART_RX_BUF
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE); //开USART1 Tx DMA中断
USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE); //使能串口3的DMA接收
USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE); //使能串口3的DMA接收
}
最后是中断函数
void USART1_IRQHandler(void) //串口1中断服务程序
{
if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
{
USART1_RX_CNT = USART1->SR;
USART1_RX_CNT = USART1->DR; //清USART_IT_IDLE标志
DMA_Cmd(DMA1_Channel5,DISABLE);
USART_ITConfig(USART1, USART_IT_IDLE, DISABLE); //静止中断,防覆盖
USART1_RX_CNT = USART1_REC_LEN - DMA_GetCurrDataCounter(DMA1_Channel5);
DMA_SetCurrDataCounter(DMA1_Channel5,USART1_REC_LEN);
DMA_Cmd(DMA1_Channel5,ENABLE);
USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);
}
}
//串口1 TX DMA终端
void DMA1_Channel4_IRQHandler(void)
{
if(DMA_GetITStatus(DMA1_FLAG_TC4))
{
DMA_ClearFlag(DMA1_FLAG_TC4);
if(Txed_Num > Max_Tx_Cnt)
{
Txed_Num = 0;
return;
}
TX_DATA(DATA_Len);
Txed_Num = Txed_Num + 1;
}
}
//DMA发送代码
void TX_DATA(u8 len)
{
while(DMA_GetCurrDataCounter(DMA1_Channel4)!=0); //等待通道7传输完成
DMA_Cmd(DMA1_Channel4, DISABLE ); //关闭 指示的通道
DMA_SetCurrDataCounter(DMA1_Channel4,len);//DMA通道的DMA缓存的大小
DMA_Cmd(DMA1_Channel4, ENABLE); //开启DMA传输
}
总结
串口DMA发送中断配置时,易遗漏
DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE);
本例是DMA发送中断在 连续、分次发送 场景下的应用。