歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux輸入子系統(input subsystem)之按鍵輸入和LED控制

Linux輸入子系統(input subsystem)之按鍵輸入和LED控制

日期:2017/3/1 9:10:54   编辑:Linux編程

實驗現象:在控制台打印按鍵值,並且通過按鍵控制相應的LED亮滅。

1.代碼

input_subsys_drv.c

#include <linux/module.h>
#include <linux/version.h>

#include <linux/init.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/sched.h>
#include <linux/pm.h>
#include <linux/sysctl.h>
#include <linux/proc_fs.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/input.h>
#include <linux/irq.h>

#include <asm/gpio.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>


struct pin_desc{
int irq;
char *name;
unsigned int pin;
unsigned int key_val;
};

struct pin_desc pins_desc[4] = {
{IRQ_EINT0, "S2", S3C2410_GPF0, KEY_L},
{IRQ_EINT2, "S3", S3C2410_GPF2, KEY_S},
{IRQ_EINT11, "S4", S3C2410_GPG3, KEY_ENTER},
{IRQ_EINT19, "S5", S3C2410_GPG11, KEY_LEFTSHIFT},
};

static struct input_dev *input_subsys_dev;
static struct pin_desc *irq_pd;
static struct timer_list buttons_timer;

static irqreturn_t buttons_irq(int irq, void *dev_id)
{
/* [cgw]: 按鍵IO發生邊沿中斷時重新設置定時間隔
* 用於按鍵消抖
*/
irq_pd = (struct pin_desc *)dev_id;
buttons_timer.data = irq_pd->pin;
mod_timer(&buttons_timer, jiffies+HZ/100);
return IRQ_RETVAL(IRQ_HANDLED);
}

static void buttons_timer_function(unsigned long data)
{
struct pin_desc * pindesc = irq_pd;
unsigned int pinval;

if (!pindesc)
return;

/* [cgw]: 獲取按鍵IO狀態 */
pinval = s3c2410_gpio_getpin((unsigned int)data);

/* [cgw]: 根據按鍵IO狀態上報按鍵事件 */
if (pinval)
{
/* [cgw]: 上報按鍵彈起 */
input_report_key(input_subsys_dev, pindesc->key_val, 0);
//input_sync(input_subsys_dev);
}
else
{
/* [cgw]: 上報按鍵按下 */
input_report_key(input_subsys_dev, pindesc->key_val, 1);
//input_sync(input_subsys_dev);
}

//printk("timer occur!\n");
}


static int led_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
{
printk("led event!\n");
printk("value: 0x%x\n", value);

/* [cgw]: 根據應用程序下發的LED控制事件
* 亮滅LED
*/
//if (code == SND_BELL) {
if (code == LED_MUTE) {
if (value == 0xAA) {
/* [cgw]: 點亮 */
s3c2410_gpio_setpin(S3C2410_GPF4, 0);
} else if (value == 0xEE) {
/* [cgw]: 熄滅 */
s3c2410_gpio_setpin(S3C2410_GPF4, 1);
}

return 0;
}

return -1;
}

int input_subsys_open(struct input_dev *dev)
{
int i, retval;

/* [cgw]: 設置按鍵IO為中斷輸入 */
s3c2410_gpio_cfgpin(S3C2410_GPF0, S3C2410_GPF0_EINT0);
s3c2410_gpio_cfgpin(S3C2410_GPF2, S3C2410_GPF2_EINT2);
s3c2410_gpio_cfgpin(S3C2410_GPG3, S3C2410_GPG3_EINT11);
s3c2410_gpio_cfgpin(S3C2410_GPG11, S3C2410_GPG11_EINT19);

/* [cgw]: 設置LED IO為輸出,初始為熄滅LED */
s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);
s3c2410_gpio_setpin(S3C2410_GPF4, 1);

s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);
s3c2410_gpio_setpin(S3C2410_GPF5, 1);

s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);
s3c2410_gpio_setpin(S3C2410_GPF5, 1);

/* [cgw]: 為按鍵IO分配中斷線 */
for (i = 0; i < 4; i++)
{
retval = request_irq(pins_desc[i].irq, buttons_irq, IRQT_BOTHEDGE, pins_desc[i].name, &pins_desc[i]);
}

printk("input subsys open!\n");

return 0;
}

static int input_subsys_init(void)
{
/* [cgw]: 分配一個輸入設備 */
input_subsys_dev = input_allocate_device();
input_subsys_dev->name = "input_subsys_dev";

/* [cgw]: 設置支持的事件類型 */
set_bit(EV_KEY, input_subsys_dev->evbit);
//set_bit(EV_REP, input_subsys_dev->evbit);

set_bit(EV_LED, input_subsys_dev->evbit);
//set_bit(EV_SND, input_subsys_dev->evbit);

/* [cgw]: 設置支持的事件碼 */
set_bit(KEY_L, input_subsys_dev->keybit);
set_bit(KEY_S, input_subsys_dev->keybit);
set_bit(KEY_ENTER, input_subsys_dev->keybit);
set_bit(KEY_LEFTSHIFT, input_subsys_dev->keybit);

set_bit(LED_MUTE, input_subsys_dev->ledbit);
//set_bit(SND_BELL, input_subsys_dev->sndbit);

/* [cgw]: 分配輸入設備的open方法,用戶操作open(/dev/xxx, ...)時調用 */
input_subsys_dev->open = input_subsys_open;
/* [cgw]: 分配輸入設備的event方法,用戶在應用程序write()時 */
input_subsys_dev->event = led_event;

/* [cgw]: 注冊輸入設備 */
input_register_device(input_subsys_dev);

/* [cgw]: 初始化定時器,用於按鍵消抖 */
init_timer(&buttons_timer);
buttons_timer.function = buttons_timer_function;
add_timer(&buttons_timer);

printk("input subsys init!\n");

return 0;
}

static void input_subsys_exit(void)
{
int i;

/* [cgw]: 釋放按鍵IO中斷 */
for (i = 0; i < 4; i++)
{
free_irq(pins_desc[i].irq, &pins_desc[i]);
}

/* [cgw]: 刪除定時器 */
del_timer(&buttons_timer);
/* [cgw]: 注銷輸入設備 */
input_unregister_device(input_subsys_dev);
/* [cgw]: 釋放輸入設備內存空間 */
input_free_device(input_subsys_dev);
}

module_init(input_subsys_init);

module_exit(input_subsys_exit);

MODULE_LICENSE("GPL");

input_subsys_test.c

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>

#include <linux/input.h>

int fd;

void my_signal_fun(int signum)
{
struct input_event buttons_event, leds_event;

/* [cgw]: 異步通知產生時返回的數據 */
read(fd, &buttons_event, sizeof(struct input_event));

/* [cgw]: 打印事件類型,事件碼,事件值 */
printf("type: 0x%x code: 0x%x value: 0x%x\n",
buttons_event.type,
buttons_event.code,
buttons_event.value);

/* [cgw]: 返回的是KEY_L或KEY_S值 */
if (buttons_event.code == KEY_L || buttons_event.code == KEY_S) {
/* [cgw]: 按鍵彈起 */
if (buttons_event.value == 0) {

/* [cgw]: 構造一個EV_LED事件 */

//leds_event.type = EV_SND;
leds_event.type = EV_LED;
//leds_event.code = SND_BELL;
leds_event.code = LED_MUTE;

/* [cgw]: KEY_L和KEY_S控制LED的亮滅 */
if (buttons_event.code == KEY_L) {
leds_event.value = 0xAA;
} else if (buttons_event.code == KEY_S) {
leds_event.value = 0xEE;
}

/* [cgw]: 發送LED控制事件 */
write(fd, &leds_event, sizeof(struct input_event));

printf("led write!\n");
}
}
}

int main(int argc, char **argv)
{
int Oflags;

/* [cgw]: 設置需要處理的信號SIGIO,即輸入文件會請求一個SIGIO
* 信號,當有新數據到來這個信號會發給filp->f_owner進程
*/
signal(SIGIO, my_signal_fun);

fd = open("/dev/event1", O_RDWR | O_NONBLOCK);

//printf("fd = 0x%x\n", fd);

if (fd < 0)
{
printf("can't open!\n");
}

/* [cgw]: 根據文件標識符fd,設置能夠獲得這個文件的進程(owner)
* getpid()獲得當前進程ID
*/
fcntl(fd, F_SETOWN, getpid());

/* [cgw]: 獲得file->f_flags標志 */
Oflags = fcntl(fd, F_GETFL);

/* [cgw]: 置位FASYNC使能異步通知 */
fcntl(fd, F_SETFL, Oflags | FASYNC);

while (1)
{
/* [cgw]: 休眠 */
sleep(1000);
}

return 0;
}

makefile

KERN_DIR = /work/system/linux-2.6.22.6

all:
make -C $(KERN_DIR) M=`pwd` modules

clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order

obj-m += input_subsys_drv.o

2. 實驗

2.1

安裝驅動程序:

insmod input_subsys_drv.ko

1 # insmod input_subsys_drv.ko
2 input: input_subsys_dev as /class/input/input1
3 input subsys open!
4 input subsys init!

運行應用程序

./input_subsys_test

# ./input_subsys_test
type: 0x1 code: 0x26 value: 0x1
type: 0x1 code: 0x26 value: 0x0
led event!
value: 0xaa
led write!
type: 0x11 code: 0x7 value: 0xaa
type: 0x1 code: 0x1f value: 0x1
type: 0x1 code: 0x1f value: 0x0
led event!
value: 0xee
led write!
type: 0x11 code: 0x7 value: 0xee

3. 現象分析

按一下按鍵KEY_L,終端輸出:

type: 0x1 code: 0x26 value: 0x1  //按鍵按下
type: 0x1 code: 0x26 value: 0x0  //按鍵彈起
led event!                       //應用程序操作write, 發送LED控制事件,調用led_event()
value: 0xaa                      //讀到的LED亮的命令
led write!                       //返回應用程序繼執行
type: 0x11 code: 0x7 value: 0xaa //因為應用程序發送了事件給驅動程序,驅動把這個事件作為一個輸入事件(相當於按鍵輸入事件),同樣通過異步通知反饋到應用程序

The end!

Copyright © Linux教程網 All Rights Reserved