libgpio

Johny Mattsson johny.mattsson+fbsd at gmail.com
Sat Nov 8 02:01:10 UTC 2014


On 8 November 2014 02:44, Warner Losh <imp at bsdimp.com> wrote:On Nov 6,
2014, at 11:41 PM, Rui Paulo <rpaulo at me.com> wrote:

> Finally a question: What does Linux do here? Is there a standard interface
> that we could use to leverage off applications written for Linux?


On Linux, userspace GPIO is typically* used through the GPIO SysFS module
which provides entries under /sys/class/gpio (or wherever you've mounted
your sysfs).

Individual pins are transferred to userspace control by writing the pin
number to the "export" file, which when successful then adds a virtual
directory for that GPIO line, with entries for direction, value, edge and
whether it's active-low. Depending on the backing kernel driver and
hardware, interrupt support may or may not be available. When available
it's enabled by writing "rising", "falling" or "both" (again subject to
hw/driver) to the "edge" file, and then select(2)ing on the "value" file
return when said edge is triggered. Changing the output on a line is as
simple as writing "0" or "1" to the "value" file.

Example (drive gpio #32 high):

echo 32 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio32/direction  # this annoyingly/reassuringly
clears this pin - i.e. cat value gives 0
echo 1 > /sys/class/gpio/gpio32/value
# some time later, maybe
echo 32 > /sys/class/gpio/unexport


There is no support for synchronously setting multiple pins through this
interface. Even if there was, it would have to be limited to within the one
controller/bank. Also, if I remember the i.MX25 correctly, there was no
hardware support for doing more than one line at a time anyway due to the
register layout. While such a feature would be nice, I wouldn't stress too
much about getting it in. At best, support for it will be sporadic
depending on hardware.

The (limited) documentation for GPIO SysFS on Linux, other than the source
itself, can be found here:
  https://www.kernel.org/doc/Documentation/gpio/sysfs.txt


Cheers,
/Johny

*) Based on personal experience at $work, and the companies we work with,
doing various embedded micro and embedded Linux things.


More information about the freebsd-embedded mailing list