Using libgpiod to access GPIO - DhruvHMehta/yocto-assignments-base GitHub Wiki
This page describes the use of libgpiod library to access GPIOs on the Raspberry Pi. The library helps map the gpiochip0 char driver (found at /dev/gpiochip0) to user space application.
The old (and most used) way of interfacing with gpios was using the sysfs interface. However, this method is deprecated since kernel v4.8 and is replaced with the gpiochip0 char device driver. To use this driver, the maintainer of the GPIO kernel framework (Bartosz Golaszewski) released the libgpiod library.
- Start by installing the required package on your local machine
sudo apt-get install libgpiod-dev
This will install the required libgpiod development package along with its dependencies.
-
Use the example code which contains the gpioreadtest and gpiowritetest applications. These are derived from the libgpiod examples provided by the author.
Use the Makefile to compile these on your local machine first. -
Create a recipe for installing this test code to your target machine. An example recipe is shown which is contained in the gpiotest package. Note the use of DEPENDS to build the application.
-
Modify the build script to add the new gpiotest package in the conf/bblayers.conf and the recipe in the conf/local.conf. You will need to add poky, open-embedded and raspberrypi as submodules to your project before you run the script.
-
Build the image. You should see that the meta-gpiotest package is added. After flashing the image to the SD Card, boot your Raspberry Pi.
-
Check the /usr/bin directory. The gpioreadtest and gpiowritetest applications should be visible. You can run these and test the output at the respective pins using a logic analyzer or an LED.
- You can now write your own application to interface with the GPIOs based on these example applications. Refer to the libgpiod source to figure out how to use the APIs.