` 不管是gpio还是led的控制都有两种方式,一种是在串口终端直接输入echo等命令控制灯亮灭,一种是编写应用程序去自动控制。
Led的路径是 /sys/class/leds/usr_led1/,而led的名称则由设备树标明,如下是米尔设备树代码。 &ps7_axi { gpio-leds { compatible = "gpio-leds"; led_r { label = "led_r"; gpios = <&gpio0 114 1>; default-state = "on"; linux,default-trigger = "heartbeat"; }; led_g { label = "led_g"; gpios = <&gpio0 115 1>; default-state = "on"; linux,default-trigger = "heartbeat"; }; led_b { label = "led_b"; gpios = <&gpio0 116 1>; default-state = "on"; linux,default-trigger = "heartbeat"; }; usr_led1 { label = "usr_led1"; gpios = <&gpio0 0 1>; default-state = "off"; linux,default-trigger = "none"; }; usr_led2 { label = "usr_led2"; gpios = <&gpio0 9 1>; default-state = "off"; linux,default-trigger = "none"; };
}; 第一种方式shell控制灯: 通过shell进入usr_led1文件路径 然后点亮命令是 echo 1 >brightness 关闭是echo 0>brightness; 然后输入echo heartbeat>trigger是让usr_led1这个灯一闪一闪的心跳灯。
第二种方式应用程序控制灯: 应用程序的操作流程是先打开usr_led1的trigger文件,然后将“none”字符串写入这个文件,以取得对此led的控制。然后再打开此led的brightness文件,写入字符“1”或者“0”进行灯的点亮和关闭操作。具体代码如下: void led_settrigger() { int ret; int fd; fd = open("/sys/class/leds/usr_led1/trigger",O_WRONLY); char tri[NAME_MAX_LENGTH] = "none"; printf("set trigger to none
"); if (fd < 0) { printf("Can't open this led
"); } ret = write(fd, "none", strlen(tri)); close(fd); } void led_twinkle() { int ret; int fd = open("/sys/class/leds/usr_led1/brightness", O_WRONLY); if (fd < 0) { printf("Can't open this led
"); } while(1) { ret = write(fd, "1", 2); sleep(1); ret = write(fd, "0", 2); sleep(1); } close(fd);
}
`
|