mini2440 按键驱动添加定时器消抖动

时间:2021-12-01 23:29:38

测试程序和Makefile同前面的实验一样的,这里只记录一下驱动的源代码就行了,改动不大,就是把唤醒进程和发送异步信号的操作移动到定时器的超时函数里面去了,这样做的目的是为了消除按键的机械抖动。

驱动源代码:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/gpio.h>
#include <linux/sched.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <mach/regs-gpio.h>

#defineGRH_MODULE_NAME"key_interrupt"

static int major;
static struct class *key_interrupt_class;
static struct class_device *key_interrupt_device;
static int key_value;

//wait_event_interruptible函数需要的两个变量
static DECLARE_WAIT_QUEUE_HEAD(grh_wait_interrupt); //休眠的进程队列头
static volatile int sleep_for_interrupt; //这个变量为0的时候read函数会休眠,中断里面将其置1,read函数末尾将其设置为0

//异步信号队列定义
static struct fasync_struct *grh_async_queue;

//定义一个定时器,用于按键防抖动
static struct timer_list grh_timer_shake;
static int first_timer_timeout;

//pin_desc是对每一个按键中断的描述,不仅仅可以是整数,也可以是更复杂到的字段,这里用简单的按键值就行了
int pin_desc[6] = {
1, 2, 3, 4, 5, 6
};


//中断处理函数
static irqreturn_t grh_handle_key_eint(int irq, void *dev_id){
int *p;
p = dev_id;

//printk(KERN_EMERG"key pressed! key=%d\n", *p);
key_value = *p;

//重新设置定时器的超时时间,其中HZ是一秒钟对应的系统计数值,下面的定时器是100ms的
mod_timer(&grh_timer_shake, jiffies+HZ/10);


//唤醒休眠进程和发送异步信号的工作交给定时器超时处理函数来实现

return IRQ_HANDLED;
}

static void grh_timer_shake_handler(unsigned long n){
if(first_timer_timeout){
first_timer_timeout = 0;
return;
}

//唤醒休眠的进程
sleep_for_interrupt = 1;
wake_up_interruptible(&grh_wait_interrupt);

//向用户空间进程发送异步信号
kill_fasync(&grh_async_queue, SIGIO, POLL_IN);
}


static void init_key(void){
//注册irq中断处理函数,将按键值和中断号绑定,所有清中断操作以及初始化中断相关寄存器的操作全部交给
//内核自动完成了,不再需要像裸机程序一样显式地对寄存器进行读写了,中断发生后会自动跳到grh_handle_key_eint
request_irq(IRQ_EINT8, grh_handle_key_eint, IRQ_TYPE_EDGE_FALLING, "key1", pin_desc);
request_irq(IRQ_EINT11, grh_handle_key_eint, IRQ_TYPE_EDGE_FALLING, "key2", pin_desc+1);
request_irq(IRQ_EINT13, grh_handle_key_eint, IRQ_TYPE_EDGE_FALLING, "key3", pin_desc+2);
request_irq(IRQ_EINT14, grh_handle_key_eint, IRQ_TYPE_EDGE_FALLING, "key4", pin_desc+3);
request_irq(IRQ_EINT15, grh_handle_key_eint, IRQ_TYPE_EDGE_FALLING, "key5", pin_desc+4);
request_irq(IRQ_EINT19, grh_handle_key_eint, IRQ_TYPE_EDGE_FALLING, "key6", pin_desc+5);
}

static int key_interrupt_open(struct inode *inode, struct file *file){
printk(KERN_EMERG"DRIVER: OPEN\n");
sleep_for_interrupt = 0;
init_key();
return 0;
}

static ssize_t key_interrupt_write(struct inode *inode, const char __user *buf, size_t count, loff_t *ppos){
printk(KERN_EMERG"DRIVER: WRITE\n");
return 0;
}

static ssize_t key_interrupt_read(struct file *file, char __user *buf, size_t count, loff_t *ppos){
printk(KERN_EMERG"DRIVER: READ\n");
//根据sleep_for_interrupt的数值决定是否将驱动进程加进休眠队列grh_wait_interrupt中,立即休眠进程
wait_event_interruptible(grh_wait_interrupt, sleep_for_interrupt);
copy_to_user(buf, &key_value, 4);

//下一次进入read的时候继续休眠等待中断发生
sleep_for_interrupt = 0;
return 0;
}

int key_interrupt_release(struct inode *inode, struct file *file){
//注销中断
free_irq(IRQ_EINT8, pin_desc);
free_irq(IRQ_EINT11, pin_desc+1);
free_irq(IRQ_EINT13, pin_desc+2);
free_irq(IRQ_EINT14, pin_desc+3);
free_irq(IRQ_EINT15, pin_desc+4);
free_irq(IRQ_EINT19, pin_desc+5);

printk(KERN_EMERG"DRIVER: RELEASE\n");
return 0;
}

//sys_poll会反复在死循环里面调用key_interrupt_poll
static unsigned int key_interrupt_poll(struct file *file, struct poll_table_struct *wait){
unsigned int mask;
printk(KERN_EMERG"DRIVER POLL\n");
poll_wait(file, &grh_wait_interrupt, wait); //把当前进程挂到休眠队列里面,但是不立即休眠
mask = 0;
if(sleep_for_interrupt){ //中断发生了
mask |= POLLIN | POLLRDNORM; //把有可读数据的标志位设置为1,用户层可以得到这个mask
}

return mask;
/*
如果返回的mask是0那么进程直接进入定时休眠,如果在定时休眠过程中中断发生了,sys_poll里面的
定时休眠结束,sys_poll又会循环调用key_interrupt_poll,但这个时候mask一定返回非零数值了,这时
sys_poll中的休眠结束,进程继续运行。如果定时休眠过程中中断一直没有发生,那么定时休眠超时之后,
sys_poll再调用一次key_interrupt_poll,然会判断是否超时,如果超时直接结束进程的休眠。这是
Linux内核的poll机制的大概原理,这样在用户层调用一次poll函数,进程最多休眠时间就是传进来的wait参数,
中断一发生,定时休眠立刻结束,否则进程就休眠到一次定时休眠结束为止。
*/
}

//用户空间程序调用fcntl(fd, F_SETFL, flag | FASYNC)的时候,下面的异步通知设置函数会被调用
static int key_interrupt_fasync(int fd, struct file *file, int on){
//fasync_helper函数会将用户空间进程的pid传入grh_async_queue里面
//这样中断处理函数里发出的信号才能被用户空间的应用程序收到
printk(KERN_EMERG"DRIVER : FASYNC\n");

//初始化grh_async_queue的工作交给fasync_helper来做,驱动程序不去实现了
return fasync_helper(fd, file, on, &grh_async_queue);
}

static struct file_operations key_interrupt_fops = {
.owner = THIS_MODULE,
.open = key_interrupt_open,
.write = key_interrupt_write,
.read = key_interrupt_read,
.release = key_interrupt_release,
.poll = key_interrupt_poll,
.fasync = key_interrupt_fasync,
};

int key_interrupt_module_init(void){
printk(KERN_EMERG"INIT MODULE!\n");

//初始化防抖动的timer,默认的超时时间是0
init_timer(&grh_timer_shake);
grh_timer_shake.function = grh_timer_shake_handler;
add_timer(&grh_timer_shake);
first_timer_timeout = 1;

//register the driver with the device
major = register_chrdev(0, GRH_MODULE_NAME, &key_interrupt_fops);

//create my own device class
key_interrupt_class = class_create(THIS_MODULE, "key_interrupt_class");

//create my device of my own class
key_interrupt_device = device_create(key_interrupt_class, NULL, MKDEV(major,0), NULL, "key_interrupt_device");

return 0;
}

void key_interrupt_module_exit(void){
unregister_chrdev(major, GRH_MODULE_NAME);
device_unregister(key_interrupt_device);
class_destroy(key_interrupt_class);
printk(KERN_EMERG"EXIT MODULE!\n");
}

module_init(key_interrupt_module_init);
module_exit(key_interrupt_module_exit);

MODULE_AUTHOR("GRH");
MODULE_VERSION("1.0");
MODULE_DESCRIPTION("KEY POLL DRIVER");
MODULE_LICENSE("GPL");