Control GPIO using libgpiod API

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Control GPIO using libgpiod API

Jump to solution
2,680 Views
dhruvinrajpura
Contributor III
Hello,

I'm using IMX8ULP EVK9 board and i have onboard LED. I'm able to control LED using "gpioset -c /dev/gpiochip4 20=1" command. Now I'm trying to control LED using C code but on that time i'm getting "Request lines failed: Invalid argument" error.

I'm using libgpiod 2.0.

Following is my C code

    const char *chipname = "/dev/gpiochip4";
    unsigned int line_num = 20;
    struct gpiod_chip *chip;
    struct gpiod_request_config *req_cfg;
    struct gpiod_line_config *line_cfg;
    struct gpiod_line_request *request;

    // Open the GPIO chip
    chip = gpiod_chip_open(chipname);
    if (!chip) {
        perror("Open chip failed");
        return 1;
    }

    // Allocate and configure the request config
    req_cfg = gpiod_request_config_new();
    gpiod_request_config_set_consumer(req_cfg, "led_control");

    // Allocate line config
    line_cfg = gpiod_line_config_new();

    // Request the line
    unsigned int offsets[] = {line_num};
    request = gpiod_chip_request_lines(chip, req_cfg, line_cfg);
    if (!request) {
        perror("Request lines failed");
        gpiod_chip_close(chip);
        return 1;
    }

    // Blink the LED
    for (int i = 0; i < 10; i++) {
        gpiod_line_request_set_value(request, line_num, GPIOD_LINE_VALUE_ACTIVE); // Turn on LED
        sleep(1);
        gpiod_line_request_set_value(request, line_num, GPIOD_LINE_VALUE_INACTIVE); // Turn off LED
        sleep(1);
    }

    // Clean up
    gpiod_line_request_release(request);
    gpiod_request_config_free(req_cfg);
    gpiod_line_config_free(line_cfg); // Free line_cfg
    gpiod_chip_close(chip);
Labels (1)
0 Kudos
Reply
1 Solution
2,642 Views
Zhiming_Liu
NXP TechSupport
NXP TechSupport

Hello,

Please refer libgpiod 2.0 examples to modify your code.

https://github.com/brgl/libgpiod/tree/v2.2.x/examples

Best Regards,
Zhiming

View solution in original post

0 Kudos
Reply
2 Replies
2,643 Views
Zhiming_Liu
NXP TechSupport
NXP TechSupport

Hello,

Please refer libgpiod 2.0 examples to modify your code.

https://github.com/brgl/libgpiod/tree/v2.2.x/examples

Best Regards,
Zhiming

0 Kudos
Reply
1,591 Views
Robbi
Contributor II

Hi @Zhiming_Liu ,

                      I'm trying to read GPIO line values in imx91  using libgpiod by c application. But showing gpiod.h is not found. I installed libgpiod-tools and able to see gpiod.h in /usr/include/. But when I compile recipe it is showing error undefined reference to API used. It is linking error is there any way to read value of GPIO using C application.

Thanks

0 Kudos
Reply