按键驱动的恩恩怨怨之防抖动

转载请注明出处:http://blog.csdn.net/ruoyunliufeng/article/details/24540403

一.驱动代码

#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 class *sixthdrv_class;
static struct class_device	*sixthdrv_class_dev;

//volatile unsigned long *gpfcon;
//volatile unsigned long *gpfdat;

static struct timer_list buttons_timer;    //定义一个结构体(定时器)

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

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

static struct fasync_struct *button_async;     //定义发送结构


struct pin_desc{                            //定义结构体
	unsigned int pin;
	unsigned int key_val;
};


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

/*
 * K1,K2,K3,K4相应GPG0,GPG3,GPG5,GPG6
 */

struct pin_desc pins_desc[4] = {                  //定义一个结构体数组
	{S3C2410_GPG0, 0x01},
	{S3C2410_GPG3, 0x02},
	{S3C2410_GPG5, 0x03},
	{S3C2410_GPG6, 0x04},
};

static struct pin_desc *irq_pd;                //发生中断时的引脚描写叙述

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

static DECLARE_MUTEX(button_lock);     //定义相互排斥锁

/*
  * 确定按键值
  */
static irqreturn_t buttons_irq(int irq, void *dev_id)         //參数中断号,和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);
	}

	/* GPG0,GPG3,GPG5,GPG6为中断引脚: EINT8,EINT11,EINT13,EINT14 */
	request_irq(IRQ_EINT8,  buttons_irq, IRQT_BOTHEDGE, "K1", &pins_desc[0]);  //设置引脚,使能中断
	request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "K2", &pins_desc[1]);
	request_irq(IRQ_EINT13, buttons_irq, IRQT_BOTHEDGE, "K3", &pins_desc[2]);
	request_irq(IRQ_EINT14, buttons_irq, IRQT_BOTHEDGE, "K4", &pins_desc[3]);	

	return 0;
}

ssize_t sixth_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 1;
}


int sixth_drv_close(struct inode *inode, struct file *file)
{
	//atomic_inc(&canopen);
	free_irq(IRQ_EINT8,  &pins_desc[0]);       //出链,禁止中断
	free_irq(IRQ_EINT11, &pins_desc[1]);
	free_irq(IRQ_EINT13, &pins_desc[2]);
	free_irq(IRQ_EINT14, &pins_desc[3]);
	up(&button_lock);                        //释放信号量
	return 0;
}

static unsigned sixth_drv_poll(struct file *file, poll_table *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) //应用程序调用接口,fasync_helper即调用
{
	printk("driver: sixth_drv_fasync
");
	return fasync_helper (fd, filp, on, &button_async);  //初始化结构体,使中断中能够使用
}


static struct file_operations sencod_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 void buttons_timer_function(unsigned long data)
{
	struct pin_desc * pindesc = irq_pd;   //?定义一个结构体指针使他的初值为ID
	unsigned int pinval;

	if (!pindesc)
		return;
	
	pinval = s3c2410_gpio_getpin(pindesc->pin);   //系统函数独处引脚值(GPF0)

	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);   //有中断发送告诉应用程序
}


static int sixth_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, "sixth_drv", &sencod_drv_fops);  //注冊

	sixthdrv_class = class_create(THIS_MODULE, "sixth_drv");       //自己主动创建设备

	sixthdrv_class_dev = class_device_create(sixthdrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */

//	gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
//	gpfdat = gpfcon + 1;

	return 0;
}

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


module_init(sixth_drv_init);

module_exit(sixth_drv_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>


/* sixthdrvtest 
  */
int fd;

void my_signal_fun(int signum)  //读取按键值,打印
{
	unsigned char key_val;
	read(fd, &key_val, 1);
	printf("key_val: 0x%x
", key_val);
}

int main(int argc, char **argv)
{
	unsigned char key_val;
	int ret;
	int Oflags;

	//signal(SIGIO, my_signal_fun);
	
	fd = open("/dev/buttons", O_RDWR);
	if (fd < 0)
	{
		printf("can't open!
");
		return -1;
	}

	//fcntl(fd, F_SETOWN, getpid());
	
	//Oflags = fcntl(fd, F_GETFL); 
	
	//fcntl(fd, F_SETFL, Oflags | FASYNC);


	while (1)
	{
		ret = read(fd, &key_val, 1);
		printf("key_val: 0x%x, ret = %d
", key_val, ret);
		//sleep(5);
	}
	
	return 0;
}

三.分析

           小伙伴们还记得我上两节按键的时候有时候会出现两下的情况吗?归根结底就是没消抖动,按键是要消抖动的,玩过单片机的同学都知道,延时就好了。如今我们玩ARM了事实上道理也一样。详细怎么做?

           1.定义结构体

static struct timer_list buttons_timer;    //定义一个结构体(定时器)
static struct pin_desc *irq_pd;                //发生中断时的引脚描写叙述

             2.初始化(在入口函数中)

	init_timer(&buttons_timer);        //初始化定时器
	buttons_timer.function = buttons_timer_function;//设置处理函数
	add_timer(&buttons_timer);         //定时器告诉内核
            3.处理函数

将原来中断中的处理函数放到定时器处理函数中(void buttons_timer_function(unsigned long data))然后在中断处理函数中

static irqreturn_t buttons_irq(int irq, void *dev_id)         //參数中断号,和ID
{
	/* 10ms后启动定时器 */
	irq_pd = (struct pin_desc *)dev_id;
	mod_timer(&buttons_timer, jiffies+HZ/100);       //改动定时器的超时时间
	return IRQ_RETVAL(IRQ_HANDLED);
}
这段代码就是实现消抖的核心所在:

                          a.抖动的产生:

        通常的按键所用开关为机械弹性开关,当机械触点断开、闭合时,因为机械触点的弹性作用,一个按键开关在闭合时不会立即稳定地接通,在断开时也不会一下子断开。因而在闭合及断开的瞬间均伴随有一连串的抖动,为了不产生这样的现象而作的措施就是按键消抖。抖动时间的长短由按键的机械特性决定,一般为5ms~10ms。

                           b.我们怎样实现消抖:

由上图我们能够看出就是这些锯齿是我们识别了以为是按下了(事实上是抖动)所以我们用定时器延时10MS等按键状态稳定了再进行处理。

                           c.代码实现:

	mod_timer(&buttons_timer, jiffies+HZ/100);       //改动定时器的超时时间
这里的100就决定了延迟时间用HZ代表1S的意思,1/100就是10MS了。

                4.工作截图



原文地址:https://www.cnblogs.com/mengfanrong/p/3870518.html