我试图编写一个基本的Linux用户空间应用程序。由于某些原因,我能够打开导出文件并以给定的编号导出GPIO。但是,导出它之后,我无法指定它是输入还是输出,因为没有创建/sys/class/gpio/gpio<#>/direction。结果,我的C犯了错误。
这是代码
#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;
}run的输出:
root@plnx_arm:~# /usr/bin/basic-gpio GPIO测试运行..。 成功导出GPIO 无法打开GPIO方向
档案在那里
root@plnx_arm:~# ls /sys/class/gpio/gpio 971/ active_low器件方向边功率子系统value值
发布于 2017-11-01 16:01:28
您需要打开文件“/sys/class/gpio/gpio 971/direction”,而不是“/sys/class/gpio 971/direction”。
directionfd = open("/sys/class/gpio/gpio971/direction", O_RDWR);
if(directionfd < 0)
{
printf("Cannot open GPIO direction it\n");
exit(1);
}您可以引用1,并获得导出/取消导出/设置方向/读/写gpio引脚的代码。
https://stackoverflow.com/questions/47057966
复制相似问题