在 C 中使用 libgpiod 永久更改引脚状态
Change pin state permanently using libgpiod in C
我正在编写一个 C 程序,它控制 Raspberry PI 上的 GPIO 状态。我正在使用 libgpiod
并调用此函数:
gpiod_ctxless_set_value(GPIO_CHIP_NAME, RESET_PIN, 1, false, "some consumer", NULL, NULL);
但是,在函数执行后,引脚立即将其状态更改回低电平。如何永久更改它?或者,至少,直到程序退出?
如@0andriy 所述,出于我的目的,我需要更多低级函数,而不是无上下文的。这是我使用通用 libgpiod
函数的测试示例。
#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;
}
我正在编写一个 C 程序,它控制 Raspberry PI 上的 GPIO 状态。我正在使用 libgpiod
并调用此函数:
gpiod_ctxless_set_value(GPIO_CHIP_NAME, RESET_PIN, 1, false, "some consumer", NULL, NULL);
但是,在函数执行后,引脚立即将其状态更改回低电平。如何永久更改它?或者,至少,直到程序退出?
如@0andriy 所述,出于我的目的,我需要更多低级函数,而不是无上下文的。这是我使用通用 libgpiod
函数的测试示例。
#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;
}