驱动程序之_1_字符设备_8_按键消抖

驱动程序之_1_字符设备_8_按键消抖

按下按键时,由于按键抖动,会产生几个高低脉冲,而cpu处理太快,这几次高低脉冲被当作有效信号处理,处理办法:使用定时器,检测到第一次按下后,启动定时器,延迟一段时间(一般取10ms),再检测一次,如果这时候检测到按键按下,则确定有按键按下

驱动程序中运行流程:
按键中断触发后,记录键值,修改定时值,10ms后,触发定时器中断,将键值传送到应用程序

创建定时器

init_timer(&buttons_timer);

设定延时值,一般使用系统提供的滴答时钟(jiffies每10ms一次加1,HZ被定义为100)

mod_timer(&buttons_timer,jiffies+HZ/100);

初始化定时器

init_timer(&buttons_timer);

设定定时器中断服务程序

buttons_timer.function = buttons_timer_function;

将定时器添加到定时器队列

add_timer(&buttons_timer);

定时器中断服务程序函数原型

void (*function)(unsigned long);

附上驱动程序代码和测试用的应用程序代码

驱动程序

#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/device.h>
#include <linux/irq.h>

static struct class *my_irq_class;
static struct class_device *my_irq_class_device;

static unsigned char key_val;

static DECLARE_MUTEX(button_lock);

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

static volatile int ev_press = 0;

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

struct pin_desc key_pins_desc[4]={
	{S3C2410_GPF0,0x01},
	{S3C2410_GPF2,0x02},
	{S3C2410_GPG3,0x03},
	{S3C2410_GPG11,0x04},
};

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

			 
	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);
}



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 my_irq_drv_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",&key_pins_desc[0]);
	request_irq(IRQ_EINT2,buttons_irq,IRQT_BOTHEDGE,"S3",&key_pins_desc[1]);
	request_irq(IRQ_EINT11,buttons_irq,IRQT_BOTHEDGE,"S4",&key_pins_desc[2]);
	request_irq(IRQ_EINT19,buttons_irq,IRQT_BOTHEDGE,"S5",&key_pins_desc[3]);	
	
	return 0;
}

int my_irq_drv_close(struct inode *inode,struct file *file)
{
	up(&button_lock);
 
	free_irq(IRQ_EINT0,&key_pins_desc[0]);
	free_irq(IRQ_EINT2,&key_pins_desc[1]);
	free_irq(IRQ_EINT11,&key_pins_desc[2]);
	free_irq(IRQ_EINT19,&key_pins_desc[3]);	
	
	return 0;
}


ssize_t my_irq_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);	
	}

	ev_press = 0;
	
	copy_to_user(buf,&key_val,1);

	return 0;
}



static struct file_operations irq_drv_fops = {
	.owner = THIS_MODULE,
	.open = my_irq_drv_open,
	.read = my_irq_drv_read,
	.release = my_irq_drv_close,
};

static int major;

static int my_irq_drv_init(void)
{
	major = register_chrdev(0,"my_irq_drv",&irq_drv_fops);
	my_irq_class = class_create(THIS_MODULE,"my_irq_drv");
	my_irq_class_device = class_device_create(my_irq_class,NULL,MKDEV(major,0),NULL,"key_irq_drv");


	init_timer(&buttons_timer);
	buttons_timer.function = buttons_timer_function;
	add_timer(&buttons_timer);

	return 0;
}

static void my_irq_drv_exit(void)
{
	unregister_chrdev(major,"my_irq_drv");
	class_device_unregister(my_irq_class_device);
	class_destroy(my_irq_class);
}

module_init(my_irq_drv_init);
module_exit(my_irq_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 <unistd.h>
#include <fcntl.h>

int main(int argc,char *args[])
{
	unsigned char key_val;	
	int fd;
	int ret;

	fd = open("/dev/key_irq_drv",O_RDWR);

	if(fd < 0)
	{
		printf("can't open\r\n");
		return 0;
	}
	printf("open successfully\r\n");
	
	while(1)
	{
		ret = read(fd,&key_val,1);
		printf("key_val : 0x%x , ret : %d\r\n",key_val,ret);
	}

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值