使用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 USART3_DMA_RX_BUFFER_MAX_LENGTH (255)
- #define USART3_DMA_TX_BUFFER_MAX_LENGTH (255)
- uint8_t USART3_DMA_RX_Buffer[USART3_DMA_RX_BUFFER_MAX_LENGTH];
- uint8_t USART3_DMA_TX_Buffer[USART3_DMA_TX_BUFFER_MAX_LENGTH];
1、USART3 TX DMA初始化程序
- void USART3_DMA_Tx_Configuration(void)
- {
- DMA_InitTypeDef DMA_InitStructure;
-
-
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE); //DMA1时钟使能
- DMA_DeInit(DMA1_Channel2);
- DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART3->DR; //DMA外设地址
- DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)USART3_DMA_TX_Buffer; //发送缓存指针
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //传输方向
- DMA_InitStructure.DMA_BufferSize = USART3_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(DMA1_Channel2 , &DMA_InitStructure); //初始化DMA1_Channel2
- DMA_ClearFlag(DMA1_FLAG_GL2);
- DMA_Cmd(DMA1_Channel2 , DISABLE); //开启DMA传输
- }
2、USART3 RX DMA初始化程序
- void USART3_DMA_Rx_Configuration(void)
- {
- DMA_InitTypeDef DMA_InitStructure;
-
-
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE); //DMA1时钟使能
- DMA_DeInit(DMA1_Channel3);
- DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART3->DR);
- DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)USART3_DMA_RX_Buffer; //接收缓存指针
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
- DMA_InitStructure.DMA_BufferSize = USART3_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(DMA1_Channel3, &DMA_InitStructure);
- DMA_ClearFlag(DMA1_FLAG_GL3);
- DMA_Cmd(DMA1_Channel3 , ENABLE);
- }
3、USART3 启动DMA发送初始化程序
- void USART3_DMA_Begin_Send(uint8_t *send_buffer , uint16_t nSendBytes)
- {
- if (nSendBytes < USART3_DMA_TX_BUFFER_MAX_LENGTH)
- {
- memcpy(USART3_DMA_TX_Buffer , send_buffer , nSendBytes);
- DMA_Cmd(DMA1_Channel2 , DISABLE); //关闭DMA传输
- DMA_SetCurrDataCounter(DMA1_Channel2 , nSendBytes); //数据传输量
- DMA_Cmd(DMA1_Channel2 , ENABLE); //开启DMA传输
- }
- }
4、USART3 DMA方式端口初始化程序(包含DMA配置)
- //
- //DMA中断方式
- //
- void USART3_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
-
-
- // config USART3 clock
- RCC_APB1PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB1Periph_USART3, ENABLE);
-
- // Configure USART3 Tx (PB.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(GPIOB, &GPIO_InitStructure); // PB.10
-
- // Configure USART3 Rx (PB.11) as input floating
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOB, &GPIO_InitStructure); // PB.11
-
- // USART3 mode config
- USART_InitStructure.USART_BaudRate = 57600; //USR-WIFI232-B2(WIFI)默认使用57600
- 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(USART3, &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(USART3, ENABLE);
- USART_ClearFlag(USART3, USART_FLAG_TC); //清除发送完成标志
- while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);//等待空闲帧发送完成后再清零发送完成标志(警告:如果不使能USART_Mode_Tx,会导致单片机在这里死机)
- USART_ClearFlag(USART3, USART_FLAG_TC); //清除发送完成标志
-
- USART_ITConfig(USART3, USART_IT_RXNE, DISABLE);
- USART_ITConfig(USART3, USART_IT_TXE, DISABLE);
- USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);
- USART_ITConfig(USART3, USART_IT_TC, ENABLE);
- USART_DMACmd(USART3 , USART_DMAReq_Tx,ENABLE);
- USART_DMACmd(USART3 , USART_DMAReq_Rx,ENABLE);
-
- //错误:
- //1、以下情况初始化STM32F013VC单片机串口3,则单片机会死机
- // USART_ClearFlag(USART3, USART_FLAG_TC); //清除发送完成标志
- // while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET); //等待空闲帧发送完成后再清零发送完成标志(警告:如果不使能USART_Mode_Tx,会导致单片机在这里死机)
- // USART_ClearFlag(USART3, USART_FLAG_TC); //清除发送完成标志
- // USART_Cmd(USART1, ENABLE);
- //正确:
- //2、以下情况初始化STM32F013VC单片机串口3,则正常运行
- // USART_Cmd(USART3, ENABLE);
- // USART_ClearFlag(USART3, USART_FLAG_TC); //清除发送完成标志
- // while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET); //等待空闲帧发送完成后再清零发送完成标志(警告:如果不使能USART_Mode_Tx,会导致单片机在这里死机)
- // USART_ClearFlag(USART3, USART_FLAG_TC); //清除发送完成标志
- //总结:
- //3、因此USART_Cmd(USART3, ENABLE);必须放在while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);前面执行
- }
5、USART3 DMA中断接收和DMA中断发送
- //
- //DMA中断方式
- //
- void USART3_IRQHandler(void)
- {
- uint16_t ch;
- uint8_t Buffer[10];
- BaseType_t xHigherPriorityTaskWoken = pdFALSE;
-
-
- if (USART_GetITStatus(USART3,USART_IT_IDLE) != RESET)
- {
- USART_ClearITPendingBit(USART3 , USART_IT_IDLE); //必须先清除总线空闲中断标识,然后读一下数据寄存器,DMA接收才会正确(先读SR,然后读DR才能清除空闲中断标识)注意:这句必须要,否则不能够清除中断标志位。
- ch = USART_ReceiveData(USART3); //必须先清除总线空闲中断标识,然后读一下数据寄存器,DMA接收才会正确(先读SR,然后读DR才能清除空闲中断标识)注意:这句必须要,否则不能够清除中断标志位。
-
- DMA_Cmd(DMA1_Channel3 , DISABLE); //关闭DMA,防止处理其间有数据
- DMA_ClearFlag(DMA1_FLAG_GL3 | DMA1_FLAG_TC3 | DMA1_FLAG_HT3 | DMA1_FLAG_TE3);
- ch = USART3_DMA_RX_BUFFER_MAX_LENGTH - DMA_GetCurrDataCounter(DMA1_Channel3);
- if (ch > 0)
- {
- //MB_USART3.Outtime_mark = TRUE;
- MB_USART3.receCount = ch;
- memcpy(MB_USART3.mscomm_buffer , USART3_DMA_RX_Buffer , MB_USART3.receCount);
- }
- DMA_SetCurrDataCounter(DMA1_Channel3 , USART3_DMA_RX_BUFFER_MAX_LENGTH);
- DMA_Cmd(DMA1_Channel3, ENABLE);
- //-----------------------------------------------
- //由于USART3使用DMA通信,所以不需要3.5字符静止时间
- //-----------------------------------------------
- //TIM_Cmd(TIM1, DISABLE);
- //TIM_SetCounter(TIM1, 0x00);
- //TIM_Cmd(TIM1, ENABLE);
-
- //xSemaphoreGiveFromISR(xSemaphoreMODBUS, &xHigherPriorityTaskWoken);//发送同步信号
- //如果xHigherPriorityTaskWoken = pdTRUE,那么退出中断后切到当前最高优先级任务执行
- //portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
-
- Buffer[0] = 1;
- xQueueSendToBackFromISR(xQueue , Buffer , &xHigherPriorityTaskWoken);
- portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
- }
- else if (USART_GetITStatus(USART3,USART_IT_TC)!= RESET)
- {
- USART_ClearITPendingBit(USART3, USART_IT_TC);
- DMA_ClearFlag(DMA1_FLAG_GL2 | DMA1_FLAG_TC2 | DMA1_FLAG_HT2 | DMA1_FLAG_TE2);
- DMA_SetCurrDataCounter(DMA1_Channel2 , 0);
- }
- }
6、主程序
- void main(void)
- {
- USART3_Configuration();
- USART3_DMA_Tx_Configuration();
- USART3_DMA_Rx_Configuration();
-
- while (1)
- {
- //在合适的时候调用USART3_DMA_Begin_Send(uint8_t *send_buffer , uint16_t nSendBytes)
- // 通过DMA中断方式将数据发送出去
- }
- }
本程序经过多款产品验证OK