GPIO Interface and OPeration method


GPIO stands for “General Purpose Input/Output”,it includes input, output, reuse function properties. Developers often use GPIO in development of hardware drivers in order to control the peripheral hardware.

ISVP uses Linux standard GPIOLIB's interface. GPIOLIB provides a unified GPIO application/release/setup/access interface, according to GPIOLIB's settings,it needs to be called in Kernel space. If it is User space, GPIO is needed. Two ways of doing so:

1. GPIOLIB

2.1 Header files and API

GPIOLIB's header file:include/linux/gpio.h

Drivers' codes:

#include <linux/gpio.h>

Definition of API in header fileinclude/asm-generic/gpio.h:

int gpio_request(unsigned gpio, const char *label);
void gpio_free(unsigned gpio);
int gpio_direction_input(unsigned gpio);
int gpio_direction_output(unsigned gpio, int value);
int gpio_get_value(unsigned int gpio);
void gpio_set_value(unsigned int gpio, int value);
int gpio_to_irq(unsigned int gpio);

and so on. Refer to kernel/Documentation/gpio.txt for more informations.

2.2 Codes

GPIOLIB is the Linux kernel's standard GPIO driver. You might see such name in: Software I2C,Fixed regulator,interrupts, ...

2. sysfs GPIO

sysfs gpio is the Linux standard user space operation GPIO interface. Users can use the command line or apply directly the GPIO input/output , high and low level properties and so on... In general, GPIO debugging or simple GPIO applications(such as IR-Cut operation), can be quickly developed through the sysfs gpio interface.

2.1 kernel options

In the kernel opensource's directory run the command:$ make menuconfig in order to access the configuration interface, then select the following options:

Device Drivers  --->
      -*- GPIO Support  --->
           [*]   /sys/class/gpio/... (sysfs interface)

In general, the kernel's default configuration has been checked for this option.

2.2 GPIO's application and release

Before operating on sysfs gpio, we need to apply for it first. It is worth noting that because the application of sysfs gpio will be in the kernel request_gpio. If you try to apply again for sysfs gpio, it will fail because the gpio has already been applied in the kernel.

apply/release GPIO method:

$ cd /sys/class/gpio
$ echo [gpio_num] > export         #apply for GPIO
$ echo [gpio_num] > unexport       #release GPIO

Notes:gpio_num is the gpio's number 。Formula is:

PA(n) = 0 * 32 + n
PB(n) = 1 * 32 + n
PC(n) = 2 * 32 + n
...

for example:Apply PB(10) = 1 * 32 + 10 = 42

$ echo 42 > export

After applying/sys/class/gpio's directory will have the gpio42 directory.

$ echo 42 > unexport

gpio42's directory will disapear after release. After release, gpio's status will be recovered but it will always be in applying status(level).

2.3 set input/output mode

After applying gpio, access the gpioN directory,for example:gpio42,then proceed as follows:

$ echo out > direction             #set PB10 as an output mode
$ echo in > direction              #set PB10 as an input mode

2.4 set effective level

gpioN's directory has an activelow node,represents the current gpio of a finite level, by default it is 0, which means gpio is low when the input/output value is 0, gpio is high when input/output is 1. Similarly, gpio is high when activelow is 1,or input/output value is 0,and gpio is low when input/output value is 1. We can say that gpio's level = value ^active_low.

$ echo 0 > active_low       #value is 0,represents low level. value is 0, high.
$ echo 1 > active_low       #value is 1,represents low level. value is 0, high.

2.5 input/output

gpioN's directory has a value node,representing gpioN's level:if gpio is on input mode,then read the value's content XOR activelow (which will determine the GPIO's level); if gpio is on output mode,then set the gpio's level (value's content XOR activelow).

$ cat value                 #read level (input mode)
$ echo 0 > value            #set level (output mode)