Main Site 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. Users who are members of group gpio have permissions to write 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:

echo 17 > /sys/class/gpio/export

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.

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.

cat /sys/class/gpio/gpio17/value

echo out > /sys/class/gpio/gpio17/direction
echo 1 > /sys/class/gpio/gpio17/value

... or in a single command ....
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.

echo 0 > /sys/class/gpio/gpio17/value
echo in > /sys/class/gpio/gpio17/direction
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

echo 1 > /sys/class/gpio/gpio17/active_low

Allowing None Root Device Permissions





Contents Up Previous Next


HiPi Modules Copyright © 2013 - 2016 Mark Dootson