sysfs is dead! long live libgpiod! (libgpiod for linux & CircuitPython)

Libgpiod
Earlier this year we wrote about the announcement of mainline kernel integration of libgpiod on the Adafruit blog (2/10/2018).

libgpiod is intended to be a fast kernel-level-supported method for writing/reading/monitoring GPIO pins on various linux boards, replacing the two main methods we see these days: sysfs file pokin’ and devmem twiddling.

With sysfs style control, you end up with files such as /sys/class/gpio/gpio16 (for pin #16) that you can write and read from to set direction and read values. it works ok, and is very easy to use with shell scripts, but is clunky from C or python, and is slow and incomplete (for example, pullup/down’s are not supported).

With devmem twiddling, you open up a file point directly to chip memory and start poking and prodding at the registers directly, somwhat similar to older computer’s ‘peek’ and ‘poke’ commands, or the microcontroller style PORTB |= 0x01 . Benefits? it is heckin’ fast. Downsides are that its a horrible idea to poke into memory, and you end up having to make register maps for each processor and revision because the registers move around. You can see some example code here for our DHT driver for Raspberry Pi
https://github.com/adafruit/Adafruit_Python_DHT/blob/master/source/Raspberry_Pi/pi_mmio.c

…and then this different code for a BeagleBone (cause the register map is different)
https://github.com/adafruit/Adafruit_Python_DHT/blob/master/source/Beaglebone_Black/bbb_mmio.c

As you can tell, this quickly becomes hard to support and its dangerous – you need to be running as root and its incredibly easy to accidentally poke the wrong location.

With more linux boards coming out with GPIO (there’s probably 4 more released since we wrote this post) – having a consistent, reliable, complete GPIO interface is pretty important to avoid icky unmaintainable code. So we’re pretty psyched about libgpiod. From our experiments, its much much faster than sysfs. Its not as fast as mem twiddling, but that’s not too surprising, there’s still kernel messages and error checking done. A Pi 3 got us 400Khz pin output toggles in a loop in C, 100KHz in Python examples, and that’s pretty good for bitbanging. (For SPI or I2C, use the hardware peripherals, they can go multi-MHz and can be shared between processes!)

We’ve started with a basic ‘collect gpio pulses and store them in a circular buffer’ program, written by Brennen Bearnes:
https://github.com/adafruit/libgpiod_pulsein

This is basically code that will replace our Python DHT driver, and has the benefit of being forward compatible with any other Linux board that runs a 4.8+ kernel. We’ll slowly be replacing other CircuitPython code to use libgpiod, so that we can have broad support for CircuitPython on a Raspberry Pi, BeagleBone or Onion.io.

There’s not a lot of libgpiod code out there, and libgpiod doesn’t come stock on Linux distros yet which may be why its taking a little while to catch on. There’s bindings for C and Python. Here’s a script that can help you get started by compiling it for you
https://github.com/adafruit/Raspberry-Pi-Installer-Scripts/blob/master/libgpiod.sh

If you have any controlling votes in a distro, please have libgpiod available thru your package manager! The latest code is here, and as you can tell there’s a lot of active development 🙂
https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/


Related


Adafruit publishes a wide range of writing and video content, including interviews and reporting on the maker market and the wider technology world. Our standards page is intended as a guide to best practices that Adafruit uses, as well as an outline of the ethical standards Adafruit aspires to. While Adafruit is not an independent journalistic institution, Adafruit strives to be a fair, informative, and positive voice within the community – check it out here: adafruit.com/editorialstandards

Stop breadboarding and soldering – start making immediately! Adafruit’s Circuit Playground is jam-packed with LEDs, sensors, buttons, alligator clip pads and more. Build projects with Circuit Playground in a few minutes with the drag-and-drop MakeCode programming site, learn computer science using the CS Discoveries class on code.org, jump into CircuitPython to learn Python and hardware together, TinyGO, or even use the Arduino IDE. Circuit Playground Express is the newest and best Circuit Playground board, with support for CircuitPython, MakeCode, and Arduino. It has a powerful processor, 10 NeoPixels, mini speaker, InfraRed receive and transmit, two buttons, a switch, 14 alligator clip pads, and lots of sensors: capacitive touch, IR proximity, temperature, light, motion and sound. A whole wide world of electronics and coding is waiting for you, and it fits in the palm of your hand.

Have an amazing project to share? The Electronics Show and Tell is every Wednesday at 7:30pm ET! To join, head over to YouTube and check out the show’s live chat and our Discord!

Join us every Wednesday night at 8pm ET for Ask an Engineer!

Join over 38,000+ makers on Adafruit’s Discord channels and be part of the community! http://adafru.it/discord

CircuitPython – The easiest way to program microcontrollers – CircuitPython.org


New Products – Adafruit Industries – Makers, hackers, artists, designers and engineers! — New Products 11/15/2024 Featuring Adafruit bq25185 USB / DC / Solar Charger with 3.3V Buck Board! (Video)

Python for Microcontrollers – Adafruit Daily — Select Python on Microcontrollers Newsletter: PyCon AU 2024 Talks, New Raspberry Pi Gear Available and More! #CircuitPython #Python #micropython @ThePSF @Raspberry_Pi

EYE on NPI – Adafruit Daily — EYE on NPI Maxim’s Himalaya uSLIC Step-Down Power Module #EyeOnNPI @maximintegrated @digikey

Adafruit IoT Monthly — Halloween, WiLo, and more!

Maker Business – Adafruit Daily — The worlds largest car exporter: China

Electronics – Adafruit Daily — Are you grounded?

Get the only spam-free daily newsletter about wearables, running a "maker business", electronic tips and more! Subscribe at AdafruitDaily.com !



No Comments

No comments yet.

Sorry, the comment form is closed at this time.