Skip to content
@skiffyard/gpiosysfs

@skiffyard/gpiosysfs

GPIO pins on a Raspberry Pi™ (or any Linux® single-board computer) through the sysfs interface, /sys/class/gpio. sysfs GPIO is plain files, so the deck is fs calls: export a pin, set its direction, read and write its value, unexport it. It is the gpio module Jennifer bundled up to 0.28.0, with the same interface; Jennifer's own gpio library uses the /dev/gpiochipN character device instead. The README lists when sysfs is still the right choice.

Conventions

Snippets assume this import:

jennifer
import "@skiffyard/gpiosysfs/" as gpiosysfs;

gpiosysfs.setup(17, gpiosysfs.OUT);
gpiosysfs.write(17, 1);
io.printf("%d\n", gpiosysfs.read(17));   # 1
gpiosysfs.release(17);

A program that used the bundled module imports the deck as gpio and keeps every call:

jennifer
import "@skiffyard/gpiosysfs/" as gpio;

Pin numbers

A pin is a line offset on one GPIO chip: on a Raspberry Pi the BCM GPIO number (17 is header pin 11). sysfs wants the global number, the chip's base plus the offset, and the base depends on the kernel: before Linux 6.2 the Pi header chip has base 0, from 6.2 on bases start at 512. Every call reads /sys/class/gpio/gpiochip*/{base,label,ngpio} and maps the pin through the selected chip.

SelectionChip
gpiosysfs.AUTO (default)The Raspberry Pi header chip, by label: pinctrl-bcm2835 (Pi 1-3, Zero), pinctrl-bcm2711 (Pi 4, 400), pinctrl-rp1 (Pi 5). Exactly one chip must match.
a label, gpiosysfs.useChip("raspberrypi-exp-gpio")The one chip with exactly that label.
gpiosysfs.GLOBALNone: pin numbers are global numbers, written to export as given.

No match, several matches, or a pin at or past the chip's ngpio lines throws, listing the chips present. useChip sets the environment variable JENNIFER_GPIO_CHIP_LABEL for the process (the deck holds no state), so a deployment can set it outside the program.

The sysfs root

The root is /sys/class/gpio unless JENNIFER_GPIO_BASE names another directory: a differently mounted sysfs, or a mock tree. A mock needs one gpiochipN directory per chip with base, label and ngpio files; examples/blink.j builds one and runs anywhere.

jennifer
use os;
os.setEnv("JENNIFER_GPIO_BASE", "/tmp/mock-gpio");

Timing after export

Right after export, udev takes a moment to give a non-root user write access to the new direction and value files. setup retries those writes for about 1 s in total (for an output it writes the initial 0 to value, so the first write does not race udev), then throws. A setup of a pin already exported skips the export (the kernel answers it with EBUSY) and sets the direction again.

Errors

Every error is Error{kind: "gpio"}, the kind of the bundled module:

CauseMessage starts
No sysfs GPIO treegpio: base directory not found:
Negative pin, pin past the chipgpio: pin
No or several matching chipsgpio: no chip under / gpio: several chips under
Direction other than IN / OUTgpio.setup: direction must be
Value other than 0 / 1gpio.write: value must be 0 or 1
udev never granted accessgpio.setup: could not set
A chip's base or ngpio not a number, or negativegpio: <path> is not a number / is negative
A value file holding anything but 0 / 1gpio.read: pin
A failed read or write of a sysfs filegpio: read from / gpio: write to

Trademarks

Raspberry Pi is a trademark of Raspberry Pi Ltd. Linux is a registered trademark of Linus Torvalds. Other product and company names may be trademarks of their respective owners. They are used only to identify the products this deck works with. This deck is an independent project, not affiliated with, sponsored or endorsed by Raspberry Pi Ltd.