Linux-下如何操作GPIO


原文链接: Linux-下如何操作GPIO

查看所有可操作的GPIO
cat /sys/kernel/debug/gpio

可以使用系统中的GPIOLIB模块在用户空间提供的sysfs接口,实现应用层对GPIO的独立控制。

使用 Linux Kernel 提供的 sysfs 來控制 GPIO

在要寫程式之前,我們先來使用 Linux Kernel 提供的 sysfs 來控制 GPIO。


用户空间gpio的调用

用户空间访问gpio,即通过sysfs接口访问gpio,下面是/sys/class/gpio目录下的文件:

--export/unexport文件
--gpioN指代具体的gpio引脚
--gpio_chipN指代gpio控制器
// 必须知道以上接口没有标准device文件和它们的链接。 

(1) export/unexport文件接口:

/sys/class/gpio/export,该接口只能写不能读
// 用户程序通过写入gpio的编号来向内核申请将某个gpio的控制权导出到用户空间

比如  echo 19 > export 
// 上述操作会为19号gpio创建一个节点gpio19,此时目录下生成gpio19的目录
// 和(unexport)导出的效果相反。 

比如 echo 19 > unexport
// 上述操作将会移除gpio19这个节点。 

(2) /sys/class/gpio/gpioN

// 指代某个具体的gpio端口,里边有如下属性文件
direction 表示gpio端口的方向,读取结果是in或out。
// 该文件也可以写,写入out 时该gpio设为输出同时电平默认为低。
// 写入low或high则不仅可以设置为输出 还可以设置输出的电平。 
// 当然如果内核不支持或者内核代码不愿意,将不会存在这个属性
// 比如内核调用了gpio_export(N,0)就表示内核不愿意修改gpio端口方向属性 

value 表示gpio引脚的电平,0(低电平)1(高电平)
// 如果gpio被配置为输出,这个值是可写的,记住任何非零的值都将输出高电平,
// 如果某个引脚能并且已经被配置为中断,则可以调用poll(2)函数监听该中断,中断触发后poll(2)函数就会返回。

edge 表示中断的触发方式,edge文件有如下四个值:"none", "rising", "falling","both"。
// none表示引脚为输入,不是中断引脚
// rising表示引脚为中断输入,上升沿触发
// falling表示引脚为中断输入,下降沿触发
// both表示引脚为中断输入,边沿触发
// 这个文件节点只有在引脚被配置为输入引脚的时候才存在。 当值是none时可以通过如下方法将变为中断引脚
// echo "both" > edge;对于是both,falling还是rising依赖具体硬件的中断的触发方式。
// 此方法即用户态gpio转换为中断引脚的方式

active_low 
// 相互调换高低电平设置

(3) /sys/class/gpio/gpiochipN

gpiochipN表示的就是一个gpio_chip,用来管理和控制一组gpio端口的控制器: 
// base   和N相同,表示控制器管理的最小的端口编号。 
// lable   诊断使用的标志(并不总是唯一的) 
// ngpio  表示控制器管理的gpio端口数量(端口范围是:N ~ N+ngpio-1) 

1. 首先先將 GPIO4 設定成可以用 sysfs 控制
echo 4 > /sys/class/gpio/export

2. 設定 GPIO4 為輸出腳
echo out > /sys/class/gpio/gpio4/direction

3. 設定 GPIO4 輸出值為 1 (0: 低電位, 1: 高電位)
echo 1 > /sys/class/gpio/gpio4/value

4. 設定 GPIO4 輸出值為 0 (0: 低電位, 1: 高電位)
echo 0 > /sys/class/gpio/gpio4/value

5. 取消建立出來的 GPIO4 node
echo 4 > /sys/class/gpio/unexport

在你執行以上第 3 步的時候,你可以看到 LED 亮了起來,直到第 4 步時,才又變回原本的狀態。

若想要使用 Bash 來控制 GPIO,則可以採用此種方式。


使用 debugfs 來觀看目前的 GPIO 設定

我們可以使用 debugfs 來察看目前的 GPIO 設定,首先掛載 debugfs

mount -t debugfs debug /d

接著就可以使用

cat /d/gpio

來取得目前 GPIO 的狀況

root@raspberrypi:/home/pi#
cat /d/gpio
GPIOs 0-53, bcm2708_gpio:
gpio-4  (sysfs                ) out hi
#!/bin/bash

echo "INITGPIO" >> /root/startup.log

# i2c-6
echo "Init I2C-6"
if [[ ! -e /sys/class/gpio/gpio27 ]]; then
   echo 27 > /sys/class/gpio/export
fi
echo "mode1" > /sys/kernel/debug/gpio_debug/gpio27/current_pinmux
if [[ ! -e /sys/class/gpio/gpio28 ]]; then
   echo 28 > /sys/class/gpio/export
fi
echo "mode1" > /sys/kernel/debug/gpio_debug/gpio28/current_pinmux
echo "  GPIO27 pinmux: $(cat /sys/kernel/debug/gpio_debug/gpio27/current_pinmux)"
echo "  GPIO28 pinmux: $(cat /sys/kernel/debug/gpio_debug/gpio28/current_pinmux)"


# i2c-1
echo "Init I2C-1"
if [[ ! -e /sys/class/gpio/gpio19 ]]; then
   echo 19 > /sys/class/gpio/export
fi
echo "mode1" > /sys/kernel/debug/gpio_debug/gpio19/current_pinmux
if [[ ! -e /sys/class/gpio/gpio20 ]]; then
   echo 20 > /sys/class/gpio/export
fi
echo "mode1" > /sys/kernel/debug/gpio_debug/gpio20/current_pinmux
echo "  GPIO19 pinmux: $(cat /sys/kernel/debug/gpio_debug/gpio19/current_pinmux)"
echo "  GPIO20 pinmux: $(cat /sys/kernel/debug/gpio_debug/gpio20/current_pinmux)"

# UART-1
echo "Init UART-1"
if [[ ! -e /sys/class/gpio/gpio128 ]]; then
   echo 128 > /sys/class/gpio/export
fi
echo "mode1" > /sys/kernel/debug/gpio_debug/gpio128/current_pinmux
if [[ ! -e /sys/class/gpio/gpio129 ]]; then
   echo 129 > /sys/class/gpio/export
fi
echo "mode1" > /sys/kernel/debug/gpio_debug/gpio129/current_pinmux
if [[ ! -e /sys/class/gpio/gpio130 ]]; then
   echo 130 > /sys/class/gpio/export
fi
echo "mode1" > /sys/kernel/debug/gpio_debug/gpio130/current_pinmux
if [[ ! -e /sys/class/gpio/gpio131 ]]; then
   echo 131 > /sys/class/gpio/export
fi
echo "mode1" > /sys/kernel/debug/gpio_debug/gpio131/current_pinmux
echo "  GPIO128 pinmux: $(cat /sys/kernel/debug/gpio_debug/gpio128/current_pinmux)"
echo "  GPIO129 pinmux: $(cat /sys/kernel/debug/gpio_debug/gpio129/current_pinmux)"
echo "  GPIO130 pinmux: $(cat /sys/kernel/debug/gpio_debug/gpio130/current_pinmux)"
echo "  GPIO131 pinmux: $(cat /sys/kernel/debug/gpio_debug/gpio131/current_pinmux)"

echo "Init SPI-5.1"
if [[ ! -e /sys/class/gpio/gpio83 ]]; then
   echo 83 > /sys/class/gpio/export
fi
echo "mode0" > /sys/kernel/debug/gpio_debug/gpio83/current_pinmux
echo "out" > /sys/kernel/debug/gpio_debug/gpio83/current_direction
echo "pulldown" > /sys/kernel/debug/gpio_debug/gpio83/current_pullmode
echo "  GPIO83 pinmux: $(cat /sys/kernel/debug/gpio_debug/gpio83/current_pinmux)"
echo "  GPIO83 direction: $(cat /sys/kernel/debug/gpio_debug/gpio83/current_direction)"
echo "  GPIO83 pullmode: $(cat /sys/kernel/debug/gpio_debug/gpio83/current_pullmode)"

if [[ ! -e /sys/class/gpio/gpio81 ]]; then
   echo 81 > /sys/class/gpio/export
fi
echo "mode0" > /sys/kernel/debug/gpio_debug/gpio81/current_pinmux
echo "out" > /sys/kernel/debug/gpio_debug/gpio81/current_direction
echo "pulldown" > /sys/kernel/debug/gpio_debug/gpio81/current_pullmode
echo "  GPIO81 pinmux: $(cat /sys/kernel/debug/gpio_debug/gpio81/current_pinmux)"
echo "  GPIO81 direction: $(cat /sys/kernel/debug/gpio_debug/gpio81/current_direction)"
echo "  GPIO81 pullmode: $(cat /sys/kernel/debug/gpio_debug/gpio81/current_pullmode)"

if [[ ! -e /sys/class/gpio/gpio109 ]]; then
   echo 109 > /sys/class/gpio/export
fi
echo "mode1" > /sys/kernel/debug/gpio_debug/gpio109/current_pinmux
echo "pulldown" > /sys/kernel/debug/gpio_debug/gpio109/current_pullmode
echo "  GPIO109 pinmux: $(cat /sys/kernel/debug/gpio_debug/gpio109/current_pinmux)"
echo "  GPIO109 direction: $(cat /sys/kernel/debug/gpio_debug/gpio109/current_direction)"
echo "  GPIO109 pullmode: $(cat /sys/kernel/debug/gpio_debug/gpio109/current_pullmode)"

if [[ ! -e /sys/class/gpio/gpio111 ]]; then
   echo 111 > /sys/class/gpio/export
fi
echo "mode1" > /sys/kernel/debug/gpio_debug/gpio111/current_pinmux
echo "pulldown" > /sys/kernel/debug/gpio_debug/gpio111/current_pullmode
echo "  GPIO111 pinmux: $(cat /sys/kernel/debug/gpio_debug/gpio111/current_pinmux)"
echo "  GPIO111 direction: $(cat /sys/kernel/debug/gpio_debug/gpio111/current_direction)"
echo "  GPIO111 pullmode: $(cat /sys/kernel/debug/gpio_debug/gpio111/current_pullmode)"

if [[ ! -e /sys/class/gpio/gpio115 ]]; then
   echo 115 > /sys/class/gpio/export
fi
echo "mode1" > /sys/kernel/debug/gpio_debug/gpio115/current_pinmux
echo "pulldown" > /sys/kernel/debug/gpio_debug/gpio115/current_pullmode
echo "  GPIO115 pinmux: $(cat /sys/kernel/debug/gpio_debug/gpio115/current_pinmux)"
echo "  GPIO115 direction: $(cat /sys/kernel/debug/gpio_debug/gpio115/current_direction)"
echo "  GPIO115 pullmode: $(cat /sys/kernel/debug/gpio_debug/gpio115/current_pullmode)"

echo "Init LEDSTRIP"
if [[ ! -e /sys/class/gpio/gpio15 ]]; then
   echo 15 > /sys/class/gpio/export
fi
echo "mode0" > /sys/kernel/debug/gpio_debug/gpio15/current_pinmux
echo "out" > /sys/kernel/debug/gpio_debug/gpio15/current_direction
echo "pullup" > /sys/kernel/debug/gpio_debug/gpio15/current_pullmode
echo "  GPIO15 pinmux: $(cat /sys/kernel/debug/gpio_debug/gpio15/current_pinmux)"
echo "  GPIO15 direction: $(cat /sys/kernel/debug/gpio_debug/gpio15/current_direction)"
echo "  GPIO15 pullmode: $(cat /sys/kernel/debug/gpio_debug/gpio15/current_pullmode)"

echo "Init MICARRAY"
if [[ ! -e /sys/class/gpio/gpio40 ]]; then
   echo 40 > /sys/class/gpio/export
fi
echo "mode1" > /sys/kernel/debug/gpio_debug/gpio40/current_pinmux
echo "pulldown" > /sys/kernel/debug/gpio_debug/gpio40/current_pullmode
echo "  GPIO40 pinmux: $(cat /sys/kernel/debug/gpio_debug/gpio40/current_pinmux)"
echo "  GPIO40 direction: $(cat /sys/kernel/debug/gpio_debug/gpio40/current_direction)"
echo "  GPIO40 pullmode: $(cat /sys/kernel/debug/gpio_debug/gpio40/current_pullmode)"

if [[ ! -e /sys/class/gpio/gpio41 ]]; then
   echo 41 > /sys/class/gpio/export
fi
echo "mode1" > /sys/kernel/debug/gpio_debug/gpio41/current_pinmux
echo "pulldown" > /sys/kernel/debug/gpio_debug/gpio41/current_pullmode
echo "  GPIO41 pinmux: $(cat /sys/kernel/debug/gpio_debug/gpio41/current_pinmux)"
echo "  GPIO41 direction: $(cat /sys/kernel/debug/gpio_debug/gpio41/current_direction)"
echo "  GPIO41 pullmode: $(cat /sys/kernel/debug/gpio_debug/gpio41/current_pullmode)"

if [[ ! -e /sys/class/gpio/gpio42 ]]; then
   echo 42 > /sys/class/gpio/export
fi
echo "mode1" > /sys/kernel/debug/gpio_debug/gpio42/current_pinmux
echo "pulldown" > /sys/kernel/debug/gpio_debug/gpio42/current_pullmode
echo "  GPIO42 pinmux: $(cat /sys/kernel/debug/gpio_debug/gpio42/current_pinmux)"
echo "  GPIO42 direction: $(cat /sys/kernel/debug/gpio_debug/gpio42/current_direction)"
echo "  GPIO42 pullmode: $(cat /sys/kernel/debug/gpio_debug/gpio42/current_pullmode)"

echo "Init CAMLIDAR-XSHUT"
if [[ ! -e /sys/class/gpio/gpio82 ]]; then
   echo 82 > /sys/class/gpio/export
fi
echo "mode0" > /sys/kernel/debug/gpio_debug/gpio82/current_pinmux
echo "out" > /sys/kernel/debug/gpio_debug/gpio82/current_direction
echo "pullup" > /sys/kernel/debug/gpio_debug/gpio82/current_pullmode
echo "low" > /sys/kernel/debug/gpio_debug/gpio82/current_value
echo "  GPIO82 pinmux: $(cat /sys/kernel/debug/gpio_debug/gpio82/current_pinmux)"
echo "  GPIO82 direction: $(cat /sys/kernel/debug/gpio_debug/gpio82/current_direction)"
echo "  GPIO82 pullmode: $(cat /sys/kernel/debug/gpio_debug/gpio82/current_pullmode)"


motors="MTRPAN MTRTILTL MTRTILTU MTRROTATE"
motor_wires="IN1 IN2 IN3 IN4"
declare -A motor_gpio_pin=(
   [MTRPAN-IN1]="13"
   [MTRPAN-IN2]="165"
   [MTRPAN-IN3]="12"
   [MTRPAN-IN4]="183"
   [MTRROTATE-IN1]="182"
   [MTRROTATE-IN2]="114"
   [MTRROTATE-IN3]="79"
   [MTRROTATE-IN4]="15"
   [MTRTILTU-IN1]="44"
   [MTRTILTU-IN2]="46"
   [MTRTILTU-IN3]="48"
   [MTRTILTU-IN4]="14"
   [MTRTILTL-IN1]="84"
   [MTRTILTL-IN2]="45"
   [MTRTILTL-IN3]="47"
   [MTRTILTL-IN4]="49"
)
declare -A motor_gpio_direction=(
   [MTRPAN-IN1]="out"
   [MTRPAN-IN2]="out"
   [MTRPAN-IN3]="out"
   [MTRPAN-IN4]="out"
   [MTRTILTL-IN1]="out"
   [MTRTILTL-IN2]="out"
   [MTRTILTL-IN3]="out"
   [MTRTILTL-IN4]="out"
   [MTRTILTU-IN1]="out"
   [MTRTILTU-IN2]="out"
   [MTRTILTU-IN3]="out"
   [MTRTILTU-IN4]="out"
   [MTRROTATE-IN1]="out"
   [MTRROTATE-IN2]="out"
   [MTRROTATE-IN3]="out"
   [MTRROTATE-IN4]="out"
)
declare -A motor_gpio_mode=(
   [MTRPAN-IN1]="0"
   [MTRPAN-IN2]="0"
   [MTRPAN-IN3]="0"
   [MTRPAN-IN4]="0"
   [MTRTILTL-IN1]="0"
   [MTRTILTL-IN2]="0"
   [MTRTILTL-IN3]="0"
   [MTRTILTL-IN4]="0"
   [MTRTILTU-IN1]="0"
   [MTRTILTU-IN2]="0"
   [MTRTILTU-IN3]="0"
   [MTRTILTU-IN4]="0"
   [MTRROTATE-IN1]="0"
   [MTRROTATE-IN2]="0"
   [MTRROTATE-IN3]="0"
   [MTRROTATE-IN4]="0"
)
declare -A motor_gpio_pullmode=(
   [MTRPAN-IN1]="pullup"
   [MTRPAN-IN2]="pullup"
   [MTRPAN-IN3]="pullup"
   [MTRPAN-IN4]="pullup"
   [MTRTILTL-IN1]="pullup"
   [MTRTILTL-IN2]="pullup"
   [MTRTILTL-IN3]="pullup"
   [MTRTILTL-IN4]="pullup"
   [MTRTILTU-IN1]="pullup"
   [MTRTILTU-IN2]="pullup"
   [MTRTILTU-IN3]="pullup"
   [MTRTILTU-IN4]="pullup"
   [MTRROTATE-IN1]="pullup"
   [MTRROTATE-IN2]="pullup"
   [MTRROTATE-IN3]="pullup"
   [MTRROTATE-IN4]="pullup"
)

declare -A motor_gpio_pullstrength=(
   [MTRFOLD-IN1]="20k"
   [MTRFOLD-IN2]="20k"
   [MTRFOLD-IN3]="20k"
   [MTRFOLD-IN4]="20k"
   [MTRPAN-IN1]="20k"
   [MTRPAN-IN2]="20k"
   [MTRPAN-IN3]="20k"
   [MTRPAN-IN4]="20k"
   [MTRTILTL-IN1]="20k"
   [MTRTILTL-IN2]="20k"
   [MTRTILTL-IN3]="20k"
   [MTRTILTL-IN4]="20k"
   [MTRTILTU-IN1]="20k"
   [MTRTILTU-IN2]="20k"
   [MTRTILTU-IN3]="20k"
   [MTRTILTU-IN4]="20k"
   [MTRROTATE-IN1]="20k"
   [MTRROTATE-IN2]="20k"
   [MTRROTATE-IN3]="20k"
   [MTRROTATE-IN4]="20k"
)

function init_gpio()
{
   local pin_num="$1"; local gpio_mode="$2"; local gpio_direction="$3"; local gpio_pull_mode="$4"; local gpio_pull_strength="$5"

   if [[ ! -e /sys/class/gpio/gpio$pin_num ]]; then
      echo "$pin_num" > /sys/class/gpio/export
   fi
   echo "mode$gpio_mode" > /sys/kernel/debug/gpio_debug/gpio$pin_num/current_pinmux
   echo "$gpio_pull_mode" > /sys/kernel/debug/gpio_debug/gpio$pin_num/current_pullmode
   echo "$gpio_direction" > /sys/kernel/debug/gpio_debug/gpio$pin_num/current_direction
   echo "$gpio_pull_strength" > /sys/kernel/debug/gpio_debug/gpio$pin_num/current_pullstrength
   if [[ $gpio_direction == "out" ]]; then
      echo "low" > /sys/kernel/debug/gpio_debug/gpio$pin_num/current_value
   fi

   echo "  GP$pin_num pinmux: $(cat /sys/kernel/debug/gpio_debug/gpio$pin_num/current_pinmux)"
   echo "  GP$pin_num direction: $(cat /sys/kernel/debug/gpio_debug/gpio$pin_num/current_direction)"
   echo "  GP$pin_num pullmode: $(cat /sys/kernel/debug/gpio_debug/gpio$pin_num/current_pullmode)"
   echo "  GP$pin_num pullstrength: $(cat /sys/kernel/debug/gpio_debug/gpio$pin_num/current_pullstrength)"
   echo "  GP$pin_num value: $(cat /sys/kernel/debug/gpio_debug/gpio$pin_num/current_value)"
}

function init_motors()
{
   for motor in $motors; do
      echo "Init $motor"
      for wire in $motor_wires; do
         init_gpio ${motor_gpio_pin[$motor-$wire]} ${motor_gpio_mode[$motor-$wire]} ${motor_gpio_direction[$motor-$wire]} ${motor_gpio_pullmode[$motor-$wire]} ${motor_gpio_pullstrength[$motor-$wire]}
      done
   done
}

init_motors

`