AnsweredAssumed Answered

Efficient way to read a GPIO

Question asked by xii on Sep 26, 2012
Latest reply on Sep 26, 2012 by James.Kosin

Hi,

 

Following the documentation from here: http://docs.blackfin.uclinux.org/doku.php?id=linux-kernel:drivers:gpio-sysfs I am trying to read a GPIO input efficiently, but, in the code below, only the first read() call works (returns 2) ; further call to read() returns 0 (ie nothing to read):

int fd;

unsigned int i = 1000;

gpio_export(19);

gpio_dir_in(19);

 

fd = open("/sys/class/gpio/gpio19/value", O_RDONLY);

 

while(i > 0)

{

i--;

 

if(read(fd, c, 2) < 0) break;

printf("c = %c\n", c[0]);

}

 

close(fd);

gpio_unexport(19);

 

If I close and reopen "/sys/class/gpio/gpio19/value" between each read(), it works as expected, but that's not efficient at all. How to do fast read() on a GPIO?

 

 

Note: I am using new GPIO sysfs interface on a BF537 with uClinux 2009R1.1.

 

 

Many thanks,

Xavier

 

P.S. for writing a GPIO there is no such problem: the file descriptor can stay opened and any write() will be effective.

Outcomes