Control GPIO using libgpiod API

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Control GPIO using libgpiod API

ソリューションへジャンプ
2,727件の閲覧回数
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);
ラベル(1)
0 件の賞賛
返信
1 解決策
2,689件の閲覧回数
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 件の賞賛
返信
2 返答(返信)
2,690件の閲覧回数
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 件の賞賛
返信
1,638件の閲覧回数
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 件の賞賛
返信