1

TLDR; doubts:

  • When using CLI, what is the easiest way to manage GPIOs on the Pi? What is the more correct (official) way?
  • Is there some official documentation about this topic for the Pi and Raspberry Pi OS? (I wasn't able to find it)
  • What are gpiochip512, gpiochip570, gpiochip578 that I see when doing ls /sys/class/gpio/?
  • Are there other ways other than /sys/class/gpio/export and pinctrl to control GPIOs?
  • What are the benefits from using one method over the other?

Context

I have a Raspberry Pi 4 with Raspberry Pi OS (Bookworm). I was using Raspbian until last week when I upgraded to the latest Raspberry Pi OS (I delayed that because I didn't have a real need to upgrade and updating always takes some time).

Well now I see that the method I was using to access gpio pins and using them changed.

Example

The example below just turns on a LED attached to GPIO 13.

Method 1 - /sys/class/gpio/export

I was using:

echo 13 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio13/direction
echo 1 > /sys/class/gpio/gpio13/value

But that now throws an error:

$ echo 13 > /sys/class/gpio/export
-bash: echo: write error: Invalid argument

So I did some online searching and found these two resources:

The first one states that the GPIO pin numbers have changed somehow and I was able to use this to replaces my previous code to:

echo 525 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio525/direction
echo 1 > /sys/class/gpio/gpio525/value

To come up with that 525 this was my logic:

$ ls /sys/class/gpio/
export  gpiochip512  gpiochip570  gpiochip578  unexport
$ cat /sys/class/gpio/gpiochip5*/ngpio
58
8
2

So I guessed that the one starting with 512 was the one I could use, it's the only one that has 40 or more pins. And then I just added 13 to that starting point.

Method 2 - pinctrl

I also tried pinctrl from the second link, which translates my example to:

pinctrl set 13 op
pinctrl set 13 dh
loco.loop
  • 147
  • 1
  • 5

1 Answers1

0

Based on the number of "close votes" (4 so far!) your Q is apparently perceived as "soliciting opinion". However, the rules here allow for "subjective questions". I'll offer the following answer.

That said, your question: "What is the more correct (official) way?" seems a bit odd to me. Why would you worry about the "official" way? As a partial answer, I'd suggest you worry less about the "official" way, and concentrate on what works, and makes sense to you. You'll ultimately get the answers to these important questions from experience, and that will ultimately come from use - your use... not someone else's, and especially not from pushy know-it-alls masquerading as software developers.

As far as "official documentation", you'll find a white paper titled "A history of GPIO usage on Raspberry Pi devices, and current best practices" prepared by "The Raspberries" on their Product Information Portal. It strikes me as a brief and objective attempt to review the current state of GPIO control for Raspberry Pi; I'd recommend that you read it.

AFAIK, using sysfs to control GPIO pins in a discrete, "ON or OFF" manner, is a non-starter - it is no longer supported by the kernel. However, sysfs is very much alive and well for GPIO control; for example in PWM applications, or using I2C for things like sensor interface. Honestly - I can no longer recall why the Linux kernel crowd pushed "discrete" GPIO control out of sysfs - but it may have been $$$.

There are several different solutions for "discrete" GPIO control that I am aware of - and certainly more that I have no experience with - I'll list the ones I have some personal experience with:

  1. pinctrl - which you've mentioned
  2. wiringPi - a "born again" GPIO library & "tool set" making a comeback :)
  3. libgpiod (and libgpiod tools) - the "kernel-sponsored" library

Personally, I've found pinctrl to be the most straightforward & easiest to use. I've only begun using it recently - having used wiringPi for several years before the package was dropped in bookworm. I've tried libgpiod, but found it... well you can read my thoughts here in a question I asked recently that's not too different from yours.

Seamus
  • 23,558
  • 5
  • 42
  • 83