Skip to content

Instantly share code, notes, and snippets.

@wkliang
Last active January 10, 2024 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wkliang/9b89bdfd7a5c05be7c938de5764cefb9 to your computer and use it in GitHub Desktop.
Save wkliang/9b89bdfd7a5c05be7c938de5764cefb9 to your computer and use it in GitHub Desktop.
raspberry pi 5 sysfs + udev to control gpio
#!/bin/bash
if [ $1 == 'CH1' ] ; then # gpio 26, PIN37
ch=425
elif [ $1 == 'CH2' ] ; then # gpio 20, PIN38
ch=419
elif [ $1 == 'CH3' ]; then # gpio 21, PIN40
ch=420
else
echo "Parameter error"
exit
fi
echo $ch > /sys/class/gpio/export
flag=true
while $flag ; do
# ls -l /sys/class/gpio/gpio$ch/
echo waiting for udev to set permission right
echo out > /sys/class/gpio/gpio$ch/direction
if [ $? -eq 0 ] ; then
flag=false
else
flag=true
fi
sleep 1
done
echo 0 > /sys/class/gpio/gpio$ch/value
sleep 1
echo 1 > /sys/class/gpio/gpio$ch/value
echo $ch > /sys/class/gpio/unexport
exit
# https://www.kernel.org/doc/Documentation/gpio/sysfs.txt
# https://stackoverflow.com/questions/76138933/raspberry-pi-gpio-sysfs-fopen-permission-denied
# https://doc.embedfire.com/linux/imx6/linux_base/zh/latest/linux_app/gpio_subsystem/gpio_subsystem.html
# grep PIN /sys/kernel/debug/gpio
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment