Electronic – Set STM32 GPIO clock and data pins as fast as possible


I have an STM32 that toggles nine GPIO pins repeatedly (one clock pin and eight data pins to load an FPGA image using SelectMap). I am doing this using the standard library function GPIO_WriteBit that modifies one GPIO bit, and changing one pin at a time.

Unfortunately, this turns out to be quite slow. Is there a way I can make "raw GPIO toggles" very very fast? Is there a clock parameter I should change? Can I use some sort of FIFO, or interrupt-based method?

I have configured the GPIO pins as follows:

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

(Note: The reference manual (see page 133) states that the GPIO is capable of:

Fast toggle capable of changing every two clock cycles

But I cannot see how to activate this fast toggling.)

Best Answer

I answered to your related question why you could only toggle at 4 MHz when you expected 100 MHz:

If the 4 MHz is about 4 MHz, and not exactly 100 MHz/25 then the problem is probably with the C function GPIO_WriteBit.

For high speed operations and operations which require accurate timing you better code in assembly than in C. If you look at the assembly code created by GPIO_WriteBit it may be half a page long, depending on what kind of features the function has, and how much the compiler's optimizer can do with it.

You don't say which development toolchain you're using, but many/most C compilers can handle in-line assembly.

So, write the functions in assembly. A function like

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

should take no more than 2 instructions in assembly, while the compiled C code may take 20 times as much. Or more.

Related Topic