20240624在飞凌OK3588-C的Buildroot下查证GPIO64和gpiochip64的差异
2024/6/24 20:19
GPIOchip代表GPIO控制器的编号,gpio代表特定GPIO的引脚号
本文以linux R4/Buildroot位例子,同样适用于Android12和其他【使用linux内核的】操作系统。
https://www.jianshu.com/p/481365ecafa0
linux GPIO
gpio_operation 通过/sys/文件接口操作IO端口 GPIO到文件系统的映射
◇ 控制GPIO的目录位于/sys/class/gpio,操作需要管理员权限
◇ /sys/class/gpio/export文件用于通知系统需要导出控制的GPIO引脚编号
◇ /sys/class/gpio/unexport 用于通知系统取消导出
◇ /sys/class/gpio/gpiochipX目录保存系统中GPIO寄存器的信息,包括每个寄存器控制引脚的起始编号base,寄存器名称,引脚总数
导出一个引脚的操作步骤
◇ 首先计算此引脚编号,引脚编号 = 控制引脚的寄存器基数 + 控制引脚寄存器位数
◇ 向/sys/class/gpio/export写入此编号,比如12号引脚,在shell中可以通过以下命令实现,命令成功后生成/sys/class/gpio/gpio12目录,如果没有出现相应的目录,说明此引脚不可导出
◇ direction文件,定义输入输入方向,可以通过下面命令定义为输出。direction接受的参数:in, out, high, low。high/low同时设置方向为输出,并将value设置为相应的1/0
◇ value文件是端口的数值,为1或0
手底下板子的引脚和引脚号的对应关系
A4 228
C5 245
B5 237
B4 236
关于引脚号的计算
以GPIO5_B4为例
PIN计算 A=0 B=1 C=2 D=3
pin=18+4=12
gpionumber = BANK32+PIN
所以对应的序号为5*32+12=172
相关的LOG:
root@ok3588:/#
root@ok3588:/# cat sys/kernel/debug/gpio
gpiochip0: GPIOs 0-31, parent: platform/fd8a0000.gpio, gpio0:
gpio-0 ( |work ) out lo
gpio-4 ( |cd ) in hi ACTIVE LOW
gpio-24 ( |vbus5v0-typec0 ) out lo
gpio-27 ( |vbus5v0-typec1 ) out lo
gpiochip1: GPIOs 32-63, parent: platform/fec20000.gpio, gpio1:
gpio-38 ( |bt-wake ) out hi
gpio-44 ( |snps,reset ) out hi ACTIVE LOW
gpio-54 ( |vcc-5g ) out lo ACTIVE LOW
gpio-61 ( |hdmirx-det ) in hi ACTIVE LOW
gpiochip2: GPIOs 64-95, parent: platform/fec30000.gpio, gpio2:
gpio-84 ( |vcc3v3-sdmmc ) out hi
gpiochip3: GPIOs 96-127, parent: platform/fec40000.gpio, gpio3:
gpio-106 ( |dsi0-en ) out hi
gpio-113 ( |enable ) in lo
gpio-118 ( |dsi1-en ) out hi
gpiochip4: GPIOs 128-159, parent: platform/fec50000.gpio, gpio4:
gpio-128 ( |sbu1-dc ) out lo
gpio-129 ( |sbu1-dc ) out lo
gpio-130 ( |sbu2-dc ) out lo
gpio-136 ( |sbu2-dc ) out lo
gpio-137 ( |enable ) out hi
gpiochip5: GPIOs 509-511, parent: platform/rk806-pinctrl.1.auto, rk806-gpio, can sleep:
root@ok3588:/#
root@ok3588:/#
root@ok3588:/#
root@ok3588:/#
root@ok3588:/#
root@ok3588:/#
root@ok3588:/# cd /sys/class/gpio/
root@ok3588:/sys/class/gpio#
root@ok3588:/sys/class/gpio# ls -l
total 0
--w------- 1 root root 4096 Jan 1 08:00 export
lrwxrwxrwx 1 root root 0 Jan 1 08:00 gpiochip0 -> ../../devices/platform/fd8a0000.gpio/gpio/gpiochip0
lrwxrwxrwx 1 root root 0 Jan 1 08:00 gpiochip128 -> ../../devices/platform/fec50000.gpio/gpio/gpiochip128
lrwxrwxrwx 1 root root 0 Jan 1 08:00 gpiochip32 -> ../../devices/platform/fec20000.gpio/gpio/gpiochip32
lrwxrwxrwx 1 root root 0 Jan 1 08:00 gpiochip509 -> ../../devices/platform/feb20000.spi/spi_master/spi2/spi2.0/rk806-pinctrl.1.auto/gpio/gpiochip509
lrwxrwxrwx 1 root root 0 Jan 1 08:00 gpiochip64 -> ../../devices/platform/fec30000.gpio/gpio/gpiochip64
lrwxrwxrwx 1 root root 0 Jan 1 08:00 gpiochip96 -> ../../devices/platform/fec40000.gpio/gpio/gpiochip96
--w------- 1 root root 4096 Jan 1 08:00 unexport
root@ok3588:/sys/class/gpio#
root@ok3588:/sys/class/gpio#
root@ok3588:/#
soot@ok3588:/# cat /sys/kernel/debug/pinctrl/pinctrl-rockchip-pinctrl/pinmux-pins
Pinmux settings per pin
Format: pin (name): mux_owner gpio_owner hog?
pin 0 (gpio0-0): leds gpio0:0 function leds_gpio group work-led-gpio
pin 32 (gpio1-0): feb90000.serial (GPIO UNCLAIMED) function uart6 group uart6m1-xfer
pin 64 (gpio2-0): (MUX UNCLAIMED) (GPIO UNCLAIMED)
pin 96 (gpio3-0): fe1c0000.ethernet (GPIO UNCLAIMED) function gmac1 group gmac1-rgmii-bus
pin 128 (gpio4-0): (MUX UNCLAIMED) gpio4:128
root@ok3588:/#
root@ok3588:/#
root@ok3588:/#
root@ok3588:/# cd /sys/class/gpio/
root@ok3588:/sys/class/gpio#
root@ok3588:/sys/class/gpio# ls -l
total 0
--w------- 1 root root 4096 Jan 1 08:00 export
lrwxrwxrwx 1 root root 0 Jan 1 08:00 gpiochip0 -> ../../devices/platform/fd8a0000.gpio/gpio/gpiochip0
lrwxrwxrwx 1 root root 0 Jan 1 08:00 gpiochip128 -> ../../devices/platform/fec50000.gpio/gpio/gpiochip128
lrwxrwxrwx 1 root root 0 Jan 1 08:00 gpiochip32 -> ../../devices/platform/fec20000.gpio/gpio/gpiochip32
lrwxrwxrwx 1 root root 0 Jan 1 08:00 gpiochip509 -> ../../devices/platform/feb20000.spi/spi_master/spi2/spi2.0/rk806-pinctrl.1.auto/gpio/gpiochip509
lrwxrwxrwx 1 root root 0 Jan 1 08:00 gpiochip64 -> ../../devices/platform/fec30000.gpio/gpio/gpiochip64
lrwxrwxrwx 1 root root 0 Jan 1 08:00 gpiochip96 -> ../../devices/platform/fec40000.gpio/gpio/gpiochip96
--w------- 1 root root 4096 Jan 1 08:00 unexport
root@ok3588:/sys/class/gpio#
root@ok3588:/sys/class/gpio#
root@ok3588:/sys/class/gpio# echo 128 > export
sh: echo: write error: Device or resource busy
root@ok3588:/sys/class/gpio#
root@ok3588:/sys/class/gpio# echo 64 > export
root@ok3588:/sys/class/gpio#
root@ok3588:/sys/class/gpio# ls -l
total 0
--w------- 1 root root 4096 Jan 1 08:06 export
lrwxrwxrwx 1 root root 0 Jan 1 08:06 gpio64 -> ../../devices/platform/fec30000.gpio/gpiochip2/gpio/gpio64
lrwxrwxrwx 1 root root 0 Jan 1 08:00 gpiochip0 -> ../../devices/platform/fd8a0000.gpio/gpio/gpiochip0
lrwxrwxrwx 1 root root 0 Jan 1 08:00 gpiochip128 -> ../../devices/platform/fec50000.gpio/gpio/gpiochip128
lrwxrwxrwx 1 root root 0 Jan 1 08:00 gpiochip32 -> ../../devices/platform/fec20000.gpio/gpio/gpiochip32
lrwxrwxrwx 1 root root 0 Jan 1 08:00 gpiochip509 -> ../../devices/platform/feb20000.spi/spi_master/spi2/spi2.0/rk806-pinctrl.1.auto/gpio/gpiochip509
lrwxrwxrwx 1 root root 0 Jan 1 08:00 gpiochip64 -> ../../devices/platform/fec30000.gpio/gpio/gpiochip64
lrwxrwxrwx 1 root root 0 Jan 1 08:00 gpiochip96 -> ../../devices/platform/fec40000.gpio/gpio/gpiochip96
--w------- 1 root root 4096 Jan 1 08:00 unexport
root@ok3588:/sys/class/gpio#
root@ok3588:/sys/class/gpio# cd gpio64/
root@ok3588:/sys/class/gpio/gpio64#
root@ok3588:/sys/class/gpio/gpio64# ls -l
total 0
-rw-r--r-- 1 root root 4096 Jan 1 08:06 active_low
lrwxrwxrwx 1 root root 0 Jan 1 08:06 device -> ../../../gpiochip2
-rw-r--r-- 1 root root 4096 Jan 1 08:06 direction
-rw-r--r-- 1 root root 4096 Jan 1 08:06 edge
drwxr-xr-x 2 root root 0 Jan 1 08:06 power
lrwxrwxrwx 1 root root 0 Jan 1 08:06 subsystem -> ../../../../../../class/gpio
-rw-r--r-- 1 root root 4096 Jan 1 08:06 uevent
-rw-r--r-- 1 root root 4096 Jan 1 08:06 value
root@ok3588:/sys/class/gpio/gpio64#
root@ok3588:/sys/class/gpio/gpio64# cat direction
in
root@ok3588:/sys/class/gpio/gpio64# cat value
1
root@ok3588:/sys/class/gpio/gpio64#
root@ok3588:/sys/class/gpio/gpio64# echo out > direction
root@ok3588:/sys/class/gpio/gpio64#
root@ok3588:/sys/class/gpio/gpio64# echo 1 > value
root@ok3588:/sys/class/gpio/gpio64#
root@ok3588:/sys/class/gpio/gpio64# cat direction
out
root@ok3588:/sys/class/gpio/gpio64#
root@ok3588:/sys/class/gpio/gpio64# cat value
1
root@ok3588:/sys/class/gpio/gpio64#
root@ok3588:/sys/class/gpio/gpio64# cd ..
root@ok3588:/sys/class/gpio#
root@ok3588:/sys/class/gpio# ls -l
total 0
--w------- 1 root root 4096 Jan 1 08:06 export
lrwxrwxrwx 1 root root 0 Jan 1 08:06 gpio64 -> ../../devices/platform/fec30000.gpio/gpiochip2/gpio/gpio64
lrwxrwxrwx 1 root root 0 Jan 1 08:00 gpiochip0 -> ../../devices/platform/fd8a0000.gpio/gpio/gpiochip0
lrwxrwxrwx 1 root root 0 Jan 1 08:00 gpiochip128 -> ../../devices/platform/fec50000.gpio/gpio/gpiochip128
lrwxrwxrwx 1 root root 0 Jan 1 08:00 gpiochip32 -> ../../devices/platform/fec20000.gpio/gpio/gpiochip32
lrwxrwxrwx 1 root root 0 Jan 1 08:00 gpiochip509 -> ../../devices/platform/feb20000.spi/spi_master/spi2/spi2.0/rk806-pinctrl.1.auto/gpio/gpiochip509
lrwxrwxrwx 1 root root 0 Jan 1 08:00 gpiochip64 -> ../../devices/platform/fec30000.gpio/gpio/gpiochip64
lrwxrwxrwx 1 root root 0 Jan 1 08:00 gpiochip96 -> ../../devices/platform/fec40000.gpio/gpio/gpiochip96
--w------- 1 root root 4096 Jan 1 08:00 unexport
root@ok3588:/sys/class/gpio#
root@ok3588:/sys/class/gpio#
root@ok3588:/sys/class/gpio#
root@ok3588:/sys/class/gpio# cd gpiochip64/
root@ok3588:/sys/class/gpio/gpiochip64# ls -l
total 0
-r--r--r-- 1 root root 4096 Jan 1 08:15 base
lrwxrwxrwx 1 root root 0 Jan 1 08:15 device -> ../../../fec30000.gpio
-r--r--r-- 1 root root 4096 Jan 1 08:15 label
-r--r--r-- 1 root root 4096 Jan 1 08:15 ngpio
drwxr-xr-x 2 root root 0 Jan 1 08:15 power
lrwxrwxrwx 1 root root 0 Jan 1 08:15 subsystem -> ../../../../../class/gpio
-rw-r--r-- 1 root root 4096 Jan 1 08:00 uevent
root@ok3588:/sys/class/gpio/gpiochip64# cat base
64
root@ok3588:/sys/class/gpio/gpiochip64#
root@ok3588:/sys/class/gpio/gpiochip64# cat device
cat: device: Is a directory
root@ok3588:/sys/class/gpio/gpiochip64#
root@ok3588:/sys/class/gpio/gpiochip64# cat label
gpio2
root@ok3588:/sys/class/gpio/gpiochip64#
root@ok3588:/sys/class/gpio/gpiochip64# cat ngpio
32
root@ok3588:/sys/class/gpio/gpiochip64#
root@ok3588:/sys/class/gpio/gpiochip64# cat power/
cat: power/: Is a directory
root@ok3588:/sys/class/gpio/gpiochip64#
root@ok3588:/sys/class/gpio/gpiochip64# cat subsystem
cat: subsystem: Is a directory
root@ok3588:/sys/class/gpio/gpiochip64#
root@ok3588:/sys/class/gpio/gpiochip64# cat uevent
root@ok3588:/sys/class/gpio/gpiochip64#
root@ok3588:/sys/class/gpio/gpiochip64#
参考资料:
百度:gpio64 gpiochip64
https://blog.csdn.net/weixin_46829095/article/details/129479648
GPIO应用
https://www.cnblogs.com/xingboy/p/16065582.html
Linux 操作GPIO--IMX6篇【转】
https://home.eeworld.com.cn/my/space-uid-361439-blogid-661069.html
MTK7688之openwrt下的GPIO操作