diff --git a/Software/Development/OperatingSystem/Linux/Subsystem/GPIO/用户态_GPIO_操作指南.md b/Software/Development/OperatingSystem/Linux/Subsystem/GPIO/用户态_GPIO_操作指南.md new file mode 100644 index 0000000..2725fc9 --- /dev/null +++ b/Software/Development/OperatingSystem/Linux/Subsystem/GPIO/用户态_GPIO_操作指南.md @@ -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 +```