gpio在Linux系统的配置是通过文件来完成的,文件在/sys/class/gpio下。首先cd /sys/class/gpio。用ls查看文件
export是用来使能端口的文件
unexport是用来失效端口的文件
gpiochip0用来记录GPIOA相关32位寄存器信息
gpiochip32用来记录GPIOB相关32位寄存器信息
gpiochip64用来记录GPIOC相关32位寄存器信息
gpiochip96用来记录GPIOD相关32位寄存器信息
gpiochip128用来记录GPIOE相关32位寄存器信息
在/sys/class/gpio文件夹下通过 echo [端口编号]>export命令使能端口
例如:echo 58 > export 此时创建一个gpio58的文件夹
gpio的输入输出性质是由direction来控制的
在gpio58文件夹下通过 echo [动作] > direction来控制 动作可以接受四个参数:in out high low
in、out用于指定输入输出
high、low 将端口设置成高电平或低电平,电平高低是输出才能配置的属性
电平设置还可以通过 echo [电平] > direction 电平可以接受两个参数 0(低电平)或1(高电平)
gpio 的编码是由 GPIOx0 的编号加上目的端口编号得到的,因而,58 是由
nxp.gpio.1 的首个端口编码 32 加上 26 得到的,也就是 GPIOB0 加上了 26 偏移量,即 GPIOB26 num=(n-1)*32+m
其中num是GPIO的编号,n是第几组gpio,m是当前的gpio的序号。经过计算PC23的GPIO编号为87
点亮LED步骤
#1、用命令行点亮
cd /sys/class/gpio
echo 58 > export
cd /gpio58
echo out > direction
echo 1 > direction
#2、用sysfs文件系统的方式
如果要用在C程序中,也可以分为以下几步:
第一步:在/sys/class/gpio/生成gpio相关的文件夹
第二步:设置gpio输入输出方向
第三步:写gpio的值
程序如下:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
int gpio_export(int pin) //打开对应的端口 开发板上 gpiob27 对应59
{
char buffer[64];
int len;
int fd;
fd = open("/sys/class/gpio/export", O_WRONLY);
if (fd < 0)
{
//MSG("Failed to open export for writing!\n");
printf("Failed to open export for writing!\n");
return(-1);
}
len = snprintf(buffer, sizeof(buffer), "%d", pin);
if (write(fd, buffer, len) < 0)
{
// MSG("Failed to export gpio!");
printf("Failed to export gpio!");
return -1;
}
close(fd);
return 0;
}
int gpio_direction(int pin, int dir)
{
static const char dir_str[] = "in\0out";
char path[64];
int fd;
snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/direction", pin);
fd = open(path, O_WRONLY);
if (fd < 0)
{
printf("Failed to open gpio direction for writing!\n");
return -1;
}
if (write(fd, &dir_str[dir == 0 ? 0 : 3], dir == 0 ? 2 : 3) < 0)
{
printf("Failed to set direction!\n");
return -1;
}
close(fd);
return 0;
}
int gpio_write(int pin, int value)
{
static const char values_str[] = "01";
char path[64];
int fd;
snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin);
fd = open(path, O_WRONLY);
if (fd < 0)
{
printf("Failed to open gpio value for writing!\n");
return -1;
}
if (write(fd, &values_str[value == 0 ? 0 : 1], 1) < 0)
{
printf("Failed to write value!\n");
return -1;
}
close(fd);
return 0;
}
int gpio_read(int pin)
{
char path[64];
char value_str[3];
int fd;
snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin);
fd = open(path, O_RDONLY);
if (fd < 0)
{
printf("Failed to open gpio value for reading!\n");
return -1;
}
if (read(fd, value_str, 3) < 0)
{
printf("Failed to read value!\n");
return -1;
}
close(fd);
return (atoi(value_str));
}
int main()
{
// gpio_export(71); //参考原理图计算对应的编号
//gpio_direction(71, 1); // 输入、输出设置 输出1
//gpio_write(71,1 ); //默认输出为低电平
gpio_export(58); //参考原理图计算对应的编号
gpio_direction(58, 1); // 输入、输出设置 输出1
gpio_write(58, 1); //默认输出为低电平
usleep(1000);
// gpio_write(58, 0); //默认输出为低电平
//gpio_export(58); //参考原理图计算对应的编号
//gpio_direction(58, 1); // 输入、输出设置 输出1
//gpio_write(58, 1); //默认输出为低电平
}