【随笔记】全志平台 gpio-leds 驱动应用
阅读原文时间:2023年07月08日阅读:3

内核版本:Linux 4.9

硬件原理:

  1. GPIO 通过一颗 MOS 管(2N7002ET1G),接到 LED 负极,LED 正极接电源。
  2. GPIO 通过拉高导通 MOS ,将 LED 接地,实现点亮 LED。

配置内核驱动:make ARCH=arm64 menuconfig

Device Drivers  --->
    -*- LED Support  --->
        <*>   LED Support for GPIO connected LEDs
        -*-   LED Trigger support  --->
            <*>   LED Timer Trigger
            <*>   LED One-shot Trigger
            <*>   LED Heartbeat Trigger
            <*>   LED backlight Trigger
            [*]   LED CPU Trigger
            <*>   LED GPIO Trigger
            <*>   LED Default ON Trigger

配置硬件信息:sys_config.fex

全志平台便捷方式配置,也可以使用通用的 dts 配置方式

[leds]
compatible = "gpio-leds";

[leds/red]
label = "red";
linux,default-trigger = "timer"
gpio = port:PL08<1><default><default><0>

[leds/blue]
label = "blue";
linux,default-trigger = "heartbeat"
gpio = port:PL07<1><default><default><0>

正常情况下可以看到两颗灯以不同的节奏闪烁

  1. 检查相应设备节点

    root@localhost:/# ls /sys/class/leds
    blue red

    root@linux:/# ls /sys/class/leds/blue
    brightness device invert max_brightness power subsystem trigger uevent

    root@linux:/# ls /sys/class/leds/red
    brightness delay_on max_brightness subsystem uevent
    delay_off device power trigger

  2. 关闭触发功能

    root@linux:/# echo "none" > /sys/class/leds/blue/trigger
    root@linux:/# echo "none" > /sys/class/leds/red/trigger

  3. 点亮关闭指定 LED(高亮、低灭)

    root@linux:/# echo 1 > /sys/class/leds/blue/brightness
    root@linux:/# echo 0 > /sys/class/leds/blue/brightness

  4. 设置定时闪烁 LED(亮 100ms,灭 1000ms)

    root@linux:/# echo "timer" > /sys/class/leds/blue/trigger
    root@linux:/# echo 100 > /sys/class/leds/blue/delay_on
    root@linux:/# echo 1000 > /sys/class/leds/blue/delay_off