GPIO

From eLinux.org
Jump to: navigation, search

General Purpose Input or Output: a generic I/O line on a SoC is often referred to as a GPIO. By definition a GPIO is a line that can be used as an input or output. In some cases vendors will to a signal that support external interrupts as a GPIO line as well.

References

Application space GPIO support

Linux GPIO support includes the ability to export GPIO control and status for use with applications using sysfs. No other driver can be using the GPIO of interest. Simple tools like cat and echo can be used to quickly read the current value of a GPIO input or to set the level of a GPIO output.

Configure the kernel for GPIO support in sysfs

You likely need to enable GPIO support in sysfs before GPIOs are accessible from application space.

Symbol: GPIO_SYSFS [=y]
  Prompt: /sys/class/gpio/... (sysfs interface)
    Defined at drivers/gpio/Kconfig:51
    Depends on: GPIOLIB && SYSFS && EXPERIMENTAL
     Location:
      -> Kernel configuration
        -> Device Drivers
         -> GPIO Support (GPIOLIB [=y])
 

Enable GPIO from application space

In this example, GPIO 22 is make available (exported) to application space.

GPIO=22

cd /sys/class/gpio
ls
echo $GPIO > export
ls

Notice on the first ls that gpio22 doesn't exist, but does after you export GPIO 22 to user space.

cd /sys/class/gpio/gpio$GPIO
ls

There are files to set the direction and retrieve the current value.

echo "in" > direction
cat value

You can configure the GPIO for output and set the value as well.


echo "out" > direction
echo 1 > value

GPIO interrupts from user space

The following example uses LeopardBoard 365.

LeopardBoard 365 GPIO 0 connection

On the LeopardBoard 365, the only GPIO I could find that was usable for interrupt input is GPIO0, also called CMOS_TRIGGER in the schematics. In looking at the schematics resistor R12 is not loaded and one of the pads connects to CMOS_TRIGGER. This R12 pad is the one closest to R11. If you hold the LeopardBoard 365 with the SD card slot facing you and rotate the board until the SD card slot is on the bottom edge, the the R12 pads are to the right of J6 and to the left of the SD card slot upper left corner.

Using poll() to monitor for GPIO 0 change

The gpio-int-test.c program shows one way of using the sysfs file /sys/class/gpio/gpio0/value to block program execution using poll() until the input level on GPIO0 changes. The tricky part was figuring out to use POLLPRI instead of POLLIN as the event to monitor. You must have GPIO support in sysfs for this program to work (or you will not see the /sys/class/gpio directory).


The gpio-int-test.c program uses poll() to wake up every 3 seconds (using poll() timeout mechanism) at which time it prints a period. The poll() function is also watching for input from stdin and for an interrupt from GPIO 0.

Here is an example output. I started gpio-int to watch GPIO 0. I waited around 12 seconds (4 timeout periods), then pressed the letter 'a' twice followed by enter key. Then I shorted the haywire to 3.3V that is accessible on pin 5 on the JTAG connector. JTAG pin 5 is across from the JTAG missing pin). I exited the program using cntl-C.

/root # gpio-int 0 

....aa

poll() stdin read 0xA61

poll() stdin read 0xA61

poll() stdin read 0xA0A
..
poll() GPIO 0 interrupt occurred (len 0)

poll() GPIO 0 interrupt occurred (len 0)

poll() GPIO 0 interrupt occurred (len 0)

poll() GPIO 0 interrupt occurred (len 0)
..^C

Viewing GPIO Configuration

For some SoCs (like TI's DM365) you can use debugfs to view the current GPIO configuration. You may also be able to use debugfs to see if the GPIO pin is multiplex as a GPIO or is dedicated to some other function.

Configure the kernel to enable debugfs:

Symbol: DEBUG_FS [=y]
   Prompt: Debug Filesystem
     Defined at lib/Kconfig.debug:77
     Depends on: SYSFS     
     Location:
       -> Kernel configuration
         -> Kernel hacking         

Boot the target hardware and mount debugfs:

mount -t debugfs none /sys/kernel/debug

Dump the GPIO configuration.

cat /sys/kernel/debug/gpio

Dump the pin multiplexing configuration.


cat /sys/kernel/debug/omap_mux/board 

An example output is:

/ # mount -t debugfs none /sys/kernel/debug
/ # cat /sys/kernel/debug/gpio
GPIOs 0-31, DaVinci:
 gpio-22  (sysfs               ) in  lo
 gpio-23  (sysfs               ) in  lo
 gpio-24  (sysfs               ) in  lo
 gpio-25  (sysfs               ) in  lo

GPIOs 32-63, DaVinci:
 gpio-57  (sysfs               ) out lo

GPIOs 64-95, DaVinci:
 gpio-66  (usb                 ) out lo

GPIOs 96-103, DaVinci: