中断方式实现按键驱动

1.参考:input-programing.txt文件,里面有式例。从里面copy一个大概的模板出来。
2.我们板子所有的中断资源都在:arch/arm/mach-s5pv210/include/irqs.h,我们inlucde <asm/irq.h>就可以了,因为后面的头文件有包含前面的头文件。
3.request_irq
typedef irqreturn_t (*irq_handler_t)(int, void *);
request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,
const char *name, void *dev)
参数1:irq编号
参数2:中断处理函数
参数3:flag,中断的触发方式,高低电平触发/上下降沿触发
参数4:名字
参数5:dev,一般NULL
4.irq触发模式,linux/interrupt.h中定义的
#define IRQF_TRIGGER_NONE 0x00000000
#define IRQF_TRIGGER_RISING 0x00000001
#define IRQF_TRIGGER_FALLING 0x00000002
#define IRQF_TRIGGER_HIGH 0x00000004
#define IRQF_TRIGGER_LOW 0x00000008
#define IRQF_TRIGGER_MASK (IRQF_TRIGGER_HIGH | IRQF_TRIGGER_LOW |
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)
#define IRQF_TRIGGER_PROBE 0x00000010


5.cat /proc/interrupts ,可以看到我们系统里面所有被申请的中断,request_irq函数里面的name就是对应这里的。


实际试验中,在我们的x210开发板上,event1对应的是触摸屏。
cat /proc/interrupts,可以看到所以已经被使用了的中断。
当我们加载驱动之后,发现/dev/input/多了event2,说明我们的驱动是被
evdev.c匹配到的,加载了之后/sys/class/input/目录下面多了event2和input2.
cat /proc/interrupt:可以看到多了: 34: 0 s5p_vic_eint button-sw5


驱动源码:
#include <linux/input.h>
#include <linux/module.h>
#include <linux/init.h>

#include <asm/irq.h>
#include <asm/io.h>

#include <linux/gpio.h>

#include <linux/interrupt.h>

static struct input_dev *button_dev;
#define BUTTON_IRQ IRQ_EINT2

static irqreturn_t button_interrupt(int irq, void *dummy)
{
int flag;
s3c_gpio_cfgpin(S5PV210_GPH0(2), S3C_GPIO_SFN(0x0));
flag = gpio_get_value(S5PV210_GPH0(2));
s3c_gpio_cfgpin(S5PV210_GPH0(2), S3C_GPIO_SFN(0x0f));

input_report_key(button_dev, KEY_LEFT, flag);
input_sync(button_dev);
return IRQ_HANDLED;

}

static int __init s5pv210_button_init(void)
{
int error;

error = gpio_request(S5PV210_GPH0(2), "gph0_2");
if (error)
{
	printk(KERN_INFO "gpio_request fail\n");
}
//set to the eint2 mode
s3c_gpio_cfgpin(S5PV210_GPH0(2), S3C_GPIO_SFN(0x0f));

if (request_irq(BUTTON_IRQ, button_interrupt, IRQF_TRIGGER_FALLING, "button-sw5", NULL))
{
   	printk(KERN_ERR "button.c: Can't allocate irq %d\n", BUTTON_IRQ);
   	return -EBUSY;
}

button_dev = input_allocate_device();
if (!button_dev) {
	printk(KERN_ERR "button.c: Not enough memory\n");
	error = -ENOMEM;
	goto err_free_irq;
}

button_dev->evbit[0] = BIT_MASK(EV_KEY);
button_dev->keybit[BIT_WORD(KEY_LEFT)] = BIT_MASK(KEY_LEFT);

error = input_register_device(button_dev);
if (error) {
	printk(KERN_ERR "button.c: Failed to register device\n");
	goto err_free_dev;
}

return 0;

err_free_dev:
input_free_device(button_dev);
err_free_irq:
free_irq(BUTTON_IRQ, button_interrupt);
return error;
}

static void __exit s5pv210_button_exit(void)
{
input_unregister_device(button_dev);
free_irq(BUTTON_IRQ, button_interrupt);
}

module_init(s5pv210_button_init);
module_exit(s5pv210_button_exit);

// MODULE_xxx这种宏作用是用来添加模块描述信息
MODULE_LICENSE(“GPL”); // 描述模块的许可证
MODULE_AUTHOR(“Mark 867439374@qq.com”); // 描述模块的作者
MODULE_DESCRIPTION(“s5pv210 button driver”); // 描述模块的介绍信息
MODULE_ALIAS(“s5pv210_button”); // 描述模块的别名信息


1.申请gpio使用权限,设置gpio为外部中断模式
2.申请中断号,设置下降沿触发,并绑定中断触发函数
3.申请input_dev内存,动态分配
4.填充input_dev
设置事件类型为EV_KEY:
button_dev->evbit[0] = BIT_MASK(EV_KEY);
设置对应事件类型里面的值为:KEY_LEFT
button_dev->keybit[BIT_WORD(KEY_LEFT)] = BIT_MASK(KEY_LEFT);
5.input_register_device 注册input设备
中断函数中:
1.关中断,得到gpio的电平,再开中断
2.上报数据,上报sync包


1代表的是EV_KEY事件,105代表KEY_LEFT,gpio的值为0代表按下

type = 0x1
code = 0x69 = 105
value = 0x0

同步包
type = 0x0
code = 0x0
value = 0x0

1代表的是EV_KEY事件,105代表KEY_LEFT,gpio的值为1代表弹起
type = 0x1
code = 0x69
value = 0x1

同步包
type = 0x0
code = 0x0
value = 0x0

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值