环境:
主机:WIN8
开发环境:MDK5.13
mcu: stm32f407VGT6
阐明:
之前用STM32F103完结DMA收发串口数据,现在项目中选用STM32F407,所以将此机制移植到F4上。
STM32F103上用DMA收发串口数据文章:
STM32的串口选用DMA方法发送数据测试
STM32的串口选用DMA方法接纳数据测试
源代码:
串口初始化代码:
/** 初始化串口/static void init_uart(void){//界说中止结构体NVIC_InitTypeDef NVIC_InitStructure ;//界说IO初始化结构体GPIO_InitTypeDef GPIO_InitStructure;//界说串口结构体 USART_InitTypeDef USART_InitStructure;//界说DMA结构体DMA_InitTypeDef DMA_InitStructure;//翻开串口对应的外设时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);//串口发DMA装备 //发动DMA时钟RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);//DMA发送中止设置NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream6_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure);//DMA通道装备DMA_DeInit(DMA1_Stream6);DMA_InitStructure.DMA_Channel = DMA_Channel_4; //外设地址DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART2->DR);//内存地址DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Tx_Buf_Gsm;//dma传输方向DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;//设置DMA在传输时缓冲区的长度DMA_InitStructure.DMA_BufferSize = TX_LEN_GSM;//设置DMA的外设递加形式,一个外设DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//设置DMA的内存递加形式DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//外设数据字长DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//内存数据字长DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;//设置DMA的传输形式DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//设置DMA的优先等级DMA_InitStructure.DMA_Priority = DMA_Priority_High;//指定假如FIFO形式或直接形式将用于指定的流 : 不使能FIFO形式 DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; //指定了FIFO阈值水平DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; //指定的Burst搬运装备内存传输 DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; //指定的Burst搬运装备外围搬运 */ DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; //装备DMA1的通道 DMA_Init(DMA1_Stream6, &DMA_InitStructure); //使能中止DMA_ITConfig(DMA1_Stream6,DMA_IT_TC,ENABLE); //串口收DMA装备 //发动DMA时钟RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);//DMA通道装备DMA_DeInit(DMA1_Stream5);DMA_InitStructure.DMA_Channel = DMA_Channel_4;//外设地址DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART2->DR);//内存地址DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Rx_Buf_Gsm;//dma传输方向DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;//设置DMA在传输时缓冲区的长度DMA_InitStructure.DMA_BufferSize = RX_LEN_GSM;//设置DMA的外设递加形式,一个外设DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//设置DMA的内存递加形式DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//外设数据字长DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//内存数据字长DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//设置DMA的传输形式DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//设置DMA的优先等级DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;//指定假如FIFO形式或直接形式将用于指定的流 : 不使能FIFO形式 DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; //指定了FIFO阈值水平DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; //指定的Burst搬运装备内存传输 DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; //指定的Burst搬运装备外围搬运 */ DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; //装备DMA1的通道 DMA_Init(DMA1_Stream5, &DMA_InitStructure); //使能通道DMA_Cmd(DMA1_Stream5,ENABLE);//初始化串口参数 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_InitStructure.USART_BaudRate = DEFAULT_BAUD_GSM; //初始化串口 USART_Init(USART2,&USART_InitStructure); //中止装备USART_ITConfig(USART2,USART_IT_TC,DISABLE);USART_ITConfig(USART2,USART_IT_RXNE,DISABLE);USART_ITConfig(USART2,USART_IT_IDLE,ENABLE); //装备中止 //NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //通道设置为串口中止 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //中止占先等级NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //中止呼应优先级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //翻开中止 NVIC_Init(&NVIC_InitStructure); //选用DMA方法发送USART_DMACmd(USART2,USART_DMAReq_Tx,ENABLE);//选用DMA方法接纳USART_DMACmd(USART2,USART_DMAReq_Rx,ENABLE);//中止装备USART_ITConfig(USART2,USART_IT_TC,DISABLE);USART_ITConfig(USART2,USART_IT_RXNE,DISABLE);USART_ITConfig(USART2,USART_IT_TXE,DISABLE);USART_ITConfig(USART2,USART_IT_IDLE,ENABLE); //发动串口 USART_Cmd(USART2, ENABLE); //设置IO口时钟 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2); GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);//管脚形式:输出口GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //类型:推挽形式GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //上拉下拉设置GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //IO口速度GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//管脚指定GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;//初始化GPIO_Init(GPIOA, &GPIO_InitStructure);//管脚形式:输进口GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //上拉下拉设置GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //管脚指定GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//初始化GPIO_Init(GPIOA, &GPIO_InitStructure); }
发送数据:
/** 接口函数:向gsm模块发送数据*参数:data:发送数据寄存地址* size:发送数据字节数/void drv_gsm_tx(uint8_t *data,uint16_t size){//等候闲暇while (Flag_Tx_Gsm_Busy);Flag_Tx_Gsm_Busy = 1;//数据memcpy(Tx_Buf_Gsm,data,size);//设置传输数据长度DMA_SetCurrDataCounter(DMA1_Stream6,size);//翻开DMA,开端发送DMA_Cmd(DMA1_Stream6,ENABLE);}
中止处理函数:
/** 接口函数:DMA发送中止处理函数/void drv_gsm_deal_irq_dma_tx(void){if(DMA_GetITStatus(DMA1_Stream6,DMA_IT_TCIF6) != RESET) {//铲除标志位DMA_ClearFlag(DMA1_Stream6,DMA_FLAG_TCIF6);//封闭DMADMA_Cmd(DMA1_Stream6,DISABLE);//翻开发送完结中止,发送最终两个字节USART_ITConfig(USART2,USART_IT_TC,ENABLE);}}/** 处理发送完结中止*回来:0:未发生,1:现已发生/uint8_t drv_gsm_deal_irq_tx_end(void){if(USART_GetITStatus(USART2, USART_IT_TC) != RESET){//封闭发送完结中止USART_ITConfig(USART2,USART_IT_TC,DISABLE);//发送完结Flag_Tx_Gsm_Busy = 0;return 1;} return 0;}/** 处理接纳完结中止*参数:buf:接纳的数据* len:接纳的数据长度*回来:0:未发生,其他:现已发生,此值为接纳的数据长度/uint8_t drv_gsm_deal_irq_rx_end(uint8_t *buf){ uint16_t len = 0;//接纳完结中止if(USART_GetITStatus(USART2, USART_IT_IDLE) != RESET){USART2->SR;USART2->DR; //清USART_IT_IDLE标志//封闭DMADMA_Cmd(DMA1_Stream5,DISABLE);//铲除标志位DMA_ClearFlag(DMA1_Stream5,DMA_FLAG_TCIF5);//取得接纳帧帧长len = RX_LEN_GSM - DMA_GetCurrDataCounter(DMA1_Stream5);memcpy(buf,Rx_Buf_Gsm,len);//设置传输数据长度DMA_SetCurrDataCounter(DMA1_Stream5,RX_LEN_GSM);//翻开DMADMA_Cmd(DMA1_Stream5,ENABLE);return len;} return 0;}
中止函数:
/** GSM模块:DMA发送中止处理函数/void DMA1_Stream6_IRQHandler(void){gsm_dma_tx_irq_handler();}/** GSM模块:串口中止处理函数/void USART2_IRQHandler(void) {gsm_irq_handler();}
留意:
接纳完结中止中必需要铲除标志位
//铲除标志位DMA_ClearFlag(DMA1_Stream5,DMA_FLAG_TCIF5);
而且有必要在封闭DMA之后,这个查办在F103中并非有必要的
中止处理函数:
/** 接口函数:DMA发送中止处理函数/void gsm_dma_tx_irq_handler(void){inf_gsm_deal_irq_dma_tx();}/** 接口函数:串口中止处理函数*参数:data:接纳数据寄存地址*回来:接纳数据长度/void gsm_irq_handler(void) { struct _Gsm_Rx rx;uint8_t i = 0;//发送完结中止处理inf_gsm_deal_irq_tx_end();//接纳完结中止处理rx.len = inf_gsm_deal_irq_rx_end(rx.buf);if (rx.len != 0){//告诉观察者for (i = 0;i < Len_Observer;i++){Observer[i](rx);}} }
接口函数界说:
/** 接口函数:DMA发送中止处理函数/void inf_gsm_deal_irq_dma_tx(void){drv_gsm_deal_irq_dma_tx();}/** 处理发送完结中止*回来:0:未发生,1:现已发生/uint8_t inf_gsm_deal_irq_tx_end(void){return drv_gsm_deal_irq_tx_end();}/** 处理接纳完结中止*参数:buf:接纳的数据* len:接纳的数据长度*回来:0:未发生,其他:现已发生,此值为接纳的数据长度/uint8_t inf_gsm_deal_irq_rx_end(uint8_t *buf){return drv_gsm_deal_irq_rx_end(buf);}