I am using Linux kernel based on ARM based Linux 3.12 (imx233 CPU). My goal is to detect a GPIO finger change (1 to 0).
I can read the pin value constantly calling the function below (during the (1) loop)
int GPIO_read_value(int pin){ int gpio_value = 0; char path[35] = {'\0'}; FILE *fp; sprintf(path, "/sys/class/gpio/gpio%d/value", pin); if ((fp = fopen(path,"rb+")) == NULL){
But this causes too much CPU load. I do not use usleep or nanosleep , because the contact change occurs within a very short time, because of which I miss the event.
As far as I know, using poll() not possible. Is there any poll() function that I can use to detect GPIO finger changes?
EDIT: Just in case, if I am doing something wrong, here is my use of poll() , which does not detect shift change
struct pollfd pollfds; int fd; int nread, result; pollfds.fd = open("/sys/class/gpio/gpio51/value", O_RDWR); int timeout = 20000; char buffer[128]; if( pollfds.fd < 0 ){ printf(" failed to open gpio \n"); exit (1); } pollfds.events = POLLIN; printf("fd opens..\n"); while (1) { result = poll (&pollfds, 0, timeout); switch (result) { case 0: printf ("timeout\n"); break; case -1: printf ("poll error \n"); exit (1); default: printf("something is happening..\n"); if (pollfds.revents & POLLIN) { nread = read (pollfds.fd, buffer, 8); if (nread == 0) { printf ("result:%d\n", nread); exit (0); } else { buffer[nread] = 0; printf ("read %d from gpio: %s", nread, buffer); } } } } close(fd);
EDIT2: the code https://developer.ridgerun.com/wiki/index.php/Gpio-int-test.c works fine with poll() I needed to define a rising / falling edge for the interrupt and fix it a bit for definition. This solves my problem, however it may be good for some other people to hear / learn alternative methods.
c linux posix embedded-linux gpio
Angs
source share