linux用户空间控制gpio的两种方法

根据网络上找到的资料, 在kernel 4.8开始,加入了libgpiod的支持;而原有基于sysfs的访问方式,将被逐渐放弃。
当前内核中的GPIO架构如下: 

对比下 sysfs 和 gpiod的两种方式:

1 . sysfs的方式

内核空间的常用函数:

gpio_request,gpio_direction_output,gpio_free

用户空间对gpio进行操作, 命令行如下:

echo 28 > /sys/class/gpio/export

echo "out" > /sys/class/gpio/gpio28/direction

echo 1 > /sys/class/gpio/gpio28/value

 

2.  gpiod的方式

内核空间的主要几个函数:

//获取GPIO设备,多个设备时需要附带index参数
struct gpio_desc *gpiod_get(struct device *dev, const char *con_id,
                    enum gpiod_flags flags)
 
struct gpio_desc *gpiod_get_index(struct device *dev,
                      const char *con_id, unsigned int idx,
                      enum gpiod_flags flags)

//函数释放
void gpiod_put(struct gpio_desc *desc)
void gpiod_put_array(struct gpio_descs *descs)


int gpiod_direction_input(struct gpio_desc *desc)
int gpiod_direction_output(struct gpio_desc *desc, int value)
int gpiod_get_value(const struct gpio_desc *desc);
void gpiod_set_value(struct gpio_desc *desc, int value);

//获取GPIO对应的IRQ中断号 
int gpiod_to_irq(const struct gpio_desc *desc)

用户空间编程: 

1.  命令行配置

    gpiodetect    列出所有的gpio
    gpioinfo        列出某个gpio控制器的情况
    gpioset         设置gpio
    gpioget         读取gpio
    gpiomon       监控gpio的状态

# gpiodetect
gpiochip0 [209c000.gpio] (32 lines)
gpiochip1 [20a0000.gpio] (32 lines)
gpiochip2 [20a4000.gpio] (32 lines)
gpiochip3 [20a8000.gpio] (32 lines)
gpiochip4 [20ac000.gpio] (32 lines)
gpiochip5 [20b0000.gpio] (32 lines)
gpiochip6 [20b4000.gpio] (32 lines)
gpiochip7 [mcp23s08.0] (8 lines)

# gpioinfo 0    //0 对应 gpiochip0
gpiochip0 - 32 lines:
        line   0:      unnamed       unused   input  active-high
        line   1:      unnamed       unused   input  active-high
        line   2:      unnamed       unused   input  active-high
        line   3:      unnamed        "scl"  output  active-high [used open-drain]
        line   4:      unnamed       unused   input  active-high
        line   5:      unnamed       unused   input  active-high
        line   6:      unnamed        "sda"  output  active-high [used open-drain]
        line   7:      unnamed "Push Button"  input  active-low  [used]
        line   8:      unnamed       unused   input  active-high
        line   9:      unnamed          "?"  output  active-high [used]
        line  10:      unnamed       unused   input  active-high
        line  11:      unnamed       unused   input  active-high

# gpioset 0 9=0      //设置gpio控制器0的第9个gpio输出0 

# gpioget 0 9

# gpiomon 0 9

2  使用libgpiod编程

 以下是在mt7628 openwrt下写的一个测试例子 ,对gpiochip0 (gpio11) 做读写操作

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <gpiod.h>
 
void main(void) {
    struct gpiod_chip *chip;
    struct gpiod_line *line;
    int req, value;
 
    /* open the GPIO bank */
    chip = gpiod_chip_open("/dev/gpiochip0");
    //or chip = gpiod_chip_open_by_name("gpiochip0");
    if (!chip) {
		printf("Fail to open gpiochip0!\n");
        return ;
	}
 
    line = gpiod_chip_get_line(chip, 11);
    if (!line) {
		printf("Fail to get gpio line!\n");
        goto err;
	}
 
    req = gpiod_line_request_input(line, "gpio_state");
    if (req) { 
		printf("Fail to config input!\n");
        goto err;
	}
    value = gpiod_line_get_value(line);
    printf("GPIO value is: %d\n", value);
 
	if(gpiod_line_is_requested(line)) {
		printf("release gpio request!,and go on\n");
		gpiod_line_release(line);
	}
 
    /* config as output and set a description */
    req = gpiod_line_request_output(line, "gpio-bline", GPIOD_LINE_ACTIVE_STATE_HIGH);
    if (req)  { 
		printf("Fail to config output!\n");
        goto err;
	}
 
    while (1)
    {
       /* GPIO Toggle */
        value = !value;
        gpiod_line_set_value(line, value);
        /* print LED status */
        if(value)
            printf("LED turns ON\n");
        else
	        printf("LED turns OFF\n");
   
        sleep(1);
    }
 
err:
    gpiod_chip_close(chip);
    return;
}

 

参考文章: 
https://www.cnblogs.com/dakewei/p/12810536.html
https://linus.blog.csdn.net/article/details/105085365
 

  • 4
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值