7. GPIO Operation Guide

7.1. GPIO Preparation

  • Use the kernel released by SDK.

7.2. Operation Process

  • By default, the GPIO-related kernel modules are built into the kernel and no load commands need to be executed.

  • GPIO can be input mode or output mode by executing GPIO read or write commands under the console or by calling GPIO APIs in kernel or user space APP.

7.3. Operation Example

7.3.1. GPIO Operation Command Example

Step 1: Use the echo command in the console to export GPIO number N for manipulation:

echo N > /sys/class/gpio/export

N Indicates the number of the GPIO to be operated. GPIO number = GPIO group number + offset value.

Taking GPIO1_2 pin in the schematic diagram as an example, GPIO1 corresponds to GPIO group number 448 and offset value 2.

So the GPIO number N is 448 + 2 = 450

The corresponding group number is as below:

GPIO0 corresponds to the linux group number 480

GPIO1 corresponds to the linux group number 448

GPIO2 corresponds to the linux group number 416

GPIO3 corresponds to the linux group number 384

PWR_GPIO corresponds to the linux group number 352

After echo N > /sys/class/gpio/export, generate the directory: /sys/class/gpio/gpioN

Step 2: Use the echo command in the console to set the GPIO direction:

for input: echo in > /sys/class/gpio/gpioN/direction

for output: echo out > /sys/class/gpio/gpioN/direction

For example:

set GPIO1_2 (the number is 450) to input mode:

echo in > /sys/class/gpio/gpio450/direction

set GPIO1_2 (the number is 450)to output mode:

echo out > /sys/class/gpio/gpio450/direction

Step 3: Use the cat commands in the console to read GPIO input values or use the echo commands to set GPIO output values :

check the input value:

cat /sys/class/gpio/gpioN/value

output low:

echo 0 > /sys/class/gpio/gpioN/value

output high:

echo 1 > /sys/class/gpio/gpioN/value

Step 4: After the resource is used, run the echo command on the console to release resources:

echo N > /sys/class/gpio/unexport

Note: You can enable the sysfs debug function of GPIO by turning on the CONFIG_DEBUG_FS option, and check the base number corresponding to GPIO PIN with the following command before operation:

cat /sys/kernel/debug/gpio

7.3.2. GPIO Operation Program Example with Kernel Space

GPIO Read-Write Operation program example in kernel space:

Step 1: Register GPIO:

gpio_request(gpio_num, NULL);

gpio_num is the GPIO number to be operated on, which is equal to “GPIO Group Number + Intra-Group Offset Number”

Step 2: Set GPIO direction:

for input:gpio_direction_input(gpio_num)
for output:gpio_direction_output(gpio_num, gpio_out_val)

Step 3: View GPIO input values or set GPIO output value:

check the input value: gpio_get_value(gpio_num);
output low:gpio_set_value(gpio_num, 0);
output high:gpio_set_value(gpio_num, 1);

Step 4: Release registered GPIO number:

gpio_free(gpio_num);

GPIO interrupt operation program example with kernel mode:

Step 1: Register GPIO:

gpio_request(gpio_num, NULL);

gpio_num is the GPIO number to be operated on, which is equal to "GPIO Group Number + Intra-Group Offset Number"

Step 2: Set GPIO Direction:

gpio_direction_input(gpio_num);

For GPIO pins to be interrupt sources, the direction must be configured as input mode.

Step 3: The interrupt number corresponding to the GPIO number of the mapping operation:

irq_num = gpio_to_irq(gpio_num);

The interrupt number is the return value.

Step 4: Register interrupt:

request_irq(irq_num, gpio_dev_test_isr, irqflags, "gpio_dev_test", &gpio_irq_type))

Irqflags is a type of interrupt that needs to be registered, and the common types are:

IRQF_SHARED :Sharing Interrupt;

IRQF_TRIGGER_RISING :Rising edge triggering;

IRQF_TRIGGER_FALLING :Drop edge trigger;

IRQF_TRIGGER_HIGH :High level trigger;

IRQF_TRIGGER_LOW :Low level trigger

Step 5: Release registered interrupts and GPIO numbers at end:

free_irq(gpio_to_irq(gpio_num), &gpio_irq_type);
gpio_free(gpio_num);

7.3.3. GPIO Operation Example with User Mode

GPIO Read-Write Operation program example with user space:

Step 1: Number the GPIO as expor for manipulation:

fp = fopen("/sys/class/gpio/export", "w");
fprintf(fp, "%d", gpio_num);
fclose(fp);

gpio_num is the GPIO number to be operated on, which is equal to “GPIO Group Number + Intra-Group Offset Number”

Step 2: Set GPIO direction:

fp = fopen("/sys/class/gpio/gpio%d/direction", "rb+");
for input:fprintf(fp, "in");
for output:fprintf(fp, "out");

fclose(fp);

Step 3: View GPIO input value or set GPIO output value:

fp = fopen("/sys/class/gpio/gpio%d/direction", "rb+");
check input:fread(buf, sizeof(char), sizeof(buf) - 1, fp);
output low:
strcpy(buf, “0”);
fwrite(buf, sizeof(char), sizeof(buf) - 1, fp);
output high:
strcpy(buf, “1”);
fwrite(buf, sizeof(char), sizeof(buf) - 1, fp);

Step 4: Number the GPIO manipulated as unexport:

fp = fopen("/sys/class/gpio/unexport", "w");
fprintf(fp, "%d", gpio_num);
fclose(fp);