GPIO Event Driver

From Gumstix User Wiki
Revision as of 10:27, 18 February 2010 by Dhylands (Talk | contribs) (Added instructions on building for OE)

Jump to: navigation, search

This page documents the gpio-event driver, which allows multiple GPIO lines to be monitored from user-space.

Overview

The gpio-event driver consists of a loadable kernel module, which registers an interrupt handler, along with an example user-mode program, which allows the settings to be manipulated and changes to be reported.

Driver Sources

The source code can be found here. The app directory contains the example user mode application, and the module directory contains the loadable kernel module. The Makefiles have been setup such that if you define the environment variable OVEROTOP then you should be able to just type make in top level directory and both the app and module will be built.

There are also some bitbake recipes in the gpio-event-app and gpio-event-module directories.

Adding pins to be monitored

The gpio-event program can be used to add gpio pins to be monitored. Simply run the gpio-event program, passing it a pin specification of the form: pin:edge:debounce. pin should be the GPIO number that you want to monitor. Edge should be r, f, or b to monitor rising, falling, or both edges, and debounce should be the number of milliseconds of debounce that you'd like. A debounce of 0 may be used for "clean" signals. For example:

gpio-event 58:f:20 59:r:20 60:b:20

would cause gpio 58 to be monitored for falling edges, gpio 59 to be monitored for rising edges, and gpio 60 to be monitored for both rising and falling edges. Each pin will have a debounce of 20 milliseconds (which should be adequate for most pushbuttons).

You can pass as many pins to be monitored as you'd like on the command line. Each call is cumulative. If you specify the same pin, the new specification will override whatever is currently set. You can see the current pins being monitored by using:

cat /proc/gpio-event/pins

Passing in a negative gpio number will cause the pin to stop being monitored.

Detecting events

Once pins are setup to be monitored, any program can read /dev/gpio-event to get notified of the events. By default, an ASCII string of the form: "pin edge timestamp" followed by a newline will be delievered to /dev/gpio-event for each event that occurs. Running gpio-event with the -m or --monitor command line will cause the events to be monitored and printed. You could also use the command:

cat /dev/gpio-event

to achieve a similar result. For C programs, you can opt to use a binary interface instead. The binary versus ASCII setting is maintained on a per-file basis, so each program could each open /dev/gpio-event with a different ASCII/binary setting.

The edge will be an R or an F depending on whether a rising or falling edge is being reported. The timestamp is a number with the integer portion being the current system time (as returned from the gettimeofday function) and the fractional portion reporting in microseconds.

Lauching a script

The gpio-event program can also be given the -e or --execute command line option, and the provided program or script will be launched with the same 3 paramters (gpio edge timestamp). For example:

gpio-event --execute echo

will produce similar output to

gpio-event --monitor

Integrating/Building with Open Embedded

In order to build the driver under OE, you can do the following:

 cd to the directory containing org.embedded.dev
 svn co http://svn.hylands.org/linux/oe/user.collection user.collection
 bitbake task-gpio-all

This will build the following packages for gpio-event:

./deploy/glibc/ipk/overo/gpio-event-module_svn20100218-r50.5_overo.ipk
./deploy/glibc/ipk/armv7a/gpio-event-app_svn20100218-r2.5_armv7a.ipk

and these packages for the User GPIO Driver

./deploy/glibc/ipk/overo/gpio-module_svn20100218-r50.5_overo.ipk
./deploy/glibc/ipk/armv7a/gpio-app_svn20100218-r0.5_armv7a.ipk
./deploy/glibc/ipk/armv7a/gpio-lib_svn20100218-r0.5_armv7a.ipk

Note that the numeric portions of the pathnames will almost certainly be different.