zoukankan      html  css  js  c++  java
  • 001字符设备驱动(定时器防抖)

    使用定时器实现按键防抖(第十二课/第八节)

    在执行以阻塞方式读取按键值的时候会有多个中断的出现

    原因:因为按键是一个机械开关,按下松开的时候里面的金属弹片可能抖动了好几次,由于是边沿触发,导致多次中断的发生。

    解决方法:定时器消抖
    设计思路:
    每产生一次IRQ中断就修改这个定时器的超时时间,直到产生最后一次IRQ中断后10ms才执行真正的中断函数。
    首先看看别人是怎么使用这个定时器的:搜索 "add_timer"




    我们仿造这也写一个这样的函数

    根据我们的设计思路,每发生一次按键中断都会更改一次定时器的超时时间。所以在按键的中断函数里就只做重置超时时间的工作。

    使用(cat /proc/interrupt)命令可以查看系统时间中断的时间

    把我们真正想要处理的代码应该放在定时器的中断函数里面

    定时器消抖驱动程序(seventh_chrdev.c)

    #include <linux/module.h>
    #include <linux/kernel.h>
    #include <linux/fs.h>
    #include <linux/init.h>
    #include <linux/delay.h>
    
    #include <linux/irq.h>
    #include <linux/poll.h>
    
    #include <asm/uaccess.h>
    #include <asm/irq.h>
    #include <asm/io.h>
    #include <asm/arch/regs-gpio.h>
    #include <asm/hardware.h>
    /*
     * eint 0  GPF0
     * eint 2  GPF2
     * eint 11 GPG3
     * eint 19 GPG11
     */
    static struct class *seventh_chrdev_class;
    static struct class_device *seventh_chrdev_class_dev;
    
    static volatile unsigned char key_val;
    
    static struct timer_list buttons_timer;
    static struct pin_desc *irq_pd;
    
    static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
    
    /* 中断事件标志, 中断服务程序将它置1,s3c24xx_buttons_read将它清0 */
    static volatile int ev_press = 0;
    
    static struct fasync_struct *button_async;
    
    static DECLARE_MUTEX(button_lock);     //定义互斥锁        定义和初始化
    
    struct pin_desc{
            unsigned int pin;
            unsigned int key_val;
        };
    struct pin_desc pins_desc[4] = {
            {S3C2410_GPF0,  0x01},
            {S3C2410_GPF2,  0x02},
            {S3C2410_GPG3,  0x03},
            {S3C2410_GPG11, 0x04},
        };
    
    static irqreturn_t button_irq(int irq, void *dev_id)
    {
        irq_pd = (struct pin_desc *)dev_id;
        //这个jiffies是一个全局变量,它是系统时间,执行定时器中断的条件是:系统的时间大于等于定时器的超时时间
        //系统的时间每10ms加一次
        mod_timer(&buttons_timer, jiffies+HZ/100);
        return IRQ_HANDLED;
    }
    
    
    static int seventh_chrdev_open(struct inode *inode, struct file *file)
    {
        printk("seventh_chrdev_open
    
    ");
        if(file->f_flags & O_NONBLOCK)          //非阻塞
        {
            if (down_trylock(&button_lock))    //试图获取信号量,如果获取不到就立刻返回
                return -EBUSY;
        }
        else
        {
            /* 获取信号量 */
            down(&button_lock);
        }
        request_irq(IRQ_EINT0,   button_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
        request_irq(IRQ_EINT2,   button_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);
        request_irq(IRQ_EINT11, button_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);
        request_irq(IRQ_EINT19, button_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);
    
        return 0;
    }
    
    
    static ssize_t seventh_chrdev_read(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
    {
        if (count != 1)
            return -EINVAL;
        if(file->f_flags & O_NONBLOCK)
        {
            if (!ev_press)
                return -EAGAIN;
        }
        else
        {
    
            /* 如果没有按键动作, 休眠 */
            wait_event_interruptible(button_waitq, ev_press);
        }
    
        copy_to_user(buf, &key_val, 1);
        ev_press = 0;
    
        return 1;
    }
    
    int seventh_chrdev_release(struct inode *inode, struct file *file)
    {
        printk("seventh_chrdev_release
    
    ");
        free_irq(IRQ_EINT0,   &pins_desc[0]);
        free_irq(IRQ_EINT2,   &pins_desc[1]);
        free_irq(IRQ_EINT11,  &pins_desc[2]);
        free_irq(IRQ_EINT19,  &pins_desc[3]);
        up(&button_lock);
        return 0;
    }
    
    static unsigned int seventh_chrdev_poll(struct file *file, poll_table *wait)
    {
        unsigned int mask = 0;
        poll_wait(file, &button_waitq, wait);    //不会立即休眠,只是把当前进程挂接进去
    
        if (ev_press)
            mask |= (POLLIN | POLLRDNORM);
    
        return mask;
    }
    
    static int seventh_chrdev_fasync(int fd, struct file *filp, int on)
    {
        printk("seventh_chrdev_fasync
    
    ");
        return fasync_helper(fd, filp, on, &button_async);
    }
    
    static struct file_operations seventh_chrdev_fops = {
        .owner    =  THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
        .open     =  seventh_chrdev_open,
        .read     =  seventh_chrdev_read,
        .release  =  seventh_chrdev_release,
        .poll      =  seventh_chrdev_poll,
        .fasync   =  seventh_chrdev_fasync,
        };
    
    
    int major;
    static void buttons_timer_function(unsigned long data)
    {
        struct pin_desc *pindesc = irq_pd;
        unsigned char pinval;
    
        if(!pindesc)
            return;
        pinval = s3c2410_gpio_getpin(pindesc->pin);
        //printk("pinval = %d, irq_val = %d
    ",pinval, irq_val);
        if(pinval)
        {
            /* 松开 */
            key_val = pindesc->key_val;
        }
        else
        {
            /* 按下 */
            key_val = (0x80 | pindesc->key_val);
        }
            wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */
            ev_press = 1;
    
            kill_fasync (&button_async, SIGIO, POLL_IN);
    }
    static int seventh_chrdev_init(void)
    {
        init_timer(&buttons_timer);
        buttons_timer.function = buttons_timer_function;
        //buttons_timer.expires = jiffies + 15*HZ;
        add_timer(&buttons_timer);
    
        major = register_chrdev(0, "seventh_chrdev", &seventh_chrdev_fops);  //注册
        seventh_chrdev_class = class_create(THIS_MODULE, "seventhchrdev");
        seventh_chrdev_class_dev = class_device_create(seventh_chrdev_class, NULL, MKDEV(major, 0), NULL, "seventh_chrdev");
    
        return 0;
    }
    
    static void seventh_chrdev_exit(void)
    {
        unregister_chrdev(major, "seventh_chrdev");                    //卸载
        class_device_unregister(seventh_chrdev_class_dev);
        class_destroy(seventh_chrdev_class);
    }
    
    module_init(seventh_chrdev_init);
    module_exit(seventh_chrdev_exit);
    
    MODULE_LICENSE("GPL");
    

    测试程序(seventh_chrdev_test.c)

    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    
    #include <sys/types.h>
    #include <unistd.h>
    
    int fd;
    
    int main(int argc, char **argv)
    {
        volatile unsigned char key_val;
        int ret;
    
        fd = open("/dev/seventh_chrdev",O_RDWR);    //阻塞方式
        if(fd < 0)
        {
            printf("can't open!
    ");
            return -1;
        }
    
        while(1)
        {
            ret = read(fd, &key_val, 1);
            printf("ret : %d,  key_val = 0x%x
    ", ret, key_val);
        }
        return 0;
    }
    

    测试:

    1. 修改Makefile
    2. 编译并拷贝到(first_fs)目录
    3. 加载模块(insmod ./seventh_chrdev.ko)
    4. 运行测试程序(./seventh_chrdev_test)

    即使按上半小时也不会出现多次连续按下的键值了。

    <wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">

  • 相关阅读:
    单例设计模式
    使用JfreeChart生成统计图
    session的生命周期
    json
    struts2 ognl标签使用
    集合小结
    多线程
    内部类和匿名内部类
    模版方法设计模式
    mac中yeoman构建你的项目
  • 原文地址:https://www.cnblogs.com/luosir520/p/11446976.html
Copyright © 2011-2022 走看看