I am trying to read a GPIO value using whenever it changes the state.
/sys/class/gpio/gpio499/value
I have set /sys/class/gpio/gpio499/edge
to be both
I am trying to monitor the change in value using poll command in one separate thread. Here is the code snippet :
void PIN_gpio_poll(size_t gpio) //GPIO 499
{
char path[30];
char cValue;
int fd;
int ret_poll;
int ret_read;
struct pollfd pollfd;
int i;
pollfd.events = POLLPRI | POLLERR; /* look for GPIO status change. */
snprintf(path, 30, PHDRIVER_LINUX_CFG_DIR "/gpio%u/value", gpio);
fd = open(path, O_RDONLY);
if (fd == -1)
{
printf("Gpio_poll _ERROR\r\n");
}
pollfd.fd = fd;
ret_read = read(pollfd.fd, &cValue, 1); // Dummy Read to clear
while (1)
{
lseek(fd, 0, SEEK_SET);
ret_read = read(fd, &cValue, 1);
printf("Value=%c, RET READ=%d\n",cValue,ret_read);
// ret_poll = poll( &pollfd, 1, -1 );
ret_poll = poll( &pollfd, 1, 10000 ); //10sec timeout
printf("******REVENTS=%x\n",pollfd.revents);
if( ret_poll == -1 )
{
printf("Gpio_poll poll failed\r\n");
close(fd);
}else{
// if (pollfd.revents & POLLPRI )
{
lseek(fd, 0, SEEK_SET);
ret_read = read(pollfd.fd, &cValue, 1);
if(ret_read > 0)
{
printf("Cvalue = %c\n",cValue);
}
}
}
}
}
The problem I am facing is if I set events as POLLIN, poll returns immediately. This is understood because there is always data to be read in value (0 or 1) GPIO. I referred https://www.kernel.org/doc/Documentation/gpio/sysfs.txt and set events as POLLPRI | POLLERR . But in this method the poll returns only after timeout. It doesn't return when value of the GPIO is changed. Is there anything I am missing the trick here?? I have also set /sys/class/gpio/gpio499/edge
to rising, falling, but nothing seems to be working.
EDIT:
Here is the output of grep -r . /sys/class/gpio/gpio499
/sys/class/gpio/gpio499/edge:both
/sys/class/gpio/gpio499/power/control:auto
/sys/class/gpio/gpio499/power/runtime_active_time:0
grep: /sys/class/gpio/gpio499/power/autosuspend_delay_ms: Input/output error
/sys/class/gpio/gpio499/power/runtime_status:unsupported
/sys/class/gpio/gpio499/power/runtime_suspended_time:0
/sys/class/gpio/gpio499/value:1
/sys/class/gpio/gpio499/active_low:0
/sys/class/gpio/gpio499/direction:in
Note: I want to detect value from 1 to 0.