树莓派 - gpio-led platform driver 控制LED
树莓派3b板上有两个LED, pwr (power) 和 act (activity)。是platform_driver gpio-led驱动。
可以通过设备树和gpio-led来额外控制一个LED。如下图在P25和GND之间接入一个发光二极管。
在设备树bcm2710-rpi-3-b.dts中添加一个LED2来控制外部的LED
&leds {
act_led: act {
label = "led0";
linux,default-trigger = "mmc0";
gpios = <&virtgpio 0 0>;
};
pwr_led: pwr {
label = "led1";
linux,default-trigger = "input";
gpios = <&expgpio 7 0>;
};
test_led: test {
label = "led2";
linux,default-trigger = "input";
gpios = <&gpio 26 0>;
};
};
重新编译dtb,
- make bcm2709_defconfig
- make dtbs
将生成的bcm2710-rpi-3-b.dtb复制到SD卡boot下, 启动后,可以看到LED2出现
pi@raspberrypi:~ $ cd /sys/class/leds/
pi@raspberrypi:/sys/class/leds $ ls
led0 led1 led2
pi@raspberrypi:/sys/class/leds $ cd led2
pi@raspberrypi:/sys/class/leds/led2 $ ls
brightness device max_brightness power subsystem trigger uevent
pi@raspberrypi:/sys/class/leds/led2 $ cat trigger
none rc-feedback kbd-scrolllock kbd-numlock kbd-capslock kbd-kanalock kbd-shiftlock kbd-altgrlock kbd-ctrllock kbd-altlock kbd-shiftllock kbd-shiftrlock kbd-ctrlllock kbd-ctrlrlock timer oneshot heartbeat backlight gpio cpu cpu0 cpu1 cpu2 cpu3 default-on [input] panic mmc1 mmc0 rfkill-any rfkill0 rfkill1
通过设置brightness可以亮灭LED
heartbeat 来触发LED blink
pi@raspberrypi:/sys/class/leds/led2 $ cat brightness
0
pi@raspberrypi:/sys/class/leds/led2 $ sudo sh -c "echo 1 > brightness"
pi@raspberrypi:/sys/class/leds/led2 $ cat brightness
0
pi@raspberrypi:/sys/class/leds/led2 $ sudo sh -c "echo heartbeat > trigger"
pi@raspberrypi:/sys/class/leds/led2 $ sudo sh -c "echo none > trigger"
pi@raspberrypi:/sys/class/leds/led2 $
Reference
Linux下用文件IO的方式操作GPIO(/sys/class/gpio)
https://blog.csdn.net/lu_embedded/article/details/53061901