GPIO operations can be performed through the sys file system interface. When the file system operates GPIO, you must first check whether the GPIO you want to operate is occupied by other drivers. If it is occupied, subsequent operations will report a busy warning.
Take operating an interface of the expansion pin as an example, and use a multimeter to measure the voltage. (ECSPI2_MOSI_3V3, GPIO5_IO11,139)
Export GPIO
root@myd-jx8mp:~# echo 139 > /sys/class/gpio/export
After the export is successful, the gpio139 directory will be generated in the /sys/class/gpio/ directory.
To set/view the GPIO direction setting output, enter the following command:
root@myd-jx8mp:~# echo out > /sys/class/gpio/gpio139/direction
Or set the input:
root@myd-jx8mp:~# echo in > /sys/class/gpio/gpio139/direction
Check gpio direction:
root@myd-jx8mp:~# cat /sys/class/gpio/gpio139/direction out
Return in represents input, and return out represents output.
Set/view the value of GPIO Set the output low:
root@myd-jx8mp:~# echo "0" > /sys/class/gpio/gpio139/value
Or to set the output high:
root@myd-jx8mp:~# echo "1" > /sys/class/gpio/gpio139/value
View the value of gpio:
root@myd-jx8mp:~# cat /sys/class/gpio/gpio139/value 1
You can see that GPIO5_IO11 outputs a high level, and then you can use a multimeter to measure the ECSPI2_MOSI_3V3 pin of the J25 expansion IO, and you can see that the voltage is about 3.3V.
Purpose of GPIO68 attribute files
file name |
path |
effect |
active_low |
/sys/class/gpio/gpio139/active_low |
It has read and write properties. It is used to determine whether the value in value is flipped - 0 does not flip, 1 flips. |
edge |
/sys/class/gpio/gpio139/edge |
Has read and write properties. Set GPIO interrupt, or detect whether an interrupt occurs. |
subsystem |
/sys/class/gpio/gpio139/subsystem |
A symbolic link pointing to the parent directory. |
value |
/sys/class/gpio/gpio139/value |
Has read and write properties. The level status of GPIO is set or read. |
direction |
/sys/class/gpio/gpio139/direction |
Has read and write properties. Used to view or set GPIO input and output |
power |
/sys/class/gpio/gpio139/power |
Related information about equipment power supply |
uevent |
/sys/class/gpio/gpio139/uevent |
Communication interface between the kernel and udev (automatic device discovery program) |
device |
/sys/class/gpio/gpio139/device |
A symbolic link pointing to the parent directory. |
In actual applications, gpio is operated by reading and writing Linux system files in C language. The sample code is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
#define DEV_PATH "/sys/class/gpio/gpio139/value" // 输入输出电平值设备
#define EXPORT_PATH "/sys/class/gpio/export" // GPIO设备导出设备
#define DIRECT_PATH "/sys/class/gpio/gpio139/direction" // GPIO输入输出控制设备
#define OUT "out"
#define IN "in"
#define GPIO "139" //gpio号
#define HIGH_LEVEL "1"
#define LOW_LEVEL "0"
int main(int argc, char ** argv)
{
static int fd_dev, fd_export, fd_dir, ret;
char buf[10], direction[4];
fd_export = open(EXPORT_PATH, O_WRONLY); // 打开GPIO设备导出设备
if(fd_export < 0) {
perror("open export:");
return -1;
}
write(fd_export, GPIO, strlen(GPIO));
fd_dev = open(DEV_PATH, O_RDWR); // 打开输入输出电平值设备
if(fd_dev < 0) {
perror("open gpio:");
return -1;
}
fd_dir = open(DIRECT_PATH, O_RDWR); // 打开GPIO输入输出控制设备
if(fd_dir < 0) {
perror("open direction:");
return -1;
}
ret = read(fd_dir, direction, sizeof(direction)); // 读取GPIO输入输出方向
if(ret < 0) {
perror("read direction:");
close(fd_export);
close(fd_dir);
close(fd_dev);
return -1;
}
printf("default direction:%s", direction);
strcpy(buf, IN);
ret = write(fd_dir, buf, strlen(IN));
if(ret < 0) {
perror("write direction:");
close(fd_export);
close(fd_dir);
close(fd_dev);
return -1;
}
ret = read(fd_dir, direction, sizeof(direction));
if(ret < 0) {
perror("read direction:");
close(fd_export);
close(fd_dir);
close(fd_dev);
return -1;
}
ret = read(fd_dev, buf, sizeof(buf)); // 读取GPIO输入电平值
if(ret < 0) {
perror("read gpio:");
close(fd_export);
close(fd_dir);
close(fd_dev);
return -1;
}
printf("current direction:%sinput level:%s", direction, buf);
close(fd_export);
close(fd_dir);
close(fd_dev);
return 0;
}