OpenWRT中的按键和灯的GPIO控制实现_转

本文转自:OpenWRT中的按键和灯的GPIO控制实现

基于BarrierBreaker版本,基于AR9331 AP121 Demo单板 来进行描述

1.灯

A.在mach-ap121.c中,定义了灯所对应的GPIO定义:

#define AP121_GPIO_LED_WLAN 0

#define AP121_GPIO_LED_USB 1

 并定义了灯的GPIO结构对象:

static struct gpio_led ap121_leds_gpio[] __initdata = {
{
.name    = "ap121:green:usb",
.gpio    = AP121_GPIO_LED_USB,
.active_low    = 0,
},
{
.name    = "ap121:green:wlan",
.gpio    = AP121_GPIO_LED_WLAN,
.active_low    = 0,
},
 }

在初始化函数:ap121_setup 中,利用ath79_register_leds_gpio(-1, ARRAY_SIZE(ap121_leds_gpio), ap121_leds_gpio);实现了LED device的注册。此函数调用后,会创建platform类型的设备,并和leds-gpio驱动(leds-gpio.c)实现了绑定。这样,就会在/sys/devices/platform/leds-gpio/目录中,产生对应的led灯的控制目录:

drwxr-xr-x    2 root     root             0 Jan  1  1970 ap121:green:usb
drwxr-xr-x    2 root     root             0 Jan  1  1970 ap121:green:wlan
B.进入上述任意一个目录,如:ap121:green:wlan,会有如下文件:
-rw-r--r--    1 root     root          4096 Jan 15 06:19 brightness 
lrwxrwxrwx    1 root     root             0 Jan 15 06:04 device -> ../../../leds-gpio
-r--r--r--    1 root     root          4096 Jan 15 06:04 max_brightness
lrwxrwxrwx    1 root     root             0 Jan 15 06:04 subsystem -> ../../../../../class/leds
-rw-r--r--    1 root     root          4096 Jan 15 06:04 trigger
-rw-r--r--    1 root     root          4096 Jan 15 06:04 uevent

则通过 echo 1 > brightness,就可以控制灯亮; echo 0 > brightness,就可以控制灯灭 。

 2.按键

A.在mach-ap121.c中,定义了按键对应的GPIO以及数据结构对象:

#define AP121_GPIO_BTN_JUMPSTART 11

#define AP121_GPIO_BTN_RESET 12

 以及

static struct gpio_keys_button ap121_gpio_keys[] __initdata = {
{
.desc    = "jumpstart button",
.type    = EV_KEY,
.code    = KEY_WPS_BUTTON, //定义在gpio-button-hotplug.c
.debounce_interval = AP121_KEYS_DEBOUNCE_INTERVAL,
.gpio    = AP121_GPIO_BTN_JUMPSTART,
.active_low    = 1,
},
{
.desc    = "reset button",
.type    = EV_KEY,
.code    = KEY_RESTART,    //定义在gpio-button-hotplug.c
.debounce_interval = AP121_KEYS_DEBOUNCE_INTERVAL,
.gpio    = AP121_GPIO_BTN_RESET,
.active_low    = 1,
},
 }

 在初始化函数:ap121_setup 中,利用

ath79_register_gpio_keys_polled(-1, AP121_KEYS_POLL_INTERVAL,ARRAY_SIZE(ap121_gpio_keys),ap121_gpio_keys);

实现了KEY device的注册。此函数调用后,会创建platform类型的设备,并和gpio-keys-polled驱动(gpio-button-hotplug.c
)实现了绑定。

B. 当按键时,则触发button_hotplug_event函数(gpio-button-hotplug.c):调用button_hotplug_create_event产生uevent事件,调用button_hotplug_fill_even填充事件(JSON格式),并最终调用button_hotplug_work发出uevent广播

上述广播,被procd进程中的hotplug_handler (procd/plug/hotplug.c) 收到,并根据etc/hotplug.json中预先定义的JSON内容匹配条件,定位到对应的执行函数,具体为:
[ "if",
[ "and",
[ "has", "BUTTON" ],
[ "eq", "SUBSYSTEM", "button" ],
],
[ "exec", "/etc/rc.button/%BUTTON%" ]
],
[ "if",
[ "eq", "SUBSYSTEM",
[ "net", "input", "usb", "ieee1394", "block", "atm", "zaptel", "tty", "button" ]
],
[ "exec", "/sbin/hotplug-call", "%SUBSYSTEM%" ]
],

在openwrt的GPIO系统上,主要使用了如下技术点:

driver-device-platform, uevent,procd,JSON,UCI;当然还需要读懂对应芯片的Datasheet 

注:博主有很多openwrt应用文档,推荐学习。

原文地址:https://www.cnblogs.com/embedded-linux/p/10200779.html