*.版权声明:本篇文章为原创,可随意转载,转载请注明出处,谢谢!另我创建一个QQ群82642304,欢迎加入!
691 "value" ... reads as either 0 (low) or 1 (high). If the GPIO
692 is configured as an output, this value may be written;
693 any nonzero value is treated as high.
694
695 If the pin can be configured as interrupt-generating interrupt
696 and if it has been configured to generate interrupts (see the
697 description of "edge"), you can poll(2) on that file and
698 poll(2) will return whenever the interrupt was triggered. If
699 you use poll(2), set the events POLLPRI and POLLERR. If you
700 use select(2), set the file descriptor in exceptfds. After
701 poll(2) returns, either lseek(2) to the beginning of the sysfs
702 file and read the new value or close the file and re-open it
703 to read the value.
704
705 "edge" ... reads as either "none", "rising", "falling", or
706 "both". Write these strings to select the signal edge(s)
707 that will make poll(2) on the "value" file return.
708
709 This file exists only if the pin can be configured as an
710 interrupt generating input pin.
就是说可以通过读取/sys/class/gpio/gpioN/value的值来获取中断。
5. 但是不是简单的read,而是通过epoll、poll、select等这些IO复用函数来控制,对于epoll或者poll,需要监听的事件是EPOLLPRI或POLLPRI,而不是EPOLLIN或POLLIN,对于select,需要将文件描述符放在exceptfds中,而且文件描述符被触发时需要通过调用read读取数据,还要通过lseek将文件流指针置回文件开头。
对于epoll来说,伪代码如下
...
#include
...
int main(int argc,char * argv[]){
struct epoll_event evd;
struct epoll_event * events;
...
int epollfd = epoll_create(10);
...
events = calloc (10, sizeof(struct epoll_event));
evd.data.fd = fd; //fd 即为open /sys/class/gpio/gpioN/value返回的句柄
evd.events = EPOLLPRI;
epoll_ctl(epollfd,EPOLL_CTL_ADD,fd,&evd);
while (1) {
n = epoll_wait(epollfd,events,10,-1);
for (i = 0;i < n;i++) {
if (events[i].events & EPOLLPRI) {
memset(buf,0x00,sizeof(buf));
read(events[i].data.fd,buf,sizeof(buf));
lseek(events[i].data.fd,0,SEEK_SET);
//do yourself
}
}
}
}
对于poll,伪代码如下
...
#include
...
int main(int argc,char* argv[]){
struct pollfd fdset;
unsigned char buf[128];
while (1) {
memset(&fdset,0x00,sizeof(struct pollfd));
fdset.fd = fd; //fd 即为open /sys/class/gpio/gpioN/value返回的句柄
fdset.events = POLLPRI;
poll(&fdset,1,3000);
if (fdset.events & POLLPRI) {
read(fdset.fd,buf,sizeof(buf));
lseek(fdset.fd,0,SEEK_SET);
//do yourself
}
}
}
select的话我就不写了,一样的做法。
另,我实际测试,使用epoll或者poll时监听事件(POLLIN | POLLET)也是可以的。