首页 > 代码库 > 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 = http://www.mamicode.com/{
{
.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 = http://www.mamicode.com/{
{
.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控制实现
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。