C 即使GPIO FD发生变化,投票仍不返回
我试图读取GPIO值,只要它改变状态C 即使GPIO FD发生变化,投票仍不返回,c,linux,embedded-linux,file-descriptor,poll-syscall,C,Linux,Embedded Linux,File Descriptor,Poll Syscall,我试图读取GPIO值,只要它改变状态 /sys/class/gpio/gpio499/value 我已将/sys/class/gpio/gpio499/edge设置为两者 我试图在一个单独的线程中使用poll命令监视值的变化。以下是代码片段: void PIN_gpio_poll(size_t gpio) //GPIO 499 { char path[30]; char cValue; int fd; int ret_po
/sys/class/gpio/gpio499/value
我已将/sys/class/gpio/gpio499/edge
设置为两者
我试图在一个单独的线程中使用poll命令监视值的变化。以下是代码片段:
void PIN_gpio_poll(size_t gpio) //GPIO 499
{
char path[30];
char cValue;
int fd;
int ret_poll;
int ret_read;
struct pollfd pollfd;
int i;
pollfd.events = POLLPRI | POLLERR; /* look for GPIO status change. */
snprintf(path, 30, PHDRIVER_LINUX_CFG_DIR "/gpio%u/value", gpio);
fd = open(path, O_RDONLY);
if (fd == -1)
{
printf("Gpio_poll _ERROR\r\n");
}
pollfd.fd = fd;
ret_read = read(pollfd.fd, &cValue, 1); // Dummy Read to clear
while (1)
{
lseek(fd, 0, SEEK_SET);
ret_read = read(fd, &cValue, 1);
printf("Value=%c, RET READ=%d\n",cValue,ret_read);
// ret_poll = poll( &pollfd, 1, -1 );
ret_poll = poll( &pollfd, 1, 10000 ); //10sec timeout
printf("******REVENTS=%x\n",pollfd.revents);
if( ret_poll == -1 )
{
printf("Gpio_poll poll failed\r\n");
close(fd);
}else{
// if (pollfd.revents & POLLPRI )
{
lseek(fd, 0, SEEK_SET);
ret_read = read(pollfd.fd, &cValue, 1);
if(ret_read > 0)
{
printf("Cvalue = %c\n",cValue);
}
}
}
}
}
我面临的问题是,如果我将事件设置为POLLIN,poll将立即返回。这是可以理解的,因为在值(0或1)GPIO中始终存在要读取的数据。我将事件称为POLLPRI | POLLERR,并将其设置为POLLPRI。但在此方法中,轮询仅在超时后返回。当GPIO的值更改时,它不会返回。这里有什么我错过的技巧吗??我还将/sys/class/gpio/gpio499/edge
设置为上升、下降,但似乎没有任何效果
编辑:
下面是grep-r的输出/sys/class/gpio/gpio499
/sys/class/gpio/gpio499/edge:both
/sys/class/gpio/gpio499/power/control:auto
/sys/class/gpio/gpio499/power/runtime_active_time:0
grep: /sys/class/gpio/gpio499/power/autosuspend_delay_ms: Input/output error
/sys/class/gpio/gpio499/power/runtime_status:unsupported
/sys/class/gpio/gpio499/power/runtime_suspended_time:0
/sys/class/gpio/gpio499/value:1
/sys/class/gpio/gpio499/active_low:0
/sys/class/gpio/gpio499/direction:in
注意:我想检测从1到0的值。函数:poll()
不能像发布的代码所期望的那样工作
建议:1)读取文件以获取当前输入值。2) 执行旋转循环,读取值,直到值发生变化,类似于:
readbytes = read( fd, &cValue, 1 );
while( readbytes > 0 )
{
if( (off_t)-1 == lseek(fd, 0, SEEK_SET) )
{ // then lseek failed
perror( "lseek failed" );
exit( EXIT_FAILURE );
}
// implied else, lseek successful
readbytes = read(fd, &new_cValue, 1);
if( 0 >= readbytes )
{
perror( "read failed" );
exit( EXIT_FAILURE );
}
// implied else, read successful
if( cValue != new_cValue )
{ // then state changed
cValue = new_cValue;
break;
}
}
此循环确实会消耗更多的CPU周期,但应该可以工作。函数:poll()
不能像发布的代码所期望的那样工作
建议:1)读取文件以获取当前输入值。2) 执行旋转循环,读取值,直到值发生变化,类似于:
readbytes = read( fd, &cValue, 1 );
while( readbytes > 0 )
{
if( (off_t)-1 == lseek(fd, 0, SEEK_SET) )
{ // then lseek failed
perror( "lseek failed" );
exit( EXIT_FAILURE );
}
// implied else, lseek successful
readbytes = read(fd, &new_cValue, 1);
if( 0 >= readbytes )
{
perror( "read failed" );
exit( EXIT_FAILURE );
}
// implied else, read successful
if( cValue != new_cValue )
{ // then state changed
cValue = new_cValue;
break;
}
}
此循环确实会消耗更多的CPU周期,但应该可以工作您是否确保该引脚配置为输入?(
cat/sys/class/gpio/gpio499/direction
sayin?)可能是grep-r的输出/sys/class/gpio/gpio499
(编辑到您的问题中)可以帮助您在Ctx上找到可能重复的yes。方向设置为inOk,其余看起来很好,哪个gpio驱动程序在这里工作?可能它有一个错误或行为是未实现的?你是否确定,该引脚配置为输入?(cat/sys/class/gpio/gpio499/direction
sayin?)可能是grep-r的输出/sys/class/gpio/gpio499
(编辑到您的问题中)可以帮助您在Ctx上找到可能重复的yes。方向设置为inOk,其余看起来很好,哪个gpio驱动程序在这里工作?也许它有一个bug或者行为没有实现?