GPIO stands for General-Purpose Input/Output, which is a general-purpose pin that can be dynamically configured and controlled during software operation. On the RK platform, except for some pins with special purposes (such as DDR, MIPI, etc.), other pins, if not configured for multiplexing, are GPIO by default. The GPIO driver of RK provides the standard GPIO interface under Linux. There is a set of sysfs nodes for user-mode GPIO operations under Linux. The application layer mainly implements GPIO programming by operating these nodes.
The following is an introduction to the operation of GPIO1_A4 and GPIO1_A7. The positions of these two GPIOs are as follows
Short these two pins during testing, as shown in the figure below
The nodes below “/sys/class/gpio” require root privileges to operate. Using the command line or a program compiled in C language to operate gpio requires root privileges. If you are using ssh or LX terminal, execute the following command to obtain root privileges first.
sudo su
Under the general Linux framework, all GPIOs are numbered. For the chips on the RK platform, the numbering is calculated as follows:
(gpio controller number - 0)*32+(port number - ‘A’)*8+index number
GPIO1_A4 has a controller number of 1, a port number of A, and an index number of 4, so the number is
(1-0) * 32 + 0 * 8 + 4 = 36
Similarly, GPIO1_A7 has a number of 39
Set GPIO36 and GPIO39 to user mode operation
echo 36 > /sys/class/gpio/export
echo 39 > /sys/class/gpio/export
Set GPIO36 to output Set GPIO39 to input
echo out > /sys/class/gpio/gpio36/direction
echo in > /sys/class/gpio/gpio39/direction
When GPIO36 is output, set the level
When GPIO39 is input, read its level, 1 is high, 0 is low
GPIO36 outputs high level, read GPIO39 level
echo 1 > /sys/class/gpio/gpio36/value
cat /sys/class/gpio/gpio39/value
GPIO36 outputs low level, read GPIO39 level
echo 0 > /sys/class/gpio/gpio36/value
cat /sys/class/gpio/gpio39/value
Cancel GPIO36 GPIO39 user mode operation
echo 36 > /sys/class/gpio/unexport
echo 39 > /sys/class/gpio/unexport
The result of the above operation process is as follows
By connecting to the libperipheral_api.a static library, you can use C language to call the following interface 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 number
* @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 number
* @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 number
* @param level: level 0 - low 1- high
* @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 number
* @param level: level 0 - low 1- high
* @return equal to 0 - success less than 0 - Failed
*/
int user_gpio_get_level(int pin_num, unsigned char *level);
/**
* @name: user_gpio_set_direction
* @description: Set gpio direction
* @param pin_num: pin number
* @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 number
* @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 test demo source code test.c in the same path, and the compilation command is as follows
aarch64-none-linux-gnu-gcc test.c peripheral_api.a -I. -o gpiotest
The running results are as follows