Emmanuel Vadot's Journal


FreeBSD, Electronics and more


FreeBSD GPIO Benchmark

I was reading this article that I find via hackaday and decided to do the same kind of benchmark with FreeBSD GPIO on my Olimex A20-SOM-EVN board.

So yeah, it's not the same board so result cannot really be compared but still this is interesting.

On FreeBSD for GPIO you have libgpio, an easy to use C library. There is no file-system access to gpio like Linux, but if your language of choice doesn't have binding for libgpio (most of them doesn't) you can still use gpioctl. If you use Python, I've made a CFFI based modue for libgpio named fbsd_gpio_py (For which I really need to write an article on ...).

Let's look at the result.

I've capture the result with pulseview (a sigrok frontend) and my ikalogic scanlogic 2.

For C, I've made a simple program that just configure the pin to gpio output and toggle it indefinitly :

#include <sys/types.h>
#include <stdlib.h>
#include <limits.h>

#include <libgpio.h>

int
main(int argc, char *argv[])
{
        gpio_handle_t handle;
        int unit;
        int pin;

        if (argc != 3)
                return (1);

        unit = (int)strtol(argv[1], (char **)NULL, 10);
        pin = (int)strtol(argv[2], (char **)NULL, 10);
        handle = gpio_open(unit);
        gpio_pin_output(handle, pin);
        while (1) {
        gpio_pin_toggle(handle, pin);
    }
}

Save the file as gpio.c, compile and run:

cc gpio.c -lgpio -o gpio
# I use pin 13 on gpioc controller 0
./gpio 0 133

Here are the results :

gpio_c

One period is ~18 microseconds (i.e. ~55Khz), kinda nice.

Let try python, here is the source of the script I used :

#!/usr/bin/env python

import sys
from fbsd_gpio import GpioPin

if __name__ == '__main__':
    if len(sys.argv) != 3:
        sys.exit(1)
    pin = GpioPin(int(sys.argv[2]), unit=int(sys.argv[1]))
    pin.output = True
    while True:
        pin.toggle()

And the result :

gpio_c

We see the downside of interpreted language but still a 68 microsecond period (~14Khz) is still nice.

We're not at the spec of memory-mapped gpio but the result are still really nice, maybe someday I'll do a benchmark from kernelland.