实验现象:在控制台打印按键值,并且通过按键控制相应的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";