1. 用GPIO sysfs读写IO(Seeed的设备树不支持这种操作,仅作为介绍)
o2 X! `. h0 w( z0 N8 k在Linux中,最常见的读写GPIO方式就是用GPIO sysfs interface,是通过操作/sys/class/gpio目录下的export、unexport、gpio{N}/direction, gpio{N}/value(用实际引脚号替代{N})等文件实现的,经常出现shell脚本里面。比如在shell中控制树莓派3B的GPIO12:
8 K1 A5 V. n! p- y4 G, Q- sudo su
* T1 M8 P9 M" `/ R9 _( Z - cd /sys/class/gpio
+ ]) k7 S; M7 }) [9 x, m* ^ - echo 12 > export
" g3 r- ^$ b( { r - echo out > gpio12/direction # io used for output: Q0 t5 O9 z2 `8 c# H% g5 G! o
- echo 1 > gpio12/value # output logic 1 level
; f: ]/ |4 \" k: A) }# f! `, G - echo 0 > gpio12/value # output logic 0 level Q; W7 K( a5 X! h; M* t
- echo 12 > unexport
6 \7 ?. u, r# X
复制代码 基于GPIO sysfs interface封装的库很多,这里推荐vsergeev写的python-periphery、c-periphery和lua-periphery,python、lua和c任君挑选,通用性挺好的。
6 U( |; g# i# U" v+ ?; Q0 w1 w- ~9 i* `$ G) j! K! ^
L) W8 f& V8 e# n
GPIO sysfs interface目前用的较广泛,但存在一些问题,比如不能并发获取sysfs属性、基本是为shell设计接口。所以从linux 4.8开始gpiod取代了它。
5 G/ Y. e- u$ z' j+ oSince linux 4.8 the GPIO sysfs interface is deprecated. User space should use
- b* F* ?, W. I1 c) K' athe character device instead. This library encapsulates the ioctl calls and+ {0 T& M7 B! Y% E: n' I
data structures behind a straightforward API. 2. 用libgpiod读写IO(Seeed支持这种操作)% q$ p* @9 d8 T
新的设计gpiod,GPIO访问控制是通过操作字符设备文件(比如/dev/gpiodchip0)实现的,并通过libgpiod提供一些命令工具、c库以及python封装。
7 g8 R! m6 K9 \8 K/ `( `The new character device interface guarantees all allocated resources are
8 m1 e3 D4 G; z' nfreed after closing the device file descriptor and adds several new features
. o+ o+ d1 A1 p* [) T+ n6 ]that are not present in the obsolete sysfs interface (like event polling,
: |* I1 K- i- A" F( A& ?setting/reading multiple values at once or open-source and open-drain GPIOs).! w( ~' |3 E+ ~! C' h
W8 ^ P3 [' {& U& vUnfortunately interacting with the linux device file can no longer be done
8 V/ b) R$ t `- Xusing only standard command-line tools. This is the reason for creating a4 [* p& F5 K" g) [- v7 O, U
library encapsulating the cumbersome, ioctl-based kernel-userspace interaction
8 q# \4 r7 `6 g6 G {6 ?( ~7 Cin a set of convenient functions and opaque data structures.
& [& |- o/ `8 [& f# g9 l3 J4 i- }7 y2 x1 i
Additionally this project contains a set of command-line tools that should6 Z! n6 v' T$ N8 [
allow an easy conversion of user scripts to using the character device. 通过libgpiod提供的gpioset、gpioget、gpiomon可以快速的读写GPIO和检测输入事件。+ l2 [/ F. o r. q" P* x
- sudo apt install -y gpiod
8 a) ^1 K1 b W- b - sudo gpioset 0 12=0
5 ?* y$ q% h+ C: a$ G4 u - sudo gpiomon 0 12 # detect event on gpio12
复制代码 gpiod由于比较新,用的人还非常少,虽说libgpiod里面有python封装,但还没有打包到debian stretch的仓库里面,所以用python ctypes封装了一份,在voice-engine/gpio-next,控制一个LED的python代码是这样:
, {, ?2 c+ A2 |; C- d4 Q2 u- import time
8 `+ a) s, k' y - from gpio_next import GPIO
; A- R9 Z- l: c, G; l& p4 H - 6 d. p- ^8 R9 A( l( k+ F$ q+ i
- LED = GPIO(12, direction=1)
1 T1 k* l; z3 o+ T - for i in range(10):
3 f, C/ d( P7 s) [: z$ u! u - LED.write(i & 1)
$ V+ I, W, P4 Z" z- i. r - time.sleep(1)
复制代码 当然也可以通过C语言调用系统操作的方式控制GPIO,具体的方法参照我的这篇博客:5 P1 r* b, r5 k0 C# d$ |
C语言调用shell命令
4 F! F3 E# V4 a; K3 t3 r
o. _+ F. E5 b0 y' r) E: _- j9 U, A% [( m3 r- G( A: J
用sysfs,或gpiod,都是用linux对GPIO封装的接口,封装之后的通用性好了,但性能会变弱。在Linux中同样也可以直接操作GPIO寄存器这里网上资源比较多且较为深入。笔者能力有限,所以在此不再叙述。$ m) o& h4 j h9 Q+ b+ {: J
Q" H4 U- k* o- G% {7 d
2 F. y* u( x# p7 N; P9 K! d
% m" F+ L i7 o* R" ` |