GPIO使用文档

这篇文档介绍如何在Ubuntu下访问GPIO。

注意
  1. 此文档仅支持4.9内核
  2. 所有对GPIO的操作都是在root权限下进行的,切换到root:
1
2
3
khadas@Khadas:~$ su
Password:
root@Khadas:/home/khadas#

获取GPIO数值

计算方法

GPIO数组的计算方法为:Number = Banks + Pins

  1. Banks是指GPIO Ranges的基数值
  2. Pins是指你需要计算的GPIO引脚在对应ranges的排序

数值计算示例

Amlogic芯片通常都会包括AOBUS和Periphs两个GPIO Ranges。这里对每个Ranges都有一个计算的示例供参考。

AOBUS

  1. 获取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就为496

  1. 获取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

每个GPIO前面的pin代表的就是对应的Pins

Periphs

  1. 获取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就是410

  1. 获取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. 计算Number

这里以GPIOX_10为例,

GPIOX_14 = Banks + Pins = 401 + 89 = 490

AOBUS

  1. 获取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就为496

  1. 获取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

每个GPIO前面的pin代表的就是对应的Pins

Periphs

  1. 获取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就是410

  1. 获取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. 计算Number

这里以GPIOX_10为例,

GPIOX_14 = Banks + Pins = 401 + 89 = 490

AOBUS

  1. 获取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就为496

  1. 获取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

每个GPIO前面的pin代表的就是对应的Pins

Periphs

  1. 获取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就是410

  1. 获取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. 计算Number

这里以GPIOX_10为例,

GPIOX_10 = Banks + Pins = 410 + 76 = 486

GPIO使用示例

这里使用GPIODV24读取GPIODV25的引脚输出值,使用杜邦线将物理引脚的22和23连接在一起。

  1. 将GPIODV24和GPIODV25设置为普通引脚(默认复用为i2c)

修改/boot/env.txt,

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

从overlays中移除i2c3,

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

重启,让配置生效。

  1. 计算GPIO数值:

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

  1. 设置GPIODV_24为读模式
  • 申请GPIO
1
root@Khadas:/home/khadas# echo 473 > /sys/class/gpio/export
  • 设置成读模式
1
root@Khadas:/home/khadas# echo in > /sys/class/gpio/gpio473/direction
  1. 设置GPIODV_25为写模式
  • 申请GPIO
1
root@Khadas:/home/khadas# echo 474 > /sys/class/gpio/export
  • 设置成写模式
1
root@Khadas:/home/khadas# echo out > /sys/class/gpio/gpio474/direction
  1. 测试
  • 设置GPIODV_25输出高电平并用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
  • 设置GPIODV_25为低电平并用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

这里使用GPIODV24读取GPIODV25的引脚输出值,使用杜邦线将物理引脚的22和23连接在一起。

  1. 将GPIODV24和GPIODV25设置为普通引脚(默认复用为i2c)

修改/boot/env.txt,

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

从overlays中移除i2c3,

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

重启,让配置生效。

  1. 计算GPIO数值:

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

  1. 设置GPIODV_24为读模式
  • 申请GPIO
1
root@Khadas:/home/khadas# echo 473 > /sys/class/gpio/export
  • 设置成读模式
1
root@Khadas:/home/khadas# echo in > /sys/class/gpio/gpio473/direction
  1. 设置GPIODV_25为写模式
  • 申请GPIO
1
root@Khadas:/home/khadas# echo 474 > /sys/class/gpio/export
  • 设置成写模式
1
root@Khadas:/home/khadas# echo out > /sys/class/gpio/gpio474/direction
  1. 测试
  • 设置GPIODV_25输出高电平并用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
  • 设置GPIODV_25为低电平并用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

这里使用GPIOA14读取GPIOA15的引脚输出值,使用杜邦线将物理引脚的22和23连接在一起。

  1. 将GPIOA14和GPIOA15设置为普通引脚(默认复用为i2c)

修改/boot/env.txt,

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

从overlays中移除i2c3,

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

重启,让配置生效。

  1. 计算GPIO数值:

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

  1. 设置GPIOA_14为读模式
  • 申请GPIO
1
root@Khadas:/home/khadas# echo 474 > /sys/class/gpio/export
  • 设置成读模式
1
root@Khadas:/home/khadas# echo in > /sys/class/gpio/gpio474/direction
  1. 设置GPIOA_15为写模式
  • 申请GPIO
1
root@Khadas:/home/khadas# echo 475 > /sys/class/gpio/export
  • 设置成写模式
1
root@Khadas:/home/khadas# echo out > /sys/class/gpio/gpio475/direction
  1. 测试
  • 设置GPIOA_15输出高电平并用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
  • 设置GPIOA_15为低电平并用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