STM32F407的串口采用DMA收發(fā)數(shù)據(jù)
主機(jī):WIN8
本文引用地址:http://m.butianyuan.cn/article/201611/318272.htm開(kāi)發(fā)環(huán)境:MDK5.13
mcu: stm32f407VGT6
說(shuō)明:
之前用STM32F103實(shí)現(xiàn)DMA收發(fā)串口數(shù)據(jù),現(xiàn)在項(xiàng)目中采用STM32F407,所以將此機(jī)制移植到F4上。
STM32F103上用DMA收發(fā)串口數(shù)據(jù)文章:
STM32的串口采用DMA方式發(fā)送數(shù)據(jù)測(cè)試
STM32的串口采用DMA方式接收數(shù)據(jù)測(cè)試
源代碼:
串口初始化代碼:
/** 初始化串口/static void init_uart(void){//定義中斷結(jié)構(gòu)體NVIC_InitTypeDef NVIC_InitStructure ;//定義IO初始化結(jié)構(gòu)體GPIO_InitTypeDef GPIO_InitStructure;//定義串口結(jié)構(gòu)體 USART_InitTypeDef USART_InitStructure;//定義DMA結(jié)構(gòu)體DMA_InitTypeDef DMA_InitStructure;//打開(kāi)串口對(duì)應(yīng)的外設(shè)時(shí)鐘 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);//串口發(fā)DMA配置 //啟動(dòng)DMA時(shí)鐘RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);//DMA發(fā)送中斷設(shè)置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; //外設(shè)地址DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART2->DR);//內(nèi)存地址DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Tx_Buf_Gsm;//dma傳輸方向DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;//設(shè)置DMA在傳輸時(shí)緩沖區(qū)的長(zhǎng)度DMA_InitStructure.DMA_BufferSize = TX_LEN_GSM;//設(shè)置DMA的外設(shè)遞增模式,一個(gè)外設(shè)DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//設(shè)置DMA的內(nèi)存遞增模式DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//外設(shè)數(shù)據(jù)字長(zhǎng)DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//內(nèi)存數(shù)據(jù)字長(zhǎng)DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;//設(shè)置DMA的傳輸模式DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//設(shè)置DMA的優(yōu)先級(jí)別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轉(zhuǎn)移配置內(nèi)存?zhèn)鬏?DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; //指定的Burst轉(zhuǎn)移配置外圍轉(zhuǎn)移 */ DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; //配置DMA1的通道 DMA_Init(DMA1_Stream6, &DMA_InitStructure); //使能中斷DMA_ITConfig(DMA1_Stream6,DMA_IT_TC,ENABLE); //串口收DMA配置 //啟動(dòng)DMA時(shí)鐘RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);//DMA通道配置DMA_DeInit(DMA1_Stream5);DMA_InitStructure.DMA_Channel = DMA_Channel_4;//外設(shè)地址DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART2->DR);//內(nèi)存地址DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Rx_Buf_Gsm;//dma傳輸方向DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;//設(shè)置DMA在傳輸時(shí)緩沖區(qū)的長(zhǎng)度DMA_InitStructure.DMA_BufferSize = RX_LEN_GSM;//設(shè)置DMA的外設(shè)遞增模式,一個(gè)外設(shè)DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//設(shè)置DMA的內(nèi)存遞增模式DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//外設(shè)數(shù)據(jù)字長(zhǎng)DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//內(nèi)存數(shù)據(jù)字長(zhǎng)DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//設(shè)置DMA的傳輸模式DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//設(shè)置DMA的優(yōu)先級(jí)別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轉(zhuǎn)移配置內(nèi)存?zhèn)鬏?DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; //指定的Burst轉(zhuǎn)移配置外圍轉(zhuǎn)移 */ DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; //配置DMA1的通道 DMA_Init(DMA1_Stream5, &DMA_InitStructure); //使能通道DMA_Cmd(DMA1_Stream5,ENABLE);//初始化串口參數(shù) 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; //通道設(shè)置為串口中斷 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //中斷占先等級(jí)NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //中斷響應(yīng)優(yōu)先級(jí) NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打開(kāi)中斷 NVIC_Init(&NVIC_InitStructure); //采用DMA方式發(fā)送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); //啟動(dòng)串口 USART_Cmd(USART2, ENABLE); //設(shè)置IO口時(shí)鐘 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; //上拉下拉設(shè)置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; //上拉下拉設(shè)置GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //管腳指定GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//初始化GPIO_Init(GPIOA, &GPIO_InitStructure); }
發(fā)送數(shù)據(jù):
/** 接口函數(shù):向gsm模塊發(fā)送數(shù)據(jù)*參數(shù):data:發(fā)送數(shù)據(jù)存放地址* size:發(fā)送數(shù)據(jù)字節(jié)數(shù)/void drv_gsm_tx(uint8_t *data,uint16_t size){//等待空閑while (Flag_Tx_Gsm_Busy);Flag_Tx_Gsm_Busy = 1;//數(shù)據(jù)memcpy(Tx_Buf_Gsm,data,size);//設(shè)置傳輸數(shù)據(jù)長(zhǎng)度DMA_SetCurrDataCounter(DMA1_Stream6,size);//打開(kāi)DMA,開(kāi)始發(fā)送DMA_Cmd(DMA1_Stream6,ENABLE);}
中斷處理函數(shù):
/** 接口函數(shù):DMA發(fā)送中斷處理函數(shù)/void drv_gsm_deal_irq_dma_tx(void){if(DMA_GetITStatus(DMA1_Stream6,DMA_IT_TCIF6) != RESET) {//清除標(biāo)志位DMA_ClearFlag(DMA1_Stream6,DMA_FLAG_TCIF6);//關(guān)閉DMADMA_Cmd(DMA1_Stream6,DISABLE);//打開(kāi)發(fā)送完成中斷,發(fā)送最后兩個(gè)字節(jié)USART_ITConfig(USART2,USART_IT_TC,ENABLE);}}/** 處理發(fā)送完成中斷*返回:0:未產(chǎn)生,1:已經(jīng)產(chǎn)生/uint8_t drv_gsm_deal_irq_tx_end(void){if(USART_GetITStatus(USART2, USART_IT_TC) != RESET){//關(guān)閉發(fā)送完成中斷USART_ITConfig(USART2,USART_IT_TC,DISABLE);//發(fā)送完成Flag_Tx_Gsm_Busy = 0;return 1;} return 0;}/** 處理接收完成中斷*參數(shù):buf:接收的數(shù)據(jù)* len:接收的數(shù)據(jù)長(zhǎng)度*返回:0:未產(chǎn)生,其他:已經(jīng)產(chǎn)生,此值為接收的數(shù)據(jù)長(zhǎng)度/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標(biāo)志//關(guān)閉DMADMA_Cmd(DMA1_Stream5,DISABLE);//清除標(biāo)志位DMA_ClearFlag(DMA1_Stream5,DMA_FLAG_TCIF5);//獲得接收幀幀長(zhǎng)len = RX_LEN_GSM - DMA_GetCurrDataCounter(DMA1_Stream5);memcpy(buf,Rx_Buf_Gsm,len);//設(shè)置傳輸數(shù)據(jù)長(zhǎng)度DMA_SetCurrDataCounter(DMA1_Stream5,RX_LEN_GSM);//打開(kāi)DMADMA_Cmd(DMA1_Stream5,ENABLE);return len;} return 0;}
中斷函數(shù):
/** GSM模塊:DMA發(fā)送中斷處理函數(shù)/void DMA1_Stream6_IRQHandler(void){gsm_dma_tx_irq_handler();}/** GSM模塊:串口中斷處理函數(shù)/void USART2_IRQHandler(void) {gsm_irq_handler();}
注意:
接收完成中斷中必須要清除標(biāo)志位
//清除標(biāo)志位DMA_ClearFlag(DMA1_Stream5,DMA_FLAG_TCIF5);并且必須在關(guān)閉DMA之后,這個(gè)語(yǔ)句在F103中并非必須的
中斷處理函數(shù):
/** 接口函數(shù):DMA發(fā)送中斷處理函數(shù)/void gsm_dma_tx_irq_handler(void){inf_gsm_deal_irq_dma_tx();}/** 接口函數(shù):串口中斷處理函數(shù)*參數(shù):data:接收數(shù)據(jù)存放地址*返回:接收數(shù)據(jù)長(zhǎng)度/void gsm_irq_handler(void) { struct _Gsm_Rx rx;uint8_t i = 0;//發(fā)送完成中斷處理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);}} }
接口函數(shù)定義:
/** 接口函數(shù):DMA發(fā)送中斷處理函數(shù)/void inf_gsm_deal_irq_dma_tx(void){drv_gsm_deal_irq_dma_tx();}/** 處理發(fā)送完成中斷*返回:0:未產(chǎn)生,1:已經(jīng)產(chǎn)生/uint8_t inf_gsm_deal_irq_tx_end(void){return drv_gsm_deal_irq_tx_end();}/** 處理接收完成中斷*參數(shù):buf:接收的數(shù)據(jù)* len:接收的數(shù)據(jù)長(zhǎng)度*返回:0:未產(chǎn)生,其他:已經(jīng)產(chǎn)生,此值為接收的數(shù)據(jù)長(zhǎng)度/uint8_t inf_gsm_deal_irq_rx_end(uint8_t *buf){return drv_gsm_deal_irq_rx_end(buf);}
評(píng)論