增加 Linux 用户态 GPIO 操作指南.

Signed-off-by: ithink.chan <chenyang@autoai.com>
This commit is contained in:
ithink.chan 2020-04-14 13:19:14 +08:00
parent bbf29d7051
commit 1733ab1c4a
1 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,52 @@
# 用户态 GPIO 操作指南
## 获取 GPIO 范围
### 通过 kernel debug 获取
```sh
cd /sys/kernel/debug
cat gpio
# 输出信息
gpiochip8: GPIOs 348-355, parent: i2c/4-0020, pca9654, can sleep:
gpiochip7: GPIOs 356-359, parent: platform/e6055800.gpio, e6055800.gpio:
gpiochip6: GPIOs 360-391, parent: platform/e6055400.gpio, e6055400.gpio:
gpiochip5: GPIOs 392-417, parent: platform/e6055000.gpio, e6055000.gpio:
gpiochip4: GPIOs 418-435, parent: platform/e6054000.gpio, e6054000.gpio:
gpiochip3: GPIOs 436-451, parent: platform/e6053000.gpio, e6053000.gpio:
gpiochip2: GPIOs 452-466, parent: platform/e6052000.gpio, e6052000.gpio:
gpiochip1: GPIOs 467-495, parent: platform/e6051000.gpio, e6051000.gpio:
gpiochip0: GPIOs 496-511, parent: platform/e6050000.gpio, e6050000.gpio:
```
以上输出信息中的 496-511 代表 gpiochip0第一组 GPIO每组 GPIO 为 0-31的范围为496-511。举例说明 GPIO Port5 Pin23 为 392+23=415。
## 在 Shell 中使用 GPIO
```sh
cd /sys/class/gpio/
# 导出指定编号的引脚,作为 GPIO 使用
echo 167 > export
cd gpio167
# 设置为输出模式
echo “out” > direction
# 输出高电平
echo 1 > value
# 输出低电平
echo 0 > value
# 设置为输入模式
echo “in” > direction
# 非中断引脚
echo “none” > edge
# 读取引脚值
cat value
# 上升沿触发中断
echo “rising” > edge
# 下降沿触发中断
echo “falling” > edge
# 边沿触发中断
echo “both” > edge
cd ..
# 删除导出的 GPIO
echo 167 > unexport
```