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: https://dl.dropboxusercontent.com/u/82765462/BBB/gpio.c

Borrowed from Mark Yoder’s example: http://elinux.org/EBC_Exercise_11b_gpio_via_mmap

Same result as this person: http://chiragnagpal.com/examples.html

Compile:

gcc -o gpio gpio.c

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

Then
./gpio

And measure on a scope…

Screenshot: https://dl.dropboxusercontent.com/u/82765462/BBB/gpio_mmap.jpg

Jesse,

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.