linux gpio_keys framework 用法

1. 先將 menuconfig 裡 GPIO Buttons 打開 
Device Drivers  --->  Input device support  --->  Keyboards  --->  GPIO Buttons

2. 將 gpio-key 定義好, 並註冊.

struct gpio_keys_button {
/* Configuration parameters */
int code; /* input event code (KEY_*, SW_*) */
int gpio;
int active_low;
char *desc;
int type; /* input event type (EV_KEY, EV_SW) */
int wakeup; /* configure the button as a wake-up source */
int debounce_interval; /* debounce ticks interval in msecs */
bool can_disable;
};

struct gpio_keys_platform_data {
struct gpio_keys_button *buttons;
int nbuttons;
unsigned int poll_interval; /* polling interval in msecs -
  for polling driver only */
unsigned int rep:1; /* enable input subsystem auto repeat */
int (*enable)(struct device *dev);
void (*disable)(struct device *dev);
};

#include
#include

static struct gpio_keys_button bfin_gpio_keys_table[] = {
{
.code = KEY_POWER, /* See include/linux/input.h */
.gpio = REV_DET0, /* GPIO number */
.active_low = 1,
.desc = "Power", /* Button description*/
.wakeup = 0,
.debounce_interval = 100,
},
{
.code = KEY_PAUSE, /* See include/linux/input.h */
.gpio = REV_DET2, /* GPIO number */
.active_low = 1,
.desc = "Pause", /* Button description*/
.wakeup = 0,
.debounce_interval = 100,
}
};

static struct gpio_keys_platform_data bfin_gpio_keys_data = {
.buttons  = bfin_gpio_keys_table,
.nbuttons = ARRAY_SIZE(bfin_gpio_keys_table),
};

static struct platform_device bfin_device_gpiokeys = {
.name = "gpio-keys",
.id   = -1,
.dev = {
.platform_data = &bfin_gpio_keys_data,
},
};

platform_device_register(&bfin_device_gpiokeys);

3. 編好 kernel 後, 你會看到 /dev/input/event0, 表示你 ...  成功了 ...

4. hexdump -C /dev/event0, 可以看到 event 的資訊

00000000  fa f9 84 52 52 e1 03 00  01 00 74 00 01 00 00 00  |...RR.....t.....|
00000010  fa f9 84 52 57 e1 03 00  00 00 00 00 00 00 00 00  |...RW...........|
00000020  fa f9 84 52 b8 ae 06 00  01 00 74 00 00 00 00 00  |...R......t.....|
00000030  fa f9 84 52 bb ae 06 00  00 00 00 00 00 00 00 00  |...R............|
00000040  fa f9 84 52 2a 6a 0b 00  01 00 74 00 01 00 00 00  |...R*j....t.....|

 或是利用 evtest 來看 ... 
/ # ./evtest /dev/input/event0 
Input driver version is 1.0.1
Input device ID: bus 0x19 vendor 0x1 product 0x1 version 0x100
Input device name: "gpio-keys"
Supported events:
 Event type 0 (Sync)
 Event type 1 (Key)
Event code 116 (Power)
Event code 119 (Pause)
Testing ... (interrupt to exit)

Event: time 1384446679.843564, type 1 (Key), code 116 (Power), value 1
Event: time 1384446679.843570, -------------- Report Sync ------------
Event: time 1384446680.004220, type 1 (Key), code 116 (Power), value 0
Event: time 1384446680.004223, -------------- Report Sync ------------

PS:evtest 請參考 http://processors.wiki.ti.com/index.php/Evtest

留言

這個網誌中的熱門文章

NMEA標準格式 -- GPS

網路 Transformer 的用途

cut,sed,awk 字串處理