led驱动 Linux官方内核已提供led驱动,源码 /drivers/leds/leds-gpio.c 平台文件添加对按键驱动的支持: arch/mips/loongson1/ls1c/ls1c300a_openloongson_v2.0_board.c 文件中已经添加: #if defined(CONFIG_LEDS_GPIO) || defined(CONFIG_LEDS_GPIO_MODULE) #include struct gpio_led gpio_leds[] = { { .name = "led_green", .gpio = 50, .active_low = 1, // .default_trigger = "timer", /* 触发方式 */ .default_trigger = NULL, .default_state = LEDS_GPIO_DEFSTATE_ON, }, { .name = "led_yellow", .gpio = 51, .active_low = 1, // .default_trigger = "heartbeat", /* 触发方式 */ .default_trigger = NULL, .default_state = LEDS_GPIO_DEFSTATE_ON, }, { .name = "led_blue", .gpio = 52, .active_low = 1, // .default_trigger = "timer", /* 触发方式 */ .default_trigger = NULL, .default_state = LEDS_GPIO_DEFSTATE_ON, }, { .name = "led_red", .gpio = 53, .active_low = 1, // .default_trigger = "timer", /* 触发方式 */ .default_trigger = NULL, .default_state = LEDS_GPIO_DEFSTATE_ON, }, { .name = "led_orange", .gpio = 54, .active_low = 1, // .default_trigger = "timer", /* 触发方式 */ .default_trigger = NULL, .default_state = LEDS_GPIO_DEFSTATE_ON, }, }; static struct gpio_led_platform_data gpio_led_info = { .leds = gpio_leds, .num_leds = ARRAY_SIZE(gpio_leds), }; static struct platform_device leds = { .name = "leds-gpio", .id = -1, .dev = { .platform_data = &gpio_led_info, } }; #endif //#if defined(CONFIG_LEDS_GPIO) || defined(CONFIG_LEDS_GPIO_MODULE) 以绿色led为例说明: .name = "led_green", 显示的名称,可以根据实际修改 .gpio = 50, gpio号,根据原理图连接的gpio号 .active_low = 1, 表示低电平点亮,根据原理图和实际使用情况修改 // .default_trigger = "timer", 触发方式,linux内核提供了很多出发方式,如心跳灯,硬盘灯,自定义的时间亮灭等.... 不使用的话设置为NULL .default_trigger = NULL, .default_state = LEDS_GPIO_DEFSTATE_ON,系统启动后该led的默认状态,这里表示点亮。 内核配置 Device Drivers ---> [*] LED Support ---> <*> LED Support for GPIO connected LEDs [*] Platform device bindings for GPIO LEDs 设备 /sys/class/leds/ 目录下会生成对应设备名的目录,如led_green对应的是ls1c300a_openloongson_v2.0_board.c文件中的.name = "led_green", cd /sys/class/leds/led_green/ ls brightness device max_brightness subsystem uevent 修改brightness的值即可改变led的亮灭 echo 0 > brightness 或 echo 1 > brightness 应用层使用系统调用即可以控制,或者编程控制该设备节点也可以。