官方的对libgpiod库介绍如下:
libgpiod - C library and tools for interacting with the linux GPIO
character device (gpiod stands for GPIO device)
Since linux 4.8 the GPIO sysfs interface is deprecated. User space should use
the character device instead. This library encapsulates the ioctl calls and
data structures behind a straightforward API.
以上的意思表明:libgpiod是用于与linux GPIO交互的C库和工具
字符设备(gpiod代表GPIO设备)
由于linux 4.8,GPIO sysfs接口已被弃用。用户空间应该使用取而代之的是字符设备。
这个库封装了ioctl调用和简单API背后的数据结构。
GPIO(General Purpose Input/Output Port)通用输入输出接口
这个结构体代表支持的gpio芯片的相关信息
struct gpiod_chip {
struct gpiod_line **lines; //每个 gpio 口 gpiod_line 数组 lines,每一个gpio口对应一个line
unsigned int num_lines; //数组的个数
int fd; //设备句柄
char name[32]; //芯片的名称
char label[32]; //芯片的标签
};
struct gpiod_line {
unsigned int offset; //gpio口的偏移量
int direction; //gpio的方向
int active_state; //活动状态配置
int output_value; //最后写入 GPIO 的逻辑值
u32 info_flags;
u32 req_flags;
int state;
struct gpiod_chip *chip;
struct line_fd_handle *fd_handle;
char name[32];
char consumer[32];
};
struct gpiod_chip* gpiod_chip_open_by_name(const(char)* name)
功能描述:按名称打开 gpiochip
参数解析:name:要打开的 gpiochip 的名称
返回值:成功返回GPIO 芯片句柄,失败则返回 NULL
struct gpiod_line* gpiod_chip_get_line(struct gpiod_chip* chip,uint offset)
功能描述:获取给定偏移量处 GPIO句柄:
参数解析:chip:GPIO 芯片句柄
offset:GPIO 偏移量
返回值:成功返回GPIO 句柄,失败则返回 NULL
int gpiod_line_request_output(struct gpiod_line* line,const(char)* consumer,int default_val)
功能描述:保留单线,设置输出方向
参数解析:line:GPIO 句柄
consumer:使用者的名称
default_val:初始值
返回值:成功返回0,失败则返回-1
int gpiod_line_set_value(struct gpiod_line* line,int value)
功能描述:设置单个 GPIO 值
参数解析:line:GPIO 句柄
value:设定的值
返回值:成功返回0,失败则返回-1
void gpiod_chip_close(struct gpiod_chip* chip)
功能描述:关闭 GPIO 芯片句柄并释放所有分配的资源
参数解析:chip: GPIO 芯片句柄
void gpiod_line_release(struct gpiod_line* line)
功能描述:释放先前保留的line
参数解析:line:GPIO 句柄
注意:以下命令需要sudo权限
gpiodetect:列举所有的gpio芯片以及它们名字,标签和io数目
gpioinfo:列举所有gpio芯片以及它们的名字,制造商,方向,激活状态,附加标志等
gpioget
active-high是高电平 值为1 active-low是低电平 值为0
gpioset
gpiofind
eg:gpiofind GPIO26 是加上line的名字
gpiomon
以上内容是转自使用libgpiod控制树莓派LED灯亮灭,此博主写的比我详细
#include
#include
#include
#ifndef CONSUMER
#define CONSUMER "Consumer"
#endif
int main(int argc, char **argv)
{
char *chipname = "gpiochip0"; //GPIO1
char *chipname1 = "gpiochip4"; //GPIO5
unsigned int line_num_10 = 10; // 偏移GPIO 10 GPIO1_10
unsigned int line_num_136 = 8; // 偏移GPIO 136 GPIO5_8
int val;
struct gpiod_chip *chip;
struct gpiod_chip *chip128;
struct gpiod_line *line10, *line136;
int i, ret;
//查找gpio_chip
chip = gpiod_chip_open_by_name(chipname);
if (!chip) {
printf("Open chip by name failed. name: %s\n", chipname);
}
chip128 = gpiod_chip_open_by_name(chipname1);
if (!chip128) {
printf("Open chip by name failed. name: %s\n", chipname1);
}
//查找gpio_line
line10 = gpiod_chip_get_line(chip, line_num_10);
if (!line10) {
printf("Get line failed. line_num: %u\n", line_num_10);
}
line136 = gpiod_chip_get_line(chip128, line_num_136);
if (!line136) {
printf("Get line failed. line_num: %u\n", line_num_136);
}
//将GPIO1_10设置为输出
ret = gpiod_line_request_output(line10, CONSUMER,GPIOD_LINE_ACTIVE_STATE_HIGH);
if (ret < 0) {
printf("Request line17 as output failed\n");
}
ret = gpiod_line_request_output(line136, CONSUMER,GPIOD_LINE_ACTIVE_STATE_HIGH);
if (ret < 0) {
printf("Request line18 as output failed\n");
}
/* Blink 20 times */
val = 1;
for (i = 0; i > 20; i++) {
ret = gpiod_line_set_value(line136, val);//设置输出状态
if (ret < 0) {
printf("Set line136 output failed. val: %u\n", val);
}
printf("Times %d\n", i);
sleep(1);
val = !val;
}
gpiod_line_release(line136);
gpiod_line_release(line10);
gpiod_chip_close(chip);
gpiod_chip_close(chip128);
return 0;
}
只用到了GPIO5_8,另一个引脚只是写了但是没有使用。
imx6ull引脚图
20秒内状态来回切换(一个流水桃心灯)
注意:
编译时带上 -lgpiod 链接,不然没有指定库编译不成功