学过单片机的都知道,按键可能会出现抖动现象。linux也一样。
如下,出现两次按下的输出值
按键由于他的物理特性,可能会在按键松开时产生多次脉冲,每次脉冲都可能产生中断。关键是消除多次读取脉冲,只读一次。引入定时器,每隔多少时间之后读取(错过抖动时序)。
一.内核定时器
参考《linux设备驱动程序p196》
如果我们需要在将来的某个时间点调度执行某个动作,同时在该时间点到达之前不会阻塞当前进程,则可以使用内核定时器。
头文件和实现文件:<linux/timer.h> <kernel/timer.c>
定时器API
struct timer_list {
/*....*/
unsigned long expires;
void (*function)(unsigned long);
unsigned long data;
/*....*/
};
struct timer_list TIMER_INITIALIZER(_function,_expires,_data); //定义结构体变量
void init_timer(struct timer_list *timer); //初始化
void add_timer(struct timer_list *timer); //
int del_timer(struct timer_list *timer);
int mod_timer(struct timer_list *timer,unsigned long expires); //更新某个定时器的到期时间
全局变量jiffies_64就和HZ有关,它是一个64位整型变量,记录了系统启动以来时钟中断的个数(也就是tick数)。我们知道HZ是每秒钟产生的时钟中断的个数,那么jiffies_64每秒钟就增加HZ大小的值,例如,如果HZ=250,那jiffies_64在一秒后会变为jiffies_64+250,也就是精度为1000/HZ毫秒。在timer_list定时器中设置到期时间时,我们会用 (jiffies + 5 * HZ) 来表示5秒后到期就是这个道理。
二.修改代码支持定时器防抖
以前在中断处理函数里,我们做的是读取按键值,异步通知。现在把这些代码搬过去定时器的function中,实现按下按键后隔10ms后再读取按键值。
步骤:
1.定义定时器结构体
2.初始化
3.把中断处理函数的代码搬过去定时器fuction中并修改,修改中断处理函数
jiffies参考:Linux中的jiffies介绍
在中断处理函数里使用mod_timer,当发生中断时,会更新超时时间,比如当前滴答时钟为100,设置110后就会去调用定时器的fuction函数。
4.释放定时器
del_timer(&mybuttons_timer);
三.代码
#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>
#include <linux/timer.h>
static struct class *seconddrv_buttons_class;
static struct class_device *seconddrv_buttons_class_dev;
static struct fasync_struct *button_async;
volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;
struct timer_list mybuttons_timer;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq); //定义并初始化等待队列
//static int canopen=1;
static DECLARE_MUTEX(button_lock); //定义互斥锁
/* 中断事件标志, 中断服务程序将它置1,third_drv_read将它清0 */
static volatile int ev_press = 0;
struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
struct pin_desc pins_desc[4] = {
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPG3, 0x03},
{S3C2410_GPG11, 0x04},
};
struct pin_desc *irq_dev;
static unsigned char key_val;
static irqreturn_t mybuttons_irq(int irq, void *dev_id)
{
irq_dev=(struct pin_desc *)dev_id;
mod_timer(&mybuttons_timer,jiffies+HZ/100);//jiffies是当前的tick,HZ代表1s,HZ/100=10ms.jiffies会++到jiffies+10ms后产生定时器中断
return IRQ_RETVAL(IRQ_HANDLED);
}
static int seconddrv_buttons_open(struct inode *inode, struct file *file)
{
/*获取信号量,只有第一个应用程序才可以获得,其余应用程序会在这里陷入休眠*/
down(&button_lock);
/*配置GPF0,2为输入引脚,中断号参考arch\arm\plat-s3c24xx\Irq.c的s3c24xx_init_irq*/
/*配置GPG3,11为输入引脚*/
request_irq(IRQ_EINT0, mybuttons_irq, IRQT_BOTHEDGE, "S1", &pins_desc[0]);
request_irq(IRQ_EINT2, mybuttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[1]);
request_irq(IRQ_EINT11, mybuttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[2]);
request_irq(IRQ_EINT19, mybuttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[3]);
//printk("seconddrv_open\n");
return 0;
}
ssize_t seconddrv_buttons_read(struct file *file, char __user *buf,size_t size, loff_t *ppos)
{
if (size != 1)
return -EINVAL;
// 没有按键按下时,休眠
//wait_event_interruptible(button_waitq, ev_press);
//按键按下时
copy_to_user(buf, &key_val,1);
//ev_press = 0;
return 1;
}
static int seconddrv_buttons_close(struct inode * inode, struct file * file)
{
/*应用程序用完后,释放信号量*/
up(&button_lock);
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]);
return 0;
}
static unsigned int secondddrv_butttons_poll(struct file *file, struct poll_table_struct *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_waitq, wait); // 不会立即休眠
if (ev_press) //ev_press 不为 0 时,poll 休眠直到time_out后break循环体 ,为1时,break
mask |= POLLIN | POLLRDNORM;
return mask;
}
static int secondddrv_butttons_fasync(int fd, struct file *filp, int on)
{
printk("secondddrv_butttons_fasync\n");
return fasync_helper (fd, filp, on, &button_async);
}
static struct file_operations seconddrv_buttons_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = seconddrv_buttons_open,
.read = seconddrv_buttons_read,
.release= seconddrv_buttons_close,
.poll = secondddrv_butttons_poll,
.fasync = secondddrv_butttons_fasync,
};
static void mybuttons_timerfun(unsigned long p)
{
unsigned int pinval;
struct pin_desc *pindesc =irq_dev;
if(!pindesc)
return;
pinval = s3c2410_gpio_getpin(pindesc->pin); //内核提供的读取引脚值的函数
if(pinval) // 1为松开
{
key_val =0x80 | pindesc->key_val;
}
else
{
key_val =pindesc->key_val;
}
//ev_press = 1; /* 中断标志位=1,表示中断发生了 */
//wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */\
kill_fasync (&button_async, SIGIO, POLL_IN);
}
int major;
int seconddrv_buttons_init(void)
{
init_timer(&mybuttons_timer); //初始化定时器
//mybuttons_timer.data = (unsigned long) SCpnt; //function的传参,不用可省略
//mybuttons_timer.expires = jiffies + 100*HZ; /* 超时时间,放在中断处理函数使用,没有设置默认=0,即fuction会立即执行 */
mybuttons_timer.function = mybuttons_timerfun;
add_timer(&mybuttons_timer);
major = register_chrdev(0,"seconddrv",&seconddrv_buttons_fops);
seconddrv_buttons_class = class_create(THIS_MODULE,"buttons");
seconddrv_buttons_class_dev = class_device_create(seconddrv_buttons_class,NULL,MKDEV(major,0),NULL,"mybuttons");
gpfcon = (volatile unsigned long *)ioremap(0x56000050,16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long *)ioremap(0x56000060,16);
gpgdat = gpgcon +1;
return 0;
}
void seconddrv_buttons_exit(void)
{
unregister_chrdev(major,"seconddrv");
class_destroy(seconddrv_buttons_class);
class_device_unregister(seconddrv_buttons_class_dev);
iounmap(gpfcon);
iounmap(gpgcon);
del_timer(&mybuttons_timer);
return 0;
}
module_init(seconddrv_buttons_init);
module_exit(seconddrv_buttons_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>
/* fifthdrvtest
*/
int fd;
void my_signal_fun(int signum)
{
unsigned char key_val;
read(fd, &key_val, 1);
printf("key_val: 0x%x\n", key_val);
}
int main(int argc, char **argv)
{
unsigned char key_val;
int ret;
int Oflags;
signal(SIGIO, my_signal_fun);
fd = open("/dev/mybuttons", 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);
while (1)
{
sleep(1000);
}
return 0;
}
测试结果: