i'm writing a library for my raspberry pi 2 for gpio control.
I'm trying to test a function for read a gpio driven by interrupts with sysfs but i can't do that my function stops in poll() function.
This is my fucntion
int gpio_waitForInterrupt(GPIO_t *gpio, uint32_t timeout)
{
char buf[50] = {0};
snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/value", gpio->number);
int fd = open(buf, O_RDONLY);
if (fd == -1) return -1;
struct pollfd pfd;
memset(&pfd,0,sizeof(struct pollfd));
pfd.fd = fd;
pfd.events = POLLPRI;
char c;
int value=0;
int ret = poll(&pfd, 1, timeout);
if(ret==0)
{
errno=ETIMEDOUT;
return -1;
}else if(ret<0){
return -1;
}
if(pfd.revents&POLLPRI)
{
if(read(fd, &c, 1)<0)
{
close(fd);
return -1;
}
if(lseek(fd, 0, SEEK_SET)<0)
{
close(fd);
return -1;
}
if(c=='1') value=1;
else value=0;
gpio->value=value;
if(gpio->intCallback)
{
gpio->intCallback(gpio);
}
}
close(fd);
return value;
}
and this is my example test program.
int main (void)
{
GPIO_t gpio;
gpio_init(&gpio, 17);
if(gpio_config(&gpio, INPUT,0)<0)
{
perror("Couldn't config gpio.");
return -1;
}
if(gpio_setEdge(&gpio,RISING_EDGE)<0)
{
perror("Couldn't config edge.");
return -1;
}
while(1)
{
puts("Waiting for GPIO.");
int retVal=gpio_waitForInterrupt(&gpio,-1); //Infinite waiting
printf("WaiForInterrupt returned with value %d \n", retVal);
if(retVal<0)
{
perror("Error waiting GPIO.");
return -1;
}
}
return 0;
}
My output is:
Waiting for GPIO.
WaiForInterrupt returned with value 0
Waiting for GPIO.
WaiForInterrupt returned with value 0
Waiting for GPIO.
WaiForInterrupt returned with value 0
Waiting for GPIO.
WaiForInterrupt returned with value 0
Waiting for GPIO.
WaiForInterrupt returned with value 0
Waiting for GPIO.
WaiForInterrupt returned with value 0
Waiting for GPIO.
WaiForInterrupt returned with value 0
Waiting for GPIO.
WaiForInterrupt returned with value 0
Waiting for GPIO.
WaiForInterrupt returned with value 0
Waiting for GPIO.
WaiForInterrupt returned with value 0
Waiting for GPIO.
WaiForInterrupt returned with value 0
Waiting for GPIO.
WaiForInterrupt returned with value 0
Waiting for GPIO.
WaiForInterrupt returned with value 0
Waiting for GPIO.
WaiForInterrupt returned with value 0
Waiting for GPIO.
WaiForInterrupt returned with value 0
Waiting for GPIO.
WaiForInterrupt returned with value 0
Waiting for GPIO.
WaiForInterrupt returned with value 0
Waiting for GPIO.
WaiForInterrupt returned with value 0
Waiting for GPIO.
As you can see poll function is returning constantly with POLLPRI flag set and is not waiting for level change of gpio.
I'm controlling this GPIO with the next gpio (GPIO27) configured as output.
I don't know what is doing wrong.