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  中,利用

th79_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 ) 实现了绑定。

当按键时,则触发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%" ]
	],








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值