用DMA直接驱动GPIO,实现GPIO最高输出速率
扫描二维码
随时随地手机看文章
STM32F303芯片,72M的主频,GPIO的达到了14.4M的翻转速率
再来上代码:
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内存到内存的模式,直接把Gpio_data的数据循环的搬到GPIOC的BSRR寄存器上来控制GPIOC上电平的翻转,这样使得GPIO的速度达到了最快,输出70ns的脉宽,这已经是达到了DMA总线带宽的极限,要想再提高速度的话,就得提高STM32芯片的主频。
再来看用代码来实现的话,GPIO能达到多快的速度。
先是用库函数来操作,在Main函数的While循环来翻转电平
while(1)
{
GPIO_Write(GPIOC,0xffff);
GPIO_Write(GPIOC,0x0000);
}
可以看到最快只能输出320ns的脉宽电平,而且高低电平脉宽还不一样,那是因为处理while(1)占用了CPU的时间导致的。
再来看用寄存器直接操作GPIO
while(1)
{
GPIOC->ODR = 0xFFFF;
GPIOC->ODR = 0x0000;
}
可以看到,最小脉宽电平可以达到40ns,直接操作寄存器的速度明显要到库函数操作要快好多,低电平的时间要比高电平的时间长很多,同样也是因为处理while的原因。
用CPU操作寄存器可以达到最快的电平翻转,但是这样CPU的资源全用在这上面,不能再做其它的操作。如果用DMA的话,完全可以不占用CPU资源来达到更高速率的GPIO翻转速度。假如我们要根据自己的需求来产生我们所需要的时序,那要怎样做。其实也很简单,我们只要用定时器触发DMA搬一次数据到BSRR寄存器,然后再通过调整Gpio_data数组里的数据,就可以来实现我们所要的时序。
下面的代码就是通过这种方式来实现让GPIOC产生1us脉宽的时钟,
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;