Adafruit Holiday Shipping Deadlines 2018: Attention! Please place all UPS Next Day orders by Monday 12/17/2018 11 AM EST
0

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


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, or even use Arduino IDE. Circuit Playground Express is the newest and best Circuit Playground board, with support for MakeCode, CircuitPython, 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.

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

CircuitPython – Python on Microcontrollers is here!

Have an amazing project to share? Join the SHOW-AND-TELL every Wednesday night at 7:30pm ET on Google+ Hangouts.

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

Follow Adafruit on Instagram for top secret new products, behinds the scenes and more https://www.instagram.com/adafruit/


Maker Business — Japanese word working and more in December’s issue of HackSpace magazine!

Wearables — Solder-less magic

Electronics — = != ==.

Biohacking — Finding Bliss with Anandamide

Python for Microcontrollers — sysfs is dead! long live libgpiod! libgpiod for linux & Python running hardware @circuitpython @micropython @ThePSF #Python @Adafruit #Adafruit

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.