你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】STM32MP1开发板教程GPIO的简单操作

[复制链接]
STMCU-管管 发布时间:2021-6-2 14:32
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
  1. sudo su
    ) o5 k& y3 C3 U: D8 T9 ]- v
  2. cd /sys/class/gpio
    2 G6 Y7 q6 a+ S9 M) A; I, N
  3. echo 12 > export6 m' I/ h" D2 W% }
  4. echo out > gpio12/direction       # io used for output
    . I& [% M2 z' }6 l, r
  5. echo 1 > gpio12/value             # output logic 1 level
    , z% Z; Q' Q5 b5 D
  6. echo 0 > gpio12/value             # output logic 0 level
    4 S5 r9 ^3 @) V, J- ]" }) ~: y
  7. 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
  1. sudo apt install -y gpiod) [9 Q  i' m: {4 T
  2. sudo gpioset 0 12=0
    . n: a- V  G+ U5 e* L9 E
  3. 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
  1. import time. \' G& B& Z4 |. d4 Z* N- C9 i, Q: D
  2. from gpio_next import GPIO, P: ]2 t) |1 J/ ~
  3. 8 I' W3 W* P- d9 i! q  e6 Q
  4. LED = GPIO(12, direction=1). w, N* ~  ]* Q6 s
  5. for i in range(10):* ^; F6 f# }# V6 t
  6.     LED.write(i & 1)3 D* b: m" S. ?8 H+ S% r, _- y
  7.     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
赞 收藏 评论0 发布时间:2021-6-2 14:32

举报

0个回答

所属标签

相似技术帖

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版