Skip to content

Instantly share code, notes, and snippets.

@mkresin
Last active June 12, 2018 10:36
Show Gist options
  • Save mkresin/061ad551aa5391668ab20eb8c8abf01e to your computer and use it in GitHub Desktop.
Save mkresin/061ad551aa5391668ab20eb8c8abf01e to your computer and use it in GitHub Desktop.
Test a range of pin, a single pin or all pins of either a specific GPIO controller or on all GPIO controller
#!/bin/sh
# Copyright (C) 2017 Mathias Kresin
#
# This is free software, licensed under the GNU General Public License v2.
#
# Test a range of pin, a single pin or all pins of either a specific GPIO
# controller or on all GPIO controller
#
# Usage:
#
# Test a range of pins:
# gpio_output_test.sh --start=<pin> --end=<pin>
#
# Test a single pin
# gpio_output_test.sh --pin=<pin>
#
# Test a range of pins/single pin only on specific controller
# gpio_output_test.sh --controller=<nr> --pin=<pin>
#
# Test all pins on all controller
# gpio_output_test.sh
#
GPIOSYSFS="/sys/class/gpio"
for i in "$@"; do
case $i in
--controller*)
CONTROLLER="${i#*=}"
shift
;;
--start*|--pin=*)
START="${i#*=}"
shift
;;
--end*)
END="${i#*=}"
shift
;;
esac
done
echo "Found the following GPIO controller:"
CURCONTROLLER=0
for GPIOCHIP in ${GPIOSYSFS}/gpiochip*;do
echo " ${CURCONTROLLER}: $GPIOCHIP"
CURCONTROLLER=$((CURCONTROLLER + 1))
done
CURCONTROLLER=0
for GPIOCHIP in ${GPIOSYSFS}/gpiochip*;do
GPIOBASE=$(cat "${GPIOCHIP}/base")
GPIOSTARTPIN=${START}
GPIOENDPIN=${END}
GPIOPINCOUNT=$(cat "${GPIOCHIP}/ngpio")
[ -n "${CONTROLLER}" ] && [ "${CURCONTROLLER}" -ne "${CONTROLLER}" ] && {
CURCONTROLLER=$((CURCONTROLLER + 1))
continue
}
echo ""
echo "Testing ${GPIOCHIP} (controller nr: ${CURCONTROLLER}, nr. of GPIOs: ${GPIOPINCOUNT})"
[ -z "$GPIOSTARTPIN" ] && [ -z "$GPIOENDPIN" ] && {
GPIOSTARTPIN=0
GPIOENDPIN=$((GPIOPINCOUNT - 1))
}
[ -z "$GPIOENDPIN" ] && GPIOENDPIN="${GPIOSTARTPIN}"
for i in $(seq $GPIOSTARTPIN $GPIOENDPIN); do
GPIOPIN=$((i + GPIOBASE))
[ "$i" -ge "$GPIOPINCOUNT" ] && {
echo " This gpio controller doesn't have a Pin ${i} (${GPIOPIN}). Skipping."
continue
}
echo " ${GPIOCHIP} - Pin ${i} (${GPIOPIN})"
echo "${GPIOPIN}" > "${GPIOSYSFS}/export" 2> /dev/null || {
echo " Pin in use. Skipping."
continue
}
echo out > "${GPIOSYSFS}/gpio${GPIOPIN}/direction"
ITERATIONS=0
while [ $ITERATIONS -lt 5 ]; do
echo 0 > "${GPIOSYSFS}/gpio${GPIOPIN}/value"
sleep 1
echo 1 > "${GPIOSYSFS}/gpio${GPIOPIN}/value"
sleep 1
ITERATIONS=$((ITERATIONS + 1))
done
echo "${GPIOPIN}" > "${GPIOSYSFS}/unexport"
done
CURCONTROLLER=$((CURCONTROLLER + 1))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment