Using a GPIO in Linux

Using a GPIO in Linux

Almost all function pins of a SOC are GPIO ( General Purpose Input Output).
They can be configured to serve as a simple input, output or with a special function (i.e. I2C or UART).
Most pins can be configured with additional settings like drive-strength, pull-up or -down, keeper, etc.

Using a GPIO from Command Line

As already mentioned a pin can be configured to serve as GPIO or as a special function.
The following commands only work as expected, when the pin is configured as GPIO!

Path in Sysfs:

/sys/class/gpio

First thing to do, is to export control of a GPIO to userspace by writing its number to 'export':

$ echo 68 > /sys/class/gpio/export

This will create an entry for gpio3.IO[4] = ((bank-1)*32)+pin = ((3-1)*32)+4 = 68 at:

/sys/class/gpio/gpio68

To reverse the effect of exporting GPIO 68 to userspace call:

$ echo 68 > /sys/class/gpio/unexport

Configure GPIO as Input

$ echo in > /sys/class/gpio/gpio68/direction

Read GPIO level

$ cat /sys/class/gpio/gpio68/value

Configure GPIO as Output

$ echo out > /sys/class/gpio/gpio68/direction

Instead of using 'out', you can use 'low' and 'high' to ensure glitch free initialization:

$ echo high > /sys/class/gpio/gpio68/direction
$ echo low > /sys/class/gpio/gpio68/direction

Write GPIO level

Set pin low:

$ echo 0 > /sys/class/gpio/gpio68/value

Set pin high:

$ echo 1 > /sys/class/gpio/gpio68/value

Trizeps VII & Trizeps VIII GPIOs

SODIMM

Name

1

MIC_OUT

 

 

2

AD3

 

 

3

MIC_GND

 

 

4

AD2

 

 

5

LINEIN_L

 

 

6

AD1

 

 

7

LINEIN_R

 

 

8

AD0

 

 

9

AGND

 

 

10

VDDA

 

 

11

AGND

 

 

12

VDDA