Consider updating gpio doc with information regarding libgpiod

My understanding is the /sys/class/gpio method is deprecated in favor of libgpiod.
libgpiod works fine on the 2 GPIO pins on the StarFive2 board I have tried it on. 55 output and 42 input.

#include <gpiod.h>
const char *chipname = "gpiochip0";
struct gpiod_chip *chip;
struct gpiod_line *lampline;
chip=gpiod_chip_open_by_name(chipname);
lampline=gpiod_chip_get_line(chip,55);
gpiod_line_request_output(lampline,"myflashlamp",0);
gpiod_line_set_value(lampline, 0);
gpiod_line_set_value(lampline, 1);

that code above toggles a output pin.
same setup code but

gpiod_line_request_input(switchline,"myswitch");
val=gpiod_line_get_value(switchline);

sets up for input and gets the value.

Also if you don’t like polling (and who does) you can do:

   rc=gpiod_ctxless_event_monitor(chipname,
                                      GPIOD_CTXLESS_EVENT_BOTH_EDGES,lineno,
                                     GPIOD_LINE_ACTIVE_STATE_HIGH,"myswitchmon",&rec,
                                     NULL,switchcb,NULL);

with the eventcb (I called it switchcb) looking like:

int switchcb(int event, unsigned int offset, const struct timespec *ts, void *unused)
{
// whatever you want here
    return(GPIOD_CTXLESS_EVENT_CB_RET_OK);
}

and when the GPIO is activated (in this case either way 1->0 or 0->1 you can configure it however), the callback will be invoked.

1 Like