STM32F1非阻塞DMA+USART程序模板

此程序是STM32F1系列单片机使用非阻塞异步DMA+USART发送与接收的程序,适合单片机部分处理速度大于上位机的情形。程序使用DMA进行数据传输,串口空闲标志位查询方式接收,几乎不占用程序处理时间。

#include "stm32f10x.h"
#include <string.h>
uint8_t USART1_TxBuf[256];
uint8_t USART1_RxBuf[256];
void USART1_Init(uint32_t baud) //初始化USART1
{
   GPIO_InitTypeDef GPIO_InitStructure;
   USART_InitTypeDef USART_InitStructure;
   DMA_InitTypeDef DMA_InitStructure;
   
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
   
   //GPIO初始化
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
   GPIO_Init(GPIOA, &GPIO_InitStructure);
   
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//输入上拉
   GPIO_Init(GPIOA, &GPIO_InitStructure);
   
   //USART初始化
   USART_DeInit(USART1); //复位USART1外设
   USART_InitStructure.USART_BaudRate = baud; //串口波特率
   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_Tx | USART_Mode_Rx; //使能收发
   USART_Init(USART1, &USART_InitStructure); //初始化串口
   USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); //开启串口空闲中断 不配置NVIC只使用中断标志位
   USART_DMACmd(USART1, USART_DMAReq_Tx | USART_DMAReq_Rx, ENABLE); //开启串口DMA请求
   
   //DMA初始化
   DMA_DeInit(DMA1_Channel4); //USART1_Tx
   DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART1->DR);
   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)USART1_TxBuf;
   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //内存到外设
   DMA_InitStructure.DMA_BufferSize = sizeof(USART1_TxBuf); //仅占位作用
   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_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
   
   DMA_Init(DMA1_Channel4, &DMA_InitStructure); //USART1_Tx
   
   DMA_DeInit(DMA1_Channel5); //USART1_Rx
   DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART1->DR);
   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)USART1_RxBuf;
   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //外设到内存
   DMA_InitStructure.DMA_BufferSize = sizeof(USART1_RxBuf);;
   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_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
   
   DMA_Init(DMA1_Channel5, &DMA_InitStructure); //USART1_Rx
   
   //开启DMA与串口
   DMA_Cmd(DMA1_Channel5, ENABLE); //USART1_Rx
   USART_Cmd(USART1, ENABLE);//使能串口
}
uint16_t USART1_Read(void *buf) { //读取串口接收缓冲区
   uint16_t buf_size;
   
   if(USART_GetITStatus(USART1, USART_IT_IDLE) != ENABLE) { //没有接收到数据
      return 0;
   }
   
   DMA_Cmd(DMA1_Channel5, DISABLE); //关闭USART1_Rx DMA以配置
   USART_ReceiveData(USART1); //先读SR 再读DR 清除标志位
   
   buf_size = sizeof(USART1_RxBuf) - DMA_GetCurrDataCounter(DMA1_Channel5); //获取数据大小
   memcpy(buf, USART1_RxBuf, buf_size); //复制缓冲区
   memset(USART1_RxBuf, 0, sizeof(USART1_RxBuf)); //清空数据区
   DMA_ClearFlag(DMA1_FLAG_TC5); //清除传输完成标志位 防止溢出
   
   DMA_SetCurrDataCounter(DMA1_Channel5, sizeof(USART1_RxBuf)); //重新设定数据大小
   DMA_Cmd(DMA1_Channel5, ENABLE); //重新开启USART1_Rx DMA
   
   return buf_size;
}
void USART1_Write(void *buf, uint16_t size) { //串口发送
   DMA_Cmd(DMA1_Channel4, DISABLE); //关闭USART1_Tx DMA以配置
   
   DMA_SetCurrDataCounter(DMA1_Channel4, size); //设置发送数据大小
   memcpy(USART1_TxBuf, buf, size); //复制缓冲区
   DMA_ClearFlag(DMA1_FLAG_TC4); //清除传输完成标志位
   
   DMA_Cmd(DMA1_Channel4, ENABLE); //重新开启USART1_Tx DMA 开始发送
}