Documentation:Linux/GPIO

From AVRFreaks Wiki

Jump to: navigation, search

Contents

[edit] Interfaces explained

There are 2 main ways which can be used to talk to gpio pins on an AVR32 from userspace:

  • gpio-dev is an AVR32-specific interface, shipped only in .atmel kernels. You can change the state of a number of GPIOs at one time and you can receive pin change notifications by means of a blocking read. It can only deal with gpio inside the AVR32, not on an I/O expander.
  • gpio-sysfs is a user interface to the in kernel gpio framework. As such you have the same interface no matter what platform you're developing for. It deals with pins strictly one at a time. It is available in all mainline kernels from 2.6.27 onwards. At the time of writing it does not have support for pin change notification but patches to implement this exist, just message Squidgit and he'll get them to you. gpio-sysfs also doesn't care whether the gpios are built in to the AVR32 or whether they're on an external gpio expander, they're all interfaced the same way.

If you're inside the kernel then you've got one option:

  • gpio framework. This is the name of the framework through which gpios are accessed from within the kernel. gpio-sysfs is built on top of this for userspace access.

[edit] gpio-dev

Many others have shown how to use gpio-dev, I won't repeat their work here.

[edit] gpio-sysfs

gpio-sysfs is the preferred method of gpio interfacing from userspace. Use this unless you absolutely need to update multiple gpios simultaniously or you must use a kernel version before 2.6.27. The full documentation is alongside the gpio-framework documentation, here.

[edit] File structure

The control files are all contained in

#: ls/sys/class/gpio
export       gpiochip0    gpiochip32   gpiochip96
gpiochip128  gpiochip64   unexport

Initially all that is in this folder are gpiochipN folders. There is one of these for each chip which provides gpio pins. On an AT32AP7000 for example, you have 4 gpiochipN folders corresponding to Ports A, B, C, D and E. Note that inside the kernel, Porta A through E and actually called pio0 through pio4 respectively. You can find out which gpiochip folder is which by examinging the "label" file in each one of those folders. For example gpiochip32 corresponds to pio1/PortB:

#: cat /sys/class/gpio/gpiochip32/label
pio1

For any gpios which have been exported for userspace access, you'll also see a gpioN folder where N is the number of the gpio. More on this shortly.

[edit] GPIO Numbers

To get access to a gpio, you must find the gpio number. This is done by finding the base number of the chip to which that gpio belongs then adding the number of the pin on that chip. For example, PC8 is the Port C base plus 8.

Each base can be found by looking in the "base" file of the corresponding gpiochipN folder. It's also the number at the end of the folder name. So in this example, we're looking for Port C, called pio2. This label is found in the gpiochip64 folder and as such the base number is 64. We want pin 8 so 8 + 64 is 72, this is our gpio number.

NOTE: This may sound complex but for chips which are always attached, like ones built in to the AVR32 chip, the numbers stay the same. You can find the gpio number once and use it for all time.

If you have any I/O expanders connected to, eg, the SPI bus, they'll have their own gpiochip folders here. The base numbers for these detachable expanders may change so you should find the base number and recalculate the gpio number each time the board starts up or the adapter is plugged in.

[edit] Getting access to a GPIO

Some times the kernel has already given you access to a gpio. If so you'll see gpioN folders in /sys/class/gpio where N is the gpio number. If you don't have this folder already, you need to create it. Once you've got the GPIO number, you request it from the kernel. Do this by writing the gpio number to the export file like

#: echo 72 > /sys/class/gpio/export

Once you've done this you'll see a folder, /sys/class/gpio/gpio72. In this folder you'll see the attributes needed to control the gpio.

#: ls /sys/class/gpio/gpio72
device         power          uevent
direction      subsystem      value

Out of all of these, you only need to pay attention to "value" and "direction".

If you don't need control of the gpio anymore, you can unexport it

#: echo 72 > /sys/class/gpio/unexport

[edit] Getting and Setting Direction

To set the direction, write one of "in", "out", "high" or "low" to your gpio's direction attribute. "low" and "out" have the same effect, to change the pin to an output which is initially low. "high" changes the pin to an output which is initially high. "in" changes the pin to an input.

This file will read as "in" or "out" so you can tell the direction.

#: echo "high" > /sys/class/gpio/gpio72/direction
#: cat /sys/class/gpio/gpio72/direction
out

[edit] Getting and Setting State

To set an output high or low, write a '1' or '0' to the value attribute. To read the value, just read this file.

[edit] Putting it all together

Right, lets put it all together and get access to PB2, set it to an initially high output, check that it has worked, set the output low then change it to an input and read the state.

#: cat /sys/class/gpio/gpiochip0/label
pio0
#: cat /sys/class/gpio/gpiochip32/label
pio1
#: cat /sys/class/gpio/gpiochip32/base
32
So we've found that Port B (pio1) is gpiochip32, the base is 32. We want pin 2 of this so our gpio number is 34
#: echo 34 > /sys/class/gpio/export
#: echo "high" > /sys/class/gpio/gpio34/direction
#: cat /sys/class/gpio/gpio34/direction
out
#: cat /sys/class/gpio/gpio34/value
1
#: echo 0 > /sys/class/gpio/gpio34/value
#: cat /sys/class/gpio/gpio34/value
0
#: echo "in" > /sys/class/gpio/gpio34/direction
#: cat /sys/class/gpio/gpio34/value
[some value]

Done!

[edit] gpio framework

The GPIO framework documentation in the Linux kernel is quite complete. View it here. Remember this is only useful if you're coding inside the kernel.

Personal tools