完整的程序如下:

#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_edge(int pin, int edge); 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);
printf("%s,%d,%d\n",buffer,sizeof(buffer),len);
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; gpio_unexport(44);
gpio_unexport(45); //p8_12 init
gpio_export(44);
gpio_direction(44, 1);//output out
gpio_write(44, 1); //p8_11 init
gpio_export(45);
gpio_direction(45, 0);//input in
gpio_edge(45,2);
gpio_fd = open("/sys/class/gpio/gpio45/value",O_RDONLY);
if(gpio_fd < 0)
{
MSG("Failed to open value!\n");
return -1;
}
fds[0].fd = gpio_fd;
fds[0].events = POLLPRI; while(1)
{
ret = read(gpio_fd,buff,10);
if( ret == -1 )
MSG("read\n"); 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"); //gpio_write(44, cnt++%2);
printf("**********************************\n");
}
usleep(5);
}
return 0;
}

linux下使用文件IO监听GPIO中断的更多相关文章

  1. linux下oracle数据库服务和监听的启动停止

    oracle数据库是重量级的,其管理非常复杂,将其在linux平台上的启动和关闭步骤整理一下. 安装完毕oracle以后,需要创建oracle系统用户,并在/home/oracle下面的.bash_p ...

  2. [转] Linux下用文件IO的方式操作GPIO(/sys/class/gpio)

    点击阅读原文 一.概述 通过 sysfs 方式控制 GPIO,先访问 /sys/class/gpio 目录,向 export 文件写入 GPIO 编号,使得该 GPIO 的操作接口从内核空间暴露到用户 ...

  3. Linux下用文件IO的方式操作GPIO(/sys/class/gpio)(转)

    通过sysfs方式控制GPIO,先访问/sys/class/gpio目录,向export文件写入GPIO编号,使得该GPIO的操作接口从内核空间暴露到用户空间,GPIO的操作接口包括direction ...

  4. Linux下用文件IO的方式操作GPIO(/sys/class/gpio)

    通过sysfs方式控制GPIO,先访问/sys/class/gpio目录,向export文件写入GPIO编号,使得该GPIO的操作接口从内核空间暴露到用户空间,GPIO的操作接口包括direction ...

  5. Linux下oracle启动/关闭监听(bash:lsnrctl:command not found)

    打开终端 切换帐户 # su - Oracle 启动监听 $ lsnrctl start 关闭监听 $ lsnrctl stop 切换帐户一定要加 "-" 否则会出现:   bas ...

  6. linux 系统下开机自动启动oracle 监听和实例 (亲测有效)

    [oracle@oracle11g ~]$ dbstartORACLE_HOME_LISTNER is not SET, unable to auto-start Oracle Net Listene ...

  7. [转载]linux下core文件设置与查看

    转自:https://blog.csdn.net/dingqinghui/article/details/77855330?locationNum=9&fps=1 linux下core文件设置 ...

  8. linux 下查看文件修改时间

    linux 下查看文件修改时间 等 http://blog.sina.com.cn/s/blog_6285b04e0100f4xr.html 查看文件时间戳命令:stat awk.txtFile: ` ...

  9. Linux下Python 文件内容替换脚本

    Linux下Python 文件替换脚本 import sys,os if len(sys.argv)<=4: old_text,new_text = sys.argv[1],sys.argv[2 ...

随机推荐

  1. OSCache安装

    OSCache是一个基于web应用的组件,他的安装工作主要是对web应用进行配置,大概的步骤如下: 1. 下载.解压缩OSCachehttps://java.net/downloads/oscache ...

  2. BeautifulSoup练习

    html1="""<!DOCTYPE html><html lang="en" xmlns="http://www.w3. ...

  3. 面试题:volatile关键字的作用、原理

    在只有双重检查锁,没有volatile的懒加载单例模式中,由于指令重排序的问题,我确实不会拿到两个不同的单例了,但我会拿到“半个”单例. 而发挥神奇作用的volatile,可以当之无愧的被称为Java ...

  4. Deepin 2014.2正式版发布 - 自由·独特·前卫

    感谢 deepin 的投递 deepin致力于为全球用户提供美观易用.安全可靠的Linux系统. deepin系统使用基于HTML5技术开发的深度桌面环境,搭配深度音乐.深度影院.WPS和搜狗输入法等 ...

  5. c语言学习笔记 switch case语句为什么要加break

    先来看一个没有break的例子: int main() { int a = 1; switch (a) { case 1: printf("1"); case 2: printf( ...

  6. 2.python IP/DNS地址处理之IPy/Dnspython模块

     1.IPy模块 在IP地址规划中,涉及到计算大量的IP地址,包括网段.网络掩码.广播地址.子网数.IP类型等,即便是专业的网络人员也要进行繁琐的计算,而IPy模块提供了专门针对IPV4地址与IPV6 ...

  7. css属性position的运用

    随着web标准的规范化,网页的布局也随之千变万化.各种复杂漂亮有创意的页面布局冲 击这人们的视野,相比以前的table布局那就不是一等级的事儿.这个很大一部分功劳是css 样式的引入.而这个多样性布局 ...

  8. JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(3):常用动态代理之JDK动态代理、CGLIB动态代理

    一.动态代理的理解 动态代理的意义在于生成一个占位(又称代理对象),来代理真实对象,从而控制真实对象的访问.        先来谈谈什么是代理模式.        假设这样一个场景:你的公司是一家软件 ...

  9. How Tomcat Works(二十)

    要使用一个web应用程序,必须要将表示该应用程序的Context实例部署到一个host实例中.在tomcat中,context实例可以用war文件的形式来部署,也可以将整个web应用拷贝到Tomcat ...

  10. 原型(Prototype)模式

    一. 原型(Prototype)模式 原型模式的用意是:通过给出一个原型对象来指明所要创建的对象类型,然后用复制这个原型对象的办法创建出更多的同类型对象. 从孙大圣的手段谈起 孙悟空在与黄风怪的战斗中 ...