Direct control of the GPIO with Libgpiod

Topics about the Software of Revolution Pi
Post Reply
KristinH
Posts: 1
Joined: 01 Nov 2024, 16:01

Direct control of the GPIO with Libgpiod

Post by KristinH »

Hello,

I’m trying to follow the instructions for direct control of the GPIO using the standard Linux library Libgpiod and I'm not suceeding. (https://revolutionpi.com/en/tutorials/e ... ol-compact)

I have disabled piControl by using modprobe blacklist, built the code and run it on a standard build Rpi, but then the code fails on the RevPi. When I run gpioinfo the IO lines are still showing as [used].

I would like to be able to access the three bi-colour LEDs to start with.

What am I missing, please?

Code: Select all

#include <stdio.h>
#include <unistd.h>
#include <gpiod.h>
int main() {
    struct gpiod_chip        * chip;
    struct gpiod_line        * line;
    int rv, value;
    printf("testing GPIO\n");
    chip = gpiod_chip_open_by_number(0);
    if (!chip)
    {
        printf("chip failed\n");                                      // RevPi fails here
        return -1;
    }
    line = gpiod_chip_get_line(chip, 3);
    if (!line) {
        gpiod_chip_close(chip);
        printf("line failed\n");
        return -1;
    }
    rv  = gpiod_line_request_output(line, "test", 0);
    if (rv)
    {
        gpiod_chip_close(chip);
        printf("input failed\n");
        return -1;
    }
    value = gpiod_line_set_value(line, 1);
    sleep(1);
    value += gpiod_line_set_value(line, 0);
    printf("value - %d\n", value);
    gpiod_chip_close(chip);
    return 0;
}
User avatar
nicolaiB
KUNBUS
Posts: 931
Joined: 21 Jun 2018, 10:33
Location: Berlin
Contact:

Re: Direct control of the GPIO with Libgpiod

Post by nicolaiB »

Hi Kristin,

the LEDs are configured in devicetree as LEDs and can be accessed via sysfs. Depending on your device, the LEDs are either directly connected to the SOC or an GPIO expander. You can show the mapping with: sudo gpioinfo. As long as the LEDs are bound by the LED driver a direct GPIO access is not possible.

Nicolai
Post Reply