poll() returns both, POLLPRI & POLLERR

独自空忆成欢 提交于 2019-12-01 05:53:25

问题


I started to get in C programming with Linux and embedded systems (router hardware and openwrt). I have got interupts on GPIOs enabled, using poll works ... nearly.

I can use poll() and if i press the button to trigger the interrupt, poll() returns with a value > 0. So far so good. Now i try to use poll() on several GPIOs simultaniosly and therefor want to analyze the revents of every potential interrupt source. Allthough the interrupt seems to work, i get POLLPRI & POLLERR back and i do not understand why. Reducing the pollfd structure to 1 entry does not change anything.

char value;

int fd_btn1 = open("/sys/class/gpio/gpio14/value", O_RDONLY);
int fd_left = open("/sys/class/gpio/gpio12/value", O_RDONLY);
int fd_right = open("/sys/class/gpio/gpio13/value", O_RDONLY);

struct pollfd fds[3];
fds[0].fd = fd_btn1;
fds[1].fd = fd_left;
fds[2].fd = fd_right;
fds[0].events = POLLPRI;
fds[1].events = POLLPRI;
fds[2].events = POLLPRI;

read(fd_btn1, &value, 1);
read(fd_left, &value, 1);
read(fd_right, &value, 1);

ret = poll(fds, 1, 10000);
//debugging purpose
printf("ret: %i - revents[0]: %i", ret, fds[0].revents);

In case button was pressed (interrupt triggered): ret=1d, revents=10d

In case nothing was pressed, both is 0d


回答1:


I've found some answer alluding to your question at: http://e2e.ti.com/support/dsp/davinci_digital_media_processors/f/716/t/182883

I just ran into the POLLERR thing you're seeing too. Turns out this is how all sysfs files work, and you're using gpio through the sysfs interface.

From the sysfs GPIO kernel document: If the pin can be configured as interrupt-generating interrupt and if it has been configured to generate interrupts (see the description of "edge"), you can poll(2) on that file and poll(2) will return whenever the interrupt was triggered. If you use poll(2), set the events POLLPRI and POLLERR. Also, if you look take a look at the kernel code in fs/sysfs/file.c, you'll see that sysfs_poll returns DEFAULT_POLLMASK | POLLERR | POLLPRI.

All that said, it does seem strange that sysfs files return POLLERR, I'm not sure why they chose to do that.



来源:https://stackoverflow.com/questions/27411013/poll-returns-both-pollpri-pollerr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!