基於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