Access GPIO pins from the Ubuntu Terminal.
Note
- This document only for Amlogic 4.9 kernel on VIM1/VIM2/VIM3 and Amlogic 5.4 kernel on VIM4.
Get GPIO Number
Calculation Method
The calculation method of the GPIO array is: Number = Range Base + Pin Index.
Range Baserefers to the base value of GPIO ranges.Pin Indexrefers to the sorting of the GPIO pins you need to calculate in the corresponding ranges.
Numerical Calculation Example
Amlogic chips usually include two GPIO ranges, AOBUS and Periphs. Examples for each range is provided here for reference.
AOBUS
- Get
Range Base:
1 | $ cat /sys/kernel/debug/pinctrl/pinctrl@14/gpio-ranges |
AOBUS’ Range Base is 501.
- Get
Pin Index:
1 | $ cat /sys/kernel/debug/pinctrl/pinctrl@14/pins |
The pin in front of each GPIO represents the corresponding Pin Index.
Periphs
- Get
Range Base:
1 | $ cat /sys/kernel/debug/pinctrl/pinctrl@4b0/gpio-ranges |
Periphs’ Range Base is 401.
- Get
Pin Index:
1 | $ cat /sys/kernel/debug/pinctrl/pinctrl@4b0/pins |
- Calculate Number
Take GPIOX_14 as an example here,
GPIOX_14 = Range Base + Pin Index = 401 + 93 = 494.
AOBUS
- Get
Range Base:
1 | $ cat /sys/kernel/debug/pinctrl/pinctrl@14/gpio-ranges |
AOBUS’ Range Base is 501.
- Get
Pin Index:
1 | $ cat /sys/kernel/debug/pinctrl/pinctrl@14/pins |
The pin in front of each GPIO represents the corresponding Pin Index.
Periphs
- Get
Range Base:
1 | $ cat /sys/kernel/debug/pinctrl/pinctrl@4b0/gpio-ranges |
Periphs的Range Base is 401.
- Get
Pin Index:
1 | $ cat /sys/kernel/debug/pinctrl/pinctrl@4b0/pins |
- Calculate Number
Take GPIOX_14 as an example here,
GPIOX_14 = Range Base + Pin Index = 401 + 93 = 494
AOBUS
- Get
Range Base:
1 | $ cat /sys/kernel/debug/pinctrl/pinctrl@ff800014/gpio-ranges |
AOBUS’ Range Base is 496.
- Get
Pin Index:
1 | $ cat /sys/kernel/debug/pinctrl/pinctrl@ff800014/pins |
The pin in front of each GPIO represents the corresponding Pin Index.
Periphs
- Get
Range Base:
1 | $ cat /sys/kernel/debug/pinctrl/pinctrl@ff634480/gpio-ranges |
Periphs’ Range Base is 410.
- Get
Pin Index:
1 | $ cat /sys/kernel/debug/pinctrl/pinctrl@ff634480/pins |
- Calculate Number
Take GPIOX_10 as an example here,
GPIOX_10 = Range Base + Pin Index = 410 + 76 = 486.
- Get
Range Base:
1 | $ cat /sys/kernel/debug/pinctrl/fe000000.apb4\:pinctrl\@4000-pinctrl-meson/gpio-ranges |
- Get
Pin Index:
1 | $ cat /sys/kernel/debug/pinctrl/fe000000.apb4\:pinctrl\@4000-pinctrl-meson/pins |
- Calculate Number
Take GPIOT_19 as an examples here.
GPIOT_19 = Range Base + Pin Index = 355 + 110 = 465.
GPIO Usage
When you get the GPIO number, you can follow the steps below to control it. Here will take GPIO number 465 as a example.
- Export GPIO
1 | $ echo 465 | sudo tee /sys/class/gpio/export |
- Set GPIO direction
You can set the direction input or output.
1 | $ echo out | sudo tee /sys/class/gpio/gpio465/direction # Set GPIO output |
- Set or get GPIO value
1 | $ echo 1 | sudo tee /sys/class/gpio/gpio465/value # Set GPIO output high |
- Unexport GPIO
1 | $ echo 465 | sudo tee /sys/class/gpio/unexport |
Unexport to release the GPIO.