GPIO Access Usage

This document describes how to access GPIO under Ubuntu.

Note
  1. This document only supports the 4.9 kernel
  2. All operations on GPIO are performed under root privileges, switch to root:
1
2
3
khadas@Khadas:~$ su
Password:
root@Khadas:/home/khadas#

Get GPIO value

Calculation method

The calculation method of the GPIO array is: Number = Banks + Pins

  1. Banks refers to the base value of GPIO Ranges
  2. Pins refers 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. There is a calculation example for each Ranges here for reference.

AOBUS

  1. Get Banks
1
2
3
root@Khadas:/home/khadas# cat /sys/kernel/debug/pinctrl/pinctrl@14/gpio-ranges
GPIO ranges handled:
0: aobus-banks GPIOS [501 - 511] PINS [0 - 10]

AOBUS’ Banks is 496

  1. Get Pins
1
2
3
4
5
6
7
8
9
10
11
12
13
root@Khadas:/home/khadas# cat /sys/kernel/debug/pinctrl/pinctrl@14/pins 
registered pins: 11
pin 0 (GPIOAO_0) pinctrl@14
pin 1 (GPIOAO_1) pinctrl@14
pin 2 (GPIOAO_2) pinctrl@14
pin 3 (GPIOAO_3) pinctrl@14
pin 4 (GPIOAO_4) pinctrl@14
pin 5 (GPIOAO_5) pinctrl@14
pin 6 (GPIOAO_6) pinctrl@14
pin 7 (GPIOAO_7) pinctrl@14
pin 8 (GPIOAO_8) pinctrl@14
pin 9 (GPIOAO_9) pinctrl@14
pin 10 (GPIO_TEST_N) pinctrl@14

The pin in front of each GPIO represents the corresponding pins

Periphs

  1. Get Banks
1
2
3
root@Khadas:/home/khadas# cat /sys/kernel/debug/pinctrl/pinctrl@4b0/gpio-ranges 
GPIO ranges handled:
0: periphs-banks GPIOS [401 - 500] PINS [0 - 99]

Periphs’ Banks is 410

  1. Get Pins
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
root@Khadas:/home/khadas# cat /sys/kernel/debug/pinctrl/pinctrl@4b0/pins
registered pins: 100
pin 0 (GPIOZ_0) pinctrl@4b0
pin 1 (GPIOZ_1) pinctrl@4b0
pin 2 (GPIOZ_2) pinctrl@4b0
pin 3 (GPIOZ_3) pinctrl@4b0
pin 4 (GPIOZ_4) pinctrl@4b0
pin 5 (GPIOZ_5) pinctrl@4b0
pin 6 (GPIOZ_6) pinctrl@4b0
pin 7 (GPIOZ_7) pinctrl@4b0
pin 8 (GPIOZ_8) pinctrl@4b0
pin 9 (GPIOZ_9) pinctrl@4b0
pin 93 (GPIOX_14) pinctrl@4b0
pin 94 (GPIOX_15) pinctrl@4b0
pin 95 (GPIOX_16) pinctrl@4b0
pin 96 (GPIOX_17) pinctrl@4b0
pin 97 (GPIOX_18) pinctrl@4b0
pin 98 (GPIOCLK_0) pinctrl@4b0
pin 99 (GPIOCLK_1) pinctrl@4b0
  1. Calculate Number

Take GPIOX_10 as an example here,

GPIOX_14 = Banks + Pins = 401 + 89 = 490

AOBUS

  1. Get Banks
1
2
3
root@Khadas:/home/khadas# cat /sys/kernel/debug/pinctrl/pinctrl@14/gpio-ranges
GPIO ranges handled:
0: aobus-banks GPIOS [501 - 511] PINS [0 - 10]

AOBUS’ Banks is 496

  1. Get Pins
1
2
3
4
5
6
7
8
9
10
11
12
13
root@Khadas:/home/khadas# cat /sys/kernel/debug/pinctrl/pinctrl@14/pins 
registered pins: 11
pin 0 (GPIOAO_0) pinctrl@14
pin 1 (GPIOAO_1) pinctrl@14
pin 2 (GPIOAO_2) pinctrl@14
pin 3 (GPIOAO_3) pinctrl@14
pin 4 (GPIOAO_4) pinctrl@14
pin 5 (GPIOAO_5) pinctrl@14
pin 6 (GPIOAO_6) pinctrl@14
pin 7 (GPIOAO_7) pinctrl@14
pin 8 (GPIOAO_8) pinctrl@14
pin 9 (GPIOAO_9) pinctrl@14
pin 10 (GPIO_TEST_N) pinctrl@14

The pin in front of each GPIO represents the corresponding pins

Periphs

  1. Get Banks
1
2
3
root@Khadas:/home/khadas# cat /sys/kernel/debug/pinctrl/pinctrl@4b0/gpio-ranges 
GPIO ranges handled:
0: periphs-banks GPIOS [401 - 500] PINS [0 - 99]

Periphs的’ Banks is 410

  1. Get Pins
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
root@Khadas:/home/khadas# cat /sys/kernel/debug/pinctrl/pinctrl@4b0/pins
registered pins: 100
pin 0 (GPIOZ_0) pinctrl@4b0
pin 1 (GPIOZ_1) pinctrl@4b0
pin 2 (GPIOZ_2) pinctrl@4b0
pin 3 (GPIOZ_3) pinctrl@4b0
pin 4 (GPIOZ_4) pinctrl@4b0
pin 5 (GPIOZ_5) pinctrl@4b0
pin 6 (GPIOZ_6) pinctrl@4b0
pin 7 (GPIOZ_7) pinctrl@4b0
pin 8 (GPIOZ_8) pinctrl@4b0
pin 9 (GPIOZ_9) pinctrl@4b0
pin 93 (GPIOX_14) pinctrl@4b0
pin 94 (GPIOX_15) pinctrl@4b0
pin 95 (GPIOX_16) pinctrl@4b0
pin 96 (GPIOX_17) pinctrl@4b0
pin 97 (GPIOX_18) pinctrl@4b0
pin 98 (GPIOCLK_0) pinctrl@4b0
pin 99 (GPIOCLK_1) pinctrl@4b0
  1. Calculate Number

Take GPIOX_10 as an example here,

GPIOX_14 = Banks + Pins = 401 + 89 = 490

AOBUS

  1. Get Banks
1
2
3
root@Khadas:/home/khadas# cat /sys/kernel/debug/pinctrl/pinctrl@ff800014/gpio-ranges
GPIO ranges handled:
0: aobus-banks GPIOS [496 - 511] PINS [0 - 15]

AOBUS’ Banks is 496

  1. Get Pins
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
root@Khadas:/home/khadas# cat /sys/kernel/debug/pinctrl/pinctrl@ff800014/pins
registered pins: 16
pin 0 (GPIOAO_0) pinctrl@ff800014
pin 1 (GPIOAO_1) pinctrl@ff800014
pin 2 (GPIOAO_2) pinctrl@ff800014
pin 3 (GPIOAO_3) pinctrl@ff800014
pin 4 (GPIOAO_4) pinctrl@ff800014
pin 5 (GPIOAO_5) pinctrl@ff800014
pin 6 (GPIOAO_6) pinctrl@ff800014
pin 7 (GPIOAO_7) pinctrl@ff800014
pin 8 (GPIOAO_8) pinctrl@ff800014
pin 9 (GPIOAO_9) pinctrl@ff800014
pin 10 (GPIOAO_10) pinctrl@ff800014
pin 11 (GPIOAO_11) pinctrl@ff800014
pin 12 (GPIOE_0) pinctrl@ff800014
pin 13 (GPIOE_1) pinctrl@ff800014
pin 14 (GPIOE_2) pinctrl@ff800014
pin 15 (GPIO_TEST_N) pinctrl@ff800014

The pin in front of each GPIO represents the corresponding Pins

Periphs

  1. Get Banks
1
2
3
root@Khadas:/home/khadas# cat /sys/kernel/debug/pinctrl/pinctrl@ff634480/gpio-ranges
GPIO ranges handled:
0: periphs-banks GPIOS [410 - 495] PINS [0 - 85]

Periphs’ Banks is 410

  1. Get Pins
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
root@Khadas:/home/khadas# cat /sys/kernel/debug/pinctrl/pinctrl@ff634480/pins
registered pins: 86
pin 0 (GPIOV_0) pinctrl@ff634480
pin 1 (GPIOZ_0) pinctrl@ff634480
pin 2 (GPIOZ_1) pinctrl@ff634480
pin 3 (GPIOZ_2) pinctrl@ff634480
pin 4 (GPIOZ_3) pinctrl@ff634480
pin 5 (GPIOZ_4) pinctrl@ff634480
pin 6 (GPIOZ_5) pinctrl@ff634480
pin 7 (GPIOZ_6) pinctrl@ff634480
pin 8 (GPIOZ_7) pinctrl@ff634480
pin 9 (GPIOZ_8) pinctrl@ff634480
...
pin 76 (GPIOX_10) pinctrl@ff634480
pin 77 (GPIOX_11) pinctrl@ff634480dd
pin 78 (GPIOX_12) pinctrl@ff634480
pin 79 (GPIOX_13) pinctrl@ff634480
pin 80 (GPIOX_14) pinctrl@ff634480
pin 81 (GPIOX_15) pinctrl@ff634480
pin 82 (GPIOX_16) pinctrl@ff634480
pin 83 (GPIOX_17) pinctrl@ff634480
pin 84 (GPIOX_18) pinctrl@ff634480
pin 85 (GPIOX_19) pinctrl@ff634480
  1. Calculate Number

Take GPIOX_10 as an example here,

GPIOX_10 = Banks + Pins = 410 + 76 = 486

GPIO usage examples

Here use GPIODV24 to read the pin output value of GPIODV25, and use Dupont line to connect the physical pins 22 and 23 together.

  1. Set GPIODV24 and GPIODV25 as ordinary pins (multiplexed as i2c by default)

edit /boot/env.txt,

1
root@Khadas:/home/khadas# vim /boot/env.txt

Remove i2c3 from overlays,

1
overlays=uart4 pwm_ao_a pwm_f i2c0 i2s watchdog --> overlays=uart4 pwm_ao_a pwm_f i2s watchdog

Restart for the configuration to take effect.

  1. Calculate the GPIO value:

GPIODV_24 = 401 + 72 = 473
GPIODV_25 = 401 + 73 = 474

  1. Set GPIODV_24 to read mode
  • Export GPIO
1
root@Khadas:/home/khadas# echo 473 > /sys/class/gpio/export
  • Set to read mode
1
root@Khadas:/home/khadas# echo in > /sys/class/gpio/gpio473/direction
  1. Set GPIODV_25 to write mode
  • Export GPIO
1
root@Khadas:/home/khadas# echo 474 > /sys/class/gpio/export
  • Set to write mode
1
root@Khadas:/home/khadas# echo out > /sys/class/gpio/gpio474/direction
  1. Test
  • Set GPIODV_25 to output high level and read it with GPIODV_24
1
2
3
root@Khadas:/home/khadas# echo 1 > /sys/class/gpio/gpio474/value
root@Khadas:/home/khadas# cat /sys/class/gpio/gpio473/value
1
  • Set GPIODV_25 to low level and read with GPIODV_24
1
2
3
root@Khadas:/home/khadas# echo 0 > /sys/class/gpio/gpio474/value
root@Khadas:/home/khadas# cat /sys/class/gpio/gpio473/value
0

Here use GPIODV24 to read the pin output value of GPIODV25, and use Dupont line to connect the physical pins 22 and 23 together.

  1. Set GPIODV24 and GPIODV25 as ordinary pins (multiplexed as i2c by default)

edit /boot/env.txt,

1
root@Khadas:/home/khadas# vim /boot/env.txt

Remove i2c3 from overlays,

1
overlays=uart4 pwm_ao_a pwm_f i2c0 i2s watchdog --> overlays=uart4 pwm_ao_a pwm_f i2s watchdog

Restart for the configuration to take effect.

  1. Calculate the GPIO value:

GPIODV_24 = 401 + 72 = 473
GPIODV_25 = 401 + 73 = 474

  1. Set GPIODV_24 to read mode
  • Export GPIO
1
root@Khadas:/home/khadas# echo 473 > /sys/class/gpio/export
  • Set to read mode
1
root@Khadas:/home/khadas# echo in > /sys/class/gpio/gpio473/direction
  1. Set GPIODV_25 to write mode
  • Export GPIO
1
root@Khadas:/home/khadas# echo 474 > /sys/class/gpio/export
  • Set to write mode
1
root@Khadas:/home/khadas# echo out > /sys/class/gpio/gpio474/direction
  1. Test
  • Set GPIODV_25 to output high level and read it with GPIODV_24
1
2
3
root@Khadas:/home/khadas# echo 1 > /sys/class/gpio/gpio474/value
root@Khadas:/home/khadas# cat /sys/class/gpio/gpio473/value
1
  • Set GPIODV_25 to low level and read with GPIODV_24
1
2
3
root@Khadas:/home/khadas# echo 0 > /sys/class/gpio/gpio474/value
root@Khadas:/home/khadas# cat /sys/class/gpio/gpio473/value
0

Here use GPIODV24 to read the pin output value of GPIODV25, and use Dupont line to connect the physical pins 22 and 23 together.

  1. Set GPIODV24 and GPIODV25 as ordinary pins (multiplexed as i2c by default)
    edit /boot/env.txt,
1
root@Khadas:/home/khadas# vim /boot/env.txt

Remove i2c3 from overlays,

1
overlays=uart3 pwm_f i2c3 i2s os08a10 watchdog --> overlays=uart3 pwm_f i2s os08a10 watchdog

Restart for the configuration to take effect.

  1. Calculate the GPIO value:

GPIOA_14 = 410 + 65 = 474
GPIOA_15 = 410 + 65 = 475

  1. Set GPIOA_14 to read mode
  • Export GPIO
1
root@Khadas:/home/khadas# echo 474 > /sys/class/gpio/export
  • Set to read mode
1
root@Khadas:/home/khadas# echo in > /sys/class/gpio/gpio474/direction
  1. Set GPIOA_15 to write mode
  • Export GPIO
1
root@Khadas:/home/khadas# echo 475 > /sys/class/gpio/export
  • Set to write mode
1
root@Khadas:/home/khadas# echo out > /sys/class/gpio/gpio475/direction
  1. Test
  • Set GPIOA_15 to output high level and read it with GPIOA_14
1
2
3
root@Khadas:/home/khadas# echo 1 >  /sys/class/gpio/gpio475/value
root@Khadas:/home/khadas# cat /sys/class/gpio/gpio474/value
1
  • Set GPIOA_15 to output low level and read it with GPIOA_14
1
2
3
root@Khadas:/home/khadas# echo 0 >  /sys/class/gpio/gpio475/value
root@Khadas:/home/khadas# cat /sys/class/gpio/gpio474/value
0