通過sysfs方式控制GPIO,先訪問/sys/class/gpio目錄,向export文件寫入GPIO編號,使得該GPIO的操作接口從內核空間暴露到用戶空間,GPIO的操作接口包括direction和value等,direction控制GPIO方向,而value可控制GPIO輸出或獲得GPIO輸入。文件IO方式操作GPIO,使用到了4個函數open、close、read、write。
首先,看看系統中有沒有“/sys/class/gpio”這個文件夾。如果沒有請在編譯內核的時候加入 Device Drivers-> GPIO Support ->/sys/class/gpio/… (sysfs interface)。
/sys/class/gpio 的使用說明:
gpio_operation 通過/sys/文件接口操作IO端口 GPIO到文件系統的映射
◇ 控制GPIO的目錄位於/sys/class/gpio
◇ /sys/class/gpio/export文件用於通知系統需要導出控制的GPIO引腳編號
◇ /sys/class/gpio/unexport 用於通知系統取消導出
◇ /sys/class/gpio/gpiochipX目錄保存系統中GPIO寄存器的信息,包括每個寄存器控制引腳的起始編號base,寄存器名稱,引腳總數 導出一個引腳的操作步驟
◇ 首先計算此引腳編號,引腳編號 = 控制引腳的寄存器基數 + 控制引腳寄存器位數
◇ 向/sys/class/gpio/export寫入此編號,比如12號引腳,在shell中可以通過以下命令實現,命令成功後生成/sys/class/gpio/gpio12目錄,如果沒有出現相應的目錄,說明此引腳不可導出
◇ direction文件,定義輸入輸入方向,可以通過下面命令定義為輸出。direction接受的參數:in, out, high, low。high/low同時設置方向為輸出,並將value設置為相應的1/0
◇ value文件是端口的數值,為1或0
幾個例子:
1. 導出
/sys/class/gpio# echo 44 > export
2. 設置方向
/sys/class/gpio/gpio44# echo out > direction
3. 查看方向
/sys/class/gpio/gpio44# cat direction
4. 設置輸出
/sys/class/gpio/gpio44# echo 1 > value
5. 查看輸出值
/sys/class/gpio/gpio44# cat value
6. 取消導出
/sys/class/gpio# echo 44 > unexport
文件讀寫例程:
#include stdlib.h
#include stdio.h
#include string.h
#include unistd.h
#include fcntl.h //define O_WRONLY and O_RDONLY
//芯片復位引腳: P1_16
#define SYSFS_GPIO_EXPORT "/sys/class/gpio/export"
#define SYSFS_GPIO_RST_PIN_VAL "48"
#define SYSFS_GPIO_RST_DIR "/sys/class/gpio/gpio48/direction"
#define SYSFS_GPIO_RST_DIR_VAL "OUT"
#define SYSFS_GPIO_RST_VAL "/sys/class/gpio/gpio48/value"
#define SYSFS_GPIO_RST_VAL_H "1"
#define SYSFS_GPIO_RST_VAL_L "0"
int main()
{
int fd;
//打開端口/sys/class/gpio# echo 48 > export
fd = open(SYSFS_GPIO_EXPORT, O_WRONLY);
if(fd == -1)
{
printf("ERR: Radio hard reset pin open error.\n");
return EXIT_FAILURE;
}
write(fd, SYSFS_GPIO_RST_PIN_VAL ,sizeof(SYSFS_GPIO_RST_PIN_VAL));
close(fd);
//設置端口方向/sys/class/gpio/gpio48# echo out > direction
fd = open(SYSFS_GPIO_RST_DIR, O_WRONLY);
if(fd == -1)
{
printf("ERR: Radio hard reset pin direction open error.\n");
return EXIT_FAILURE;
}
write(fd, SYSFS_GPIO_RST_DIR_VAL, sizeof(SYSFS_GPIO_RST_DIR_VAL));
close(fd);
//輸出復位信號: 拉高>100ns
fd = open(SYSFS_GPIO_RST_VAL, O_RDWR);
if(fd == -1)
{
printf("ERR: Radio hard reset pin value open error.\n");
return EXIT_FAILURE;
}
while(1)
{
write(fd, SYSFS_GPIO_RST_VAL_H, sizeof(SYSFS_GPIO_RST_VAL_H));
usleep(1000000);
write(fd, SYSFS_GPIO_RST_VAL_L, sizeof(SYSFS_GPIO_RST_VAL_L));
usleep(1000000);
}
close(fd);
printf("INFO: Radio hard reset pin value open error.\n");
return 0;
}
另外參考網上一個網友的程序,這裡做了驗證,並實現中斷檢測函數。如下:
#include stdlib.h
#include stdio.h
#include string.h
#include unistd.h
#include fcntl.h
#include poll.h
#define MSG(args...) printf(args)
//函數聲明
static int gpio_export(int pin);
static int gpio_unexport(int pin);
static int gpio_direction(int pin, int dir);
static int gpio_write(int pin, int value);
static int gpio_read(int pin);
static int gpio_export(int pin)
{
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");
return(-1);
}
len = snprintf(buffer, sizeof(buffer), "%d", pin);
if (write(fd, buffer, len) < 0) {
MSG("Failed to export gpio!");
return -1;
}
close(fd);
return 0;
}
static int gpio_unexport(int pin)
{
char buffer[64];
int len;
int fd;
fd = open("/sys/class/gpio/unexport", O_WRONLY);
if (fd < 0) {
MSG("Failed to open unexport for writing!\n");
return -1;
}
len = snprintf(buffer, sizeof(buffer), "%d", pin);
if (write(fd, buffer, len) < 0) {
MSG("Failed to unexport gpio!");
return -1;
}
close(fd);
return 0;
}
//dir: 0-->IN, 1-->OUT
static 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) {
MSG("Failed to open gpio direction for writing!\n");
return -1;
}
if (write(fd, &dir_str[dir == 0 ? 0 : 3], dir == 0 ? 2 : 3) < 0) {
MSG("Failed to set direction!\n");
return -1;
}
close(fd);
return 0;
}
//value: 0-->LOW, 1-->HIGH
static 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) {
MSG("Failed to open gpio value for writing!\n");
return -1;
}
if (write(fd, &values_str[value == 0 ? 0 : 1], 1) < 0) {
MSG("Failed to write value!\n");
return -1;
}
close(fd);
return 0;
}
static 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) {
MSG("Failed to open gpio value for reading!\n");
return -1;
}
if (read(fd, value_str, 3) < 0) {
MSG("Failed to read value!\n");
return -1;
}
close(fd);
return (atoi(value_str));
}
// none表示引腳為輸入,��是中斷引腳
// rising表示引腳為中斷輸入,上升沿觸發
// falling表示引腳為中斷輸入,下降沿觸發
// both表示引腳為中斷輸入,邊沿觸發
// 0-->none, 1-->rising, 2-->falling, 3-->both
static int gpio_edge(int pin, int edge)
{
const char dir_str[] = "none\0rising\0falling\0both";
char ptr;
char path[64];
int fd;
switch(edge){
case 0:
ptr = 0;
break;
case 1:
ptr = 5;
break;
case 2:
ptr = 12;
break;
case 3:
ptr = 20;
break;
default:
ptr = 0;
}
snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/edge", pin);
fd = open(path, O_WRONLY);
if (fd < 0) {
MSG("Failed to open gpio edge for writing!\n");
return -1;
}
if (write(fd, &dir_str[ptr], strlen(&dir_str[ptr])) < 0) {
MSG("Failed to set edge!\n");
return -1;
}
close(fd);
return 0;
}
//GPIO1_17
int main()
{
int gpio_fd, ret;
struct pollfd fds[1];
char buff[10];
unsigned char cnt = 0;
//LED引腳初始化
gpio_export(115);
gpio_direction(115, 1);
gpio_write(115, 0);
//按鍵引腳初始化
gpio_export(49);
gpio_direction(49, 0);
gpio_edge(49,1);
gpio_fd = open("/sys/class/gpio/gpio49/value",O_RDONLY);
if(gpio_fd < 0){
MSG("Failed to open value!\n");
return -1;
}
fds[0].fd = gpio_fd;
fds[0].events = POLLPRI;
ret = read(gpio_fd,buff,10);
if( ret == -1 )
MSG("read\n");
while(1){
ret = poll(fds,1,0);
if( ret == -1 )
MSG("poll\n");
if( fds[0].revents & POLLPRI){
ret = lseek(gpio_fd,0,SEEK_SET);
if( ret == -1 )
MSG("lseek\n");
ret = read(gpio_fd,buff,10);
if( ret == -1 )
MSG("read\n");
gpio_write(115, cnt++%2);
}
usleep(100000);
}
return 0;
}