Hardware Reference
In-Depth Information
Sysfs GPIO Access
In this section, we're going to access the GPIO pins through the /sys pseudo file system.
This is the GPIO driver interface. Because it provides file system objects, it is possible to
control GPIO pins from the command line (or shell).
The C/C++ programmer might be quick to dismiss this approach, because it might
seem too slow. However, for input pins, the driver provides the advantage of providing a
reasonable edge-level detection that is not possible when accessing the GPIO registers
directly. The driver is able to receive interrupts when a GPIO state changes. This information
can in turn be passed onto the application program using poll(2) or select(2) .
Everything that you need for GPIO access is rooted in the top-level directory:
/sys/class/gpio
At this directory level, two main control pseudo files are maintained by the driver.
These are write-only:
export : Requests the kernel to export control of the requested
GPIO pin by writing its number to the file
unexport : Relinquishes control of the GPIO pin by writing its
number to the file
Note
even root gets the permission denied if you try to read these files.
Normally, the kernel manages the GPIO pins, especially if they are used for resources
that need them (like the UART). In order for an application to manipulate a GPIO pin,
it must first request that the kernel relinquish control of the requested pin. From a
userspace perspective, the operation is like opening a file. The script or program should
be prepared for failure in the event that a GPIO pin is busy.
COrreCt USe OF SUDO
It is tempting to perform some operations from a nonroot account, using sudo like this:
$ sudo echo 17 >/sys/class/gpio/export
-bash: /sys/class/gpio/export: Permission denied
this does not work because the I/O redirection is performed by the shell before the
sudo command begins. Change to interactive mode first and then the operation will
succeed:
$ sudo -i
# echo 17 >/sys/class/gpio/export
 
 
Search WWH ::




Custom Search