select(), poll() or epoll() ? for sysfs attribute
Asked Answered
B

2

8

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 :)

Bigamy answered 22/9, 2012 at 7:53 Comment(0)
A
3

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.

Assignat answered 22/9, 2012 at 8:5 Comment(1)
thanks @tony-the-lion, It means I have to poll for edge file or value file ?Bigamy
A
1

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
}
Aloisius answered 23/9, 2012 at 19:7 Comment(4)
Note that not all GPIOs are interrupt-capable. If the GPIO you're working with doesn't support interrupts (and this will depend on the exact CPU/SoC you're using), you will need to read it periodically.Polyglot
There are no doubt cases where this is true - especially when the GPIO line is implemented with an I2C peripheral or audio codec. However, in that case, you'd never be calling select() pselect(), poll() or epoll() on them. [poll() and its brethren, don't in fact poll at all]Aloisius
Hello @Marko Thank you so much for the reply. Can you please give me a sample code for select which you have mentioned above ? actually how I am doing is when I do echo 1 > file_name, then the notification should print. It will be very helpful.Bigamy
@JeshwanthKumarNK I not entirely sure what more you need than that above. I'm slightly confused why you're talking about writing to the value file in the comment above, yet asked about using select() to detect a change in the line/Aloisius

© 2022 - 2024 — McMap. All rights reserved.