Linux驱动之定时器在按键去抖中的应用

机械按键在按下的过程中会出现抖动的情况,如下图,这样就会导致本来按下一次按键的过程会出现多次中断,导致判断出错。在按键驱动程序中我们可以这么做:

在按键驱动程序中我们可以这么做来取消按键抖动的影响:当出现一个按键中断后不会马上去处理它,而是延时一个抖动时间(一般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-08-01 05:14:48

Linux驱动之定时器在按键去抖中的应用的相关文章

FPGA按键去抖verilog代码

按键去抖的原因及其分类就不罗嗦了. 在这里解释一段代码,代码是网上找的,看了半天没懂,无奈查了半天想了半天,终于明白了... module sw_debounce(clk,rst_n,sw1,sw2,sw3,//outputled_d3,led_d4,led_d5);input clk;input rst_n;input sw1,sw2,sw3; //Active lowoutput led_d3;output led_d4;output led_d5;// ------------------

linux驱动之定时器的使用

被文章摘自一下几位网友.非常感谢他们. http://blog.sina.com.cn/s/blog_57330c3401011cq3.html Linux的内核中定义了一个定时器的结构: #include<linux/timer.h> struct timer_list { struct list_head list; unsigned long expires; //定时器到期时间 unsigned long data; //作为参数被传入定时器处理函数 void (*function)(

ARM Linux 驱动Input子系统之按键驱动测试

上一篇已经谈过,在现内核的中引入设备树之后对于内核驱动的编写,主要集中在硬件接口的配置上了即xxxx.dts文件的编写. 在自己的开发板上移植按键驱动: 1.根据开发板的原理图 确定按键的硬件接口为:GPIO2_2.GPIO2_3.GPIO2_5.GPIO0_30. 修改dts文件使其与原理图的按键接口一致. gpio_buttons: [email protected]0 { compatible = "gpio-keys"; #address-cells = <1>;

linux驱动之定时器的介绍和内核时间的学习

本文章摘自下面的网友: http://blog.sina.com.cn/s/blog_6e5b342e0100m87d.html 一.内核中如何记录时间 任何程序都需要时间控制,其主要目的是: 测量时间流逝和比较时间 知道当前时间 指定时间量的延时操作 为达到这个目的,应用程序使用日历时间(年月日时分秒)或者自1970年1月1日零时零分零秒到当前的秒数来度量时间的流逝,但内核中需要更加有精度的时间度量,因此内核使用时钟嘀嗒来记录时间.时钟中断发生后内核内部时间计数器增加1(即:增加1个时钟嘀嗒)

简单的定时器实现按键消抖,还望大家多指点!

/******************************** 按独立按键使数码管加1 ********************************/  #include <reg52.h>  #define uchar unsigned char    sbit KEY = P1 ^ 0;  //定义独立按键  bit KEYSTA = 1;  //按键的初始状态  uchar code DispCode[] = {0xc0,0xf9,0xa4,0xb0,  //共阳数码管     

javascript中的函数节流和函数去抖

带着问题去尝试 首先我们要知道为什么要用到函数节流和函数去抖?我们带着以下的疑问来进行分析! 1.比如搜索框,你会用到什么事件(change.blur.keyup等)?去做什么效果?2.再比如scroll滚动事件,怎么去触发?是滚一段距离触发一次?还是滚一圈触发一次?还是滚一次触发一次?3.还包括mouseover事件是怎么触发呢?...... 场景实例 函数节流和去抖的出现场景,一般都伴随着客户端 DOM 的事件监听.举个例子,实现一个原生的拖拽功能(不能用 H5 Drag&Drop API)

嵌入式Linux驱动开发实战视频教程

嵌入式Linux驱动开发实战教程(内核驱动.看门狗技术.触摸屏.视频采集系统)适合人群:高级课时数量:109课时用到技术:嵌入式 Linux涉及项目:驱动开发.看门狗技术.触摸屏.视频采集咨询qq:1840215592 课程介绍:本课程即是针对有兴趣学习嵌入式linux驱动开发又不知道从何处着实开始学习嵌入式linux驱动开发的在校同学以及社会在职人员.本课程采用理论教学与实验相结合的方式,软件与硬件相结合的方式,重点给大家讲解嵌入式linux驱动开发的方法,系统地介绍嵌入式linux驱动开发的

Linux代码的重用与强行卸载Linux驱动

(一)Linux代码的重用 重用=静态重用(将要重用的代码放到其他的文件的头文件中声明)+动态重用(使用另外一个Linux驱动中的资源,例如函数.变量.宏等) 1.编译是由多个文件组成的Linux驱动(静态重用) 对于复杂的Linux驱动,需要使用多个源代码文件存放不同的功能代码,这样做有利于代码分类和管理,那么就不得不编译多个源代码文件,最终生成.ko文件或编译进Linux内核 下面,就介绍将3个.c文件分别编译为3个.o文件,并将这3个.o文件链接(link)成一个.ko文件——静态重用 假

第八章:Linux代码重用、Linux驱动强行卸载

(一)Linux代码的重用 重用=静态重用(将要重用的代码放到其他的文件的头文件中声明)+动态重用(使用另外一个Linux驱动中的资源,例如函数.变量.宏等) 1.编译是由多个文件组成的Linux驱动,即静态重用 对于复杂的Linux驱动,需要使用多个源代码文件存放不同的功能代码,这样做有利于代码分类和管理,那么就不得不编译多个源代码文件,最终生成.ko文件或编译进Linux内核 下面,就举例介绍将3个.c文件分别编译为3个.o文件,并将这3个.o文件链接(link)成一个.ko文件——静态重用