源:
先上图: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;
while(1){ GPIO_Write(GPIOC,0xffff); GPIO_Write(GPIOC,0x0000);}
可以看到最快只能输出320ns的脉宽电平,而且高低电平脉宽还不一样,那是因为处理while(1)占用了CPU的时间导致的。
再来看用寄存器直接操作GPIOwhile(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;