openSUSE:GPIO

跳转至: 导航, 搜索

为了管理 GPIO ,您可以使用 'libgpiod' 包里的工具,适用于内核版本 >= 4.8,包括 Leap 15.0+ 和 Tumbleweed。 您也可以使用弃用的 'sysfs' 接口。

libgpiod

安装

为了安装 libgpiod,只需要从主 OSS 仓库安装。 对于 Leap 15.0,您需要从 'hardware' 仓库进行安装。 也可以通过一键安装 : https://software.opensuse.org/package/libgpiod

检测 GPIO: gpiodetect

gpiodetect 将会列出所有 GPIO 芯片,打印它们的标签,以及 GPIO 行编号。

 gpiodetect

RPi2 返回:

 gpiochip0 [pinctrl-bcm2835] (54 ines)


获取当前 GPIO 信息: gpioinfo

 gpioinfo gpiochip0

将会列出所有指定 gpio芯片 行(这里,为 gpiochip0),他们的名称,提供者,方向,激活状态,以及其他标志。 对于 RPi2, 返回:

 gpiochip0 - 54 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       unused   input  active-high 
       line   4:      unnamed       unused   input  active-high 
       line   5:      unnamed       unused   input  active-high 
       line   6:      unnamed       unused   input  active-high 
       line   7:      unnamed       unused   input  active-high 
       line   8:      unnamed       unused   input  active-high 
       line   9:      unnamed       unused   input  active-high 
       line  10:      unnamed       unused   input  active-high 
       line  11:      unnamed       unused   input  active-high 
       line  12:      unnamed       unused   input  active-high 
       line  13:      unnamed       unused   input  active-high 
       line  14:      unnamed       unused   input  active-high 
       line  15:      unnamed       unused   input  active-high 
       line  16:      unnamed       unused   input  active-high 
       line  17:      unnamed       unused   input  active-high 
       line  18:      unnamed       unused   input  active-high 
       line  19:      unnamed       unused   input  active-high 
       line  20:      unnamed       unused   input  active-high 
       line  21:      unnamed       unused   input  active-high 
       line  22:      unnamed       unused   input  active-high 
       line  23:      unnamed       unused   input  active-high 
       line  24:      unnamed       unused   input  active-high 
       line  25:      unnamed       unused   input  active-high 
       line  26:      unnamed       unused   input  active-high 
       line  27:      unnamed       unused   input  active-high 
       line  28:      unnamed       unused   input  active-high 
       line  29:      unnamed       unused   input  active-high 
       line  30:      unnamed       unused   input  active-high 
       line  31:      unnamed       unused   input  active-high 
       line  32:      unnamed       unused   input  active-high 
       line  33:      unnamed       unused   input  active-high 
       line  34:      unnamed       unused   input  active-high 
       line  35:      unnamed       "led1"  output  active-high [used]
       line  36:      unnamed       unused   input  active-high 
       line  37:      unnamed       unused   input  active-high 
       line  38:      unnamed       unused   input  active-high 
       line  39:      unnamed       unused   input  active-high 
       line  40:      unnamed       unused   input  active-high 
       line  41:      unnamed       unused   input  active-high 
       line  42:      unnamed       unused   input  active-high 
       line  43:      unnamed       unused   input  active-high 
       line  44:      unnamed       unused   input  active-high 
       line  45:      unnamed       unused   input  active-high 
       line  46:      unnamed       unused   input  active-high 
       line  47:      unnamed       "led0"  output  active-high [used]
       line  48:      unnamed       unused   input  active-high 
       line  49:      unnamed       unused   input  active-high 
       line  50:      unnamed       unused   input  active-high 
       line  51:      unnamed       unused   input  active-high 
       line  52:      unnamed       unused   input  active-high 
       line  53:      unnamed       unused   input  active-high

读取值 (输入): gpioget

 gpioget gpiochip0 4

将读取 gpiochip0 的 gpio 4 状态值,将会返回:

 1
 gpioget gpiochip0 4 5

将会读取 gpio 4 和 5 状态值,将会返回:

 1 1

写入值 (输出): gpioset

 gpioset --mode=wait gpiochip0 4=1

将会设置 GPIO 4 的状态值为 1 并且将会停止设置该值,直到您键入 ctrl-c。

 gpioset --mode=wait gpiochip0 4=1 5=0

将会设置 GPIO 4 状态值为 1,以及 GPIO 5 状态值为 0,直到您键入 ctrl-c 退出,并停止设置值。

使用 --mode= 参数,您可以等待退出前 ctrl-c (如上述),等待一个信号,或者一些时间 (然后释放 GPIO)。

The release of the GPIO may reset it to an high-impedance (unconnected) state or leave the last value. It depends on the GPIO chip. Most SoC GPIO will reset to high impedance, whereas I2C GPIO chips will keep the last value set.

libgpiod 绑定 (C++ 和 Python)

You can use C++ binding. Examples are provided here: https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/tree/bindings/cxx/examples

For Python binding, you need to install python-libgpiod. Examples are here: https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/tree/bindings/python/examples

python3-gpiod

python3-gpiod package contains the python binding from libgpiod to access GPIO from python3.


安装

To install python3-gpiod, you just need to use zypper or YaST as it is part of openSUSE distributions repositories.

One click installation is also available : https://software.opensuse.org/package/python3-gpiod

示例

You have lots of example on https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/tree/bindings/python/examples:


sysfs 接口 (弃用)

这是旧的 (弃用) 接口仍然可用。

导出: 获取 GPIO 访问权限

 echo 229 > /sys/class/gpio/export

将会获取 GPIO 229 访问权限。

Note that the base, which is the N from /sys/class/gpio/gpiochipN, must be added to the GPIO number. This is never mentioned because on Raspbian N is 0.

There may be more than one chip providing GPIO. Try looking in /sys/class/gpio/gpiochipN/label to find out which is which.

方向: 选择输入/输出模式引脚

 echo in > /sys/class/gpio/gpio229/direction

将会设置 GPIO 229 为输入模式。

 echo out > /sys/class/gpio/gpio229/direction

将会设置 GPIO 229 位输出模式,如此初始状态值为 0,如果使用 high 设置输出模式,默认值为 1。

值: 读/写 值

 cat /sys/class/gpio/gpio229/value

将会获取当前 GPIO 229 状态值。

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

将会设置 GPIO 229 状态值为 1

取消导出: 释放 GPIO 访问

完成后,将会释放 GPIO 访问权限:

 echo 229 > /sys/class/gpio/unexport

其他