字符设备驱动-定时器

标签: 驱动按键消抖函数bugmakefile
192人阅读 评论(0) 收藏 举报
分类:

当我们用阻塞的方式来驱动按键时:

按下按键由于机械按键的波动性,会导致中断处理函数发生多次。

这里写图片描述
这里写图片描述

我们可以采用定时器来解决此bug:

这里写图片描述

Makefile:

KERN_DIR = /work/system/linux-2.6.22.6

all:
    make -C $(KERN_DIR) M=`pwd` modules 

clean:
    make -C $(KERN_DIR) M=`pwd` modules clean
    rm -rf modules.order

obj-m += seventh_drv.o

驱动程序:seventh_drv.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 <asm/uaccess.h>  
#include <asm/irq.h>  
#include <asm/io.h>  
#include <asm/arch/regs-gpio.h>  
#include <asm/hardware.h>
#include <linux/poll.h>

static struct fasync_struct *button_async;
static struct class *sixthdrv_class;
static struct class_device *sixthdrv_class_dev;

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

/* 中断事件标志, 中断服务程序将它置1,s3c24xx_sixth_read将它清0 */
static volatile int ev_press = 0;
struct timer_list buttons_timer;

volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;

struct pin_desc{
    unsigned int pin;
    unsigned int key_val;
};

/* 键值: 按下时,0x01、0x02、0x03 */
/* 键值: 松开时,0x81、0x82、0x83 */
static unsigned char key_val;

struct pin_desc pin_desc[3] = {
    {S3C2410_GPF0,0X01},
    {S3C2410_GPF2,0X02},
    {S3C2410_GPG3,0X03},
};

//atomic_t canopen = ATOMIC_INIT(1);//定义原子变量canopen并初始化为1
static DECLARE_MUTEX(button_lock);     //定义互斥锁

static struct pin_desc *irq_pd;

static irqreturn_t buttons_irq(int irq, void *dev_id)
{
    /* 设置定时器,发生中断10ms后再去读电平值 */
    irq_pd = (struct pin_desc *)dev_id;
    mod_timer(&buttons_timer,jiffies+HZ/100);
    return IRQ_RETVAL(IRQ_HANDLED);
}


static int sixth_drv_open(struct inode *inode,struct file *file)
{
#if 0
    if(!atomic_dec_and_test(&canopen))
    {
        atomic_inc(&canopen); 
        return -EBUSY;
    }
#endif

    if (file->f_flags &  O_NONBLOCK)
    {
        if (down_trylock(&button_lock))
            return -EBUSY;
    }
    else
    {
        /* 获 取 信 号 量 */
        down(&button_lock);
    }
    /* 配置GPF0,2、GPG3为中断引脚 */
    request_irq(IRQ_EINT0, buttons_irq,IRQT_BOTHEDGE,"s2",&pin_desc[0]);
    request_irq(IRQ_EINT2, buttons_irq,IRQT_BOTHEDGE,"s3",&pin_desc[1]);
    request_irq(IRQ_EINT11,buttons_irq,IRQT_BOTHEDGE,"s4",&pin_desc[2]);
    /* 配置GPF4、5、6为输入引脚 */
    *gpfcon &= ~((0x3<<4*2) | (0x3<<5*2) | (0x3<<6*2));
    *gpfcon |=  ((1<<4*2) | (1<<5*2) | (1<<6*2));
    return 0;
}

static ssize_t sixth_drv_read (struct file *file, 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
    {
    /* 如果无按键动作发生,则进行休眠状态 */
    /* 如果ev_press等于0,休眠 */
    wait_event_interruptible(button_waitq,ev_press);
    }
    /* 如果有按键动作发生,则返回按键的值 */
    copy_to_user(buf,&key_val,1);
    ev_press = 0;

    return 1;
}

static int sixth_drv_close (struct inode *inode, struct file *file)
{
    //atomic_inc(&canopen); 
    /* 释放信号量 */
    up(&button_lock);
    free_irq(IRQ_EINT0,  &pin_desc[0]);
    free_irq(IRQ_EINT2,  &pin_desc[1]);
    free_irq(IRQ_EINT11, &pin_desc[2]);
    return 0;

}

static unsigned int sixth_drv_poll(struct file *file, struct poll_table_struct *wait)
{
    unsigned int mask = 0;

    poll_wait(file, &button_waitq, wait);

    if (ev_press)
        mask |= POLLIN | POLLRDNORM;   
    return mask;
}

static int sixth_drv_fasync (int fd, struct file *filp, int on)
{
    printk("driver: sixth_drv_fasync\n");
    return fasync_helper (fd, filp, on, &button_async);
}

static struct file_operations sixth_drv_fops = {
    .owner   = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open    = sixth_drv_open,
    .read    = sixth_drv_read,
    .release = sixth_drv_close,
    .poll    = sixth_drv_poll,
    .fasync  = sixth_drv_fasync,
};

int major; 

static int buttons_timer_function(unsigned long data)
{
    struct pin_desc *pindesc = irq_pd;
    unsigned int pinval;

    if(!pindesc)
        return;

    pinval =  s3c2410_gpio_getpin(pindesc->pin);
    if(pinval)
    {
        /* 松开 */
        key_val = 0x80 | (pindesc->key_val);
        *gpfdat |= ((1<<4) | (1<<5) | (1<<6));
    }
    else
    {
        /* 按下 */
        key_val = pindesc->key_val;
        *gpfdat &= ~((1<<4) | (1<<5) | (1<<6));
    }
    ev_press = 1;                /* 表示中断发生了 */
    wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */
    kill_fasync (&button_async, SIGIO, POLL_IN);

}

static int sixth_drv_init(void)
{
        init_timer(&buttons_timer);
        buttons_timer.function = buttons_timer_function;
        major = register_chrdev(0,"sixth_drv",&sixth_drv_fops);
        sixthdrv_class = class_create(THIS_MODULE,"sixthdrv");
        sixthdrv_class_dev = class_device_create(sixthdrv_class,NULL,MKDEV(major,0),NULL,"buttons");
        gpfcon = (volatile unsigned long *)ioremap(0x56000050,16);
        gpfdat = gpfcon + 1;
        gpgcon = (volatile unsigned long *)ioremap(0x56000060,16);
        gpgdat = gpgcon + 1;
        return 0;
}

static int sixth_drv_exit(void)
{
        unregister_chrdev(major,"sixth_drv");
        class_device_unregister(sixthdrv_class_dev);
        class_destroy(sixthdrv_class);
        iounmap(gpfcon);
        iounmap(gpgcon);
        return 0;
}

module_init(sixth_drv_init);
module_exit(sixth_drv_exit);
MODULE_LICENSE("GPL");

驱动测试程序:seventhdrvtest.c

    #include <sys/types.h>  
    #include <sys/stat.h>  
    #include <fcntl.h>  
    #include <stdio.h>  
    #include <poll.h>
    #include <signal.h>
    #include <sys/types.h>
    #include <unistd.h>

    /* 
     * sixthdrvtest
     */
    int fd;

    void czg_signal_handler(int signum)
    {
        unsigned char key_val = 0;
        read(fd,&key_val,1);
        printf("key_val: 0x%x\n",key_val);
    }

    int main(int argc, char **argv)
    {
        int ret;
        int oflags;
        unsigned char key_val = 0;
        //signal(SIGIO,czg_signal_handler);
        fd = open("/dev/buttons",O_RDWR);
        if(fd < 0)
        {
            printf("can't open!\n");
            return -1;
        }
        //fcntl(fd,F_SETOWN,getpid()); // 告诉内核,发给谁
        //oflags = fcntl(fd,F_GETFL);
        //fcntl(fd,F_SETFL,oflags | FASYNC); // 改变fasync标记,
        //最终会调用到驱动的faync > fasync_helper:初始化/释放fasync_struct
        //然后当按键按下时候,在irqreturn_t buttons_irq中断处理中调用kill_fasync
        while(1)
        {
            ret = read(fd,&key_val,1);
            printf("key_val: 0x%x,ret = %d\n",key_val,ret);
            //sleep(4);
        }
        return 0;
    }

这里写图片描述

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:255967次
    • 积分:6595
    • 等级:
    • 排名:第3829名
    • 原创:306篇
    • 转载:13篇
    • 译文:89篇
    • 评论:85条
    【目录索引】

    闲适音乐

    博客专栏

    Email: 457086306@qq.com