GPIO

General Purpose Input/Output (GPIO) is a generic pin on a integrated circuit chip whose behavior (including whether it is an input or output pin) can be controlled / programmed by the user at run time.

=Overview= GPIO pins have no special purpose defined, and usually go unused by default. The idea is that sometimes the system integrator building a full system that uses the chip might find it useful to have a handful of additional digital control lines, and having these available from the chip can save the hassle of having to arrange additional circuitry to provide them.

A GPIO port is a group of GPIO pins (typically 8 GPIO pins) arranged in a group, and treated as a single port.

= Controlling GPIO on Olimex's A13-OLinuXino =

What do you need:

 * Kernel with CONFIG_GPIO_SUNXI=y If you set the option to m you should take care of loading the module. There was previously SUN4I_GPIO_UGLY option which is now deprecated.
 * bin2fex and fex2bin tools from sunxi-tools.

The Process
- Open a console and connect to your A13.

- Make a directory in /media: mkdir /media/nanda

- Mount the nanda there: mount /dev/nanda /media/nanda

- Copy the file /media/nanda/script.bin to your PC. This file configures the A13.

- Now we need to make it a text file so we use the bin2fex. On a linux machine go into the directory where you compiled the sunxi-tools and from there type this: ./bin2fex /path/to/script.bin > script.fex This will create a text file named script.fex in your current directory.

- Now we need to edit it with a text editor and define the pins that are going to be used for GPIO. Look for a section named "[gpio_para]" if there is no such section (probably there will not be) go to the bottom of the file and add it like this:

 [gpio_para] gpio_used = 1 gpio_num = 1 gpio_pin_1 = port:PE11<1>

gpio_used - do you want to use gpio 1-yes 0-no gpio_num - the number of gpio ports you want gpio_pin_N = PXN where N is the gpio pin number starting from 1, X is 1 for output or 0 for input GPIO PIN,PXN is the name of the pin you want to use. In this example I used pin PE11 wich is pin number 12 on the GPIO-2. The PXN names can be found here: http://linux-sunxi.org/A13-OLinuXino#Expansion_ports. TO CHECK: when I used PE11 this pin is part of the [csi0_para] so I went to [csi0_para] and made csi_used = 0 Not sure if this is needed, but I think it is.

- Now we need to make the modified fex file back to bin format so again from the directory where you compiled the sunxi-tools: ./fex2bin script.fex > script.bin

- Now put back the script.bin on the board and overwrite the old script.bin in /media/nanda

- Unmount the /media/nanda: umount /media/nanda - Reboot the A13

- Log back in and now if you did everything correct in /sys/devices/virtual/misc/sun4i-gpio/pin you will see "pe11" - If you solder a LED and a resistor to the right pin and ground (for example pin 2) http://linux-sunxi.org/images/e/e7/A13-olinuxino-brd.png and type: "echo 1 > /sys/devices/virtual/misc/sun4i-gpio/pin/pe11" the LED will light and "echo 0 > /sys/devices/virtual/misc/sun4i-gpio/pin/pe11" will turn it off.

C/C++ program
Lib              gpio_lib.h

compiler         eclipse c/c++

steps:

1)

#define PNXX  SUNXI_GPN(XX) 2)

if(SETUP_OK!=sunxi_gpio_init){ printf("Failed to initialize GPIO\n"); return -1; } 3)

if(SETUP_OK!=sunxi_gpio_set_cfgpin(PNXX,DIRECTION)){ printf("Failed to config GPIO pin\n"); return -1; } PNXX:             ex.PD01

DIRECTION:        OUTPUT,INPUT

4)

if(sunxi_gpio_output(PNXX,LEVEL)){ printf("Failed to set GPIO pin value\n"); return -1; } LEVEL:           HIGH,LOW

5)

sunxi_gpio_cleanup;

Other stuff
Olimex wrote another article on the subject at http://olimex.wordpress.com/2012/10/23/a13-olinuxino-playing-with-gpios/

= Accessing the GPIO pins through sysfs with mainline kernel = The GPIO pins can be accessed from user space using sysfs. To enabled this you need the following kernel option enabled: CONFIG_GPIO_SYSFS Device Drivers ---> GPIO Support  ---> /sys/class/gpio/... (sysfs interface)

To access a GPIO pin you first need to export it with echo XX > /sys/class/gpio/export with XX being the number of the desired pin. To obtain the correct number you have to calculate it from the pin name (like PH18) : (position of letter in alphabet - 1) * 32 + pin number E.g for PH18 this would be ( 8 - 1) * 32 + 18 = 224 + 18 = 242 (since 'h' is the 8th letter).

After you have successfully exported the pin you can access it through /sys/class/gpio/gpio*NUMBER* (in case of PH18 it's /sys/class/gpiogpio242).

With /sys/class/gpio/gpio*NUMBER*/direction you can set the pin to out or in using echo "out" > /sys/class/gpio/gpio*NUMBER*/direction and you can read/write the value with /sys/class/gpio/gpio*NUMBER*/value.

When you are done unexport the pin with echo XX > /sys/class/gpio/unexport

=See also=
 * JTAG
 * MicroSD Breakout
 * A10/PIO
 * A13/PIO

=References=

=External Links=
 * ALSA Development List
 * Linux Kernel Doc on GPIO
 * LinuxTV GPIO Pins Info
 * GPIO Tutorial
 * Access GPIO from Linux user space