Gpio interrupt handling,imx7

Hai,
Here I configured the gpio pin 142 to a rising edge interrupt.
It was working but sometimes it was giving interruptions for falling edges also.
Here I am attaching the code and also attaching the output observed.

#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<fcntl.h>
#include<poll.h>

int main(int args,char *argv[])
{
	int fd;
	char value;
	struct pollfd poll_gpio;
	
	poll_gpio.events = POLLPRI;
	
	//gpio export
	fd = open("/sys/class/gpio/export",O_WRONLY);
	if(fd<0)
	{
		printf("fail to export the gpio\n");
		//close(fd);
		return -1;
	}
	write(fd,"142",3);
	close(fd);
	
	// set direction
	fd = open("/sys/class/gpio/gpio142/direction",O_WRONLY);
	if(fd<0)
	{
		printf("fail to open gpio142 direction\n");
		//close(fd);
		return -1;
	}
	write(fd,"in",2);
	close(fd);
	
	// gpio interrupt config
	fd= open("/sys/class/gpio/gpio142/edge",O_WRONLY);
	if(fd<0)
	{
		printf("fail to opne gpio142 edge\n");
		return fd;
	}
	write(fd,"rising",6);
	close(fd);

	fd = open("/sys/class/gpio/gpio142/value",O_RDONLY);
	if(fd<0)
	{
		printf("fail to open gpio142 value\n");
		return -1;
	}
	
	poll_gpio.fd = fd;
	poll(&poll_gpio,1,-1);
	read(fd,&value,1);

	while(1)
	{
		printf("enter into while\n");
		poll(&poll_gpio,1,10*1000);
		if((poll_gpio.revents & POLLPRI)==POLLPRI)
		{
			lseek(fd,0,SEEK_SET);
			read(fd,&value,1);
			printf("Interrupt value : %c\n",value);

		}
	}	
}
![WhatsApp Image 2021-11-09 at 10.50.05|666x500](upload://yMxcv8w9mtWxiVUQRXpeLyEYfL0.jpeg)

**OUTPUT OBSERVED**
Interrupt value : 0
Interrupt value : 1
Interrupt value : 1
Interrupt value : 1
Interrupt value : 0
Interrupt value : 1
Interrupt value : 1
Interrupt value : 0
Interrupt value : 1
Interrupt value : 1
Interrupt value : 0
Interrupt value : 1
Interrupt value : 0
Interrupt value : 1
Interrupt value : 1
Interrupt value : 1
Interrupt value : 1
Interrupt value : 1
Interrupt value : 1
Interrupt value : 1
Interrupt value : 0
 Can anyone help me to sortout this problem.
thanks.

It takes some time between interrupt is registered and you read pin value from sysfs. So nothing’s wrong value read doesn’t always match configured interrupt edge. If you are using pulse generator, then try making pulses longer, at least 1ms long, 0 values should disappear from output.

Edward

1 Like

Here I am using continuous 3.3V input.
That input is given to the gpio.
That input was controlled with a switch manually.
so the delay is more than 1ms.

Switches usually produce lots of bouncing. Use scope and observe lots of short pulses.

Edward

1 Like

I am also thinking it happened due to the switch debouncing.

we can close this topic.

Once again, thanks for your help.