使用/sys/class/gpio进行用户空间操作gpio应注意一些问题:
1.echo "out" > direction时默认输出低电平,要想在输入该命令后输出高电平,可以使用“ echo "high" > direction ”
等等
另外,在用户态也能实现gpio的“中断”:下面是示例:
//导出gpio
sprintf(strCmd,"echo %d > /sys/class/gpio/export",dwPinNum);
...
//设置为输入
sprintf(strCmd,"echo in > /sys/class/gpio/gpio%d/direction",dwPinNum);
...
//设置触发条件,除了使用rising外,还可以falling、both等
sprintf(strCmd,"echo rising > /sys/class/gpio/gpio%d/edge",dwPinNum);
...
//最后使用poll系统调用进行监控
sprintf(strCmd,"/sys/class/gpio/gpio%d/value",dwPinNum);
sdwFd = open(strCmd,O_RDONLY);
struct pollfd fds[1];
fds[0].fd = sdwFd;
fds[0].events = POLLPRI;
sdwRet = poll(fds, 1, 3000);
...