在上一期自己构建的系统还是有很多不足,无法同时进行ACM和NCM,但是官方的是可以的,以后再慢慢研究吧。这一期进行GPIO的操作。
这里我们直接对led4进行操作,驱动已经编译进内核了,只需要对以下内容进行操作,写 1 led4 亮,写 0 led4 灭
/sys/class/leds/beaglebone:green:usr4/brightness
原理图如下:
源码如下:
#include <stdio.h>
#include <unistd.h> // 包含 sleep 函数
int main(void)
{
FILE *fd;
char on_char = '1';
char off_char = '0';
while(1)
{
// 打开文件,模式为写入(字符串)
fd = fopen("/sys/class/leds/beaglebone:green:usr4/brightness", "w");
if (fd == NULL) // 使用比较运算符 ==
{
printf("open is error \n");
return -1;
}
// 写入 '1',传递字符变量的地址
fwrite(&on_char, 1, 1, fd);
fclose(fd); // 立即关闭文件,确保写入生效
sleep(1);
// 重新打开文件写入 '0'
fd = fopen("/sys/class/leds/beaglebone:green:usr4/brightness", "w");
if (fd == NULL)
{
printf("open is error \n");
return -1;
}
fwrite(&off_char, 1, 1, fd);
fclose(fd);
sleep(1);
}
return 0;
}
成果演示: