rplantz
Posts: 97
Joined: Sun Jul 01, 2012 2:38 am

libgpiod library

Fri Mar 01, 2024 10:15 pm

There are several libraries for programming the GPIO, but I get the impression that the recommended one is libgpiod. My take is that it's the most versatile one. Am I correct?

This is for simple examples of programming a GPIO pin that I'm giving in an introductory book I'm writing, so I want things to be "standard" and "future-proof." At the same time, I want to prepare a reader for moving on to more advanced things, so I don't want to take the simplest route that may be sort of a dead-end. My examples are in C, assembly language, and Python. So far, I haven't figured out how to use assembly language on the RPi 5, which uses the RP1 chip, but I'm working on it.

warthog618
Posts: 64
Joined: Sat Jan 06, 2024 12:55 am
Location: Australia

Re: libgpiod library

Sat Mar 16, 2024 8:19 am

rplantz wrote:
Fri Mar 01, 2024 10:15 pm
There are several libraries for programming the GPIO, but I get the impression that the recommended one is libgpiod. My take is that it's the most versatile one. Am I correct?
libgpiod is the standard library for driving GPIOs via the GPIO character device, /dev/gpiochipN, which is the standard GPIO userspace interface for Linux.
That interface is the only one that works on all Pi variants. Other libraries that use the obsolete sysfs interface or the /dev/gpiomem interface will not work on the Pi5.

libgpiod v2 can do anything that is possible via that interface.
rplantz wrote: This is for simple examples of programming a GPIO pin that I'm giving in an introductory book I'm writing, so I want things to be "standard" and "future-proof." At the same time, I want to prepare a reader for moving on to more advanced things, so I don't want to take the simplest route that may be sort of a dead-end. My examples are in C, assembly language, and Python. So far, I haven't figured out how to use assembly language on the RPi 5, which uses the RP1 chip, but I'm working on it.
Then you should use libgpiod v2. That is the latest version and replaces the less versatile libgpiod v1 which has a different API and is now a dead-end.
It provides both C and Python bindings (and C++ and Rust too), and includes examples for common use cases in all of the languages it supports.
Last I heard it was not packaged for Pi OS - the available packages are libgpiod v1 (yes even the libgpiod2 package), so you would need to install it from source. They are planning to package v2 for Pi OS, but I don't think they are there yet.

Using assembly for GPIO on the Pi5 doesn't make sense to me.

rplantz
Posts: 97
Joined: Sun Jul 01, 2012 2:38 am

Re: libgpiod library

Sat Mar 16, 2024 3:55 pm

warthog618 wrote:
Sat Mar 16, 2024 8:19 am
libgpiod is the standard library for driving GPIOs via the GPIO character device, /dev/gpiochipN, which is the standard GPIO userspace interface for Linux.
That interface is the only one that works on all Pi variants. Other libraries that use the obsolete sysfs interface or the /dev/gpiomem interface will not work on the Pi5.

libgpiod v2 can do anything that is possible via that interface.

Then you should use libgpiod v2. That is the latest version and replaces the less versatile libgpiod v1 which has a different API and is now a dead-end.
It provides both C and Python bindings (and C++ and Rust too), and includes examples for common use cases in all of the languages it supports.
Last I heard it was not packaged for Pi OS - the available packages are libgpiod v1 (yes even the libgpiod2 package), so you would need to install it from source. They are planning to package v2 for Pi OS, but I don't think they are there yet.

Using assembly for GPIO on the Pi5 doesn't make sense to me.
Thank you, warthog618, for clarifying things.

I think one should avoid using assembly language. I'm saying this as one who had a five-decade career writing mostly assembly language code. But the point of my book is to show what's going on at the machine level. For example, I give a programming construct, say a while loop, in C and then show what it looks like in assembly language.

I'm trying to keep things simple for beginners, so I'll probably have to stay with libgpiod v1. I maintain a companion website, so when RPi OS moves to v2, I can post upgrades there.

warthog618
Posts: 64
Joined: Sat Jan 06, 2024 12:55 am
Location: Australia

Re: libgpiod library

Sun Mar 17, 2024 1:54 am

rplantz wrote:
Sat Mar 16, 2024 3:55 pm
But the point of my book is to show what's going on at the machine level. For example, I give a programming construct, say a while loop, in C and then show what it looks like in assembly language.
If you want to describe bare metal access to peripherals then I question whether Linux on the Pi is the appropriate platform - unless you want to explore the kernel drivers. Anything you do to the hardware directly may contend with the kernel drivers, so not something I would suggest for beginners.
rplantz wrote: I'm trying to keep things simple for beginners, so I'll probably have to stay with libgpiod v1. I maintain a companion website, so when RPi OS moves to v2, I can post upgrades there.
Again, the v1 and v2 APIs are very different, so sticking with v1 looks like creating confusion and work to me.
It really is worth switching to v2, which IMHO has a cleaner API, even it that means having to coach beginners through installing it - it isn't actually that hard, only a few commands to install the requisite packages, fetch the sources and perform the build.
And the Python bindings are already available on PyPi, so you can just pip install them.

Return to “General programming discussion”