概述
根据网络上找到的资料, 在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: %dn", value);
if(gpiod_line_is_requested(line)) {
printf("release gpio request!,and go onn");
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 ONn");
else
printf("LED turns OFFn");
sleep(1);
}
err:
gpiod_chip_close(chip);
return;
}
参考文章:
https://www.cnblogs.com/dakewei/p/12810536.html
https://linus.blog.csdn.net/article/details/105085365
最后
以上就是欢呼纸鹤为你收集整理的linux用户空间控制gpio的两种方法的全部内容,希望文章能够帮你解决linux用户空间控制gpio的两种方法所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复