1. 用GPIO sysfs读写IO(Seeed的设备树不支持这种操作,仅作为介绍)- f5 A0 [# E) }
在Linux中,最常见的读写GPIO方式就是用GPIO sysfs interface,是通过操作/sys/class/gpio目录下的export、unexport、gpio{N}/direction, gpio{N}/value(用实际引脚号替代{N})等文件实现的,经常出现shell脚本里面。比如在shell中控制树莓派3B的GPIO12:
0 E# W- {! _% k3 o/ e/ V2 {+ Z- sudo su
! k+ S0 `! w3 c; A: d- u - cd /sys/class/gpio
5 M R' j: R; E+ x- y& M5 g+ A" ~ - echo 12 > export# ^6 f' m* i2 l! j9 a& O
- echo out > gpio12/direction # io used for output4 \: B" K1 J3 z+ V7 \
- echo 1 > gpio12/value # output logic 1 level
# [, a6 Q& ?, o( K* \2 \ - echo 0 > gpio12/value # output logic 0 level) L# G& a: |1 ?: r# y0 Q( _% B6 Y
- echo 12 > unexport+ _/ q+ n6 v( m" E
复制代码 基于GPIO sysfs interface封装的库很多,这里推荐vsergeev写的python-periphery、c-periphery和lua-periphery,python、lua和c任君挑选,通用性挺好的。
! \! L! Q% ?3 w/ ^7 H# d7 @# D+ j7 k; T! t
5 D" O% P8 v- A- k9 f! ~; w/ _6 kGPIO sysfs interface目前用的较广泛,但存在一些问题,比如不能并发获取sysfs属性、基本是为shell设计接口。所以从linux 4.8开始gpiod取代了它。! M: m/ n! ^/ K6 C) s" m& u
Since linux 4.8 the GPIO sysfs interface is deprecated. User space should use" ^- Z2 u: g, G+ c
the character device instead. This library encapsulates the ioctl calls and
* n+ G1 V; h Q& J+ }data structures behind a straightforward API. 2. 用libgpiod读写IO(Seeed支持这种操作)! v! J& S. ]) L2 C
新的设计gpiod,GPIO访问控制是通过操作字符设备文件(比如/dev/gpiodchip0)实现的,并通过libgpiod提供一些命令工具、c库以及python封装。$ h$ S- {3 \* [* i6 c4 e
The new character device interface guarantees all allocated resources are
/ X8 P3 p; M) U' N6 L1 x3 `0 Mfreed after closing the device file descriptor and adds several new features
2 D! [! u7 A# l+ {% `0 Lthat are not present in the obsolete sysfs interface (like event polling,$ u `, c6 E9 A1 T6 H u
setting/reading multiple values at once or open-source and open-drain GPIOs).9 d9 \1 E7 n. z
/ g' ?& i% y$ ~; o% `. rUnfortunately interacting with the linux device file can no longer be done
9 a1 A; ?% D, V! Ousing only standard command-line tools. This is the reason for creating a
; X! ^( o" }9 z4 ]+ nlibrary encapsulating the cumbersome, ioctl-based kernel-userspace interaction
( f& f \3 a9 M& m% `! F9 T/ _) W& Yin a set of convenient functions and opaque data structures.
4 I: J ?) j+ ^1 n, u* t1 h- K- N7 t5 [) C( B
Additionally this project contains a set of command-line tools that should5 u- D( ^8 j9 |0 r: ]! ^( D
allow an easy conversion of user scripts to using the character device. 通过libgpiod提供的gpioset、gpioget、gpiomon可以快速的读写GPIO和检测输入事件。
4 ]3 b- ~' K2 b, v2 X: A- sudo apt install -y gpiod0 a# k. l5 w* y5 Y' {
- sudo gpioset 0 12=0 - }3 U! l% C. Y* p4 r3 z5 n& j
- sudo gpiomon 0 12 # detect event on gpio12
复制代码 gpiod由于比较新,用的人还非常少,虽说libgpiod里面有python封装,但还没有打包到debian stretch的仓库里面,所以用python ctypes封装了一份,在voice-engine/gpio-next,控制一个LED的python代码是这样:8 w: f7 _' N6 e4 ^: i& W
- import time" i1 M+ R* `+ ]3 d0 J' g$ h, @
- from gpio_next import GPIO3 f% `+ f+ o+ [2 o, C# {
: ^; k$ g' C, ^# x* N! z- LED = GPIO(12, direction=1)
* p1 ^0 e$ {7 h% {8 r8 x N - for i in range(10):6 l) a) q; U6 Q' K+ m1 a
- LED.write(i & 1)/ H. l |8 i- c$ v2 \% @
- time.sleep(1)
复制代码 当然也可以通过C语言调用系统操作的方式控制GPIO,具体的方法参照我的这篇博客:
: r' B' C5 q' A+ R8 k: bC语言调用shell命令
3 W2 U, E+ {5 u% l4 E
$ E; o/ Q U7 _! z! U" |2 a9 v7 j* C( `3 o3 E6 N( F) H9 B
用sysfs,或gpiod,都是用linux对GPIO封装的接口,封装之后的通用性好了,但性能会变弱。在Linux中同样也可以直接操作GPIO寄存器这里网上资源比较多且较为深入。笔者能力有限,所以在此不再叙述。9 p; s. r: U$ e- _/ N- N5 t* S
, d. u' I% O5 H+ c5 G6 R) k, M9 j j* u( C
6 d, `9 L! c2 Y/ S
|