Libgpiod as a replacement of WiringPi - cu-ecen-aeld/buildroot-assignments-base GitHub Wiki

Enable libgpiod library:

  • Go to Buildroot directory
  • Type make menuconfig on the terminal from the current directory
  • Move into Target Packages -> Select Libraries -> Select Hardware Handling -> Select [*] libgpiod
  • Once the above step is done a new submenu appears below libgpiod. Select [*] install tools
  • Save the changes and exit the menuconfig

Verifying libgpiod in the target system:

  1. After enabling the libgpiod library, create a bootable image by running ./build.sh in the parent directory.
  2. Once the bootable image is obtained and loaded into the Raspberry Pi, type cd /usr/lib in the terminal.
  3. Type ls in the same directory and verify libgpiod is installed into the target system. It should look like the image below.

libgpiod Library Commands:

Reference to the following can be found here.

  1. gpiodetect: This command lists all gpiochips present on the system, their names, labels and number of GPIO lines.
  2. gpiofind: This command finds the gpio chipname and line of offset when given the line name. [Eg: command: #gpiofind PB2, output: gpiochip0 34]
  3. gpioset: This command sets the value of specified GPIO, potentially keeps the line exported and waits until timeout. [Eg: command: #gpioset gpiochip0 34=1]
  4. gpiomon: This command waits for the events on GPIO lines, specifies which events to watch, how many events to process before exiting or whether the events should be reported to the console.

libgpiod Functions:

  1. gpiod_chip_open_by_name(): This function opens the desired GPIO chip. It returns a gpiod_chip struct which is used by the following functions.
  2. gpiod_chip_get_line(): This function opens the desired GPIO line.
  3. gpiod_line_request_input()/gpiod_line_request_output(): These functions request the use of line as an input or output.
  4. gpiod_line_get_value(): This function reads the value of an input GPIO line.
  5. gpiod_line_set_value(): This function set the value of an output GPIO line.
  6. gpiod_line_release(): This function releases the line when the work with GPIO is done.
  7. gpiod_chip_close(): This function closes the chip when the work with GPIO's belonging to that particular chip is done

libgpiod Functions equivalent to WiringPi Package Functions:

  1. pinMode(GPIO, mode): This function sets a GPIO pin to an input/output. This can be replaced with gpiod_line_request_input()/gpiod_line_request_output() as mentioned above.
  2. digitalWrite(GPIO, value): This function sets the value of a GPIO pin to high or low. This can be replaced with the gpiod_line_set_value().
  3. digitalRead(GPIO, value): This function reads the value of a GPIO pin. This can be replaced with gpiod_line_get_value().
  4. For all other functions in the wiringPi.c and wiringPi.h files here. Check the functions which are required for a specific project and replace them with the functions mentioned in the above section.
    An example code for blinking an led with the help of libgpiod function is here

Makefile

Make sure to add -lgpiod as an LDFLAG in your makefile.