1. 程式人生 > >Linux驅動之定時器在按鍵去抖中的應用

Linux驅動之定時器在按鍵去抖中的應用

arc div 測試 完整 esc arm 是否 reg blog

機械按鍵在按下的過程中會出現抖動的情況,如下圖,這樣就會導致本來按下一次按鍵的過程會出現多次中斷,導致判斷出錯。在按鍵驅動程序中我們可以這麽做:

技術分享圖片

在按鍵驅動程序中我們可以這麽做來取消按鍵抖動的影響:當出現一個按鍵中斷後不會馬上去處理它,而是延時一個抖動時間(一般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驅動之按鍵驅動編寫(中斷方式)

Linux驅動之定時器在按鍵去抖中的應用