Open BMC开发系列(六)增加对GPIO的支持

Open BMC开发系列(六)增加对GPIO的支持,第1张

Open BMC开发系列(六)增加对GPIO的支持

增加BMC对GPIO的支持,与嵌入式增加GPIO的逻辑是一样的。

1,定义设备树,安装GPIO的驱动

        默认linux都包含GPIO的驱动,查看是否安装了GPIO,去查看/sys/class/gpio是否存在,存在则表示gpio驱动已经安装。不存在,则需要打开linux内核的GPIO的驱动开关。

 2  查看GPIO口的使用情况

        有时候,我们在配置IO口时会被占用的情况,或者配置IO口后查看IO的情况,这个是否需要使用命令:gpioinfo 

        查看GPIO的base地址

root@s2600wf:/sys/class/gpio# cat /sys/devices/platform/ahb/ahb:apb/1e780000.gpio/gpio/*/base                                                                                                                                                                                                                             
792

3,读写IO的三种方法 3.1 用GPIO sysfs读写IO

在Linux中,最常见的读写GPIO方式就是用GPIO sysfs interface,是通过 *** 作/sys/class/gpio目录下的export、unexport、gpio{N}/direction, gpio{N}/value(用实际引脚号替代{N})等文件实现的,经常出现shell脚本里面。比如在shell中控制树莓派3B的GPIO12:

sudo su
cd /sys/class/gpio
echo 12 > export                  # IO的地址 base + offset
echo out > gpio12/direction       # io used for output
echo 1 > gpio12/value             # output logic 1 level
echo 0 > gpio12/value             # output logic 0 level
echo 12 > unexport

基于GPIO sysfs interface封装的库很多,这里推荐vsergeev写的python-periphery、c-periphery和lua-periphery,python、lua和c任君挑选,通用性挺好的。

GPIO sysfs interface目前用的较广泛,但存在一些问题,比如不能并发获取sysfs属性、基本是为shell设计接口。所以从linux 4.8开始gpiod取代了它。

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.
3.2 用libgpiod读写IO

新的设计gpiod,GPIO访问控制是通过 *** 作字符设备文件(比如/dev/gpiodchip0)实现的,并通过libgpiod提供一些命令工具、c库以及python封装。

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.

Additionally this project contains a set of command-line tools that should
allow an easy conversion of user scripts to using the character device.

通过libgpiod提供的gpioset、gpioget、gpiomon可以快速的读写GPIO和检测输入事件。

sudo apt install -y gpiod
sudo gpioset 0 12=0 
sudo gpiomon 0 12   # detect event on gpio12

gpiod由于比较新,用的人还非常少,虽说libgpiod里面有python封装,但还没有打包到debian stretch的仓库里面,所以用python ctypes封装了一份,在voice-engine/gpio-next,控制一个LED的python代码是这样:

import time
from gpio_next import GPIO

LED = GPIO(12, direction=1)
for i in range(10):
    LED.write(i & 1)
    time.sleep(1)

用sysfs,或gpiod,都是用linux对GPIO封装的接口,封装之后的通用性好了,但性能会变弱。如果学过51、ARM单片机,最熟悉读写GPIO方式大概就是直接 *** 作GPIO寄存器,这是最快速的 *** 控IO口方式,而在Linux中同样也可以直接 *** 作GPIO寄存器。

3.3. 寄存器直接读写IO

树莓派相对单片机,用的是带MMU的ARM SoC BCM2837,地址空间略复杂一点,有不同的地址空间的映射,从 BCM2837数据手册来看,GPIO寄存器总线地址(bus address)从0x7E200000开始(见下图),映射的物理地址是0x3F200000(参见手册第6页)。

在Linux下直接读写物理地址,先要打开设备文件/dev/mem,然后用mmap把文件映射,最后根据寄存器地址读写相应的寄存器。

可以用devmem2来读写物理内存地址,如果不能apt install devmem2,可以在https://github.com/VCTLabs/devmem2下载并编译一份。比如,读写树莓派的IO状态:

sudo devmem2 0x3F200034 w

Physical addresses range from 0x3F000000 to 0x3FFFFFFF for peripherals. The bus addresses for peripherals are setup to map on to the peripheral bus add ressrange starting at 0x7E000000. Thus a peripheral advertised here at bus address 0x7Ennnnnn is available at physical address 0x3Fnnnnnn.

Python库RPi.GPIO就是通过直接访问寄存器控制GPIO的,由于不同芯片的GPIO寄存器地址大多不一样,所以没有什么通用性。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5619132.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存