1. 用GPIO sysfs读写IO(Seeed的设备树不支持这种操作,仅作为介绍) K4 o' f- I& `7 F* A6 w/ M a
在Linux中,最常见的读写GPIO方式就是用GPIO sysfs interface,是通过操作/sys/class/gpio目录下的export、unexport、gpio{N}/direction, gpio{N}/value(用实际引脚号替代{N})等文件实现的,经常出现shell脚本里面。比如在shell中控制树莓派3B的GPIO12:3 |# L6 ]+ c) I2 l' w
- sudo su
1 I3 ]% N6 K6 n1 b! T4 E0 U - cd /sys/class/gpio
* g/ b" o8 s ~% j+ x4 h - echo 12 > export" a. s5 C9 H" U; E: i8 d
- echo out > gpio12/direction # io used for output# q5 v1 K9 f5 F% Z; o* x$ G* x
- echo 1 > gpio12/value # output logic 1 level+ [; i) u M$ T9 I. V
- echo 0 > gpio12/value # output logic 0 level# A) @: @4 W1 I/ \3 h- z
- echo 12 > unexport' g4 N7 Z: g; G" K( ~
复制代码 基于GPIO sysfs interface封装的库很多,这里推荐vsergeev写的python-periphery、c-periphery和lua-periphery,python、lua和c任君挑选,通用性挺好的。+ J q+ B( N3 L8 v/ j. P
: V, B1 j- l! v& J: j( F* Z j
! \$ T+ u& V* y3 C. L k$ B6 s4 o5 ?
GPIO sysfs interface目前用的较广泛,但存在一些问题,比如不能并发获取sysfs属性、基本是为shell设计接口。所以从linux 4.8开始gpiod取代了它。
! G- r t& b7 k4 z) hSince linux 4.8 the GPIO sysfs interface is deprecated. User space should use- P i) X n6 E+ b: Z. q
the character device instead. This library encapsulates the ioctl calls and
2 S/ \* m/ e1 R# r! Wdata structures behind a straightforward API. 2. 用libgpiod读写IO(Seeed支持这种操作)
" _: i# q" Z J. O) ^& _- S9 q新的设计gpiod,GPIO访问控制是通过操作字符设备文件(比如/dev/gpiodchip0)实现的,并通过libgpiod提供一些命令工具、c库以及python封装。
6 ]$ l+ g. p! H! l; _The new character device interface guarantees all allocated resources are
; ^$ I# T7 A: w9 n# v) Vfreed after closing the device file descriptor and adds several new features; e8 x% ], I, ]1 U# |7 r
that are not present in the obsolete sysfs interface (like event polling,
- H e% O, a4 k+ ?) v4 Tsetting/reading multiple values at once or open-source and open-drain GPIOs).
9 E5 ]9 s- B. F0 c" P9 r D1 ^: L: I* f; W: K
Unfortunately interacting with the linux device file can no longer be done
% T% d* O, x: {) zusing only standard command-line tools. This is the reason for creating a
+ d" B2 l r3 [. H6 K( s( u- a( olibrary encapsulating the cumbersome, ioctl-based kernel-userspace interaction) ?/ W5 r( M7 W
in a set of convenient functions and opaque data structures., m) h! S" W# a7 ?5 o
2 x, o: k. }, F7 h% sAdditionally this project contains a set of command-line tools that should
0 z! G9 {5 I: r! H( h3 c1 @allow an easy conversion of user scripts to using the character device. 通过libgpiod提供的gpioset、gpioget、gpiomon可以快速的读写GPIO和检测输入事件。# D' x, i/ c$ S9 l
- sudo apt install -y gpiod
1 D. [3 r1 H* W - sudo gpioset 0 12=0 ' P+ T& n L, Y$ E' [3 V1 H r
- sudo gpiomon 0 12 # detect event on gpio12
复制代码 gpiod由于比较新,用的人还非常少,虽说libgpiod里面有python封装,但还没有打包到debian stretch的仓库里面,所以用python ctypes封装了一份,在voice-engine/gpio-next,控制一个LED的python代码是这样:
% }3 V8 _# [0 z# ~/ K- import time
" ^# f7 ^# Q8 H% G5 l: o - from gpio_next import GPIO6 `3 P! M! m0 ?9 o: ^) q1 e! }
5 n& {: W% D7 v' {6 r- q* W- LED = GPIO(12, direction=1)
$ B! T& I. @" N* f3 `. V2 d - for i in range(10):0 G$ G; E. g- a; ?' T
- LED.write(i & 1)" m( E4 B2 M' A& r: l3 X. S: i# F
- time.sleep(1)
复制代码 当然也可以通过C语言调用系统操作的方式控制GPIO,具体的方法参照我的这篇博客:" F* H, H( T, I% F; h; \
C语言调用shell命令
- [5 N/ o+ u" Z1 X; D: p! A: ^! D
) ~. N9 R( j& x7 e' R4 y, C( ^/ k+ Z1 w" e/ V4 w$ K- I G# G* b
用sysfs,或gpiod,都是用linux对GPIO封装的接口,封装之后的通用性好了,但性能会变弱。在Linux中同样也可以直接操作GPIO寄存器这里网上资源比较多且较为深入。笔者能力有限,所以在此不再叙述。
5 k. U% s1 {; H# x
+ n0 K" S: O3 N
1 ~# [, _9 c" J$ c% v6 d5 `2 R
9 z! B! R3 R( A0 R- @" J |