C 导出时未创建Linux GPIO值文件
我正在尝试编写一个基本的LinuxGPIO用户空间应用程序。由于某些原因,我能够打开导出文件并导出具有给定编号的GPIO。但是,在导出它之后,我无法指定它是输入还是输出,因为没有创建/sys/class/gpio/gpio/direction文件。结果,我的C出错了 这是密码C 导出时未创建Linux GPIO值文件,c,linux,gpio,C,Linux,Gpio,我正在尝试编写一个基本的LinuxGPIO用户空间应用程序。由于某些原因,我能够打开导出文件并导出具有给定编号的GPIO。但是,在导出它之后,我无法指定它是输入还是输出,因为没有创建/sys/class/gpio/gpio/direction文件。结果,我的C出错了 这是密码 #include <stdio.h> #include <stdlib.h> #include <fcntl.h> int main() { int valuefd, exp
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
int main()
{
int valuefd, exportfd, directionfd;
printf("GPIO test running...\n");
exportfd = open("/sys/class/gpio/export", O_WRONLY);
if(exportfd < 0)
{
printf("Cannot open GPIO to export it\n");
exit(1);
}
write(exportfd, "971", 4);
close(exportfd);
printf("GPIO exported successfully\n");
directionfd = open("/sys/class/gpio971/direction", O_RDWR);
if(directionfd < 0)
{
printf("Cannot open GPIO direction it\n");
exit(1);
}
write(directionfd, "out", 4);
close(directionfd);
printf("GPIO direction set as output successfully\n");
valuefd = open("/sys/class/gpio/gpio971/value", O_RDWR);
if(valuefd < 0)
{
printf("Cannot open GPIO value\n");
exit(1);
}
printf("GPIO value opened, now toggling...\n");
while(1)
{
write(valuefd, "1", 2);
write(valuefd, "0", 2);
}
return 0;
}
运行的输出:
root@plnx_arm:~/usr/bin/basic gpio
GPIO测试正在运行
GPIO已成功导出
无法打开GPIO指向它
文件在那里
root@plnx_arm:~ls/sys/class/gpio/gpio971/
有源低设备方向边缘电源子系统
uevent值
您需要打开文件/sys/class/gpio/gpio971/direction,而不是/sys/class/gpio971/direction
directionfd = open("/sys/class/gpio/gpio971/direction", O_RDWR);
if(directionfd < 0)
{
printf("Cannot open GPIO direction it\n");
exit(1);
}
您可以参考[1],获取导出/取消导出/设置方向/读取/写入gpio引脚的代码
[1] 运行ls/sys/class/gpio/gpio971/时,文件显然就在那里。将gpio号写入导出文件和相应的gpio971目录之间可能存在延迟。如果在导出pin后插入一个短延迟,会发生什么情况?另外,最好使用perror函数显示系统调用失败的原因。有关该主题的一些讨论,请参阅。