Skip to content

Commit

Permalink
Update the GPIO sysfs interface documentation with material from kern…
Browse files Browse the repository at this point in the history
…el.org (#911)

kernel.org has a more detailed treatment of using the sysfs interface for accessing and manipulating the GPIO pins.

Providing a link to that article as well as providing a block quote of the different pin attribute pseudo files.
  • Loading branch information
RichardChambers authored Mar 23, 2021
1 parent 08d207e commit ad2ae59
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions consumer/guides/gpio.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ GPIO provides the basic Input and Output access to the physical world for 96Boar

96Boards provides access to GPIO pins through 40pin **Low Speed Expansion Header** available on all CE boards.

## GPIO access through Command Line
## GPIO access through Command Line using sysfs Interface

The following instructions are applicable to all Linux based distributions and is based on sysfs interface.
The following instructions are applicable to all Linux based distributions and is based on sysfs interface. Note that the sysfs interface to the GPIO is deprecated.

To access the GPIOs through the Linux shell, open the terminal (start menu > other > LXTerminal; right click this to add shortcut to your desktop if you desire) and give yourself super user access:
For details about the sysfs interface for the GPIO pins see [GPIO Sysfs Interface for Userspace](https://www.kernel.org/doc/html/latest/admin-guide/gpio/sysfs.html) which contains details about the paths to use and how to use the various control interfaces.

To access the GPIOs through the Linux shell you can use the sysfs interface and the psudo files that act as an interface to the kernel implementation of the GPIO device drivers. You will need to have superuser access to manipulate the pseudo files.

Start by opening up a terminal window to access the command shell, open the terminal (start menu > other > LXTerminal; right click this to add shortcut to your desktop if you desire) and give yourself super user access:

```shell
$ sudo su
Expand All @@ -30,7 +34,9 @@ Once you have superuser access, navigate to the gpio folder with the following c
# cd /sys/class/gpio
```

Before accessing any GPIO, it needs to be exported using the following command:
Before accessing any GPIO, it needs to be exported. The `export` command will instruct the kernel to create the various files and directories, pseudo files, representing the GPIO pin. After doing an `export` of a pin, the GPIO pin functionality can be used by reading and writing to various files created by using standard file I/O commands. There is also an `unexport` command to undo a previous `export` command. Also note that after a reboot, the exporting will need to be done again unless a change is made to do the pin export as part of Linux startup.

Use the following command to create the pseudo files for GPIO pin #36. For other pins, you would replace 36 with the pin number you want to use.

```shell
# echo 36 > export
Expand All @@ -49,7 +55,7 @@ After exporting GPIO, move into that gpio directory
# cd gpio36
```

Following command returns the direction of GPIO
Following command returns the direction of GPIO which is a string value of "out" or "in"
```shell
# cat direction
```
Expand All @@ -59,7 +65,7 @@ Following command sets the direction of GPIO
# echo [out or in] > direction
```

Following command returns 0 if the pin is off, 1 if the pin is on
Following command returns 0 if the pin is off or low, 1 if the pin is on or high
```shell
# cat value
```
Expand All @@ -69,7 +75,34 @@ Following command sets value of GPIO
# echo [0 or 1] > value
```

Using a multimeter set to measuring voltage, you can probe the pin you are toggleing along with one of the ground nodes (Pins 1,2,39, and 40), to watch the voltage switch between ~1.8V and 0V.
Using a multimeter set to measuring voltage, you can probe the pin you are toggleing along with one of the ground nodes (Pins 1,2,39, and 40), to watch the voltage switch between ~1.8V and 0V as the content of `value` is changed.

### GPIO signal pin attribute files

GPIO signal pins have paths like `/sys/class/gpio/gpio42`. Each has a set of attributes accessed through pseudo files that can be read from or written to. See [Paths in Sysfs](https://www.kernel.org/doc/html/latest/admin-guide/gpio/sysfs.html#paths-in-sysfs) for details.

> “direction” …
>
> reads as either “in” or “out”. This value may normally be written. Writing as “out” defaults to initializing the value as low. To ensure glitch free operation, values “low” and “high” may be written to configure the GPIO as an output with that initial value.
>
> Note that this attribute will not exist if the kernel doesn’t support changing the direction of a GPIO, or it was exported by kernel code that didn’t explicitly allow userspace to reconfigure this GPIO’s direction.
>
> “value” …
>
> reads as either 0 (low) or 1 (high). If the GPIO is configured as an output, this value may be written; any nonzero value is treated as high.
>
> If the pin can be configured as interrupt-generating interrupt and if it has been configured to generate interrupts (see the description of “edge”), you can poll(2) on that file and poll(2) will return whenever the interrupt was triggered. If you use poll(2), set the events POLLPRI and POLLERR. If you use select(2), set the file descriptor in exceptfds. After poll(2) returns, either lseek(2) to the beginning of the sysfs file and read the new value or close the file and re-open it to read the value.
>
> “edge” …
>
> reads as either “none”, “rising”, “falling”, or “both”. Write these strings to select the signal edge(s) that will make poll(2) on the “value” file return.
>
> This file exists only if the pin can be configured as an interrupt generating input pin.
>
> “active_low” …
>
> reads as either 0 (false) or 1 (true). Write any nonzero value to invert the value attribute both for reading and writing. Existing and subsequent poll(2) support configuration via the edge attribute for “rising” and “falling” edges will follow this setting.

## GPIO access through MRAA library

Expand Down

0 comments on commit ad2ae59

Please sign in to comment.