Homepage Wiki Forum Buy

Permissions GPIO

From GNUBLIN

If you login on GNUBLIN Linux as root, you will see some example applications to controll the GPIO interface. The combination of Linux and hardware makes that so exciting.

But rules in Linux expect that you work as root as less as possible. But the example scripts have to run as root.

The following shows how to create those files and award them to a user.


Create gpio.conf

Create a file /etc/gpio.conf with following content:

# configuration for user access to gpio ports
# syntax is portnr=configstring, where portnr is the 
# decimal port nr (e.g. 15) and configstring the direction
# legal config strings are "in", "out", "high" and "low"

3=out # onboard LED
15=out
14=in


Create gpio

Create a file /etc/init.d/gpio with following content:

#!/bin/bash
### BEGIN INIT INFO
# Provides:          gpio
# Required-Start:    
# Required-Stop:     
# Should-Start:      
# Should-Stop:       
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: 'Publish' access to general purpose I/Os
### END INIT INFO

USER=root
GROUP=lighttpd

g_start() {
	echo "$1" > /sys/class/gpio/export
	echo "$2" > /sys/class/gpio/gpio$1/direction
	chmod 777 /sys/class/gpio/gpio$1
	chmod 666 /sys/class/gpio/gpio$1/*
}

start_g() {
CONF=`sed s/#.*// /etc/gpio.conf`

for i in $CONF ; do
	w="${i/=/ }"
	g_start $w
done
}


g_stop() {
	echo $1 > /sys/class/gpio/unexport
}

stop_g() {
CONF=`sed s/#.*// /etc/gpio.conf`

for i in $CONF ; do
	w="${i/=/ }"
	g_stop $w
done
}

case "$1" in
    start)
	start_g
	;;
    stop)
	stop_g
	;;
    reload|force-reload)
        ;;
    restart)
	stop_g
	start_g 
	;;
    status)
	if [[ -d /sys/class/gpio/gpio3 ]] ; then
		exit 0
	else
		exit 1
	fi
        ;;
    *)
        echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload|status}" >&2
        exit 1
        ;;
esac

exit 0


You can enter users and groups by your own. The group lighthttpd is practical, if you want to get access to ports with your Web-Server.

Copy the files

At the end create a link

ln -s /etc/init.d/gpio /etc/rc5.d/S40gpio

in order to start the script automatically on the next boot. You can now test the script:

/etc/rc5.d/S40gpio start

After that, the GPIO's should be created with your defined user rights.

In other languages