Linux的input输入子系统:设备驱动之按键驱动
环境:kernel-2.6.30.4,arm-linux-gcc-4.3.3,目标板TQ2440
一、设备层驱动程序:
#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 <mach/regs-gpio.h>/*寄存器设置*/
#include <mach/hardware.h> /*hardware*/
struct pin_desc { //引脚描述,将一个引脚和一个中断号、名字、按键值进行绑定
int irq;
char *name;
unsigned int pin;
unsigned int key_val;
};
struct pin_desc pins_desc[4] = {//绑定四个引脚
{IRQ_EINT1, "K1", S3C2410_GPF1, KEY_L},
{IRQ_EINT4, "K2", S3C2410_GPF4, KEY_S},
{IRQ_EINT2, "K3", S3C2410_GPF2, KEY_ENTER},
{IRQ_EINT0, "K4", S3C2410_GPF0, KEY_LEFTSHIFT},
};
static struct input_dev *buttons_dev;
static struct pin_desc *irq_pd;
static struct timer_list buttons_timer;
//中断执行函数,当设定的4个外部中断被触发后将执行该函数
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
irq_pd = (struct pin_desc *)dev_id;//获得相应中断的相应参数
mod_timer(&buttons_timer, jiffies+HZ/100);//开启定时器,时间到将执行定时器处理函数,时间为10ms
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;
pinval = s3c2410_gpio_getpin(pindesc->pin);//获得发生中断的那个引脚的值
if (pinval)
{
input_event(buttons_dev, EV_KEY, pindesc->key_val, 0);//按下
input_sync(buttons_dev);
}
else
{
input_event(buttons_dev, EV_KEY, pindesc->key_val, 1);//松开
input_sync(buttons_dev);
}
}
static int buttons_open(struct input_dev *dev) { return 0;}
static void buttons_close(struct input_dev *dev) {}
static int buttons_init(void)
{
int i;
// 1. 分配一个input_dev结构体
buttons_dev = input_allocate_device();
//2. 设置
//2.1 能产生哪类事件
set_bit(EV_KEY, buttons_dev->evbit);//能产生按键类事件
set_bit(EV_REP, buttons_dev->evbit);//内产生重复类事件
//2.2 能产生这类操作里的哪些事件
set_bit(KEY_L, buttons_dev->keybit);
set_bit(KEY_S, buttons_dev->keybit);
set_bit(KEY_ENTER, buttons_dev->keybit);
set_bit(KEY_LEFTSHIFT, buttons_dev->keybit);
buttons_dev->name = "buttons_input";
buttons_dev->dev.init_name = "buttons_input";
buttons_dev->name = "buttons_input";
buttons_dev->dev.init_name = "buttons_input";
buttons_dev->open = buttons_open;
buttons_dev->close = buttons_close;
//3. 注册input_dev:将该设备加入到input_dev_list列表中,找寻和它匹配的input_handler
input_register_device(buttons_dev);
init_timer(&buttons_timer);//初始化一个定时器
buttons_timer.function = buttons_timer_function;//设置定时器的处理函数
add_timer(&buttons_timer);//将该定时器器加入到内核
//注册中断
//pins_desc参数就是当相应的中断发生,该值将成为对应中断函数的参数
for (i = 0; i < 4; i++)
{
request_irq(pins_desc[i].irq, buttons_irq, IRQ_TYPE_EDGE_BOTH, pins_desc[i].name, &pins_desc[i]);
}
printk("按键驱动输入子系统初始化成功!\n");
return 0;
}
static void buttons_exit(void)
{
int i;
for (i = 0;i < 4;i++)//中断注销
free_irq(pins_desc[i].irq, &pins_desc[i]);
del_timer(&buttons_timer);//删除定时器
input_unregister_device(buttons_dev);//注销驱动
input_free_device(buttons_dev);//释放内存
printk("按键驱动输入子系统卸载成功!\n");
}
module_init(buttons_init);
module_exit(buttons_exit);
MODULE_LICENSE("GPL");
Makefile:
ERN_DIR = /lib/modules/2.6.30.4-EmbedSky/build
all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
obj-m += buttons.o
二、测试程序
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <errno.h>
#include <linux/input.h>
int main(void)
{
int fd;
int key_value,i=0,count;
struct input_event ev_key;
fd = open("/dev/event1", 666);
if (fd < 0)
{
printf("can't open device buttons!\n");
exit(1);
}
for (;;)
{
count = read(fd,&ev_key,sizeof(struct input_event));
for(i=0; i<(int)count/sizeof(struct input_event); i++)
if(EV_KEY==ev_key.type)
printf("type:%d,code:%d,value:%d\n", ev_key.type,ev_key.code,ev_key.value);
if(EV_SYN==ev_key.type)
printf("syn event\n\n");
}
close(fd);
return 0;
}
我们也可以在开发板上:
#cat /dev/tty //事件将上传到shell程序的控制终端
源码下载:http://download.csdn.net/detail/hbuxiaofei/7903951
Linux的input输入子系统:设备驱动之按键驱动相关推荐
- ARM Linux内核Input输入子系统浅解
--以触摸屏驱动为例 第一章.了解linux input子系统 Linux输入设备总类繁杂,常见的包括有按键.键盘.触摸屏.鼠标.摇杆等等,他们本身就是字符设备,而linux内核将这些设备 ...
- Linux输入事件类型EV_SW,Linux的input输入子系统:总体框架
一.input输入子系统总体框架 Linux输入子系统将输入驱动抽象为三层:设备驱动层.核心层.事件处理层. 设备驱动层:将底层的硬件输入事件转化为统一事件形式,向输入核心(Input Core)汇报 ...
- linux内核input子系统解析,ARM Linux内核Input输入子系统浅解
struct list_head node; //该链表头用于将设备链接到input_dev_list }; Input_dev是一个很强大的结构体,它把所有的input设备(触摸 ...
- linux input输入子系统分析《三》:S3C2440的触摸屏驱动实例
1.1 本节阅读前提 本节的说明建立在前两节的基础之上,需要先阅读如下两篇章: linux input输入子系统分析<一>:初识input输入子系统 linux input输入子系统 ...
- Linux驱动:input输入子系统
input输入子系统 1.input输入子系统 1.1 简介 1.2 相关API函数 1.3 使用流程(驱动框架) 2.驱动示例 3.测试程序 4.测试结果 5.内核自带的input按键驱动 附:对应 ...
- linux input输入子系统分析《四》:input子系统整体流程全面分析
1 input输入子系统整体流程 本节分析input子系统在内核中的实现,包括输入子系统(Input Core),事件处理层(Event Handler)和设备驱动层.由于上节代码讲解了设备 ...
- linux input输入子系统分析《一》:初识input输入子系统
主要讲述本人在学习Linux内核input子系统的全部过程,如有分析不当,多谢指正.以下交流方式,文章欢迎转载,保留联系信息,以便交流. 邮箱:eabi010@gmail.com 主页:www.iel ...
- Linux驱动分析——input输入子系统
stm32mp157 盘古开发板 Linux内核版本4.19 目录 1.朱有鹏老师的视频课程笔记和应用测试代码: 2.input子系统架构分析 2.1.输入核心层源码分析 2.1.1.首先是核心模 ...
- linux input输入子系统分析《二》:s3c2440的ADC简单驱动实例分析
1 mini2440的ADC驱动实例 这节与输入子系统无关,出现在这里是因为后面的章节会讲到触摸屏输入子系统驱动,由于触摸屏也使用ADC,因此本节是为了说明ADC通过驱动代码是如何控制的. ...
最新文章
- 实例对象的属性和原型对象中的属性重名问题 神奇的原型链 继承 继承案例
- 让 Hangfire 使用 MongoDB 存储
- 2018.08.29 NOIP模拟 pmatrix(线性筛)
- 理解C++中拷贝构造函数
- Sourcetail 一款代码编辑神器,让看源码如丝般顺滑
- 使用文章来增加流量和排名
- 【JSOI2008】最大数
- Looking Back 2018
- 公司组织框架以及人员信息同步到钉钉相关解决方案
- canvas刷新_【前端冷知识】Canvas 滤镜的性能优化
- Windows、Linux 纷纷被爆漏洞,黑客可直取 root 权限!
- 3月11日Linux课程笔记
- bootstrap日期控件 只显示 年月
- C#海康解码器上大屏代码事例
- 网络编程释疑之:TCP半开连接的处理(好)
- python3_fake_User-Agent_创建UA伪装
- 怎么把kux格式转换成mp4?完美转换优酷kux格式
- ansys linux卸载干净,怎么把ansys删除干净
- element 表格,实现选择的行变色
- win7 计算机游戏不见了怎么办,Win10系统中的小游戏不见了怎么找回