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
3,585 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
3,547 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
3,548 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
2,496 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
%3CLINGO-SUB%20id%3D%22lingo-sub-1988460%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3EControl%20GPIO%20using%20libgpiod%20API%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1988460%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CPRE%3EHello%2C%0A%0AI'm%20using%20IMX8ULP%20EVK9%20board%20%3CSPAN%20class%3D%22%22%3Eand%3C%2FSPAN%3E%20i%20have%20onboard%20LED.%20I'm%20able%20%3CSPAN%20class%3D%22%22%3Eto%3C%2FSPAN%3E%20control%20LED%20using%20%3CSPAN%20class%3D%22%22%3E%22gpioset%20-c%20%2Fdev%2Fgpiochip4%2020%3D1%22%3C%2FSPAN%3E%20command.%20Now%20I'm%20trying%20%3CSPAN%20class%3D%22%22%3Eto%3C%2FSPAN%3E%20control%20LED%20using%20C%20code%20but%20on%20that%20time%20i'm%20getting%20%3CSPAN%20class%3D%22%22%3E%22Request%20lines%20failed%3A%20Invalid%20argument%22%3C%2FSPAN%3E%20error.%0A%0AI'm%20using%20libgpiod%20%3CSPAN%20class%3D%22%22%3E2.0%3C%2FSPAN%3E.%0A%0AFollowing%20is%20my%20C%20code%0A%0A%20%20%20%20const%20%3CSPAN%20class%3D%22%22%3Echar%3C%2FSPAN%3E%20*chipname%20%3D%20%3CSPAN%20class%3D%22%22%3E%22%2Fdev%2Fgpiochip4%22%3C%2FSPAN%3E%3B%0A%20%20%20%20unsigned%20%3CSPAN%20class%3D%22%22%3Eint%3C%2FSPAN%3E%20line_num%20%3D%20%3CSPAN%20class%3D%22%22%3E20%3C%2FSPAN%3E%3B%0A%20%20%20%20%3CSPAN%20class%3D%22%22%3Estruct%3C%2FSPAN%3E%20gpiod_chip%20*chip%3B%0A%20%20%20%20%3CSPAN%20class%3D%22%22%3Estruct%3C%2FSPAN%3E%20gpiod_request_config%20*req_cfg%3B%0A%20%20%20%20%3CSPAN%20class%3D%22%22%3Estruct%3C%2FSPAN%3E%20gpiod_line_config%20*line_cfg%3B%0A%20%20%20%20%3CSPAN%20class%3D%22%22%3Estruct%3C%2FSPAN%3E%20gpiod_line_request%20*request%3B%0A%0A%20%20%20%20%3CSPAN%20class%3D%22%22%3E%2F%2F%20Open%20the%20GPIO%20chip%3C%2FSPAN%3E%0A%20%20%20%20chip%20%3D%20gpiod%3CSPAN%20class%3D%22%22%3E_chip_open(%3CSPAN%20class%3D%22%22%3Echipname%3C%2FSPAN%3E)%3C%2FSPAN%3E%3B%0A%20%20%20%20%3CSPAN%20class%3D%22%22%3Eif%3C%2FSPAN%3E%20(!chip)%20%7B%0A%20%20%20%20%20%20%20%20perror(%3CSPAN%20class%3D%22%22%3E%22Open%20chip%20failed%22%3C%2FSPAN%3E)%3B%0A%20%20%20%20%20%20%20%20return%20%3CSPAN%20class%3D%22%22%3E1%3C%2FSPAN%3E%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20%3CSPAN%20class%3D%22%22%3E%2F%2F%20Allocate%20and%20configure%20the%20request%20config%3C%2FSPAN%3E%0A%20%20%20%20req_cfg%20%3D%20gpiod%3CSPAN%20class%3D%22%22%3E_request_config_new()%3C%2FSPAN%3E%3B%0A%20%20%20%20gpiod%3CSPAN%20class%3D%22%22%3E_request_config_set_consumer(%3CSPAN%20class%3D%22%22%3Ereq_cfg%3C%2FSPAN%3E%2C%20%3CSPAN%20class%3D%22%22%3E%22led_control%22%3C%2FSPAN%3E)%3C%2FSPAN%3E%3B%0A%0A%20%20%20%20%3CSPAN%20class%3D%22%22%3E%2F%2F%20Allocate%20line%20config%3C%2FSPAN%3E%0A%20%20%20%20line_cfg%20%3D%20gpiod%3CSPAN%20class%3D%22%22%3E_line_config_new()%3C%2FSPAN%3E%3B%0A%0A%20%20%20%20%3CSPAN%20class%3D%22%22%3E%2F%2F%20Request%20the%20line%3C%2FSPAN%3E%0A%20%20%20%20unsigned%20%3CSPAN%20class%3D%22%22%3Eint%3C%2FSPAN%3E%20offsets%3CSPAN%20class%3D%22%22%3E%5B%5D%3C%2FSPAN%3E%20%3D%20%7Bline_num%7D%3B%0A%20%20%20%20request%20%3D%20gpiod%3CSPAN%20class%3D%22%22%3E_chip_request_lines(%3CSPAN%20class%3D%22%22%3Echip%3C%2FSPAN%3E%2C%20%3CSPAN%20class%3D%22%22%3Ereq_cfg%3C%2FSPAN%3E%2C%20%3CSPAN%20class%3D%22%22%3Eline_cfg%3C%2FSPAN%3E)%3C%2FSPAN%3E%3B%0A%20%20%20%20%3CSPAN%20class%3D%22%22%3Eif%3C%2FSPAN%3E%20(!request)%20%7B%0A%20%20%20%20%20%20%20%20perror(%3CSPAN%20class%3D%22%22%3E%22Request%20lines%20failed%22%3C%2FSPAN%3E)%3B%0A%20%20%20%20%20%20%20%20gpiod%3CSPAN%20class%3D%22%22%3E_chip_close(%3CSPAN%20class%3D%22%22%3Echip%3C%2FSPAN%3E)%3C%2FSPAN%3E%3B%0A%20%20%20%20%20%20%20%20return%20%3CSPAN%20class%3D%22%22%3E1%3C%2FSPAN%3E%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20%3CSPAN%20class%3D%22%22%3E%2F%2F%20Blink%20the%20LED%3C%2FSPAN%3E%0A%20%20%20%20%3CSPAN%20class%3D%22%22%3Efor%3C%2FSPAN%3E%20(%3CSPAN%20class%3D%22%22%3Eint%3C%2FSPAN%3E%20i%20%3D%20%3CSPAN%20class%3D%22%22%3E0%3C%2FSPAN%3E%3B%20i%20%26lt%3B%20%3CSPAN%20class%3D%22%22%3E10%3C%2FSPAN%3E%3B%20i%2B%2B)%20%7B%0A%20%20%20%20%20%20%20%20gpiod%3CSPAN%20class%3D%22%22%3E_line_request_set_value(%3CSPAN%20class%3D%22%22%3Erequest%3C%2FSPAN%3E%2C%20%3CSPAN%20class%3D%22%22%3Eline_num%3C%2FSPAN%3E%2C%20GPIOD_LINE_VALUE_ACTIVE)%3C%2FSPAN%3E%3B%20%3CSPAN%20class%3D%22%22%3E%2F%2F%20Turn%20on%20LED%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20sleep(%3CSPAN%20class%3D%22%22%3E1%3C%2FSPAN%3E)%3B%0A%20%20%20%20%20%20%20%20gpiod%3CSPAN%20class%3D%22%22%3E_line_request_set_value(%3CSPAN%20class%3D%22%22%3Erequest%3C%2FSPAN%3E%2C%20%3CSPAN%20class%3D%22%22%3Eline_num%3C%2FSPAN%3E%2C%20GPIOD_LINE_VALUE_INACTIVE)%3C%2FSPAN%3E%3B%20%3CSPAN%20class%3D%22%22%3E%2F%2F%20Turn%20off%20LED%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20sleep(%3CSPAN%20class%3D%22%22%3E1%3C%2FSPAN%3E)%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20%3CSPAN%20class%3D%22%22%3E%2F%2F%20Clean%20up%3C%2FSPAN%3E%0A%20%20%20%20gpiod%3CSPAN%20class%3D%22%22%3E_line_request_release(%3CSPAN%20class%3D%22%22%3Erequest%3C%2FSPAN%3E)%3C%2FSPAN%3E%3B%0A%20%20%20%20gpiod%3CSPAN%20class%3D%22%22%3E_request_config_free(%3CSPAN%20class%3D%22%22%3Ereq_cfg%3C%2FSPAN%3E)%3C%2FSPAN%3E%3B%0A%20%20%20%20gpiod%3CSPAN%20class%3D%22%22%3E_line_config_free(%3CSPAN%20class%3D%22%22%3Eline_cfg%3C%2FSPAN%3E)%3C%2FSPAN%3E%3B%20%3CSPAN%20class%3D%22%22%3E%2F%2F%20Free%20line_cfg%3C%2FSPAN%3E%0A%20%20%20%20gpiod%3CSPAN%20class%3D%22%22%3E_chip_close(%3CSPAN%20class%3D%22%22%3Echip%3C%2FSPAN%3E)%3C%2FSPAN%3E%3B%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1988460%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CLINGO-LABEL%3Ei.MX8ULP%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2088391%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERe%3A%20Control%20GPIO%20using%20libgpiod%20API%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2088391%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F151788%22%20target%3D%22_blank%22%3E%40Zhiming_Liu%3C%2FA%3E%26nbsp%3B%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20I'm%20trying%20to%20read%20GPIO%20line%20values%20in%20%3CSTRONG%3Eimx91%26nbsp%3B%3C%2FSTRONG%3E%20using%20%3CSTRONG%3Elibgpiod%3C%2FSTRONG%3E%20by%20c%20application.%20But%20showing%20%3CSTRONG%3Egpiod.h%3C%2FSTRONG%3E%20is%20not%20found.%20I%20installed%20libgpiod-tools%20and%20able%20to%20see%20gpiod.h%20in%20%2Fusr%2Finclude%2F.%20But%20when%20I%20compile%20recipe%20it%20is%20showing%20error%20undefined%20reference%20to%20API%20used.%20It%20is%20linking%20error%20is%20there%20any%20way%20to%20read%20value%20of%20GPIO%20using%20C%20application.%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1989659%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERe%3A%20Control%20GPIO%20using%20libgpiod%20API%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1989659%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHello%2C%3C%2FP%3E%0A%3CP%3EPlease%20refer%26nbsp%3Blibgpiod%202.0%20examples%20to%20modify%20your%20code.%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fbrgl%2Flibgpiod%2Ftree%2Fv2.2.x%2Fexamples%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Fbrgl%2Flibgpiod%2Ftree%2Fv2.2.x%2Fexamples%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3EBest%20Regards%2C%3CBR%20%2F%3EZhiming%3C%2FP%3E%3C%2FLINGO-BODY%3E