用DMA直接驅(qū)動(dòng)GPIO,實(shí)現(xiàn)GPIO最高輸出速率
掃描二維碼
隨時(shí)隨地手機(jī)看文章
STM32F303芯片,72M的主頻,GPIO的達(dá)到了14.4M的翻轉(zhuǎn)速率
再來上代碼:
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
GPIOC->MODER |= 0x55555555;
GPIOC->OSPEEDR |= 0xFFFFFFF;
GPIOC->PUPDR |= 0x55555555;
u32 Gpio_data[4]= {0x0000FFFF,0xFFFF0000,0x0000FFFF,0xFFFF0000};
DMA_InitTypeDef DMA_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* TIMx clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
/* DMAx clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_DeInit(DMA1_Channel7);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&(GPIOC->BSRR));
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Gpio_data;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 4;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Enable;
DMA_Init(DMA1_Channel7, &DMA_InitStructure);
DMA1_Channel7->CCR |= DMA_CCR_EN;
用DMA內(nèi)存到內(nèi)存的模式,直接把Gpio_data的數(shù)據(jù)循環(huán)的搬到GPIOC的BSRR寄存器上來控制GPIOC上電平的翻轉(zhuǎn),這樣使得GPIO的速度達(dá)到了最快,輸出70ns的脈寬,這已經(jīng)是達(dá)到了DMA總線帶寬的極限,要想再提高速度的話,就得提高STM32芯片的主頻。
再來看用代碼來實(shí)現(xiàn)的話,GPIO能達(dá)到多快的速度。
先是用庫函數(shù)來操作,在Main函數(shù)的While循環(huán)來翻轉(zhuǎn)電平
while(1)
{
GPIO_Write(GPIOC,0xffff);
GPIO_Write(GPIOC,0x0000);
}
可以看到最快只能輸出320ns的脈寬電平,而且高低電平脈寬還不一樣,那是因?yàn)樘幚韜hile(1)占用了CPU的時(shí)間導(dǎo)致的。
再來看用寄存器直接操作GPIO
while(1)
{
GPIOC->ODR = 0xFFFF;
GPIOC->ODR = 0x0000;
}
可以看到,最小脈寬電平可以達(dá)到40ns,直接操作寄存器的速度明顯要到庫函數(shù)操作要快好多,低電平的時(shí)間要比高電平的時(shí)間長(zhǎng)很多,同樣也是因?yàn)樘幚韜hile的原因。
用CPU操作寄存器可以達(dá)到最快的電平翻轉(zhuǎn),但是這樣CPU的資源全用在這上面,不能再做其它的操作。如果用DMA的話,完全可以不占用CPU資源來達(dá)到更高速率的GPIO翻轉(zhuǎn)速度。假如我們要根據(jù)自己的需求來產(chǎn)生我們所需要的時(shí)序,那要怎樣做。其實(shí)也很簡(jiǎn)單,我們只要用定時(shí)器觸發(fā)DMA搬一次數(shù)據(jù)到BSRR寄存器,然后再通過調(diào)整Gpio_data數(shù)組里的數(shù)據(jù),就可以來實(shí)現(xiàn)我們所要的時(shí)序。
下面的代碼就是通過這種方式來實(shí)現(xiàn)讓GPIOC產(chǎn)生1us脈寬的時(shí)鐘,
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
GPIOC->MODER |= 0x55555555;
GPIOC->OSPEEDR |= 0xFFFFFFF;
GPIOC->PUPDR |= 0x55555555;
u32 Gpio_data[4]= {0x0000FFFF,0xFFFF0000,0x0000FFFF,0xFFFF0000};
DMA_InitTypeDef DMA_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* TIMx clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
/* DMAx clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_DeInit(DMA1_Channel7);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&(GPIOC->BSRR));
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Gpio_data;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 4;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel7, &DMA_InitStructure);
TIM_TimeBaseStructure.TIM_Period = 72;
TIM_TimeBaseStructure.TIM_Prescaler = 0x0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_DMACmd(TIM4, TIM_DMA_Update, ENABLE);
TIM_SelectOutputTrigger(TIM4,TIM_TRGOSource_Update);
TIM_Cmd(TIM4, ENABLE);
DMA1_Channel7->CCR |= DMA_CCR_EN;