7. 使用定时器去掉按键输入中产生抖动

本文介绍如何在Linux内核中利用定时器来处理按键输入时的抖动问题,通过电路图、驱动代码(Seven_drv.c)和测试代码进行详细说明。
摘要由CSDN通过智能技术生成
1. 电路图
2. Linux内核中使用定时器的步骤
static struct timer_list xxx_timer;		/* 定义全局的定时器 */
init_timer(&xxx_timer);					/* 初始化定时器 */
buttons_timer.function = xxx_function;	/* 设置超时调用函数 */
buttons_timer.expires = 0;				/* 超时时间, 如果不设置默认为0 */
add_timer(&xxx_timer);					/* 向内核注册定时器 ,当超时时间到后就会调用定时器函数buttons_timer_function */
mod_timer(&xxx_timer, jiffies+HZ/100);	/* 个性超时时间 */
3. 驱动代码(Seven_drv.c)
#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>

/*	描述:使用定时器去机械抖动
 *	原理:
 *		在加载驱动时初始化定时器,设置超时调用函数,将定时器加入内核定时器链表
 *		在中断函数中启动定时器,设置超时时间为10ms去抖动
 *		在超时函数中检测输入按键值,上报按键值
 */

static struct class *seven_drv_class;
static struct class_device *seven_drv_class_dev;
static struct fasync_struct *buttons_async;
static struct timer_list buttons_timer;

/*	键值:按下时, 0x01, 0x02, 0x03, 0x04
 *	键值:松开时, 0x81, 0x82, 0x83, 0x84
 */
typedef struct
{
	unsigned int pin;
	unsigned int key_val;
}s3c2440_buttons_desc; 

s3c2440_buttons_desc pins_desc[]={
	{S3C2410_GPF0, 0x01},
	{S3C2410_GPF2, 0x02},
	{S3C2410_GPG3, 0x03},
	{S3C2410_GPG11, 0x04},
};

static struct pins_desc *irq_pd;
unsigned int key_val;
/* 等待队列: 
 * 当没有按键被按下时,如果有进程调用s3c24xx_buttons_read函数,
 * 它将休眠
 */
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

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

static void buttons_timer_function (unsigned long data)
{
	unsigned pin_val;
	s3c2440_buttons_desc *pindesc = irq_pd;

	/* 如果pindesc==NULL则返回 */
	if(!pindesc)
		return;
	
	/* 读取pin值 */
	pin_val = s3c2410_gpio_getpin(pindesc->pin);

	/* 确定按键动作 */
	if(pin_val)
		key_val = pindesc->key_val | 0x80;	/* 松 开 */
	else
		key_val = pindesc->key_val;			/* 按 下 */

	ev_press = 1;                			/* 表示中断发生了 */
    wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */

	/* 向应用层发送可读信号 */
	kill_fasync(&buttons_async, SIGIO, POLL_IN);
}

irqreturn_t buttons_irq(int irq, void *dev_id)
{
	irq_pd = (struct pins_desc *)dev_id;
	/* 修改定时器超时时间 */
	/* HZ=100,在当前的系统中表示1S, HZ/100=10ms,这里设置超时时间为10ms */
	mod_timer(&buttons_timer, jiffies+HZ/100);
	return IRQ_HANDLED;
}

int seven_drv_open(struct inode *inode, struct file *file)
{
	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;
}

ssize_t seven_drv_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);	/* 如果ev_press等于0,休眠 */

	/* 如 果 有 按 键 动 作,返 回 键 值 */	
	copy_to_user(buf, &key_val, 1);
	/* 执行到这里时,ev_press等于1,将它清0 */
    ev_press = 0;
	return 0;
}

int seven_drv_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]);
	return 0;
}

unsigned int seven_drv_poll(struct file *file, struct poll_table_struct *wait)
{
	unsigned int mask=0;
	poll_wait(file, &button_waitq, wait);	/* 不会立即休眠 */
	if(ev_press)
		mask |= POLLIN | POLLRDNORM;

	return mask;
}

int seven_drv_fsync(int fd, struct file *filp, int on)
{
	/* 初始化 buttons_async结构体 */
	return fasync_helper(fd, filp, on, &buttons_async);	
}

static struct file_operations seven_drv_fops = {
	.owner		= THIS_MODULE,
	.open		= seven_drv_open,
	.read		= seven_drv_read,
	.release	= seven_drv_close,
	.poll 		= seven_drv_poll,
	.fasync		= seven_drv_fsync,
};

static int major;
static int __init seven_drv_init(void)
{
	/* 初始化定时器 */
	init_timer(&buttons_timer);
	/* 初始化定时调用函数 */
	buttons_timer.function = buttons_timer_function;
	buttons_timer.expires = 0;		/* 如果不设置默认为0 */
	/* 向内核注册定时器 ,当超时时间到后就会调用定时器函数buttons_timer_function */
	add_timer(&buttons_timer);

	major = register_chrdev(0, "seven_drv", &seven_drv_fops);

	seven_drv_class = class_create(THIS_MODULE, "seven_drv");
	seven_drv_class_dev = class_device_create(seven_drv_class, NULL, MKDEV(major, 0), NULL, "buttons_delay");
	return 0;
}

static void __exit seven_drv_exit(void)
{
	unregister_chrdev(major, "second_drv");
	class_device_unregister(seven_drv_class_dev);
	class_destroy(seven_drv_class);
	return 0;
}

module_init(seven_drv_init);
module_exit(seven_drv_exit);
MODULE_LICENSE("GPL");
4. 测试代码
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>

#define 	DEV_DEVICE		"/dev/buttons_delay"

int main(int argc, char *argv[])
{
	int fd, ret;
	unsigned char key_val;
	
	fd = open(DEV_DEVICE, O_RDWR);	/* 阻塞方式打开 */
	if(fd<0)
	{
		printf("can't open %s\n", DEV_DEVICE);
		return -1;
	}

	while(1)
	{
		ret = read(fd, &key_val, 1);
		printf("key_val=0x%x, ret=%d\n", key_val, ret);
	}
	close(fd);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值