1. 用GPIO sysfs读写IO(Seeed的设备树不支持这种操作,仅作为介绍)3 L; T; z+ f4 \+ W U \
在Linux中,最常见的读写GPIO方式就是用GPIO sysfs interface,是通过操作/sys/class/gpio目录下的export、unexport、gpio{N}/direction, gpio{N}/value(用实际引脚号替代{N})等文件实现的,经常出现shell脚本里面。比如在shell中控制树莓派3B的GPIO12:
# |) ~/ K) \0 |/ o4 F5 @" Y- sudo su
) o5 k& y3 C3 U: D8 T9 ]- v - cd /sys/class/gpio
2 G6 Y7 q6 a+ S9 M) A; I, N - echo 12 > export6 m' I/ h" D2 W% }
- echo out > gpio12/direction # io used for output
. I& [% M2 z' }6 l, r - echo 1 > gpio12/value # output logic 1 level
, z% Z; Q' Q5 b5 D - echo 0 > gpio12/value # output logic 0 level
4 S5 r9 ^3 @) V, J- ]" }) ~: y - echo 12 > unexport
: _. s8 X/ ^5 H: [4 s
复制代码 基于GPIO sysfs interface封装的库很多,这里推荐vsergeev写的python-periphery、c-periphery和lua-periphery,python、lua和c任君挑选,通用性挺好的。
+ h/ K4 j% g) f2 m" N7 {$ M, B( q; i5 r' f7 H2 R
5 K, K0 f9 {3 V* M- X+ v, }- gGPIO sysfs interface目前用的较广泛,但存在一些问题,比如不能并发获取sysfs属性、基本是为shell设计接口。所以从linux 4.8开始gpiod取代了它。8 s8 l; e) {$ t
Since linux 4.8 the GPIO sysfs interface is deprecated. User space should use
/ r. h: `. B- E1 fthe character device instead. This library encapsulates the ioctl calls and& t' Q: G7 L5 D! O
data structures behind a straightforward API. 2. 用libgpiod读写IO(Seeed支持这种操作)5 c8 N ]' i4 A9 @/ v$ p% X7 U3 P
新的设计gpiod,GPIO访问控制是通过操作字符设备文件(比如/dev/gpiodchip0)实现的,并通过libgpiod提供一些命令工具、c库以及python封装。& ?! a5 [* Y) R6 l7 e7 U5 \
The new character device interface guarantees all allocated resources are
% y! f1 x: D" @8 l3 mfreed after closing the device file descriptor and adds several new features
?6 r" V% v$ e- z% C4 Y9 e% qthat are not present in the obsolete sysfs interface (like event polling,. ^- H; c+ ~+ h& L# b( T6 N& `
setting/reading multiple values at once or open-source and open-drain GPIOs).
& z: ^+ u& p$ f# w' K8 N: K) u9 q# p" O
Unfortunately interacting with the linux device file can no longer be done: b* d* Y1 y( T2 R$ L0 r
using only standard command-line tools. This is the reason for creating a6 G0 p) @ O* F" Q2 S: N2 B4 R
library encapsulating the cumbersome, ioctl-based kernel-userspace interaction |* R' G0 b( ~& K# q" e; [1 X/ \
in a set of convenient functions and opaque data structures." P. J+ Z Q' e- }& E
- ?: c# f0 P& O: JAdditionally this project contains a set of command-line tools that should' x; J- h( }# \3 k- R
allow an easy conversion of user scripts to using the character device. 通过libgpiod提供的gpioset、gpioget、gpiomon可以快速的读写GPIO和检测输入事件。
8 v9 o( \/ n1 W& ~0 `; Q- sudo apt install -y gpiod) [9 Q i' m: {4 T
- sudo gpioset 0 12=0
. n: a- V G+ U5 e* L9 E - sudo gpiomon 0 12 # detect event on gpio12
复制代码 gpiod由于比较新,用的人还非常少,虽说libgpiod里面有python封装,但还没有打包到debian stretch的仓库里面,所以用python ctypes封装了一份,在voice-engine/gpio-next,控制一个LED的python代码是这样:
# j# c) ^) d @; X) Q9 P9 r- import time. \' G& B& Z4 |. d4 Z* N- C9 i, Q: D
- from gpio_next import GPIO, P: ]2 t) |1 J/ ~
- 8 I' W3 W* P- d9 i! q e6 Q
- LED = GPIO(12, direction=1). w, N* ~ ]* Q6 s
- for i in range(10):* ^; F6 f# }# V6 t
- LED.write(i & 1)3 D* b: m" S. ?8 H+ S% r, _- y
- time.sleep(1)
复制代码 当然也可以通过C语言调用系统操作的方式控制GPIO,具体的方法参照我的这篇博客:
# f. L: V2 _; r' n, Z9 |; AC语言调用shell命令
& Q; n2 v! F h! G3 m/ R% E
3 W8 X+ A) x3 I& {$ D
8 w5 O5 O6 [# h用sysfs,或gpiod,都是用linux对GPIO封装的接口,封装之后的通用性好了,但性能会变弱。在Linux中同样也可以直接操作GPIO寄存器这里网上资源比较多且较为深入。笔者能力有限,所以在此不再叙述。
" s5 [3 {* X9 x }1 F; \
; x& ^. I, w( K/ M% d) s* R
& p( u# ~& P# r, p) }4 [2 A5 e3 I8 a$ q. n$ Z- ?( B: T' d5 }3 e
|