机械按键在按下的过程中会出现抖动的情况,如下图,这样就会导致本来按下一次按键的过程会出现多次中断,导致判断出错。在按键驱动程序中我们可以这么做:
在按键驱动程序中我们可以这么做来取消按键抖动的影响:当出现一个按键中断后不会马上去处理它,而是延时一个抖动时间(一般10ms),如果在这个时间内再次出现中断那么再次延时10ms。这样循环,一直到在这个10ms内只有一个按键中断,那么就认为这次是真的按键值,然后在定时器处理函数里处理它。上述过程可以利用内核的定时器来实现。
定时器二要素:定时时间、定时时间到后做什么事情。根据这两个要素来编写程序,直接在sixth_drv.c的驱动程序上更改直接看到代码:
1、定时器的创建,先建立一个定时器结构
static struct timer_list buttons_timer;//定义一个定时器
2、在模块装载时初始化定时器
static int sixth_drv_init(void) { /*增加一个定时器用于处理按键抖动*/ init_timer(&buttons_timer); buttons_timer.expires = 0;//定时器的定时时间 // buttons_timer->data = (unsigned long) cs; buttons_timer.function = buttons_timeout;//定时时间到后的处理函数 add_timer(&buttons_timer);//将定义的定时器放入定时器链表 sixthmajor = register_chrdev(0, "buttons", &sixth_drv_ops);//注册驱动程序 if(sixthmajor < 0) printk("failes 1 buttons_drv register\n"); sixth_drv_class = class_create(THIS_MODULE, "buttons");//创建类 if(sixth_drv_class < 0) printk("failes 2 buttons_drv register\n"); sixth_drv_class_dev = class_device_create(sixth_drv_class, NULL, MKDEV(sixthmajor,0), NULL,"buttons");//创建设备节点 if(sixth_drv_class_dev < 0) printk("failes 3 buttons_drv register\n"); gpfcon = ioremap(0x56000050, 16);//重映射 gpfdat = gpfcon + 1; gpgcon = ioremap(0x56000060, 16);//重映射 gpgdat = gpgcon + 1; printk("register buttons_drv\n"); return 0; }
3、编写定时器处理函数
static void buttons_timeout(unsigned long data) { unsigned int pin_val; static long cnt=0; //printk("timeout cnt : %d\n",++cnt); if(pin_des==NULL) return; else { // printk("pin_des != NULL\n"); pin_val = s3c2410_gpio_getpin(pin_des->pin); if(pin_val) //按键松开 key_val = 0x80 | pin_des->key_val; else key_val = pin_des->key_val; wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */ ev_press = 1; kill_fasync(&sixth_fasync, SIGIO, POLL_IN);//发生信号给进程 } }
4、当在卸载驱动时将定时器删除;在中断处理程序中直接改变定时器的超时时间,并记录下是哪个按键按下的即可,其他处理都在定时器超时函数中。直接看到完整代码:
#include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/init.h> #include <asm/io.h> //含有iomap函数iounmap函数 #include <asm/uaccess.h>//含有copy_from_user函数 #include <linux/device.h>//含有类相关的处理函数 #include <asm/arch/regs-gpio.h>//含有S3C2410_GPF0等相关的 #include <linux/irq.h> //含有IRQ_HANDLED\IRQ_TYPE_EDGE_RISING #include <asm-arm/irq.h> //含有IRQT_BOTHEDGE触发类型 #include <linux/interrupt.h> //含有request_irq、free_irq函数 #include <linux/poll.h> #include <asm-generic/errno-base.h> //含有各种错误返回值 //#include <asm-arm\arch-s3c2410\irqs.h> static struct class *sixth_drv_class;//类 static struct class_device *sixth_drv_class_dev;//类下面的设备 static int sixthmajor; static unsigned long *gpfcon = NULL; static unsigned long *gpfdat = NULL; static unsigned long *gpgcon = NULL; static unsigned long *gpgdat = NULL; struct fasync_struct *sixth_fasync; static unsigned int key_val; struct pin_desc { unsigned int pin; unsigned int key_val; }; static struct pin_desc pins_desc[4] = { {S3C2410_GPF0,0x01}, {S3C2410_GPF2,0x02}, {S3C2410_GPG3,0x03}, {S3C2410_GPG11,0x04} }; static struct pin_desc *pin_des=NULL; static unsigned int ev_press; static DECLARE_WAIT_QUEUE_HEAD(button_waitq);//注册一个等待队列button_waitq static atomic_t open_flag = ATOMIC_INIT(1); //定义原子变量open_flag 并初始化为1 static DECLARE_MUTEX(button_lock); //定义互斥锁 static struct timer_list buttons_timer;//定义一个定时器 /* *0x01、0x02、0x03、0x04表示按键被按下 */ /* *0x81、0x82、0x83、0x84表示按键被松开 */ /* *利用dev_id的值为pins_desc来判断是哪一个按键被按下或松开 */ static irqreturn_t buttons_irq(int irq, void *dev_id) { pin_des = (struct pin_desc *)dev_id;//取得哪个按键被按下的状态 mod_timer(&buttons_timer, jiffies+HZ/100);//10ms之后调用定时器处理函数 return IRQ_HANDLED; } static int sixth_drv_open (struct inode * inode, struct file * file) { int ret; // if(atomic_dec_and_test(&open_flag)==0)//自检后是否为0,不为0说明已经被人调用 // { // atomic_inc(&open_flag);//原子变量+1 // return -EBUSY; // } if(file->f_flags & O_NONBLOCK)//非阻塞方式 { if(down_trylock(&button_lock))//获取信号量失败则返回 return -EBUSY; } else down(&button_lock);//获得信号量 ret = request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "s1", (void * )&pins_desc[0]); if(ret) { printk("open failed 1\n"); return -1; } ret = request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "s2", (void * )& pins_desc[1]); if(ret) { printk("open failed 2\n"); return -1; } ret = request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "s3", (void * )&pins_desc[2]); if(ret) { printk("open failed 3\n"); return -1; } ret = request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "s4", (void * )&pins_desc[3]); if(ret) { printk("open failed 4\n"); return -1; } return 0; } static int sixth_drv_close(struct inode * inode, struct file * file) { // atomic_inc(&open_flag);//原子变量+1 up(&button_lock);//释放信号量 free_irq(IRQ_EINT0 ,(void * )&pins_desc[0]); free_irq(IRQ_EINT2 ,(void * )& pins_desc[1]); free_irq(IRQ_EINT11 ,(void * )&pins_desc[2]); free_irq(IRQ_EINT19 ,(void * )&pins_desc[3]); return 0; } static ssize_t sixth_drv_read(struct file * file, char __user * userbuf, size_t count, loff_t * off) { int ret; if(count != 1) { printk("read error\n"); return -1; } if(file->f_flags & O_NONBLOCK)//非阻塞方式 { if(!ev_press)//判断是否有按键按下,如果没有直接返回 { key_val = 0; ret = copy_to_user(userbuf, &key_val, 1); return -EBUSY; } } else//如果没有按键动作,直接进入休眠 wait_event_interruptible(button_waitq, ev_press);//将当前进程放入等待队列button_waitq中 ret = copy_to_user(userbuf, &key_val, 1); ev_press = 0;//按键已经处理可以继续睡眠 if(ret) { printk("copy error\n"); return -1; } return 1; } static unsigned int sixth_drv_poll(struct file *file, poll_table *wait) { unsigned int ret = 0; poll_wait(file, &button_waitq, wait);//将当前进程放到button_waitq列表 if(ev_press) ret |=POLLIN;//说明有数据被取到了 return ret; } static int sixth_drv_fasync(int fd, struct file * file, int on) { int err; printk("fansync_helper\n"); err = fasync_helper(fd, file, on, &sixth_fasync);//初始化sixth_fasync if (err < 0) return err; return 0; } static struct file_operations sixth_drv_ops = { .owner = THIS_MODULE, .open = sixth_drv_open, .read = sixth_drv_read, .release = sixth_drv_close, .poll = sixth_drv_poll, .fasync = sixth_drv_fasync, }; static void buttons_timeout(unsigned long data) { unsigned int pin_val; static long cnt=0; //printk("timeout cnt : %d\n",++cnt); if(pin_des==NULL) return; else { // printk("pin_des != NULL\n"); pin_val = s3c2410_gpio_getpin(pin_des->pin); if(pin_val) //按键松开 key_val = 0x80 | pin_des->key_val; else key_val = pin_des->key_val; wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */ ev_press = 1; kill_fasync(&sixth_fasync, SIGIO, POLL_IN);//发生信号给进程 } } static int sixth_drv_init(void) { /*增加一个定时器用于处理按键抖动*/ init_timer(&buttons_timer); buttons_timer.expires = 0;//定时器的定时时间 // buttons_timer->data = (unsigned long) cs; buttons_timer.function = buttons_timeout;//定时时间到后的处理函数 add_timer(&buttons_timer);//将定义的定时器放入定时器链表 sixthmajor = register_chrdev(0, "buttons", &sixth_drv_ops);//注册驱动程序 if(sixthmajor < 0) printk("failes 1 buttons_drv register\n"); sixth_drv_class = class_create(THIS_MODULE, "buttons");//创建类 if(sixth_drv_class < 0) printk("failes 2 buttons_drv register\n"); sixth_drv_class_dev = class_device_create(sixth_drv_class, NULL, MKDEV(sixthmajor,0), NULL,"buttons");//创建设备节点 if(sixth_drv_class_dev < 0) printk("failes 3 buttons_drv register\n"); gpfcon = ioremap(0x56000050, 16);//重映射 gpfdat = gpfcon + 1; gpgcon = ioremap(0x56000060, 16);//重映射 gpgdat = gpgcon + 1; printk("register buttons_drv\n"); return 0; } static void sixth_drv_exit(void) { del_timer(&buttons_timer); unregister_chrdev(sixthmajor,"buttons"); class_device_unregister(sixth_drv_class_dev); class_destroy(sixth_drv_class); iounmap(gpfcon); iounmap(gpgcon); printk("unregister buttons_drv\n"); } module_init(sixth_drv_init); module_exit(sixth_drv_exit); MODULE_LICENSE("GPL");
5、测试代码还是沿用sisth_test.c。将驱动程序和测试程序编译后的文件放入网络文件系统,测试发现不再出现抖动情况。具体过程参考Linux驱动之按键驱动编写(中断方式)
原文地址:https://www.cnblogs.com/andyfly/p/9486304.html
时间: 2024-10-10 08:57:31