GPIO mmap toggle speed, explanation?

Curious why using mmap IO I can only toggle a GPIO pin (P8_12) at 2.78MHz. It seemed fast to me but my coworkers are laughing and saying its pathetic :wink:

My code:

Borrowed from Mark Yoder’s example:

Same result as this person:


gcc -o gpio gpio.c

Then run:
echo 44 > /sys/class/gpio/export
echo “low” > /sys/class/gpio/gpio44/direction


And measure on a scope…



I see at least two reason why it is ‘slow’:

  1. using MMAP is good to avoid OS overhead, but it does not prevent SOC bus intercommunication delays. Generally devices registers are accessed with a clock lower than CPU clock. You cannot easily improve the situation.
  2. your toggling is controlled by software execution. The CPU executes the loop: only two ‘C’ statements, but how many assembly opcodes ? Try to compile gpio.c with at least -O2. Use objdump -d to see the difference.

Aloso keep in mind the following: the faster you run, the higher will be the effect of taking interruptions (the OS is living!), so expect some periods to be really longer than the nominal period (eg. one every 10ms when Linux has 100Hz timer). This could hurt your application … or not.