2022年12月31日 星期六

Linux Kernel(19.1)- /sys/class/gpio usage


要能使用/sys/class/gpio就要先開啟相關kernel config如下, 如提示所說, 該ABI已經棄用, 改由character device /dev/gpiochipN取代, 不過這裡還是會簡單交代一下相關資訊, 作為紀錄.
  │ CONFIG_GPIO_SYSFS:                                                                             │
  │                                                                                                │
  │ Say Y here to add the legacy sysfs interface for GPIOs.                                        │
  │                                                                                                │
  │ This ABI is deprecated. If you want to use GPIO from userspace,                                │
  │ use the character device /dev/gpiochipN with the appropriate                                   │
  │ ioctl() operations instead. The character device is always                                     │
  │ available.                                                                                     │
  │                                                                                                │
  │ Symbol: GPIO_SYSFS [=y]                                                                        │
  │ Type  : bool                                                                                   │
  │ Prompt: /sys/class/gpio/... (sysfs interface)                                                  │
  │   Location:                                                                                    │
  │     -> Device Drivers                                                                          │
  │       -> GPIO Support (GPIOLIB [=y])                                                           │
  │   Defined at drivers/gpio/Kconfig:61                                                           │
  │   Depends on: GPIOLIB [=y] && SYSFS [=y]                                                       │                                   

在設定玩kernel config之後, 重新編譯kernel, 就可以看見"/sys/class/gpio"目錄, 裡面幾個重要的檔案,
export Userspace may ask the kernel to export control of a GPIO to userspace by writing its number to this file.
unexport Reverses the effect of exporting to userspace.

"echo 19 > /sys/class/gpio/export" 後, 目錄/sys/class/gpio/gpio19/就會被創建出來, 裡面會包含
direction reads as either "in" or "out"
value reads as either 0 (low) or 1 (high)
edge reads as either "none", "rising", "falling", or "both"
active_low reads as either 0 (false) or 1 (true)

此外還可以透過debugfs來檢查當前的GPIO設定
/ # cat /sys/kernel/debug/gpio
gpiochip3: GPIOs 499-499, parent: platform/basic-mmio-gpio.3.auto, sys_flash:

gpiochip2: GPIOs 500-501, parent: platform/basic-mmio-gpio.2.auto, sys_mci:
 gpio-500 (                    |cd                  ) in  lo
 gpio-501 (                    |wp                  ) in  lo

gpiochip1: GPIOs 502-509, parent: platform/basic-mmio-gpio.1.auto, sys_led:
 gpio-502 (                    |?                   ) out lo
 gpio-503 (                    |?                   ) out lo
 gpio-504 (                    |?                   ) out lo
 gpio-505 (                    |?                   ) out lo
 gpio-506 (                    |?                   ) out hi
 gpio-507 (                    |?                   ) out lo
 gpio-508 (                    |?                   ) out lo
 gpio-509 (                    |?                   ) out lo

gpiochip0: GPIOs 510-511, parent: platform/10000000.sysreg, 10000000.sysreg:

/ # ls -al /sys/class/gpio/
total 0
drwxr-xr-x    2 0        0                0 Jan  3 00:40 .
drwxr-xr-x   36 0        0                0 Jan  3 00:40 ..
--w-------    1 0        0             4096 Jan  3 00:40 export
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip499 -> ../../devices/platform/10000000.sysreg/basic-mmio-gpio.3.auto/gpio/gpiochip499
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip500 -> ../../devices/platform/10000000.sysreg/basic-mmio-gpio.2.auto/gpio/gpiochip500
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip502 -> ../../devices/platform/10000000.sysreg/basic-mmio-gpio.1.auto/gpio/gpiochip502
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip510 -> ../../devices/platform/10000000.sysreg/gpio/gpiochip510
--w-------    1 0        0             4096 Jan  3 00:40 unexport

接下來export 510, 511, 然後在unexport 511, 設定510, 相關操作如下
/ # echo 510 > /sys/class/gpio/export
/ # echo 511 > /sys/class/gpio/export
/ # ls -al /sys/class/gpio/
total 0
drwxr-xr-x    2 0        0                0 Jan  3 00:40 .
drwxr-xr-x   36 0        0                0 Jan  3 00:40 ..
--w-------    1 0        0             4096 Jan  3 00:43 export
lrwxrwxrwx    1 0        0                0 Jan  3 00:42 gpio510 -> ../../devices/platform/10000000.sysreg/gpiochip0/gpio/gpio510
lrwxrwxrwx    1 0        0                0 Jan  3 00:43 gpio511 -> ../../devices/platform/10000000.sysreg/gpiochip0/gpio/gpio511
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip499 -> ../../devices/platform/10000000.sysreg/basic-mmio-gpio.3.auto/gpio/gpiochip499
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip500 -> ../../devices/platform/10000000.sysreg/basic-mmio-gpio.2.auto/gpio/gpiochip500
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip502 -> ../../devices/platform/10000000.sysreg/basic-mmio-gpio.1.auto/gpio/gpiochip502
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip510 -> ../../devices/platform/10000000.sysreg/gpio/gpiochip510
--w-------    1 0        0             4096 Jan  3 00:40 unexport

/ # echo 511 > /sys/class/gpio/unexport
/ # ls -al /sys/class/gpio/
total 0
drwxr-xr-x    2 0        0                0 Jan  3 00:40 .
drwxr-xr-x   36 0        0                0 Jan  3 00:40 ..
--w-------    1 0        0             4096 Jan  3 00:43 export
lrwxrwxrwx    1 0        0                0 Jan  3 00:42 gpio510 -> ../../devices/platform/10000000.sysreg/gpiochip0/gpio/gpio510
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip499 -> ../../devices/platform/10000000.sysreg/basic-mmio-gpio.3.auto/gpio/gpiochip499
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip500 -> ../../devices/platform/10000000.sysreg/basic-mmio-gpio.2.auto/gpio/gpiochip500
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip502 -> ../../devices/platform/10000000.sysreg/basic-mmio-gpio.1.auto/gpio/gpiochip502
lrwxrwxrwx    1 0        0                0 Jan  3 00:40 gpiochip510 -> ../../devices/platform/10000000.sysreg/gpio/gpiochip510
--w-------    1 0        0             4096 Jan  3 00:43 unexport

/ # grep "" /sys/class/gpio/gpio510/*
/sys/class/gpio/gpio510/active_low:0
/sys/class/gpio/gpio510/direction:in
/sys/class/gpio/gpio510/value:0

/ # echo out > /sys/class/gpio/gpio510/direction
/ # grep "" /sys/class/gpio/gpio510/*
/sys/class/gpio/gpio510/active_low:0
/sys/class/gpio/gpio510/direction:out
/sys/class/gpio/gpio510/value:0

/ # cat /sys/kernel/debug/gpio
gpiochip3: GPIOs 499-499, parent: platform/basic-mmio-gpio.3.auto, sys_flash:

gpiochip2: GPIOs 500-501, parent: platform/basic-mmio-gpio.2.auto, sys_mci:
 gpio-500 (                    |cd                  ) in  lo
 gpio-501 (                    |wp                  ) in  lo

gpiochip1: GPIOs 502-509, parent: platform/basic-mmio-gpio.1.auto, sys_led:
 gpio-502 (                    |?                   ) out lo
 gpio-503 (                    |?                   ) out lo
 gpio-504 (                    |?                   ) out lo
 gpio-505 (                    |?                   ) out lo
 gpio-506 (                    |?                   ) out hi
 gpio-507 (                    |?                   ) out hi
 gpio-508 (                    |?                   ) out lo
 gpio-509 (                    |?                   ) out lo

gpiochip0: GPIOs 510-511, parent: platform/10000000.sysreg, 10000000.sysreg:
 gpio-510 (                    |sysfs               ) out lo

/ # grep "" /sys/class/gpio/gpiochip*/*
/sys/class/gpio/gpiochip499/base:499
/sys/class/gpio/gpiochip499/label:sys_flash
/sys/class/gpio/gpiochip499/ngpio:1
/sys/class/gpio/gpiochip500/base:500
/sys/class/gpio/gpiochip500/label:sys_mci
/sys/class/gpio/gpiochip500/ngpio:2
/sys/class/gpio/gpiochip502/base:502
/sys/class/gpio/gpiochip502/label:sys_led
/sys/class/gpio/gpiochip502/ngpio:8
/sys/class/gpio/gpiochip510/base:510
/sys/class/gpio/gpiochip510/label:10000000.sysreg
/sys/class/gpio/gpiochip510/ngpio:2



沒有留言:

張貼留言

熱門文章