Contents Up Previous Next

GPIO Device Driver

The current Raspbian Wheezy Linix distribution provides the Linux gpio kernel device driver that provides access to the Raspberry Pi GPIO pins.

It does this by providing access to files in the filesystem directory:

/sys/class/gpio

The module HiPi::Device::GPIO provides a Perl interface to all of the gpio device driver functions.

The HiPi Control GUI Application allows you to set GPIO options using the kernel gpio device driver.

To control the gpio pins from the command line you can write ascii commands and read values from the control files.

The kernel documentation for the gpio driver can be read at

http://www.kernel.org/doc/Documentation/gpio.txt

In /sys/class/gpio there are two files that allow you to export pins for access and unexport pins to remove access.

/sys/class/gpio/export
/sys/class/gpio/unexport

To export GPIO Pin 17 so that we can read, write and control the pin, we simply write 17 to the export file:

sudo sh -c 'echo 27 > /sys/class/gpio/export'

With default settings we must use sudo as root permissions are required for the files. Note that we cannot use the following:

sudo echo 27 > /sys/class/gpio/export

The redirection of command output to /sys/class/gpio/export applies to the output of the command, which in this case is 'sudo'. We don't have permissions to write to the file so using sudo here is pointless. The work around is to run a shell ( sh ) under sudo and pass in the whole command including redirection using the -c option.

Once GPIO pin 17 is exported the following files (or links to files) are created in a new directory, gpio17.

/sys/class/gpio/gpio17/direction
/sys/class/gpio/gpio17/value
/sys/class/gpio/gpio17/edge
/sys/class/gpio/gpio17/active_low

We can set the pin as an input or output by writing either 'in' or 'out' to the direction file. We can also read the file to query the current function of the pin. By default, output pins are configured and set low. Writing 'high' or 'low' to the direction file configures the pin as an output initially set at that level.

sudo sh -c 'echo in > /sys/class/gpio/gpio17/direction'

We read the state of the pin ( 1 or 0 for high or low) by reading the value file. For pins configured as outputs, we can set the value by writing to the file.

sudo cat /sys/class/gpio/gpio17/value

sudo sh -c 'echo out > /sys/class/gpio/gpio17/direction'
sudo sh -c 'echo 1 > /sys/class/gpio/gpio17/value'

... or in a single command ....
sudo sh -c 'echo high > /sys/class/gpio/gpio17/direction'

We can set interrupts by writing to the edge file or read the file to get the current setting. Possible edge settings are none, falling, rising, or both. We check for interrupts by using poll(2) on the value file.

sudo sh -c 'echo 0 > /sys/class/gpio/gpio17/value'
sudo sh -c 'echo in > /sys/class/gpio/gpio17/direction'
sudo sh -c 'echo falling > /sys/class/gpio/gpio17/edge'
... poll /sys/class/gpio/gpio17/value in some code elsewhere

We can invert the logic of the value pin for both reading and writing so that a high == 0 and low == 1 by wrting to the active_low file. To invert logic write 1. To revert write 0

sudo sh -c 'echo 1 > /sys/class/gpio/gpio17/active_low'

Allowing None Root Device Permissions

By default, only root can export and unexport files and write to the gpio pin files. The device manager on Linux is called udev and we can provide rules for udev that control the permissions on the device files that it creates.

With default settings the files in /sys/class/gpio are owned by user root and group root. Only the root user will have read/write permissions.

We can change this with a udev rule. We create a file in /etc/udev/rules.d that contains the line:

KERNEL=="gpio*", SUBSYSTEM=="gpio", ACTION=="add", PROGRAM="/usr/local/bin/hipi-expin gpio /sys%p"

The KERNEL, SUBSYSTEM and ACTION settings define 'matches' that udev should check to determine which device driver and action the rule applies to while the PROGRAM setting provides the path of a program that should be run. The Perl script /usr/local/bin/hipi-expin receives the name of a group and the path to the files that the device driver has created. The script changes the group of the files to the group passed in ( gpio ) and changes the permissions mode of the files to 0660

With this rule users that are members of the gpio group will be able to read and write from the gpio device files.

The HiPi Control GUI application will set or remove this rule for you and allow you to manage the gpio group and device driver options.





Contents Up Previous Next


HiPi Modules Copyright © 2013 Mark Dootson