我的内核学习笔记11:linux leds-gpio驱动应用实例 | 迟思堂工作室
A-A+

我的内核学习笔记11:linux leds-gpio驱动应用实例

2016-12-08 21:32 Linux内核研究, 嵌入式Linux 暂无评论 阅读 3,089 次

linux内核的leds-gpio是使用GPIO控制LED的驱动,只要将板子上LED灯对接的GPIO引脚号进行适当的配置,就能使用这个驱动了,十分方便。网上有很多文章分析这个驱动,就不献丑——或者后面闲时再写写吧。

本文仅本着“实用”的目的,介绍从内核配置到应用实例,以期对leds-gpio驱动有一个整体概念。

一、概述

leds-gpio封装得十分好,只需要提供可正常使用的GPIO即可。另外还具备触发器功能,其实就是控制LED的亮灭(及频率)。比如default-on是点亮LED灯的触发器,没有取消前一直亮着。heartbeat是心跳触发器,经笔者实践,此触发器是快速闪烁2次,然后灭掉,灭掉时间较亮的时间长。timer为定时触发器,即1HZ内亮灭。其它还有如ide硬盘、mmc、CPU触发器,就不一一介绍了。

leds驱动位于drivers/leds目录。leds-gpio驱动名称为“leds-gpio”,驱动文件为drivers/leds/leds-gpio.c。

触发器驱动位于drivers/leds/trigger目录。

二、内核配置

本文基于linux 3.17.1版本内核进行分析。

Device Drivers->
    -*- LED Support  --->  
        {*}   LED Class Support # 与用户空间交互的
        <M>   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

从配置中看到,笔者将LED触发器全部编译到内核中。这样方便使用和选择。

三、设备注册及使用

3.1 LED相关结构体

驱动开发者使用gpio_led对LED进行赋值,包括LED名称、GPIO引脚号、灯亮是哪个电平,还有默认状态。gpio_led结构体定义如下:

struct gpio_led {
    const char *name; // 名称,会生成/sys/.../leds/name目录
    const char *default_trigger; // 默认触发器,可写可不写,在命令行可以重新赋值
    unsigned     gpio; // GPIO引脚号
    unsigned    active_low : 1; // 为1表示低电平LED点亮
    unsigned    retain_state_suspended : 1;
    unsigned    default_state : 2; // 默认状态
    /* default_state should be one of LEDS_GPIO_DEFSTATE_(ON|OFF|KEEP) */
};

另外还要填写gpio_led_platform_data结构体,其定义如下:

struct gpio_led_platform_data {
    int         num_leds; // 一共有多少个LED灯
    const struct gpio_led *leds; // 上面的结构体指针

#define GPIO_LED_NO_BLINK_LOW    0    /* No blink GPIO state low */
#define GPIO_LED_NO_BLINK_HIGH    1    /* No blink GPIO state high */
#define GPIO_LED_BLINK        2    /* Please, blink */
    int        (*gpio_blink_set)(unsigned gpio, int state,
                    unsigned long *delay_on,
                    unsigned long *delay_off); // LED闪烁回调函数,可置为NULL
};

一个实例如下:

static struct gpio_led gpio_leds[] = {
    {
        .name = "red",
        .gpio = 33, // GP2_1   GPIO_NO = Group * 32 + Id
        .default_state = LEDS_GPIO_DEFSTATE_ON, // 默认LED亮
        .active_low = 1, // 低电平亮
        //.default_trigger = "timer", // 触发器
    },
    {
        .name = "green",
        .gpio = 34,
        .default_state = LEDS_GPIO_DEFSTATE_ON, 
        .active_low = 1,
        //.default_trigger = "heartbeat", 
    },
};

static struct gpio_led_platform_data gpio_led_info = {
     .leds          = gpio_leds,
     .num_leds     = ARRAY_SIZE(gpio_leds),
};

从结构体中知道,系统有2个LED,一个红灯,一个绿灯,都是低电平灯亮。

3.2 LED平台设备

leds-gpio驱动定义如下(drivers/leds/leds-gpio.c):

static struct platform_driver gpio_led_driver = {
    .probe        = gpio_led_probe,
    .remove        = gpio_led_remove,
    .driver        = {
        .name    = "leds-gpio",
        .owner    = THIS_MODULE,
        .of_match_table = of_match_ptr(of_gpio_leds_match),
    },
};

module_platform_driver(gpio_led_driver);

从gpio_led_driver结构体中可以看到驱动名称为leds-gpio。因此要使用这个驱动,必须另外定义一个platform设备,并调用函数platform_device_register注册。本文实例如下:

static struct platform_device leds_gpio = {
    .name = "leds-gpio",
    .id = -1,
    .dev = {
        .platform_data = &gpio_led_info,
        .release = platformdev_release,
    },
};

其中name表示设备名称,必须为“leds-gpio”,这样才能匹配并加载成功。最后提一下release成员,在较新的内核中必须对此进行赋值,否则会有错误信息提示:

Device 'leds-gpio' does not have a release() function, it is broken and must be fixed.

最后,注册leds设备——建议在板子的GPIO正常工作之后再进行注册。

platform_device_register(&leds_gpio); // 注册leds设备

注意,如果是以modules形式动态加载的话,必须要适合的地方如remove函数在卸载leds设备:

platform_device_unregister(&leds_gpio); // 卸载leds设备

四、应用实例

LED设备和驱动都正常情况下,系统启动后,会产生/sys/bus/platform/devices/leds-gpio/leds目录,其下分别有red和green两个子目录。可以分别对不同的红色LED和绿色LED做操作。

4.1 亮灭LED

将1或0写入brightness文件即可控制亮灭。

示例如下:

echo 0 > /sys/bus/platform/devices/leds-gpio/leds/green/brightness
echo 0 > /sys/bus/platform/devices/leds-gpio/leds/red/brightness 

echo 1 > /sys/bus/platform/devices/leds-gpio/leds/green/brightness
echo 1 > /sys/bus/platform/devices/leds-gpio/leds/red/brightness

4.1 触发器

直接查看trigger文件,即可知道当前系统支持的触发器,示例:

cat /sys/bus/platform/devices/leds-gpio/leds/red/trigger 
[none] timer oneshot heartbeat backlight gpio cpu0 default-on mmc0 mmc1 mmc2

在前面的驱动中注释掉了trigger,所以现在是none。

设置触发器很简单,使用ecoh将需要的触发器名称写入trigger文件即可。注意,写入的字符串一定是trigger文件已经存在的,否则会提示参数非法。写入心跳触发器示例:

echo heartbeat > /sys/bus/platform/devices/leds-gpio/leds/red/trigger

此时板子上红灯应会闪烁。
再次查看:

cat /sys/bus/platform/devices/leds-gpio/leds/red/trigger 
none timer oneshot [heartbeat] backlight gpio cpu0 default-on mmc0 mmc1 mmc2

设置值已经生效了。

参考资源:

1、内核源码官网:

2、内核源码查询:

李迟 2016.12.8 周四 晚



如果本文对阁下有帮助,不妨赞助笔者以输出更多好文章,谢谢!
donate



标签:

给我留言