前面我们对按键驱动的开发已经基本完成了,但是当你对按键多次按下的时候,你会发现打印的返回值是不正确的,这是什么原因呢?学过51单片机的同学你们都应该知道按键容易产生抖动,俗称“消抖”,我以前的处理办法是设置一个延时10ms,现在我们的按键也是需要延时的,当过了10ms我们在去读这个按键值。
我对定时器的理解如下:
static struct timer_list buttons_timer;//定义一个链表del_timer(struct timer_list * timer)//删除定时器
驱动程序:#include <linux/module.h> /*模块有关的*/
#include <linux/kernel.h>/*内核有关的*/
#include <linux/fs.h>/*文件系统有关的*/
#include <linux/init.h>
#include <linux/device.h>
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <linux/interrupt.h>/*linux中断*/
#include <linux/poll.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h> //copy_to_user
#include <mach/regs-gpio.h>/*寄存器设置S3C2410_GPF0等的定义*/
#include <mach/hardware.h>//s3c2410_gpio_getpin等的定义
#include <mach/irqs.h> //IRQ_EINT0等的定义
#include <asm/system.h>
#include <asm/signal.h>
#include <linux/fcntl.h>
/*
* 休眠用的队列的一个宏
*/
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
//static atomic_t canopen = ATOMIC_INIT(1); //定义原子变量并初始化为1
static DECLARE_MUTEX(button_lock); //定义互斥锁
#define DEVICE_NAME "timer"
static struct class *timer_class;
static struct fasync_struct *button_async;
static struct timer_list buttons_timer;
static struct pin_desc *irq_pd;
/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
static unsigned char key_val;
struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
//static unsigned char keyval;
/* 中断事件标志, 中断服务程序将它置1,third_drv_read将它清0 */
static volatile int ev_press = 0;
/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
struct pin_desc pins_desc[4] = {
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPF3, 0x03},
{S3C2410_GPF4, 0x04},
};
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
irq_pd = (struct pin_desc *)dev_id;
mod_timer(&buttons_timer, jiffies+HZ/100);
return IRQ_HANDLED;
}
static int fifth_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);
}
request_irq(IRQ_EINT0,buttons_irq, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "S2", &pins_desc[0]);
request_irq(IRQ_EINT2,buttons_irq, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "S3", &pins_desc[1]);
request_irq(IRQ_EINT3,buttons_irq, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "S4", &pins_desc[2]);
request_irq(IRQ_EINT4,buttons_irq, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, "S5", &pins_desc[3]);
return 0;
}
ssize_t fifth_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
if(size!=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 0;
}
int fifth_drv_close(struct inode *inode, struct file *file)
{
//atomic_inc(&canopen);
up(&button_lock);
free_irq(IRQ_EINT0, &pins_desc[0]);
free_irq(IRQ_EINT2, &pins_desc[1]);
free_irq(IRQ_EINT3, &pins_desc[2]);
free_irq(IRQ_EINT4, &pins_desc[3]);
return 0;
}
static int fifth_drv_fasync (int fd, struct file *filp, int on)
{
printk("driver: fifth_drv_fasync\n");
return fasync_helper (fd, filp, on, &button_async);
}
static struct file_operations fifth_drv_fops = {
.owner = THIS_MODULE,/* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open =fifth_drv_open,
.read =fifth_drv_read,
.release = fifth_drv_close,
.fasync = fifth_drv_fasync,
};
static void 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;
}
else
{
/* 按下 */
key_val = pindesc->key_val;
}
ev_press = 1; /* 表示中断发生了 */
wake_up_interruptible(&button_waitq);/* 唤醒休眠的进程 */
//kill_fasync (&button_async, SIGIO, POLL_IN);
}
int major;
static int fifth_drv_init(void)
{
init_timer(&buttons_timer);
buttons_timer.function = buttons_timer_function;
//buttons_timer.expires = 0;
add_timer(&buttons_timer);
major = register_chrdev(0, DEVICE_NAME, &fifth_drv_fops);
timer_class= class_create(THIS_MODULE, DEVICE_NAME);
device_create(timer_class, NULL, MKDEV(major, 0), NULL, "timer");
return 0;
}
static void fifth_drv_exit(void)
{
unregister_chrdev(major, "signal");
device_destroy(timer_class,MKDEV(major, 0));
class_destroy(timer_class);
}
module_init(fifth_drv_init);
module_exit(fifth_drv_exit);
MODULE_LICENSE("GPL");
测试程序不变,只是把open里面变成/dev/timer