Raspberry pi等的GPIO新工具libgpiod 【日积月累计划】
一、概述
1.1背景
大家在使用Raspberry Pi和其它的fruit pi工具时往往选择wiring pi.这是一个不错的选择,但是wiring pi的老爷子因为种种原因,不愿意再维护wiring pi。(或者说只面向自己的client。太可惜了。详细查看作者的博文。)但是raspberry pi一般已经预装了wiringpi。暂时可能还是最好的选择。尽管rpi4b 64位上使用gpio readall命令会有如下显示:
pi@raspberrypi:~ $ gpio -v
gpio version: 2.50
Copyright (c) 2012-2018 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty
Raspberry Pi Details:
Type: Unknown17, Revision: 02, Memory: 0MB, Maker: Sony
* Device tree is enabled.
*--> Raspberry Pi 4 Model B Rev 1.2
* This Raspberry Pi supports user-level GPIO access.
pi@raspberrypi:~ $ gpio readall
Oops - unable to determine board type... model: 17
但是处理也很简单:从官网下载最新版本然后用dpkg安装。
# if you want to remove it
sudo apt-get remove wiringpi
# download and install
cd ~/Downloads
wget https://project-downloads.drogon.net/wiringpi-latest.deb
sudo dpkg -i wiringpi-latest.deb
# autoremove and quroclean
sudo apt-get autoremove
sudo apt-get autoclean
rm wiringpi-last.deb
安装完成之后测试的效果:
pi@raspberrypi:~/Downloads $ gpio -v
gpio version: 2.52
Copyright (c) 2012-2018 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty
Raspberry Pi Details:
Type: Pi 4B, Revision: 02, Memory: 2048MB, Maker: Sony
* Device tree is enabled.
*--> Raspberry Pi 4 Model B Rev 1.2
* This Raspberry Pi supports user-level GPIO access.
pi@raspberrypi:~/Downloads $ gpio readall
+-----+-----+---------+------+---+---Pi 4B--+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| | | 3.3v | | | 1 || 2 | | | 5v | | |
| 2 | 8 | SDA.1 | IN | 1 | 3 || 4 | | | 5v | | |
| 3 | 9 | SCL.1 | IN | 1 | 5 || 6 | | | 0v | | |
| 4 | 7 | GPIO. 7 | IN | 1 | 7 || 8 | 1 | IN | TxD | 15 | 14 |
| | | 0v | | | 9 || 10 | 1 | IN | RxD | 16 | 15 |
| 17 | 0 | GPIO. 0 | IN | 0 | 11 || 12 | 0 | IN | GPIO. 1 | 1 | 18 |
| 27 | 2 | GPIO. 2 | IN | 0 | 13 || 14 | | | 0v | | |
| 22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23 |
| | | 3.3v | | | 17 || 18 | 0 | IN | GPIO. 5 | 5 | 24 |
| 10 | 12 | MOSI | IN | 0 | 19 || 20 | | | 0v | | |
| 9 | 13 | MISO | IN | 0 | 21 || 22 | 0 | IN | GPIO. 6 | 6 | 25 |
| 11 | 14 | SCLK | IN | 0 | 23 || 24 | 1 | IN | CE0 | 10 | 8 |
| | | 0v | | | 25 || 26 | 1 | IN | CE1 | 11 | 7 |
| 0 | 30 | SDA.0 | IN | 1 | 27 || 28 | 1 | IN | SCL.0 | 31 | 1 |
| 5 | 21 | GPIO.21 | IN | 1 | 29 || 30 | | | 0v | | |
| 6 | 22 | GPIO.22 | IN | 1 | 31 || 32 | 0 | IN | GPIO.26 | 26 | 12 |
| 13 | 23 | GPIO.23 | IN | 0 | 33 || 34 | | | 0v | | |
| 19 | 24 | GPIO.24 | IN | 0 | 35 || 36 | 0 | IN | GPIO.27 | 27 | 16 |
| 26 | 25 | GPIO.25 | IN | 0 | 37 || 38 | 0 | IN | GPIO.28 | 28 | 20 |
| | | 0v | | | 39 || 40 | 0 | IN | GPIO.29 | 29 | 21 |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+---Pi 4B--+---+------+---------+-----+-----+
pi@raspberrypi:~/Downloads $
rpi官方再介绍gpio是推荐了多个工具.其中对于C/C++的推荐工具有两个:libgpid和pigpio。
本篇文章简单介绍一下libgpiod。这个库之前再gihub上维护,但是现在已经迁移到了kernel.org,由linux内核团队官方维护。所以这意味者这个库的广泛使用能力。
1.2 libgpiod的特性
官方关于这个库的介绍如下:
libgpiod
========
libgpiod - C library and tools for interacting with the linux GPIO
character device (gpiod stands for GPIO device)
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.
自linux4.8后, gpio的sysfs接口被废弃。在用户空间应当使用character device(字符设备)代替sysfs的gpio接口。
这个库包含了ioctl调用和数据结构,它们均使用简单明了的api接口。
上面替代的character device翻译作字符设备,与之对应的是block device(块设备)。新的字符设备接口可以保证在关闭设备描述符后完整的释放资源,但是却不能在标准命令行里面调用。这是开发这个库的原因。这个库除了gcc,不依赖于其它库。但是在编译的时候,需要linux header包。
二、编译
2.1、依赖
A: 确保已经安装了antoconf,并且版本大于2.61
autoconf --version
B: linux header
编译所依赖的linux header版本可以查看configure.ac获得。比如:
[], [AC_MSG_ERROR(["libgpiod needs linux headers version >= v5.5.0"])])
另外需要用uname看一下kernel的版本
uname -srm
如果内核版本太低的话,可以将git checkout将libgpiod切换到一个合适的版本。当然也可以升级内核,但是通常这没有必要。
如果有必要升级内核,请自行搜索内核升级办法。比如rpi要增添buster-backports源才可以升级。
目前我看到的是自1.5版本以后都需要内核在5.5.0以上。但rpi目前最新版本是5.4.xx。所以还挺难受的。
(因为内核版本的问题,暂时不想升级到5.5.0,怕出问题。但又不想编译太老的版本,因为不值得。准备之后编译。所以下面的步骤均没有亲自测试。但为了文档完整性,将官方的步骤翻译贴在后面。见谅!)
2.2、配置编译安装
./autogen.sh --enable-tools=yes --prefix=<install path>
make
make install
2.3 其它说明
另外可以直接再debian中安装libgpiod-dev,libgpio2和libgpiod-doc的包。
pi@raspberrypi:~/Workspace/gpio/libgpiod $ sudo apt-cache search libgpiod
gpiod - Tools for interacting with Linux GPIO character device - binary
libgpiod-dev - C library for interacting with Linux GPIO device - static libraries and headers
libgpiod-doc - C library for interacting with Linux GPIO device - library documentation
libgpiod2 - C library for interacting with Linux GPIO device - shared libraries
python3-libgpiod - Python bindings for libgpiod (Python 3)
pi@raspberrypi:~/Workspace/gpio/libgpiod $ sudo apt search libgpiod-dev
Sorting... Done
Full Text Search... Done
libgpiod-dev/stable 1.2-3 arm64
C library for interacting with Linux GPIO device - static libraries and headers
pi@raspberrypi:~/Workspace/gpio/libgpiod $ sudo apt search libgpiod2
Sorting... Done
Full Text Search... Done
libgpiod2/stable 1.2-3 arm64
C library for interacting with Linux GPIO device - shared libraries
预计v2.0很快也会release。等内核升级后再编译吧。
三、测试
3.1 命令行
libgpiod支持6个命令行测试命令,分别是:
gpiodetect :列举所有的gpio芯片以及它们名字,标签和io数目
gpioinfo:列举所有gpio芯片以及它们的名字,制造商,方向,激活状态,附加标志等
gpioget <gpiochip_name + gpio_line_number>:获取特定gpio引脚的当前值
gpioset <gpiochip_name + gpio_line_number>:设置特定gpio引脚的当前值
gpiofind <gpio line number?>:获取gpiochip的name和offset
gpiomon <gpio line number + event ?>: 监视特定io上的特定事件
具体请看这里。
3.2 C调用
3.2.1、引用头文件<libgpiod.h>
3.2.2、定义gpio结构体
3.2.3、定义line
3.2.4、操作
3.2.5、释放
废话太多,直接上example(别人的)
https://github.com/starnight/libgpiod-example/
.
3.3 c++调用
编译时需要c++11支持
3.4 python调用
可以使用如下命令安装:
sudo apt update
sudo apt install python3-libgpiod
示例代码:
import gpiod
import time
chip=gpiod.Chip('gpiochip0')
lines = chip.get_lines([ 24 ])
lines.request(consumer='foobar', type=gpiod.LINE_REQ_DIR_OUT, default_vals=[ 0 ])
while True:
lines.set_values([ 1 ])
time.sleep(1)
lines.set_values([ 0 ])
time.sleep(1)
3.5 更详细的介绍
参见附件
四、总结
缺点是代码繁琐,不直观。目前配置稍微复杂一些,4.8以上的内核才引入libgpiod。而最新的1.5之后的版本全部需要5.5.0内核加持,这在目前的一些设备上还不适用。优点是官方库,更稳定
本文地址:https://blog.csdn.net/watershade2010/article/details/108559348
上一篇: iPhone12如何取消隐藏App