Search code examples
clinuxraspberry-pigpio

Change pin state permanently using libgpiod in C


I am writing a C program, which controls GPIO state on Raspberry PI. I am using libgpiod and call this function:

gpiod_ctxless_set_value(GPIO_CHIP_NAME, RESET_PIN, 1, false, "some consumer",  NULL, NULL); 

But, immediately after function execution pin changes its state back to LOW. How to change it permanently? Or, at least, till program exit?


Solution

  • As @0andriy mentioned, for my purposes I need more low-level functions, not a contextless. Here is my test example with generic libgpiod functions.

    #include <stdio.h>
    #include <stdlib.h>
    #include <gpiod.h>
    
    #define LED_PIN 28
    
    int main()
    {
            struct gpiod_chip *gpiochip;
            struct gpiod_line *gpioline;
            int ret;
    
            gpiochip = gpiod_chip_open("/dev/gpiochip0");
            if (gpiochip == NULL)
                    goto error1;
            printf("gpiochip open is ok\r\n");
            gpioline = gpiod_chip_get_line(gpiochip, TEST_PIN);
            if (gpioline == NULL)
                    goto error2;
            printf("gpioline open is ok\r\n");
            ret = gpiod_line_request_output(gpioline, "gpio", 0);
            if (ret != 0)
                    goto error2;
            printf("request output is ok\r\n");
            for (int i = 0; i < 5; i++)
            {
                    ret = gpiod_line_set_value(gpioline, 1);
                    printf("LED on\r\n");
                    sleep(1);
                    ret = gpiod_line_set_value(gpioline, 0);
                    printf("LED off\r\n");
                    sleep(1);
            }
            gpiod_line_release(gpioline);
    error2:
            gpiod_chip_close(gpiochip);
    error1:
            return 0;
    }