Direct GPIO driver with DMA for maximum GPIO output rate

Source: Internet
Author: User

Source: Gpio is driven directly by DMA for maximum GPIO output rate

First: stm32f303 chip, 72M of the main frequency

You can see that the Gpio has reached the 14.4M rollover rate,

Here's the code again:

rcc_ahbperiphclockcmd (RCC_AHBPERIPH_GPIOC, ENABLE); GPIOC->moder |=0x55555555; GPIOC->ospeedr |=0xFFFFFFF; GPIOC->PUPDR |=0x55555555; u32 gpio_data[4]= {0x0000ffff,0xffff0000,0x0000ffff,0xffff0000};D ma_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;D ma_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);D ma1_channel7->CCR |= dma_ccr_en;


With DMA memory to the memory mode, the Gpio_data data is transferred directly to the GPIOC BSRR register to control the flip of the GPIOC level, so that the Gpio speed reached the fastest, output 70ns pulse width, which has reached the limit of the DMA bus bandwidth, If you want to increase the speed again, you have to improve the frequency of the STM32 chip.

Then look at how fast the Gpio can be achieved with code.

First, use the library function to flip the level in the while loop of the main function.

 while (1) {  gpio_write (gpioc,0xffff);  Gpio_write (GPIOC,0x0000);}

You can see that the fastest output is only 320ns pulse width, and the high and low pulse width is not the same, because the processing while (1) takes up the CPU time caused.

Again, the direct operation of GPIO with registers
while (1)
{
GPIOC->ODR = 0xFFFF;
GPIOC->ODR = 0x0000;
}

Can be seen, the minimum pulse width can reach 40ns, the speed of the direct operation register to the library function to operate a lot faster, low-level time to high-level time is much longer, also because of processing while the reason.


With the CPU operation register can achieve the fastest level reversal, but so the CPU resources are all used on this, can not do other operations. If you use DMA, you can achieve a higher rate of gpio rollover speed without consuming CPU resources. What to do if we want to produce the time series that we need according to our own needs. It is also very simple, we just use the Timer trigger DMA to move the data to the BSRR register, and then by adjusting the data in the Gpio_data array, we can achieve the timing we want.
The following code is the way to achieve a clock that allows the GPIOC to produce a 1us pulse width,

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= the; 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;

DMA directly drives Gpio to achieve gpio maximum output rate (RPM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.