I try to use GPIO interrupt by checking the /sys/class/gpio/gpioN/value.
But the poll() function which checks the interrupt event returns the abnormal value.
First I set the gpioN to proper states in the init.rc
- write /sys/class/gpio/export "N"
- write /sys/class/gpio/gpioN/active_low "1"
- write /sys/class/gpio/gpioN/direction "in"
- write /sys/class/gpio/gpioN/edge "falling"
And the code checking the interrupt event is like the following.
jint Java_kr_iges_wallpad_gpiotest_MainActivity_GPIORead(JNIEnv *env, jobject obj, jchar port)
{
int fd, ret;
struct pollfd fdset[1];
char buf[32];
if ((fd = gpio_fd_open(port)) == -1 ){
LOGE("Read Port open error");
return -1;
}
gpio_poll_fd.fd = fd;
gpio_poll_fd.events = POLLPRI;
gpio_poll_fd.revents = 0;
while (1) {
ret = poll(&gpio_poll_fd, 1, 3000);
if (ret > 0) {
if ((gpio_poll_fd.revents & POLLPRI) == POLLPRI){
LOGD("GPIO interrupt!");
ret = 0;
break;
} else {
LOGW("No GPIO interrupt detected, weird.");
}
} else if (ret == 0) {
LOGE("poll call timed out, should not be possible!");
} else {
LOGE("poll call failed - %s.", strerror(errno));
break;
}
}
close(fd);
return ret;
}
int gpio_fd_open(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), GPIO_PATH "/gpio%d/value", gpio);
fd = open(buf, O_RDONLY | O_NONBLOCK );
if (fd < 0) {
perror("gpio/fd_open");
}
return fd;
}
The problem is that gpio_poll_fd.revents of poll() is always POLLPRI( always GPIO is Interrupted).
The /sys/class/gpio/gpioN/value is steadily changed according to GPIO input.
How can I resolve this problem?
Thanks.
tools/gpioin the kernel. - Arnout