1. 用GPIO sysfs读写IO(Seeed的设备树不支持这种操作,仅作为介绍)
7 b2 u2 K3 N7 Y& C# n在Linux中,最常见的读写GPIO方式就是用GPIO sysfs interface,是通过操作/sys/class/gpio目录下的export、unexport、gpio{N}/direction, gpio{N}/value(用实际引脚号替代{N})等文件实现的,经常出现shell脚本里面。比如在shell中控制树莓派3B的GPIO12:8 N B3 i2 p& y) u# ]- i8 [
- sudo su# U v. S% l6 g6 y! |$ {+ m
- cd /sys/class/gpio' E( Y B$ I. b- ~
- echo 12 > export
/ [- O* l: F* |" s7 C - echo out > gpio12/direction # io used for output
# q1 e# Z) y) u. m0 E - echo 1 > gpio12/value # output logic 1 level
6 N& [: Z4 a# `+ n( J6 ^/ @" d - echo 0 > gpio12/value # output logic 0 level, g2 }( L v, j2 A, [1 {9 H& y
- echo 12 > unexport/ f0 C3 X0 U- r0 ]% k% z
复制代码 基于GPIO sysfs interface封装的库很多,这里推荐vsergeev写的python-periphery、c-periphery和lua-periphery,python、lua和c任君挑选,通用性挺好的。
8 |5 T3 A! S0 E6 E+ g: e4 N( \; {: {) Q6 K/ |# W
S3 E O( ]3 h9 E( {3 kGPIO sysfs interface目前用的较广泛,但存在一些问题,比如不能并发获取sysfs属性、基本是为shell设计接口。所以从linux 4.8开始gpiod取代了它。
5 f" [6 e9 T$ B" ?6 oSince linux 4.8 the GPIO sysfs interface is deprecated. User space should use+ m% t0 i# u& i; U
the character device instead. This library encapsulates the ioctl calls and
& z' y7 }3 n" N4 ^2 Mdata structures behind a straightforward API. 2. 用libgpiod读写IO(Seeed支持这种操作)
^5 B! Z2 E7 c; Z' [9 S4 V新的设计gpiod,GPIO访问控制是通过操作字符设备文件(比如/dev/gpiodchip0)实现的,并通过libgpiod提供一些命令工具、c库以及python封装。; P+ U- q x( e# D/ h. N6 ~' F7 z
The new character device interface guarantees all allocated resources are
. f. W) }! a; q7 `; V! Wfreed after closing the device file descriptor and adds several new features
) d/ h2 q' k5 o5 t( Ythat are not present in the obsolete sysfs interface (like event polling,
. Y- L8 b* A& e% w$ |; v2 U5 }setting/reading multiple values at once or open-source and open-drain GPIOs). t C# S8 ~+ u2 ^) Y5 x
+ f. g6 \, b' M$ O, h/ B
Unfortunately interacting with the linux device file can no longer be done: j+ @; [7 c$ ~+ G' r1 v
using only standard command-line tools. This is the reason for creating a+ O5 j; \- N9 Q7 e" \; Q
library encapsulating the cumbersome, ioctl-based kernel-userspace interaction% u* d+ j0 |* F
in a set of convenient functions and opaque data structures.
B# z) v. U% f# y
$ ]! `- W) _& }0 h4 E. o; T: [Additionally this project contains a set of command-line tools that should1 t7 x+ O0 p- ^* k! }5 w
allow an easy conversion of user scripts to using the character device. 通过libgpiod提供的gpioset、gpioget、gpiomon可以快速的读写GPIO和检测输入事件。 X8 b a% p6 x6 N$ `
- sudo apt install -y gpiod' q- K z( b7 k! m1 v
- sudo gpioset 0 12=0 ) l. ` g* ], |, f; E5 M
- sudo gpiomon 0 12 # detect event on gpio12
复制代码 gpiod由于比较新,用的人还非常少,虽说libgpiod里面有python封装,但还没有打包到debian stretch的仓库里面,所以用python ctypes封装了一份,在voice-engine/gpio-next,控制一个LED的python代码是这样:# w" n/ s; @0 [, N8 l/ p; Q$ T
- import time
" n( q! L' \ Y; c8 d6 ~ - from gpio_next import GPIO
3 _8 h2 V- Z/ l) s9 ?) B! j
# p% h3 }/ I" O% y9 d- LED = GPIO(12, direction=1)7 i, `! x- q, [5 a
- for i in range(10):
. @8 ~$ A2 J. b7 T' U1 K - LED.write(i & 1)
& c% _5 E" e; ?, }; q9 s* z - time.sleep(1)
复制代码 当然也可以通过C语言调用系统操作的方式控制GPIO,具体的方法参照我的这篇博客:
( r( r0 i5 u8 L& v! tC语言调用shell命令
/ p9 K. r& U) G6 l0 f' E, V1 I1 ?7 E5 {5 E" V5 u$ ~6 y
# v6 F5 N# U& U* y( F用sysfs,或gpiod,都是用linux对GPIO封装的接口,封装之后的通用性好了,但性能会变弱。在Linux中同样也可以直接操作GPIO寄存器这里网上资源比较多且较为深入。笔者能力有限,所以在此不再叙述。3 p2 m0 S6 ?3 I3 M
' b; G' b) f3 r! x
' ? A' u: j7 J
' `; Z+ |! G# {7 ~/ Z5 |
|