NUC972 添加LED驱动 leds_gpio 方式
https://blog.csdn.net/justgg/article/details/104410089
LED驱动的方式有多种,可以自定义驱动,然后通过/dev/myled 设备文件,来控制led。
也可以通过内核 led驱动架构来控制,通过/sys/leds/myled/ 里面的各种文件来控制。内核还提供了一个非常方便的方法,就是用过leds_gpio驱动来控制LED. 我们只需要配置相关引脚就新了。
步骤如下:
1.添加配置引脚
修改 dev.c 文件 在内核目录/arch/arm/mach-nuc970/ 中。
添加头文件
-
#include <linux/leds.h> // add by simon for leds_gpio driver
添加引脚配置
-
#if defined(CONFIG_LEDS_GPIO)
-
//================leds_gpio add by simon =============
-
static struct gpio_led gpio_leds[] = {
-
{
-
.name = "red",
-
.gpio = NUC970_PH12,
-
.default_state = LEDS_GPIO_DEFSTATE_ON, // 默认LED亮
-
.active_low = 0, // 低电平亮
-
.default_trigger = "timer", // 触发器
-
},
-
};
-
static struct gpio_led_platform_data gpio_led_info = {
-
.leds = gpio_leds,
-
.num_leds = ARRAY_SIZE(gpio_leds),
-
};
-
static struct platform_device leds_gpio = {
-
.name = "leds-gpio",
-
.id = -1,
-
.dev = {
-
.platform_data = &gpio_led_info,
-
//.release = platformdev_release,
-
},
-
};
-
//=====================================================
-
#endif
2.添加设备文件
把 leds_gpio 设备添加到 static struct platform_device *nuc970_public_dev[] __initdata 这个设备数组中。 系统启动时会自动加载设备。
-
static struct platform_device *nuc970_public_dev[] __initdata = {
-
...
-
#if defined(CONFIG_LEDS_GPIO)
-
&leds_gpio,
-
#endif
-
...
-
};
3.配置内核
触发器的选择按需所需,timer是1秒周期性亮灭,heartbeat 是 闪2下,闪2下,cpu是按cpu使用率闪烁等。
4.编译
编译并更新内核,可以看到在sys目录下已经有了leds目录了,下面有个red的设备,这个名称和我之前配的相同
5.测试
/sys/devices/platform/leds-gpio/leds/red # echo 1 > brightness
/sys/devices/platform/leds-gpio/leds/red # echo 0 > brightness
/sys/devices/platform/leds-gpio/leds/red # cat trigger
[none] nand-disk mmc0 timer oneshot heartbeat backlight gpio cpu0 default-on transient
/sys/devices/platform/leds-gpio/leds/red # echo oneshot > trigger
/sys/devices/platform/leds-gpio/leds/red # echo heartbeat > trigger
****************************************************************************************************************
平台驱动文件: linux-3.10.x\drivers\leds\leds-gpio.c
secureCRT
~ # cd /sys/
/sys # ls
block class devices fs module
bus dev firmware kernel
/sys # cd class/
/sys/class # ls
bdi graphics mdio_bus net tty
block ieee80211 mem scsi_device ubi
bsg input misc scsi_disk vc
gpio leds mtd scsi_host vtconsole
/sys/class # cd leds/
/sys/class/leds # ls
red
/sys/class/leds # ls -l *
lrwxrwxrwx 1 root root 0 Jan 1 00:00 red -> ../../devices/platform/leds-gpio/leds/red
/sys/class/leds # cd ../
/sys/class # cd ..
/sys # ls
block class devices fs module
bus dev firmware kernel
/sys # cd bus/
/sys/bus # ls
clocksource event_source platform usb
cpu mdio_bus scsi workqueue
/sys/bus # cd platform/
/sys/bus/platform # ls
devices drivers_autoprobe uevent
drivers drivers_probe
/sys/bus/platform # cd devices/`
>
> /
>
>
>
>
> cd ..
> ..
>
>
>
>
>
> ls
> ls
> cd ..
>
>
>
>
>
> '
>
>
>
>
> -/bin/sh: syntax error: EOF in backquote substitution
/sys/bus/platform # ls
devices drivers_autoprobe uevent
drivers drivers_probe
/sys/bus/platform # cd devices/
/sys/bus/platform/devices # ls
alarmtimer nuc970-fmi nuc970-gpio.3 nuc970-gpio.7 nuc970-uart.0
leds-gpio nuc970-gpio.0 nuc970-gpio.4 nuc970-gpio.8 pinctrl-nuc970
nuc970-ehci nuc970-gpio.1 nuc970-gpio.5 nuc970-lcd regulatory.0
nuc970-emac0 nuc970-gpio.2 nuc970-gpio.6 nuc970-ohci
/sys/bus/platform/devices # ls -l leds-gpio/
lrwxrwxrwx 1 root root 0 Jan 1 00:05 driver -> ../../../bus/platform/drivers/leds-gpio
drwxr-xr-x 3 root root 0 Jan 1 00:00 leds
-r--r--r-- 1 root root 4096 Jan 1 00:05 modalias
lrwxrwxrwx 1 root root 0 Jan 1 00:05 subsystem -> ../../../bus/platform
-rw-r--r-- 1 root root 4096 Jan 1 00:05 uevent
/sys/bus/platform/devices # cd leds-gpio/`
> -/bin/sh: syntax error: EOF in backquote substitution
/sys/bus/platform/devices # cd leds-gpio/
/sys/devices/platform/leds-gpio # ls
driver leds modalias subsystem uevent
/sys/devices/platform/leds-gpio # cd leds/
/sys/devices/platform/leds-gpio/leds # ls
red
/sys/devices/platform/leds-gpio/leds # ls -l
drwxr-xr-x 2 root root 0 Jan 1 00:00 red
/sys/devices/platform/leds-gpio/leds # cd ..
/sys/devices/platform/leds-gpio # cd ..
/sys/bus/platform/devices # cd ..
/sys/bus/platform # ls
devices drivers_autoprobe uevent
drivers drivers_probe
/sys/bus/platform # cd drivers/
/sys/bus/platform/drivers # ls
alarmtimer nuc970-ehci nuc970-fmi nuc970-lcd nuc970-uart
leds-gpio nuc970-emac0 nuc970-gpio nuc970-ohci pinctrl-nuc970
/sys/bus/platform/drivers # cd leds-gpio/
/sys/bus/platform/drivers/leds-gpio # ls
bind leds-gpio uevent unbind
/sys/bus/platform/drivers/leds-gpio # ls -l
--w------- 1 root root 4096 Jan 1 00:06 bind
lrwxrwxrwx 1 root root 0 Jan 1 00:06 leds-gpio -> ../../../../devices/platform/leds-gpio
--w------- 1 root root 4096 Jan 1 00:06 uevent
--w------- 1 root root 4096 Jan 1 00:06 unbind
/sys/bus/platform/drivers/leds-gpio # cd leds-gpio/
/sys/devices/platform/leds-gpio # ls
driver leds modalias subsystem uevent
/sys/devices/platform/leds-gpio # cd /
~ # ls
bin etc linuxrc proc sys usr
dev lib mnt sbin tmp var
~ # echo 1 > brightness
~ # echo 0 > brightness
~ # cd /sys
/sys # ls
block class devices fs module
bus dev firmware kernel
/sys # cd devices/
/sys/devices # ls
platform software system virtual
/sys/devices # cd platform/
/sys/devices/platform # ls
alarmtimer nuc970-fmi nuc970-gpio.3 nuc970-gpio.7 nuc970-uart.0
leds-gpio nuc970-gpio.0 nuc970-gpio.4 nuc970-gpio.8 pinctrl-nuc970
nuc970-ehci nuc970-gpio.1 nuc970-gpio.5 nuc970-lcd regulatory.0
nuc970-emac0 nuc970-gpio.2 nuc970-gpio.6 nuc970-ohci uevent
/sys/devices/platform # cd leds-gpio/
/sys/devices/platform/leds-gpio # ls
driver leds modalias subsystem uevent
/sys/devices/platform/leds-gpio # cd leds/
/sys/devices/platform/leds-gpio/leds # ls
red
/sys/devices/platform/leds-gpio/leds # cd red/
/sys/devices/platform/leds-gpio/leds/red # ls
brightness delay_on max_brightness trigger
delay_off device subsystem uevent
/sys/devices/platform/leds-gpio/leds/red # echo 0 > brightness
/sys/devices/platform/leds-gpio/leds/red # echo 1 > brightness
/sys/devices/platform/leds-gpio/leds/red # cat trigger
[none] nand-disk timer oneshot heartbeat backlight cpu0 default-on
/sys/devices/platform/leds-gpio/leds/red # echo oneshot > trigger
/sys/devices/platform/leds-gpio/leds/red # echo heartbeat > trigger
/sys/devices/platform/leds-gpio/leds/red #
上一篇: 【小白日记17】----网络管理(1)
下一篇: Web APIS 第三天