linux驱动学习笔记---按键中断程序(四)

如何申请中断

中断的产生原理

 在加载函数中申请中断

	irqno = IRQ_EINT(1);
	//参数1--中断号码
			//获取中断号码的方法: 1,外部中断IRQ_EINT(x)  
			// 2, 头文件 #include <mach/irqs.h>  #include <plat/irqs.h>
	//参数2--中断的处理方法
	//参数3--中断触发方式:  高电平,低电平,上升沿,下降沿,双边沿
	/*
		#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
	*/
	//参数4--中断的描述-自定义字符串
	//参数5--传递个参数2的任意数据
	//返回值0表示正确
	ret = request_irq(irqno, key_irq_handler, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "eint1-downkey", &testdata);
	if(ret != 0)
	{
		printk("request_irq error\n");
		return ret;
	}

 在卸载函数中释放中断

// 释放中断
	//参数1--中断号码
	//参数5--和request_irq第5个参数保持一致
	free_irq(irqno, &testdata);

 实现中断处理函数

//中断处理方法
//参数1--当前产生的中断的号码
//参数2--request_irq传递过来的参数
irqreturn_t key_irq_handler(int irqno, void *dev_id)
{
	int *p = (int *)dev_id;
	//printk("-----------%s-------0x%x-----\n", __FUNCTION__, *p);

	//区分按下还是抬起
	int value = gpio_get_value(S5PV210_GPH0(1));
	if(value)
	{
		//抬起
		printk("<kernel>--down_key2 : release\n");
		//填充值
		event.code = KEY_DOWN;
		event.value = 0;
		
	}else
	{
		//按下
		 printk("<kernel>--down_key2 : pressed\n");
		event.code = KEY_DOWN;
		event.value = 1;
	}

	have_data = 1;//表示有数据了
	wake_up_interruptible(&wq_head);
	
	
	
	return IRQ_HANDLED;
}

 实现应用读取到按键数据功能

 设计一个对象--描述按键产生的数据包(这个数据包在应用层中也需要有一个相同的,相当于一个通信协议)

struct key_event{
	int code; //什么按键: 左键, 回车键
	int value; // 按键的状态: 按下/抬起 (1/0)
};

//相当于一个数据包--给用户的
static struct key_event event;

 open函数中进行清零操作,确定打开设备节点时没有数据

int key_drv_open (struct inode *inode, struct file *filp)
{
	printk("--------^_*  %s-------\n", __FUNCTION__);

	 memset(&event, 0, sizeof(struct key_event));
	have_data = 0; //为假--一开始都没有按键按下或者抬起

	
	return 0;
}

 一旦产生中断,就会给数据包赋值

//区分按下还是抬起,获取寄存器中的高低电位
int value = gpio_get_value(S5PV210_GPH0(1));

//中断处理方法
//参数1--当前产生的中断的号码
//参数2--request_irq传递过来的参数
irqreturn_t key_irq_handler(int irqno, void *dev_id)
{
	int *p = (int *)dev_id;
	//printk("-----------%s-------0x%x-----\n", __FUNCTION__, *p);

	//区分按下还是抬起
	int value = gpio_get_value(S5PV210_GPH0(1));
	if(value)
	{
		//抬起
		printk("<kernel>--down_key2 : release\n");
		//填充值
		event.code = KEY_DOWN;
		event.value = 0;
		
	}else
	{
		//按下
		 printk("<kernel>--down_key2 : pressed\n");
		event.code = KEY_DOWN;
		event.value = 1;
	}

	have_data = 1;//表示有数据了
	wake_up_interruptible(&wq_head);
	
	
	
	return IRQ_HANDLED;
}

 读完之后进行清零操作

	ret  = copy_to_user(buf, &event, count);
	if(ret > 0)
	{
		printk("copy_to_user error\n");
		return -EFAULT;
	}
	//清零,以备下次充值
	memset(&event, 0, sizeof(struct key_event));

实现阻塞功能 

要想实现阻塞功能就是需要将程序休眠,将进程调度让出来,否则read就会一直读,导致cpu使用率过高

wake_up_interruptible作为例子分析。定义如下:

#define wake_up_interruptible(x)   __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)

这里的参数x即要唤醒的等待队列对应的等待队列头。唤醒TASK_INTERRUPTIBLE类型的进程并且默认唤醒该队列上所有非独占等待进程和一个独占等待进程。

__wake_up定义如下:

/**

 * __wake_up - wake up threads blocked on a waitqueue.

 * @q: the waitqueue

 * @mode: which threads

 * @nr_exclusive: how many wake-one or wake-many threads to wake up

 * @key: is directly passed to the wakeup function

 */

void fastcall __wake_up(wait_queue_head_t *q, unsigned int mode,

                                                                           int nr_exclusive, void *key)

{

unsigned long flags;

spin_lock_irqsave(&q->lock, flags);

__wake_up_common(q, mode, nr_exclusive, 1, key);

spin_unlock_irqrestore(&q->lock, flags);

preempt_check_resched_delayed();

}

__wake_up 简单的调用__wake_up_common进行实际唤醒工作。

__wake_up_common定义如下:

/*

 * The core wakeup function.  Non-exclusive wakeups (nr_exclusive == 0) just

 * wake everything up.  If it's an exclusive wakeup (nr_exclusive == small +ve

 * number) then we wake all the non-exclusive tasks and one exclusive task.

 *

 * There are circumstances in which we can try to wake a task which has already

 * started to run but is not in state TASK_RUNNING.  try_to_wake_up() returns

 * zero in this (rare) case, and we handle it by continuing to scan the queue.

 */

static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,

                                                                                int nr_exclusive, int sync, void *key)

{

struct list_head *tmp, *next;

list_for_each_safe(tmp, next, &q->task_list) {

wait_queue_t *curr = list_entry(tmp, wait_queue_t, task_list);

unsigned flags = curr->flags;

if (curr->func(curr, mode, sync, key) &&

(flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)

break;

}

}

__wake_up_common循环遍历等待队列内的所有元素,分别执行其对应的唤醒函数。

这里的唤醒函数即先前定义等待队列项DEFINE_WAIT(__wait)时默认初始化的autoremove_wake_function函数。autoremove_wake_function最终会调用try_to_wake_up函数将进程置为TASK_RUNNING状态。这样后面的进程调度便会调度到该进程,从而唤醒该进程继续执行。

Reference:

1)       OReilly.Understanding.the.Linux.Kernel.3rd.Edition.Nov.2005.HAPPY.NEW.YEAR

2)       Linux 2.6.18_Pro500 (Montavista)

 在加载函数中定义一个等待队列头

	// 定义一个等待队列头,并且初始化
	init_waitqueue_head(&wq_head);

 在read函数中休眠

ssize_t key_drv_read(struct file *filp, char __user *buf,  size_t count, loff_t *fpos)
{
	
	int ret;

	// 在资源不可达的时候,进行休眠
	// 参数1---当前驱动中的等待队列头
	// 参数2--休眠的条件: 假的话就休眠,真就不休眠
	wait_event_interruptible(wq_head, have_data);

	ret  = copy_to_user(buf, &event, count);
	if(ret > 0)
	{
		printk("copy_to_user error\n");
		return -EFAULT;
	}
	//清零,以备下次充值
	memset(&event, 0, sizeof(struct key_event));
	have_data = 0; //没有数据了,等待下一次数据
	
	return count;
}

 在中断处理函数中唤醒

wake_up_interruptible(&wq_head);

 创建一个标志have_data,当打开设备节点时清零,这样应用层运行到read函数就会休眠,当中断启动,给have_data赋值,并唤醒进程,一旦进程调度调度到该进程,就会成功读取中断,做出反映,同时将have_data清理等待下次中断.

static int have_data; //表示当前是否有按键标志,

 完整驱动代码

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
#include <linux/input.h>
#include <linux/wait.h>
#include <linux/sched.h>

#include <asm/uaccess.h>

// 设计一个对象--描述按键产生的数据包
struct key_event{
	int code; //什么按键: 左键, 回车键
	int value; // 按键的状态: 按下/抬起 (1/0)
};



static int key_major = 0;
static struct class *key_cls;
static int testdata = 0x12345;
static unsigned int irqno ;
static wait_queue_head_t wq_head;
static int have_data; //表示当前是否有按键标志,


//相当于一个数据包--给用户的
static struct key_event event;


int key_drv_open (struct inode *inode, struct file *filp)
{
	printk("--------^_*  %s-------\n", __FUNCTION__);

	 memset(&event, 0, sizeof(struct key_event));
	have_data = 0; //为假--一开始都没有按键按下或者抬起

	
	return 0;
}

ssize_t key_drv_read(struct file *filp, char __user *buf,  size_t count, loff_t *fpos)
{
	
	int ret;

	// 在资源不可达的时候,进行休眠
	// 参数1---当前驱动中的等待队列头
	// 参数2--休眠的条件: 假的话就休眠,真就不休眠
	wait_event_interruptible(wq_head, have_data);

	ret  = copy_to_user(buf, &event, count);
	if(ret > 0)
	{
		printk("copy_to_user error\n");
		return -EFAULT;
	}
	//清零,以备下次充值
	memset(&event, 0, sizeof(struct key_event));
	have_data = 0; //没有数据了,等待下一次数据
	
	return count;
}

int key_drv_close(struct inode *inode, struct file *filp)
{
	printk("--------^_*  %s-------\n", __FUNCTION__);

	
	return 0;
}



// 4, 实现fops
const struct file_operations key_fops = {
	.owner = THIS_MODULE,
	.open = key_drv_open,
	.read = key_drv_read,
	.release =key_drv_close,
};

//中断处理方法
//参数1--当前产生的中断的号码
//参数2--request_irq传递过来的参数
irqreturn_t key_irq_handler(int irqno, void *dev_id)
{
	int *p = (int *)dev_id;
	//printk("-----------%s-------0x%x-----\n", __FUNCTION__, *p);

	//区分按下还是抬起
	int value = gpio_get_value(S5PV210_GPH0(1));
	if(value)
	{
		//抬起
		printk("<kernel>--down_key2 : release\n");
		//填充值
		event.code = KEY_DOWN;
		event.value = 0;
		
	}else
	{
		//按下
		 printk("<kernel>--down_key2 : pressed\n");
		event.code = KEY_DOWN;
		event.value = 1;
	}

	have_data = 1;//表示有数据了
	wake_up_interruptible(&wq_head);
	
	
	
	return IRQ_HANDLED;
}
static int __init key_drv_init(void)
{

	int ret;
	// 1, 申请主设备号--动态申请主设备号
	key_major = register_chrdev(0, "key_dev", &key_fops);

	// 2, 创建设备节点
	key_cls = class_create(THIS_MODULE, "key_cls");
	device_create(key_cls, NULL,  MKDEV(key_major, 0),  NULL, "key0");

	// 3,  硬件初始化-- 映射地址或者中断申请
	
	irqno = IRQ_EINT(1);
	//参数1--中断号码
			//获取中断号码的方法: 1,外部中断IRQ_EINT(x)  
			// 2, 头文件 #include <mach/irqs.h>  #include <plat/irqs.h>
	//参数2--中断的处理方法
	//参数3--中断触发方式:  高电平,低电平,上升沿,下降沿,双边沿
	/*
		#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
	*/
	//参数4--中断的描述-自定义字符串
	//参数5--传递个参数2的任意数据
	//返回值0表示正确
	ret = request_irq(irqno, key_irq_handler, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "eint1-downkey", &testdata);
	if(ret != 0)
	{
		printk("request_irq error\n");
		return ret;
	}
	
	// 定义一个等待队列头,并且初始化
	init_waitqueue_head(&wq_head);
	
	
	return 0;
}



static void __exit key_drv_exit(void)
{
	// 释放中断
	//参数1--中断号码
	//参数5--和request_irq第5个参数保持一致
	free_irq(irqno, &testdata);
	device_destroy(key_cls, MKDEV(key_major, 0));
	class_destroy(key_cls);
	unregister_chrdev(key_major, "key_dev");
}

//固定模式(就是这么写记住)   
module_init(key_drv_init);
module_exit(key_drv_exit);
MODULE_LICENSE("GPL");//GPL协议








应用层app代码 


#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

#include <linux/input.h>

// 设计一个对象--描述按键产生的数据包
struct key_event{
	int code; //什么按键: 左键, 回车键
	int value; // 按键的状态: 按下/抬起 (1/0)
};


int main(int argc, char *argv[])
{

	int on;
	int ret;
	
	struct key_event data;
	
	//直接将驱动模块当做文件来操作
	int fd = open("/dev/key0", O_RDWR);
	if(fd < 0)
	{
		perror("open");
		exit(1);
	}

	

	while(1)
	{

		ret  = read(fd, &data, sizeof(struct key_event));
		if(ret < 0)
		{
			perror("read");
			exit(1);
		}

		//解析包
		if(data.code == KEY_DOWN)
		{
			if(data.value)
			{
				printf("<app>---KEY_DOWN pressed\n");
			}else
			{
				printf("<app>---KEY_DOWN release\n");	
			}
		}
		
	}
	

	close(fd);


}

猜你喜欢

转载自blog.csdn.net/weixin_42471952/article/details/81589706