嵌入式快速入门学习笔记-input输入子系统

input输入子系统介绍

Linux通过输入子系统统一管理键盘、鼠标、触摸屏等输入设备,同时也是为了将自己编写的驱动融入到QT中。

input输入子系统架构

在这里插入图片描述
最上层:输入事件驱动层((Event) Handlers)举例:evdev.c和mousedev.c和joydev.c
中间层:输入核心层(Input Core)举例:input.c
最下层:设备驱动层(Drivers)举例:drivers/input/xxx.c文件

input输入子系统开发方法

1)输入事件驱动层和输入核心层不需要动,只需更改设备驱动层
2)核心工作是对具体输入设备硬件的操作和性能调整

通过按键中断输入键盘字符

在进行代码编写前需注意:
1)将kernel/arch/arm/mach-s5pv210/button-x210.c中的代码注释掉(可全部注释),防止对自己写的代码产生误解。

button-x210.c是BSP包内核自带的按键代码,他是通过时间轮询扫描的方式进行按键识别。

1)在s3c_button_probe()函数中初始化定时器
init_timer(&timer);
timer.function = s3cbutton_timer_handler;

timer.expires = jiffies + (HZ / 100);
add_timer(&timer);
2)在s3cbutton_timer_handler()函数中restart定时器
mod_timer(&timer, jiffies + (HZ / 100));

本次实验中是使用中断的方式判断按键并上传输入子系统,如果不注释掉button-x210.c文件,虽然申请的按键中断可以覆盖button-x210.c文件对该按键的定义,但没有申请的按键还是会通过时间轮询扫描识别。

2)

 UP     -> KP_COL0 -> GPH2_0
 RIGHT  -> KP_COL1 -> GPH2_1
 MENU   -> KP_COL3 -> GPH2_3 (KEY_A)
 BACK   -> KP_COL2 -> GPH2_2 (KEY_B)
 上面几个按键属于keypad列扫描,不是EINT类型

添加定时器消抖进行按键识别驱动代码

#include <linux/input.h> 
#include <linux/module.h> 
#include <linux/init.h>
#include <asm/irq.h> 
#include <asm/io.h>

#include <mach/irqs.h>			// arch/arm/mach-s5pv210/include/mach/irqs.h
#include <linux/interrupt.h>
#include <linux/gpio.h>

/*
 * X210:
 *
 * POWER  -> EINT1   -> GPH0_1
 * LEFT   -> EINT2   -> GPH0_2
 * DOWN   -> EINT3   -> GPH0_3
 * UP     -> KP_COL0 -> GPH2_0
 * RIGHT  -> KP_COL1 -> GPH2_1
 * MENU   -> KP_COL3 -> GPH2_3 (KEY_A)
 * BACK   -> KP_COL2 -> GPH2_2 (KEY_B)
 */

#define BUTTON_IRQ	IRQ_EINT2

static struct input_dev *button_dev;//定义全局input_dev变量
static struct timer_list button_timer;//定义全局时钟变量

//在按键中断函数中启动定时器进行消抖
static irqreturn_t button_interrupt(int irq, void *dev_id)
{
	//10ms后启动定时器处理函数
	mod_timer(&button_timer, jiffies + HZ / 100);
	return IRQ_RETVAL(IRQ_HANDLED);
}
 
static void button_timer_function(unsigned long data) 
{ 
	unsigned int pinval;

	s3c_gpio_cfgpin(S5PV210_GPH0(2), S3C_GPIO_SFN(0x0));		// input模式
	pinval = gpio_get_value(S5PV210_GPH0(2));					//获取GPIO值
	s3c_gpio_cfgpin(S5PV210_GPH0(2), S3C_GPIO_SFN(0x0f));		// eint2模式

	input_report_key(button_dev, KEY_Z, !pinval);//上报定义的按键值
	input_sync(button_dev);
}

static int __init button_init(void) 
{ 
	int error;
	
	//1.分配一个input_dev结构体
	button_dev = input_allocate_device();
	if (!button_dev) 
	{ 
		printk(KERN_ERR "key-s5pv210.c: Not enough memory\n");
		error = -ENOMEM;
		goto err_free_irq; 
	}
		
	//2.设置
	//产生哪类事件 EV_REP:可长按
	button_dev->evbit[0] = BIT_MASK(EV_KEY);
	//产生哪些操作
	button_dev->keybit[BIT_WORD(KEY_Z)] = BIT_MASK(KEY_Z);

	//3.注册
	error = input_register_device(button_dev);
	if (error) 
	{ 
		printk(KERN_ERR "key-s5pv210.c: Failed to register device\n");
		goto err_free_dev; 
	}
	
	//4.硬件相关操作
	//定时器
	init_timer(&button_timer);
	button_timer.function = button_timer_function;
	add_timer(&button_timer);
	//GPIO申请
	error = gpio_request(S5PV210_GPH0(2), "GPH0_2");
	if(error)
		printk("key-s5pv210: request gpio GPH0(2) fail");
	s3c_gpio_cfgpin(S5PV210_GPH0(2), S3C_GPIO_SFN(0x0f));		// eint2模式
	//中断申请
	if (request_irq(BUTTON_IRQ, button_interrupt, IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, "button-x210", NULL)) 
	{ 
		printk(KERN_ERR "key-s5pv210.c: Can't allocate irq %d\n", BUTTON_IRQ);
		return -EBUSY; 
	}
	
	return 0;
	
err_free_dev:
	input_free_device(button_dev);
err_free_irq:
	free_irq(BUTTON_IRQ, button_interrupt);
	return error; 
}

static void __exit button_exit(void) 
{
	del_timer(&button_timer);
	input_unregister_device(button_dev); 
	free_irq(BUTTON_IRQ, button_interrupt); 
}

module_init(button_init); 
module_exit(button_exit); 

MODULE_LICENSE("GPL");

验证

1)

ls /dev/event* -l

通过该指令查看insmod前后是否有新的event生成
2)

exec 0</dev/tty1

通过该指令查看按键上报的键值是否正确

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值