Skip to content

Instantly share code, notes, and snippets.

@huzhifeng
Created November 3, 2015 12:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huzhifeng/e3c222e6b780d82967db to your computer and use it in GitHub Desktop.
Save huzhifeng/e3c222e6b780d82967db to your computer and use it in GitHub Desktop.
Shell scripts used to test GPIO LEDs and buttons, fork and improve based on http://wiki.openwrt.org/doc/devel/add.new.device
Single GPIO test
root@OpenWrt:/# gpio_button_test.sh 1 1
[GPIO1] value 0
root@OpenWrt:/#
GPIO range test
root@OpenWrt:/# gpio_button_test.sh 0 2
[GPIO0] value 0
[GPIO1] value 0
[GPIO2] value 0
root@OpenWrt:/#
All GPIOs test
root@OpenWrt:~# gpio_button_test.sh
[GPIO0] value 0
[GPIO1] value 0
[GPIO2] value 0
[GPIO3] value 0
[GPIO4] value 1
[GPIO5] value 1
[GPIO6] value 1
[GPIO7] value 1
[GPIO8] value 0
[GPIO9] value
...
#!/bin/sh
GPIOCHIP=0
BASE=$(cat /sys/class/gpio/gpiochip${GPIOCHIP}/base)
NGPIO=$(cat /sys/class/gpio/gpiochip${GPIOCHIP}/ngpio)
max=${2:-$(($BASE+$NGPIO-1))}
gpio=${1:-$BASE}
while [ $gpio -le $max ] ; do
echo $gpio > /sys/class/gpio/export
[ -d /sys/class/gpio/gpio${gpio} ] || echo "Failed to export [GPIO$gpio]"
[ -d /sys/class/gpio/gpio${gpio} ] && {
echo in > /sys/class/gpio/gpio${gpio}/direction
echo "[GPIO${gpio}] value $(cat /sys/class/gpio/gpio${gpio}/value)"
echo ${gpio} > /sys/class/gpio/unexport
}
gpio=$((gpio+1))
done
Single GPIO test
root@OpenWrt:~# gpio_led_test.sh 18 18
Testing [GPIO18]...
[GPIO18] Trying value 0
[GPIO18] Trying value 1
root@OpenWrt:~#
GPIO range test
root@OpenWrt:~# gpio_led_test.sh 0 2
Testing [GPIO0]...
[GPIO0] Trying value 0
[GPIO0] Trying value 1
Testing [GPIO1]...
[GPIO1] Trying value 0
[GPIO1] Trying value 1
Testing [GPIO2]...
[GPIO2] Trying value 0
[GPIO2] Trying value 1
root@OpenWrt:~#
Test all GPIOs
root@OpenWrt:~# gpio_led_test.sh
Testing [GPIO0]...
[GPIO0] Trying value 0
[GPIO0] Trying value 1
Testing [GPIO1]...
[GPIO1] Trying value 0
[GPIO1] Trying value 1
Testing [GPIO2]...
[GPIO2] Trying value 0
[GPIO2] Trying value 1
Testing [GPIO3]...
[GPIO3] Trying value 0
[GPIO3] Trying value 1
Testing [GPIO4]...
[GPIO4] Trying value 0
[GPIO4] Trying value 1
Testing [GPIO5]...
[GPIO5] Trying value 0
[GPIO5] Trying value 1
Testing [GPIO6]...
[GPIO6] Trying value 0
[GPIO6] Trying value 1
Testing [GPIO7]...
[GPIO7] Trying value 0
[GPIO7] Trying value 1
Testing [GPIO8]...
[GPIO8] Trying value 0
[GPIO8] Trying value 1
Testing [GPIO9]...
[GPIO9] Trying value 0
^@[GPIO9] Trying value 1
Testing [GPIO10]...
[GPIO10] Trying value 0
[GPIO10] Trying value 1
Testing [GPIO11]...
sh: write error: Device or resource busy
Failed to export [GPIO11]
Testing [GPIO12]...
sh: write error: Device or resource busy
Failed to export [GPIO12]
Testing [GPIO13]...
sh: write error: Device or resource busy
Failed to export [GPIO13]
Testing [GPIO14]...
sh: write error: Device or resource busy
Failed to export [GPIO14]
Testing [GPIO15]...
sh: write error: Device or resource busy
Failed to export [GPIO15]
Testing [GPIO16]...
sh: write error: Device or resource busy
Failed to export [GPIO16]
Testing [GPIO17]...
[GPIO17] Trying value 0
[GPIO17] Trying value 1
Testing [GPIO18]...
[GPIO18] Trying value 0
[GPIO18] Trying value 1
Testing [GPIO19]...
[GPIO19] Trying value 0
[GPIO19] Trying value 1
Testing [GPIO20]...
[GPIO20] Trying value 0
[GPIO20] Trying value 1
Testing [GPIO21]...
[GPIO21] Trying value 0
[GPIO21] Trying value 1
Testing [GPIO22]...
[GPIO22] Trying value 0
[GPIO22] Trying value 1
root@OpenWrt:~#
#!/bin/sh
GPIOCHIP=0
BASE=$(cat /sys/class/gpio/gpiochip${GPIOCHIP}/base)
NGPIO=$(cat /sys/class/gpio/gpiochip${GPIOCHIP}/ngpio)
max=${2:-$(($BASE+$NGPIO-1))}
gpio=${1:-$BASE}
while [ $gpio -le $max ] ; do
echo "Testing [GPIO$gpio]..."
echo $gpio > /sys/class/gpio/export
[ -d /sys/class/gpio/gpio${gpio} ] || echo "Failed to export [GPIO$gpio]"
[ -d /sys/class/gpio/gpio${gpio} ] && {
echo out > /sys/class/gpio/gpio$gpio/direction
echo "[GPIO$gpio] Trying value 0"
echo 0 > /sys/class/gpio/gpio$gpio/value
sleep 3s
echo "[GPIO$gpio] Trying value 1"
echo 1 > /sys/class/gpio/gpio$gpio/value
sleep 3s
echo $gpio > /sys/class/gpio/unexport
}
gpio=$((gpio+1))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment