Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select(), poll() or epoll() ? for sysfs attribute

I am working with gpio interrupts. I have a file in "/sys/class/gpio/gpio38/value". I want a notification whenever there is a change in attribute value. So how can I achieve this in user-space. As I have already collected information, I can use select(), poll() or epoll(). So which is correct for this application ? Or please suggest me if I can use /proc/irq or something. Thanks :)

like image 263
duslabo Avatar asked Sep 22 '12 07:09

duslabo


2 Answers

I have found something here that may be of help:

GPIO signals have paths like /sys/class/gpio/gpio42/ (for GPIO #42) and have the following read/write attributes:

"value" ... reads as either 0 (low) or 1 (high). If the GPIO is configured as an output, this value may be written; any nonzero value is treated as high.

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. If you use select(2), set the file descriptor in exceptfds. After poll(2) returns, either lseek(2) to the beginning of the sysfs file and read the new value or close the file and re-open it to read the value.

Although it says it's for "gpio42", I'm guessing this may apply to your case to. If it doesn't, make a comment in my answer.

like image 194
Tony The Lion Avatar answered Oct 24 '22 05:10

Tony The Lion


You can use any of them. The point here is that you open the sysfs file for the GPIO line's value (e.g. /sys/class/gpio/gpio42/value and then block on it.

Changes in line state are signalled as an exception condition rather than a write (as might be intuitive).

In the case of select:

fd_set exceptfds;
int    res;    

FD_ZERO(&exceptfds);
FD_SET(gpioFileDesc, &exceptfds);

res = select(gpioFileDesc+1, 
             NULL,               // readfds - not needed
             NULL,               // writefds - not needed
             &exceptfds,
             NULL);              // timeout (never)

if (res > 0 && FD_ISSET(gpioFileDesc, &exceptfds))
{
     // GPIO line changed
}
like image 1
marko Avatar answered Oct 24 '22 06:10

marko