OpenWRT中的按鍵和燈的GPIO控制實現 轉

2022-06-21 05:30:09 字數 2796 閱讀 2786

本文**:openwrt中的按鍵和燈的gpio控制實現

基於barrierbreaker版本,基於ar9331 ap121 demo單板 來進行描述

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 =,

, }

在初始化函式: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,就可以控制燈滅 。

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 =,

, }

在初始化函式: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中的按鍵和燈的GPIO控制實現

基於barrierbreaker版本,基於ar9331 ap121 demo單板 來進行描述 1.燈a.在mach ap121.c中,定義了燈所對應的gpio定義 define ap121 gpio led wlan0 define ap121 gpio led usb 1並定義了燈的gpio結構物...

關於LED燈和按鍵結合的專案

以下是程式 include stm32f10x gpio.h include stm32f10x rcc.h include stm32f10x.h include core cm3.h include misc.h define led0 gpio pin 8 define led1 gpio p...

mysql g MySQL中 g和 G的作用

g的作用和mysql中的分號 是一樣 g的作用是講查詢到的內容結構旋轉90度,變成縱向結構 下面舉例說明,查詢資料庫中的存在的儲存過程狀態 show procedure status like pricing g 具體如下所示 mysql show procedure status like pri...