GPIO: General-Purpose Input/Output is a general-purpose pin that can be dynamically configured and controlled during software operation. On the RK platform, except for some specialized pins (e.g. DDR, MIPI, etc.), all other pins, if not configured for multiplexing, are GPIO by default, and RK's GPIO driver provides a standard GPIO interface to Linux, which has a set of sysfs nodes that operate GPIOs in user mode. The application layer programming of GPIO is mainly realized through the operation of these nodes.
The following is an example of operating GPIO1_A4 and GPIO1_A7. These two GPIOs are located as follows
Test by shorting these two pins as shown below
The nodes under "/sys/class/gpio" require root privileges to operate, and you need root privileges to operate gpio using the command line or a program compiled in C language. If you are using ssh or LX terminal, first execute the following command to get root privileges.
sudo su
In the common Linux framework, all GPIOs are numbered numerically. For RK platform chips, the numbering is calculated as follows
(gpio controller number - 0)*32+(port number - 'A')*8+index serial number
GPIO1_A4 has a controller number of 1, a port number of A, and an index sequence number of 4, so the number is
(1-0) * 32 + 0 * 8 + 4 = 36
Similarly GPIO1_A7 is numbered 39
Setting GPIO36 GPIO39 to user-state operation
echo 36 > /sys/class/gpio/export
echo 39 > /sys/class/gpio/export
Set GPIO 36 to output Set GPIO 39 to input
echo out > /sys/class/gpio/gpio36/direction
echo in > /sys/class/gpio/gpio39/direction
Setting the level when GPIO36 is an output
If GPIO39 is an input, read its level, 1 is high, 0 is low.
GPIO36 outputs a high level, reads the level of GPIO39.
echo 1 > /sys/class/gpio/gpio36/value
cat /sys/class/gpio/gpio39/value
GPIO36 outputs low, reads GPIO39 level
echo 0 > /sys/class/gpio/gpio36/value
cat /sys/class/gpio/gpio39/value
Cancel GPIO36 GPIO39 user-state operation
echo 36 > /sys/class/gpio/unexport
echo 39 > /sys/class/gpio/unexport
The result of the above operation flow is shown below
By linking to the libperipheral_api.a static library, the following interfaces can be called in C to operate GPIO
typedef enum {
PIN_DIRECTION_IN = 0,
PIN_DIRECTION_OUT = 1
} Enum_PinDirection;
/**
* @name: user_gpio_init
* @description: Configure a pin as gpio
* @param pin_num: pin num
* @return equal to 0 - Success; less than 0 - Failure
*/
int user_gpio_init(int pin_num);
/**
* @name: user_gpio_deinit
* @description: Release a pin
* @param pin_num: pin num
* @return equal to 0 - Success; less than 0 - Failure
*/
int user_gpio_deinit(int pin_num);
/**
* @name: user_gpio_set_level
* @description: Set gpio level
* @param pin_num: pin num
* @param level: 0 - low level; 1- high level
* @return equal to 0 - Success; less than 0 - Failure
*/
int user_gpio_set_level(int pin_num, unsigned char level);
/**
* @name: user_gpio_get_level
* @description: Get gpio level
* @param pin_num: pin num
* @param level: 0 - low level; 1- high level
* @return equal to 0 - Success; less than 0 - Failure
*/
int user_gpio_get_level(int pin_num, unsigned char *level);
/**
* @name: user_gpio_set_direction
* @description: Set gpio direction
* @param pin_num: pin num
* @param direction: PIN_DIRECTION_IN - input PIN_DIRECTION_OUT - output
* @return equal to 0 - Success; less than 0 - Failure
*/
int user_gpio_set_direction(int pin_num, unsigned char direction);
/**
* @name: user_gpio_get_direction
* @description: Get gpio direction
* @param pin_num: pin num
* @param direction: PIN_DIRECTION_IN - input PIN_DIRECTION_OUT - output
* @return equal to 0 - Success; less than 0 - Failure
*/
int user_gpio_get_direction(int pin_num, unsigned char *direction);
#include "peripheral_api.h"
void gpio_api_test(void)
{
unsigned char level = 0;
user_gpio_init(36);
user_gpio_init(39);
user_gpio_set_direction(36, PIN_DIRECTION_OUT);
user_gpio_set_direction(39, PIN_DIRECTION_IN);
user_gpio_set_level(36, 1);
user_gpio_get_level(39, &level);
printf("1.pin 39 level %d\n", level);
usleep(10);
user_gpio_set_level(36, 0);
user_gpio_get_level(39, &level);
printf("2.pin 39 level %d\n", level);
user_gpio_deinit(36);
user_gpio_deinit(39);
return;
}
int main()
{
gpio_api_test();
return 0;
}
Put the peripheral_api.a static library, peripheral_api.h and the test demo source code test.c into the same path, compile the command as follows
aarch64-none-linux-gnu-gcc test.c peripheral_api.a -I. -o gpiotest
The results of the run are as follows