
1. 用GPIO sysfs读写IO(Seeed的设备树不支持这种操作,仅作为介绍)6 S) X% U% a! j: @+ ?& g0 y# M 在Linux中,最常见的读写GPIO方式就是用GPIO sysfs interface,是通过操作/sys/class/gpio目录下的export、unexport、gpio{N}/direction, gpio{N}/value(用实际引脚号替代{N})等文件实现的,经常出现shell脚本里面。比如在shell中控制树莓派3B的GPIO12:3 s& O8 j+ ] d$ w2 {- o- N* j- g* d+ i
基于GPIO sysfs interface封装的库很多,这里推荐vsergeev写的python-periphery、c-periphery和lua-periphery,python、lua和c任君挑选,通用性挺好的。0 z- r- r* x; [ & L- Y p: @% e. S' ]/ Y* J GPIO sysfs interface目前用的较广泛,但存在一些问题,比如不能并发获取sysfs属性、基本是为shell设计接口。所以从linux 4.8开始gpiod取代了它。/ Q& \( k) M: }- p . S$ Q( c G" K Since linux 4.8 the GPIO sysfs interface is deprecated. User space should use the character device instead. This library encapsulates the ioctl calls and data structures behind a straightforward API.# i* ^, n+ \' J/ r/ _/ e+ \ ! i) n/ D! U8 `3 W 2. 用libgpiod读写IO(Seeed支持这种操作)% n: n" F( {9 Y, O: P 新的设计gpiod,GPIO访问控制是通过操作字符设备文件(比如/dev/gpiodchip0)实现的,并通过libgpiod提供一些命令工具、c库以及python封装。 ) E5 y# i: d$ w The new character device interface guarantees all allocated resources are freed after closing the device file descriptor and adds several new features that are not present in the obsolete sysfs interface (like event polling, setting/reading multiple values at once or open-source and open-drain GPIOs). Unfortunately interacting with the linux device file can no longer be done using only standard command-line tools. This is the reason for creating a library encapsulating the cumbersome, ioctl-based kernel-userspace interaction in a set of convenient functions and opaque data structures.% R1 q6 `9 g2 ~3 e( J6 z$ i 2 D/ z/ K: d, a7 j3 C! F% h Additionally this project contains a set of command-line tools that should allow an easy conversion of user scripts to using the character device.8 x4 J8 M5 D. {( T1 m: c" r 通过libgpiod提供的gpioset、gpioget、gpiomon可以快速的读写GPIO和检测输入事件。 4 X; c/ ~" K1 Q5 `$ p z* K& ?. w& x
gpiod由于比较新,用的人还非常少,虽说libgpiod里面有python封装,但还没有打包到debian stretch的仓库里面,所以用python ctypes封装了一份,在voice-engine/gpio-next,控制一个LED的python代码是这样:' B9 D; H q3 x! u ! t) l2 m$ w: m8 u# q2 t
当然也可以通过C语言调用系统操作的方式控制GPIO,具体的方法参照我的这篇博客: 用sysfs,或gpiod,都是用linux对GPIO封装的接口,封装之后的通用性好了,但性能会变弱。在Linux中同样也可以直接操作GPIO寄存器这里网上资源比较多且较为深入。笔者能力有限,所以在此不再叙述。2 m1 }, `- G M ———————————————— 版权声明:Argon_Ghost |
实战经验 | Keil工程使用NEAI库的异常问题
STM32 ISP IQTune:真正零门槛的免费ISP调整软件
【经验分享】STM32 新建基于STM32F40x 固件库的MDK5 工程
意法半导体MCU双供应链策略,打消中国客户后顾之忧
【经验分享】基于STM32使用HAL库实现USB组合设备CDC+MSC
2024意法半导体工业峰会:赋能智能电源和智能工业,构筑可持续未来
ST推出灵活、面向未来的智能电表通信解决方案,助力能源转型
意法半导体 x Qu-Bit Electronix:推动新一轮的数字声音合成革命
从STM32 MPU产品看嵌入式系统中微处理器的新变化
【Hot!】STM32全系列开发板都支持Arduino开发,你知道吗?