Using the legacy sysfs GPIO under Android and Linux the first step in the process is toe export
the particular GPIO pins you want to use. And when you are done with the GPIO pin to unexport
it.
I've been looking for an explanation of what the export
command actually does however everything I've found is about the builtin bash
command which has nothing to do with GPIO.
Then I realized the actual command from the command line was echo 938 > /sys/class/gpio/export
and /sys/class/gpio/export
is a special device file in folder /sys/class/gpio
.
The only comment that I have found indicates that writing the GPIO pin number to /sys/class/gpio/export
causes the GPIO special file associated with that GPIO pin to be "exported to user space" which then allows a user application to use the specified GPIO pin with file I/O to the special device file.
GPIO Sysfs Interface for Userspace
“export” …
Userspace may ask the kernel to export control of a GPIO to userspace by writing its number to this file.
Example: “echo 19 > export” will create a “gpio19” node for GPIO #19, if that’s not requested by kernel code.
“unexport” …
Reverses the effect of exporting to userspace.
Example: “echo 19 > unexport” will remove a “gpio19” node exported using the “export” file.
So if I specify echo 938 > /sys/class/gpio/export
then a special device file folder /sys/class/gpio/gpio938
with special device files /sys/class/gpio/gpio938/value
and /sys/class/gpio/gpio938/direction
are created. And when I do an echo 938 > /sys/class/gpio/unexport
then those special device files are removed?
In researching about using GPIO pins with a DragonBoard 410C under Android 5.1 an online course about this device I am taking said to add the following lines to the boot initialization script.
set -A pins 938 915 1017 926 937 930 914 971 901 936 935
for i in 0 1 2 3 4 5 6 7 8 9 10
do
echo ${pins[i]} > /sys/class/gpio/export;
chmod 777 /sys/class/gpio/gpio${pins[i]};
chmod 777 /sys/class/gpio/gpio${pins[i]}/value;
chmod 777 /sys/class/gpio/gpio${pins[i]}/direction;
done
My understanding is that these commands create the special device files for GPIO pins 938, 915, 1017, 926, 937, 914, 901, 936, 935 so that an application can read and write to these GPIO pins to do something such as turning an LED on and off by writing values to, for instance /sys/class/gpio/gpio938/value
.
My understanding about this boot initialization script is that this removes the need for a user to use the sudo
command with each of the shell command lines in order to perform these commands by a user before running an application that accesses the GPIO pins using sysfs
. Is that true?
My Questions
What are these special device files /sys/class/gpio/export
and /sys/class/gpio/unexport
and how are they connected to some kind of functionality in the Linux kernel which creates and destroys special device files in the /sys/class/gpio
folder?
With the suggested change to the boot initialization script are the special device files representing the GPIO pins created with access by anyone so an application program can just use the pins and not bother with export
or unexport
? A user application can just perform read/write to the special device without having to use sudo echo 938 > /sys/class/gpio/export
first?
What is the access and sharing permissions for these special files created by the boot initialization script and can multiple applications be manipulating the same GPIO pins simultaneously?