我们按下按键的时候难免会发生抖动,因此我们意识中的一次按键按下,可能实际上是发生了好几次按键动作。那么有没有一种机制可以消除这种情况呢?答案是肯定的,我们要引入定时器的概念。
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/irq.h>
#include <linux/delay.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>
#define DEVICE_NAME "keys"
volatile unsigned long *gpfcon=NULL;
volatile unsigned long *gpfdat=NULL;
volatile unsigned long *gpgcon=NULL;
volatile unsigned long *gpgdat=NULL;
static struct class *keys_class;
static struct class_device *keys_class_devs;
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 struct fasync_struct *button_async;
static unsigned char key_val;
static volatile int ev_press = 0;
static struct pin_desc *irq_pd;
static DECLARE_MUTEX(button_lock);
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static struct timer_list buttons_timer;//定义一个定时器
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
irq_pd = (struct pin_desc *)dev_id;
mod_timer(&buttons_timer, jiffies+HZ/100);
//修改定时器的值
ev_press = 1;
return IRQ_RETVAL(IRQ_HANDLED);
}
static int s3c24xx_keys_open(struct inode *inode, struct file *file)
{
if (file->f_flags & O_NONBLOCK)
{
if (down_trylock(&button_lock))
return -EBUSY;
}
else
{
down(&button_lock);
}
request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S2",&pins_desc[0]);
request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S3",&pins_desc[1]);
request_irq(IRQ_EINT11,buttons_irq, IRQT_BOTHEDGE, "S4",&pins_desc[2]);
request_irq(IRQ_EINT19,buttons_irq, IRQT_BOTHEDGE, "S5",&pins_desc[3]);
return 0;
}
static int s3c24xx_keys_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
if (filp->f_flags & O_NONBLOCK)
{
if (!ev_press) //????????°??ü°???·???
return -EAGAIN;
}
else
{
wait_event_interruptible(button_waitq, ev_press);
}
copy_to_user(buff, &key_val, 1);
ev_press = 0;
return 1;
}
int s3c24xx_keys_close(struct inode *inode, struct file *file)
{
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 int s3c24xx_keys_fasync(int fd, struct file *filp, int on)
{
printk("driver: fifth_drv_fasync\n");
return fasync_helper (fd, filp, on, &button_async);//add
}
static struct file_operations s3c24xx_keys_fops = {
.owner = THIS_MODULE,
.open = s3c24xx_keys_open,
.read = s3c24xx_keys_read,
.release = s3c24xx_keys_close,
.fasync = s3c24xx_keys_fasync,
};
int major;
/*这是定时器处理函数,本来中断要做的工作放在这里来做*/
static void buttons_timer_function(unsigned long data)
{
struct pin_desc * pindesc = irq_pd;
unsigned int pinval;
if (!ev_press)//
防止没有按键按下时进行下面的操作
return 0;
pinval = s3c2410_gpio_getpin(pindesc->pin);
if (pinval)
{
key_val = 0x80 | pindesc->key_val;
}
else
{
key_val = pindesc->key_val;
}
wake_up_interruptible(&button_waitq);
}
static int __init s3c24xx_keys_init(void)
{
init_timer(&buttons_timer);
//初始化定时器
buttons_timer.function = buttons_timer_function;
//给定时器添加处理函数
add_timer(&buttons_timer);
//将定时器注册进内核,此时定时器启动了
major=register_chrdev(0, DEVICE_NAME, &s3c24xx_keys_fops);//auto distribute major
keys_class = class_create(THIS_MODULE, "keys_class");
keys_class_devs = class_device_create(keys_class, NULL, MKDEV(major, 0), NULL, "keys");
gpfcon=(volatile unsigned long *)ioremap(0x56000050,16);
gpfdat=gpfcon+1;
gpgcon=(volatile unsigned long *)ioremap(0x56000060,16);
gpgdat=gpgcon+1;
printk(DEVICE_NAME " initialized\n");
return 0;
}
static void __exit s3c24xx_keys_exit(void)
{
unregister_chrdev(major, DEVICE_NAME);
class_device_unregister(keys_class_devs);
class_destroy(keys_class);
iounmap(gpfcon);
iounmap(gpgcon);
}
module_init(s3c24xx_keys_init);
module_exit(s3c24xx_keys_exit);
MODULE_LICENSE("GPL");
测试程序如下:
#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>
#include <fcntl.h>
int main(int argc, char **argv)
{
int fd;
unsigned char key_val;
int ret;
fd = open("/dev/keys", O_RDWR | O_NONBLOCK);
if (fd < 0)
{
printf("can't open!\n");
}
while (1)
{
ret=read(fd, &key_val, 1);
if(ret>=0)
printf("key_val: 0x%x\n", key_val);
sleep(5);
}
return 0;
}
好的,我们来简述一下流程:当注册了驱动程序后,其实定时器已经启动了。这时我们读取的话,因为没有按键按下返回值为负,所以应用程序里休眠5秒钟,什么也没有做。这是定时器也启动了,那它在干嘛呢?因为此时没有修改定时器的值,它还是初始值0,所以它一直会进入定时器处理函数,但是由于按键没有按下,无法进行后续的处理,也会返回,不会做什么事情!如果按下按键的话,会进入中断处理函数。在中断处理函数中,会将ev_press置为1,并且修改定时器的值。定时器定时一段时间后,会进入定时器处理函数,完成本来应该中断处理程序完成的工作。