1. 程式人生 > >OpenWRT中的按鍵和燈的GPIO控制實現_轉 OpenWRT中的按鍵和燈的GPIO控制實現

OpenWRT中的按鍵和燈的GPIO控制實現_轉 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應用文件,推薦學習。