linux驱动之中断方式获取键值

linux驱动之中断方式获取键值

------------------------------------------------------------------------------------------------------------------------------------------------------

回想在单片机下的中断处理

  1. 分辨是哪个中断
  2. 调用处理函数
  3. 清中断

-------------------------------------------------------------------------------------------------------------------------------------------------------

1、中断框架

trap_int  中构造

trap_int函数就是一些跳转指令

b...

b...

b  vector_irq + stubs_offset   ;vector_irq是链接地址    stubs_offset 是偏移地址

vector_irq:

  1. 保存被中断的现场
  2. asm_do_IRQ
  3. 恢复现场
  4. 。。

asm_do_IRQ:

对中断的处理。

request_irq  注冊中断

request_irq(irq,handle,irqflags,devname,dev_id)

1、分配irqaction 结构

2、setup_irq(irq,action)

0、分配一个irqaction 结构

a、在irq_desc[irq]   -> action

b、desc -> chip ->settype

c、desc -> chip -> startup /enable

irq 要申请的硬件中断号 +
handle 是向系统登记的中断处理函数 是一个回调函数,主哦功能短发生时,系统将调用这个函数,并将dev_id传递给它
irqflags 是中断处理属性  

free_irq   卸载中断

free_irq(irq, dev_id)

1、出链

2、禁止中断

2、代码编写

1、申请IRQ

这里我要特殊说明下:(向我这样的小白在使用request_irq这个函数时遇到了一个问题就是  IRQ_EINT1、IRQ_EINT4、IRQ_EINT2、IRQ_EINT0这四个宏找不到,终于原因找到了 。我配置eclipse的时候内核树的路径有些小问题。

irqs.h这个文件是在 /linux-2.6.30.4/arch/arm/mach-s3c2410/include
这个路径下。我以为这个在mach-s3c2440也会有这个路径呢。事实上不然。另一些引脚的定义也在2410的路径里面,这里要注意一下 )。

	request_irq(IRQ_EINT1,buttons_irq, IRQ_TYPE_EDGE_BOTH,"key1",&pins_desc[0]);
	request_irq(IRQ_EINT4,buttons_irq, IRQ_TYPE_EDGE_BOTH,"key2",&pins_desc[1]);
	request_irq(IRQ_EINT2,buttons_irq, IRQ_TYPE_EDGE_BOTH,"key3",&pins_desc[2]);
	request_irq(IRQ_EINT0,buttons_irq, IRQ_TYPE_EDGE_BOTH,"key4",&pins_desc[3]);

这里的pins_desc 是一个结构体数组

struct pin_desc pins_desc[4] =
{
	{S3C2410_GPF1,0x01},
	{S3C2410_GPF4,0x02},
	{S3C2410_GPF2,0x03},
	{S3C2410_GPF0,0x04},
};

这个数组里面包括了这四个中断引脚和该引脚的键值。

结构体原型是这种:

struct pin_desc{
	unsigned int pin;
	unsigned int key_value;
};

2、写中断处理函数

static irqreturn_t buttons_irq(int irq,void *dev_id)
{
	struct pin_desc * pindesc = (struct pin_desc *) dev_id;
	unsigned int pinval;
	pinval = s3c2410_gpio_getpin(pindesc -> pin);
	if(pinval)//松开
	{
		keyval = 0x80|pindesc->key_value;
	}
	else
	{
		keyval = pindesc->key_value;
	}
	ev_press =1;//中断发生
	wake_up_interruptible(&button_wait_q);
	printk("button is pressed : %d \n",irq);
	return IRQ_HANDLED;
}

说明:

pindesc :这个结构体会得知是哪个按键按下了

s3c2410_gpio_getpin:这个函数是内核提供的函数,能够获得当前引脚的高低电平

ev_press:中断事件标志。中断服务程序将他置1,read函数将他置0

wake_up_interruptible(&button_wait_q);  会唤醒休眠的进程,唤醒注冊到等待队列上的进程,当中 button_wait_q是通过例如以下方式获得的:

static DECLARE_WAIT_QUEUE_HEAD(button_wait_q);  
生成一个等待队列头wait_queue_head_t,名字为 button_wait_q

3、改动read函数

ssize_t button_dev_read(struct file *file,char __user *buf,size_t size,loff_t *ppos)
{
	if(size !=1)
	{
		return -EINVAL;
	}
	/*假设没有按键动作发生  就休眠*/
	wait_event_interruptible(button_wait_q,ev_press);
	/*假设有按键动作发生,直接返回*/
	copy_to_user(buf,&keyval,1);
	ev_press = 0;
	return 0;
}

当中:

wait_event_interruptible(button_wait_q,ev_press);   假设ev_press是假的话。就会休眠

ev_press =0; 每次读完,都将中断事件标志清零。

4、填充file_operations结构体

static struct file_operations button_sdv_fops =
{
	.owner 		= THIS_MODULE,
	.open  		= button_dev_open,
	.read 		= button_dev_read,
	.release 	= button_dev_close,
};

当中button_dev_close函数为:

int button_dev_close(struct inode* inode ,struct file *file)
{
	free_irq(IRQ_EINT1,&pins_desc[0]);
	free_irq(IRQ_EINT4,&pins_desc[1]);
	free_irq(IRQ_EINT2,&pins_desc[2]);
	free_irq(IRQ_EINT0,&pins_desc[3]);

	return 0;
}

free_irq为释放中断。当中须要两个參数。

本代码在上一篇按键的博文基础上改动。

完整的驱动代码:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <asm/io.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <mach/regs-gpio.h>
#include <mach/irqs.h>//这个在/opt/EmbedSky/linux-2.6.30.4/arch/arm/mach-s3c2410/include/mach 路径
#include <linux/interrupt.h>

MODULE_LICENSE("Dual BSD/GPL");

static struct class *buttondrv_class;
static struct class_devices *buttondrv_class_dev;

/*  */
static DECLARE_WAIT_QUEUE_HEAD(button_wait_q);
/*中断事件标志,中断服务程序将他置1,read函数将他置0*/
static volatile int ev_press =0;

volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;

static unsigned keyval;

struct pin_desc{
	unsigned int pin;
	unsigned int key_value;
};
/*按键按下时是:0x01 0x02 0x03 0x04*/
/*按键松开时是:0x81 0x82 0x83 0x84*/
struct pin_desc pins_desc[4] =
{
	{S3C2410_GPF1,0x01},
	{S3C2410_GPF4,0x02},
	{S3C2410_GPF2,0x03},
	{S3C2410_GPF0,0x04},
};
/*
 * 确定按键值
 */
static irqreturn_t buttons_irq(int irq,void *dev_id)
{
	struct pin_desc * pindesc = (struct pin_desc *) dev_id;
	unsigned int pinval;
	pinval = s3c2410_gpio_getpin(pindesc -> pin);
	if(pinval)//松开
	{
		keyval = 0x80|pindesc->key_value;
	}
	else
	{
		keyval = pindesc->key_value;
	}
	ev_press =1;//中断发生
	wake_up_interruptible(&button_wait_q);
	printk("button is pressed : %d \n",irq);
	return IRQ_HANDLED;
}
static int button_dev_open(struct inode *inode ,struct file* file)
{
	//配置按键的引脚 GPF0,1,2,4为输入引脚
	request_irq(IRQ_EINT1,buttons_irq, IRQ_TYPE_EDGE_BOTH,"key1",&pins_desc[0]);
	request_irq(IRQ_EINT4,buttons_irq, IRQ_TYPE_EDGE_BOTH,"key2",&pins_desc[1]);
	request_irq(IRQ_EINT2,buttons_irq, IRQ_TYPE_EDGE_BOTH,"key3",&pins_desc[2]);
	request_irq(IRQ_EINT0,buttons_irq, IRQ_TYPE_EDGE_BOTH,"key4",&pins_desc[3]);

	return 0;
}
ssize_t button_dev_read(struct file *file,char __user *buf,size_t size,loff_t *ppos)
{
	if(size !=1)
	{
		return -EINVAL;
	}
	/*假设没有按键动作发生  就休眠*/
	wait_event_interruptible(button_wait_q,ev_press);
	/*假设有按键动作发生,直接返回*/
	copy_to_user(buf,&keyval,1);
	ev_press = 0;
	return 0;
}
int button_dev_close(struct inode* inode ,struct file *file)
{
	free_irq(IRQ_EINT1,&pins_desc[0]);
	free_irq(IRQ_EINT4,&pins_desc[1]);
	free_irq(IRQ_EINT2,&pins_desc[2]);
	free_irq(IRQ_EINT0,&pins_desc[3]);

	return 0;
}
static struct file_operations button_sdv_fops =
{
	.owner 		= THIS_MODULE,
	.open  		= button_dev_open,
	.read 		= button_dev_read,
	.release 	= button_dev_close,
};
int major;
static int button_dev_init(void)//入口函数
{
	major = register_chrdev(0,"button_drv",&button_sdv_fops);

	buttondrv_class = class_create(THIS_MODULE,"button_drv");
	if(IS_ERR(buttondrv_class))
		return PTR_ERR(buttondrv_class);
	buttondrv_class_dev= device_create(buttondrv_class,NULL,MKDEV(major,0),NULL,"wq_button");
		if(unlikely(IS_ERR(buttondrv_class_dev)))
			return PTR_ERR(buttondrv_class_dev);

	/*映射物理地址*/
	gpfcon = (volatile unsigned long *) ioremap(0x56000050 ,16);
	gpfdat = gpfcon + 1;

	return 0;
}
static void button_dev_exit(void)
{
	unregister_chrdev(major,"button_drv");
	device_unregister(buttondrv_class_dev);
	class_destroy(buttondrv_class);

	iounmap(gpfcon);
}
module_init(button_dev_init);
module_exit(button_dev_exit);

測试代码:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
/*
 * wq_device <dev> <on|off>
 */
int main(int argc, char **argv)
{
	int cnt=0;
	int fd;
	unsigned char key_val;
	fd = open("/dev/wq_button",	O_RDWR);
	if(fd<0)
	{
		printf("can't open \n");
	}
	while(1)
	{
		read(fd,&key_val,1);
		printf("key_val = 0x%x\n",key_val);
	}
	return 0;
}
时间: 2024-12-29 07:10:16

linux驱动之中断方式获取键值的相关文章

20150218【改进信号量】IMX257实现GPIO-IRQ中断按键获取键值驱动程序

[改进信号量]IMX257实现GPIO-IRQ中断按键获取键值驱动程序 2015-02-18 李海沿 前面我们使用POLL查询方式来实现GPIO-IRQ按键中断程序 这里我们来使用信号量,让我们的驱动同时只能有一个应用程序打开. 一.首先在前面代码的基础上来一个简单的信号 1.定义一个全局的整形变量 2.在打开函数中,每次进入打开函数canopen都自减1, 3.当我们不使用时,在realease 中canopen自加1 4.这样就实现了一个简单的信号量,我们编译,测试 当我们使用两个应用程序来

20150218【改进】IMX257实现GPIO-IRQ中断按键获取键值驱动程序

[改进]IMX257实现GPIO-IRQ中断按键获取键值驱动程序 2015-02-18 李海沿 一.使用struct pin_desc 管理按键的值 1.定义结构体 2.将前面我们申请中断时写的(void *)1修改为 &pins_desc[n] 在ioctl中,设置中断中修改 在key_release中释放中修改 3.在中断程序中利用我们定义的struc pins_desc判断并得到按键的值 4.得到按键键值后,唤醒程序,在read函数中返回键值 附上驱动源程序: 1 /***********

20150218【改进Poll定时查询】IMX257实现GPIO-IRQ中断按键获取键值驱动程序

[改进Poll定时查询]IMX257实现GPIO-IRQ中断按键获取键值驱动程序 2015-02-18 李海沿 按键驱动程序中,如果不使用read函数中使程序休眠的,而是还是使用查询方式的话,可以使用Poll函数,来控制一定时间内,如果有按键发生,则立即返回键值. 同时,poll也可以同时监控多个(比如说按键,鼠标,等)一旦发生事件则立即返回. 我们在linux查看帮助: 从帮助中的说明得知, poll, ppoll - wait for some event on a file descrip

在Linux下的中断方式读取按键驱动程序

// 在Linux下的中断方式读取按键驱动程序 //包含外部中断 休眠 加入poll机制 // 采用异步通知的方式 // 驱动程序发 ---> app接收 (通过kill_fasync()发送) // 为了使设备支持异步通知机制,驱动程序中涉及以下3项工作: // 1. 支持F_SETOWN命令,能在这个控制命令处理中设置filp->f_owner为对应进程ID. // 不过此项工作已由内核完成,设备驱动无须处理. // 2. 支持F_SETFL命令的处理,每当FASYNC标志改变时,驱动程序

Linux中函数ftok如何产生键值

我们在做linux 进程间通信开发时,经常会用到ftok函数去产文唯一键值,那么这个键值是如何产生的呢. 函数原型:key_t ftok( const char * fname, int id );应用:key_t key=ftok(".",'A'); fname为已经存在的文件名,本文为"."表示当前目录: id为子序号,值范围只有8bits(0-255). 下面我们举例说明如何产生键值,代码ftok_test.c如下: #include <stdio.h&

Linux驱动设计—— 中断与时钟

中断和时钟技术可以提升驱动程序的效率 中断 中断在Linux中的实现 通常情况下,一个驱动程序只需要申请中断,并添加中断处理函数就可以了,中断的到达和中断函数的调用都是内核实现框架完成的.所以程序员只要保证申请了正确的中断号及编写了正确的中断处理函数即可. 中断的宏观分类 1.硬中断 由系统硬件产生的中断.系统硬件通常引起外部事件.外部事件事件具有随机性和突发性,因此硬件中断也具有随机性和突发性. 2.软中断 软中断是执行中断指令时产生的.软中断不用外设施加中断请求信号,因此软中断的发生不是随机

17JavaScriptDOM动态获取键值对集合中的数据====小图与大图之间的显示问题

<script type="text/javascript"> var datas = { "mv/1-1.jpg": ["mv/1.jpg", "老牛", "163cm"], "mv/2-1.jpg": ["mv/2.jpg", "老马", "165cm"], "mv/3-1.jpg": [&q

Map获取键值,Map的几种遍历方法

Map 类提供了一个称为entrySet()的方法,这个方法返回一个Map.Entry实例化后的对象集.接着,Map.Entry类提供了一个 getKey()方法和一个getValue()方法,Map.Entry同时也提供了一个setValue()方法,程序员可以使用它修改map里面的值. 法一: Map<String, String> map = new HashMap(); for (Map.Entry entry : map.entrySet()) { Object key = entr

JSONObject遍历获取键值方法合并两个JSONObject

JSONObject obj1= new JSONObject(); try { obj1.put("obj1_data", obj1_data); if (null != obj2) {//obj2已有json数据 Iterator<String> sIterator = obj2.keys(); while (sIterator.hasNext()) { // 获得key String key = sIterator.next(); // 根据key获得value, v