<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          STM32 DMA 串口透傳

          作者: 時間:2016-11-19 來源:網(wǎng)絡(luò) 收藏
          一直沒有好好的搗鼓過DMA,在調(diào)BLE項目的時候,遇到了DMA串口傳輸?shù)膯栴},傷心流涕甚長時間!!!

          DMA 網(wǎng)上的解釋一大堆,簡單總結(jié)一下:

          本文引用地址:http://www.ex-cimer.com/article/201611/318108.htm

          DMA處理過程全部是由硬件來實現(xiàn)的,速度很快!

          DMA在專門的DMA控制下,實現(xiàn)高速外設(shè)與主存儲期之間自動成批量的數(shù)據(jù)交換。

          通常有兩種交換方式:1獨占總線方式 2周期挪用方式

          DMA傳送過程

          DMA預(yù)處理

          DMA數(shù)據(jù)傳送

          DMA結(jié)束處理將總線控制權(quán)交換給CPU

          DMA內(nèi)部寄存器

          地址寄存器:存放DMA傳輸是儲存單元地址

          字節(jié)寄存器:存放DMA傳輸字節(jié)數(shù)

          控制寄存器:存放CPU設(shè)定的DMA傳輸方式

          狀態(tài)寄存器:存放DMAC當前的狀態(tài)

          DMA外設(shè)地址:基地址+偏移地址

          兩種方法設(shè)定地址:

          #defineUSART1_DR_Base 0x40013804

          #defineUSART1_DR_Base (&(USART1->DR)) 覺得還是第二種方法比較直接

          DMA兩種中斷觸發(fā)方式:

          DMA_IT_TC1傳輸完成中斷:每次更新發(fā)生在計數(shù)>=設(shè)定值

          DMA_IT_HT1傳輸過半中斷:每次更新發(fā)生在計數(shù)>(設(shè)定值/2)

          DMA_IT_GL1全局中斷:(實驗了幾次和過半中斷傳輸情況一致)

          編程的時候根據(jù)DMA請求映像來判斷通道號,然后找到目的地址和原地址基本上就算完事兒了。當然得分清是傳輸是外設(shè)和內(nèi)存,內(nèi)存和外設(shè),內(nèi)存和內(nèi)存三種情況,會有 (DMA_InitTypeDef 結(jié)構(gòu)體).DMA_M2M 進行設(shè)置。

          下面是DMA串口收發(fā)簡單例子,并沒有用到FIFO精確收發(fā),有時間再好好寫寫~

          DMA_Configuration()是DMA5通道 串口1接收通道配置函數(shù)

          DMA_Configuration2()是DMA4通道 串口1發(fā)送通道配置函數(shù)

          對于發(fā)送 可以每當發(fā)送的時候 都執(zhí)行DMA_Configuration2(),進行發(fā)送操作

          對于接收 理應(yīng)采用FIFO,對于接收數(shù)組進行處理,這里只是簡單的接收,沒有進一步處理

          /* Includes ------------------------------------------------------------------*/#include "stm32f10x.h"#include "stm32f10x_tim.h" #include "stm32f10x_rcc.h"#include "stm32f10x_map.h"	#include "stm32f10x_it.h"	  #include "misc.h" #include "bsp_timer.h"#include "stm32f10x_usart.h"#include "stm32f10x_dma.h"#include "stdio.h"#define SENDBUFF_SIZE   10vu8 SendBuff[SENDBUFF_SIZE]={0};vu8 TxBuffer[] = "hello xiao lei !!! ";vu8 NbrOfDataToTransfer = sizeof(TxBuffer)-1;GPIO_InitTypeDef GPIO_InitStructure;#define LED1_ON GPIO_SetBits(GPIOB, GPIO_Pin_5);  #define LED1_OFF GPIO_ResetBits(GPIOB, GPIO_Pin_5); #define LED2_ON GPIO_SetBits(GPIOD, GPIO_Pin_6);  #define LED2_OFF GPIO_ResetBits(GPIOD, GPIO_Pin_6); #define LED3_ON GPIO_SetBits(GPIOD, GPIO_Pin_3);  #define LED3_OFF GPIO_ResetBits(GPIOD, GPIO_Pin_3);  void RCC_Configuration(void);void LED_Config(void);void Delay(__IO uint32_t nCount);int fputc(int ch, FILE *f){USART_SendData(USART1, (u8) ch);while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET){}return ch;}/* 名    稱:void LED_Config(void)* 功    能:LED 控制初始化函數(shù)* 入口參數(shù):無* 出口參數(shù):無* 說    明:* 調(diào)用方法:無 / void LED_Config(void){RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOD , ENABLE);	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;				     //LED1  V6	   //將V6,V7,V8 配置為通用推挽輸出  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;			 //口線翻轉(zhuǎn)速度為50MHzGPIO_Init(GPIOB, &GPIO_InitStructure);					 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_3;		 //LED2, LED3	 V7 V8GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_Init(GPIOD, &GPIO_InitStructure);GPIO_Init(GPIOD, &GPIO_InitStructure);GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; 			      //LCD背光控制GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_Init(GPIOD, &GPIO_InitStructure);GPIO_ResetBits(GPIOD, GPIO_Pin_13);			              //LCD背光關(guān)閉	}void USART1_Configration(){USART_InitTypeDef USART_InitStructure;  GPIO_InitTypeDef GPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);//USART1_TXGPIO_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);//USART1_RXGPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;GPIO_Init(GPIOA, &GPIO_InitStructure);USART_InitStructure.USART_BaudRate = 9600;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_Tx | USART_Mode_Rx;USART_Init(USART1, &USART_InitStructure);USART_Cmd(USART1, ENABLE);}void DMA_Configuration(void){DMA_InitTypeDef DMA_InitStructure;NVIC_InitTypeDef NVIC_InitStructure;RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //使能DMA傳輸DMA_DeInit(DMA1_Channel5);DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&(USART1->DR));DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SendBuff;DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;                              //外設(shè)作源頭//外設(shè)是作為數(shù)據(jù)傳輸?shù)哪康牡剡€是來源DMA_InitStructure.DMA_BufferSize = 5;                                           //DMA緩存的大小 單位在下邊設(shè)定DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;                //外設(shè)地址寄存器不遞增DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;                         //內(nèi)存地址遞增DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;         //外設(shè)字節(jié)為單位DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;             //內(nèi)存字節(jié)為單位DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;                                 //工作在循環(huán)緩存模式DMA_InitStructure.DMA_Priority = DMA_Priority_High;                             //4優(yōu)先級之一的(高優(yōu)先) VeryHigh/High/Medium/LowDMA_InitStructure.DMA_M2M = DMA_M2M_Disable;                                    //非內(nèi)存到內(nèi)存DMA_Init(DMA1_Channel5, &DMA_InitStructure);                                    //根據(jù)DMA_InitStruct中指定的參數(shù)初始化DMA的通道1寄存器DMA_ITConfig(DMA1_Channel5, DMA_IT_HT, ENABLE);                                 //DMA5半傳輸完成中斷USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);                                    //使能USART1的接收DMA請求DMA_Cmd(DMA1_Channel5, ENABLE);          DMA_ClearFlag(DMA1_FLAG_TC5); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQn;  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority =  0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);}void DMA_Configuration2(void){DMA_InitTypeDef DMA_InitStructure;NVIC_InitTypeDef NVIC_InitStructure;RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //使能DMA傳輸DMA_DeInit(DMA1_Channel4);DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&(USART1->DR));DMA_InitStructure.DMA_MemoryBaseAddr = (u32)TxBuffer;DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST ;                             //外設(shè)作源頭//外設(shè)是作為數(shù)據(jù)傳輸?shù)哪康牡剡€是來源DMA_InitStructure.DMA_BufferSize = NbrOfDataToTransfer;                         //DMA緩存的大小 單位在下邊設(shè)定DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;                //外設(shè)地址寄存器不遞增DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;                         //內(nèi)存地址遞增DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;         //外設(shè)字節(jié)為單位DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;             //內(nèi)存字節(jié)為單位DMA_InitStructure.DMA_Mode = DMA_Mode_Normal ;                                  //工作在循環(huán)緩存模式DMA_InitStructure.DMA_Priority = DMA_Priority_High;                             //4優(yōu)先級之一的(高優(yōu)先) VeryHigh/High/Medium/LowDMA_InitStructure.DMA_M2M = DMA_M2M_Disable;                                    //內(nèi)存到非內(nèi)存DMA_Init(DMA1_Channel4, &DMA_InitStructure);                                    //根據(jù)DMA_InitStruct中指定的參數(shù)初始化DMA的通道1寄存器DMA_ITConfig(DMA1_Channel4, DMA_IT_TC , ENABLE);                                //DMA4傳輸完成中斷USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);                                    //使能USART1的發(fā)送DMA請求DMA_Cmd(DMA1_Channel4, ENABLE);          DMA_ClearFlag(DMA1_FLAG_TC4); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority =  1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);}/* 名    稱:int main(void)* 功    能:主函數(shù)* 入口參數(shù):無* 出口參數(shù):無* 說    明:* 調(diào)用方法:無 / int main(void){int i,j;RCC_Configuration();   				//系統(tǒng)時鐘配置//TIM3_Configuration();NVIC_Configuration();			LED_Config();	USART1_Configration();DMA_Configuration();	//DMA_Configuration2();//LED控制配置// printf("Start DMA transmission!rn");//LED1_ON ;for(i=0;i<1000000;i++);// for(j=0;j<20;j++);	DMA_Configuration2();while (1){}}/* 名    稱:void RCC_Configuration(void)* 功    能:系統(tǒng)時鐘配置為72MHZ* 入口參數(shù):無* 出口參數(shù):無* 說    明:* 調(diào)用方法:無 / void RCC_Configuration(void){   SystemInit();}void DMA1_Channel5_IRQHandler(void){u8 end;int i;	  printf("Start DMA transmission!rn");/ 全傳輸完成*///   if(DMA_GetITStatus(DMA1_IT_TC5))//    {  //  end = DMA_GetCurrDataCounter(DMA1_Channel5);//  DMA_ClearITPendingBit(DMA1_IT_GL5);///* 清中斷源//  DMA_ClearFlag(DMA1_FLAG_TC5);   // //       printf("AA%dAA:", end);//       printf("%srn", SendBuff);//	  for(i=0;i<100000;i++); //   }/// 半傳輸完成*/if(DMA_GetITStatus(DMA1_FLAG_HT5)){  end = DMA_GetCurrDataCounter(DMA1_Channel5);DMA_ClearITPendingBit(DMA1_IT_GL5);///* 清中斷源DMA_ClearFlag(DMA1_IT_TC5);   printf("AA%dAA:", end);printf("%srn", SendBuff);for(i=0;i<100000;i++); }//}void DMA1_Channel4_IRQHandler(void){if(DMA_GetITStatus(DMA1_IT_TC4)==SET){DMA_ClearFlag(DMA1_IT_TC4);//TCIE,TE,RE			printf("AAAA!");}}


          關(guān)鍵詞: STM32DMA串口透

          評論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();