1. 用GPIO sysfs读写IO(Seeed的设备树不支持这种操作,仅作为介绍)
; f; V- Q/ d2 v2 [- {, {在Linux中,最常见的读写GPIO方式就是用GPIO sysfs interface,是通过操作/sys/class/gpio目录下的export、unexport、gpio{N}/direction, gpio{N}/value(用实际引脚号替代{N})等文件实现的,经常出现shell脚本里面。比如在shell中控制树莓派3B的GPIO12:
/ q) y) \. e0 z) _- sudo su9 j9 y: a& }' @# Q; e
- cd /sys/class/gpio2 _$ a. @2 @8 Y: G2 V4 F
- echo 12 > export
# x7 i9 p" K9 w1 ]6 P, X+ P - echo out > gpio12/direction # io used for output$ f6 t6 x! C3 Y v7 \+ u# O
- echo 1 > gpio12/value # output logic 1 level
7 F D( [7 Z: n' o - echo 0 > gpio12/value # output logic 0 level& z; U0 ]; m& w- x/ x0 g
- echo 12 > unexport
' N8 K5 o9 L1 \# ~
复制代码 基于GPIO sysfs interface封装的库很多,这里推荐vsergeev写的python-periphery、c-periphery和lua-periphery,python、lua和c任君挑选,通用性挺好的。
Z/ ~4 u8 w% p$ }5 l" W G1 y( I1 S! o) b4 _7 x
" `" P4 l7 Z6 z K
GPIO sysfs interface目前用的较广泛,但存在一些问题,比如不能并发获取sysfs属性、基本是为shell设计接口。所以从linux 4.8开始gpiod取代了它。
" d' E& ` E! L" i( P6 E1 ySince linux 4.8 the GPIO sysfs interface is deprecated. User space should use
2 }5 ]+ ^" p6 c4 |+ B) J& bthe character device instead. This library encapsulates the ioctl calls and
& q u' j6 H, Y' A3 x6 gdata structures behind a straightforward API. 2. 用libgpiod读写IO(Seeed支持这种操作)1 i& F0 R1 D0 z3 T6 L5 p
新的设计gpiod,GPIO访问控制是通过操作字符设备文件(比如/dev/gpiodchip0)实现的,并通过libgpiod提供一些命令工具、c库以及python封装。* ~% w9 K1 Q9 U6 \0 h
The new character device interface guarantees all allocated resources are: J* ]2 |7 g& \/ q( g- t* H1 |
freed after closing the device file descriptor and adds several new features* F! @$ o8 n; M6 G1 t, F3 E
that are not present in the obsolete sysfs interface (like event polling,
, b5 Q) n- L: m* x# x& p1 O8 h' {setting/reading multiple values at once or open-source and open-drain GPIOs).2 u. E- B ~* i; {! s/ D, ^
; f+ J5 M a$ w- B* z: p: G6 GUnfortunately interacting with the linux device file can no longer be done
) C7 _; X G, F% z. H1 \using only standard command-line tools. This is the reason for creating a
2 W/ Q+ n# Z( F6 {( r; |( d1 `; I- [library encapsulating the cumbersome, ioctl-based kernel-userspace interaction# [0 y& u9 O9 D! D6 F
in a set of convenient functions and opaque data structures." P# I+ o6 c' M' \+ X
) _" G8 i. u$ `9 [7 R( q/ y% y3 |+ F% u
Additionally this project contains a set of command-line tools that should7 u6 M6 b! h1 X8 M$ i$ D; I
allow an easy conversion of user scripts to using the character device. 通过libgpiod提供的gpioset、gpioget、gpiomon可以快速的读写GPIO和检测输入事件。' ]5 q. G" s% C4 L
- sudo apt install -y gpiod# }0 ]+ [# c# H. z2 N
- sudo gpioset 0 12=0 3 E4 g" {* a8 u2 W, D. `
- sudo gpiomon 0 12 # detect event on gpio12
复制代码 gpiod由于比较新,用的人还非常少,虽说libgpiod里面有python封装,但还没有打包到debian stretch的仓库里面,所以用python ctypes封装了一份,在voice-engine/gpio-next,控制一个LED的python代码是这样:" t5 ^: j6 l8 t9 p) q2 W; ~
- import time" n8 \% P" y5 f- x; H! I
- from gpio_next import GPIO( r o* X u2 O& f1 @
- / e/ y7 z' b4 i
- LED = GPIO(12, direction=1)' b4 @4 _5 M1 }' b8 ]+ u
- for i in range(10):
4 L0 |% p7 u, n - LED.write(i & 1)
, n) r- _0 A0 f/ X- F6 m - time.sleep(1)
复制代码 当然也可以通过C语言调用系统操作的方式控制GPIO,具体的方法参照我的这篇博客:
+ c, `$ y. i/ \1 D0 kC语言调用shell命令
: z: K( [4 E5 C6 Q( Q" c8 F: ?# }, t' k4 f
" B8 |" O6 _/ ~
用sysfs,或gpiod,都是用linux对GPIO封装的接口,封装之后的通用性好了,但性能会变弱。在Linux中同样也可以直接操作GPIO寄存器这里网上资源比较多且较为深入。笔者能力有限,所以在此不再叙述。" J$ ?; G' |3 W* Z6 W& d8 H5 k8 {
* @4 {4 t7 h# a0 i. @: O
! Z; I! ^: s# m. t. O
( f8 F% t1 p8 W" m4 `1 D" @( O/ Z |