Linux驱动开发九:按键中断+定时去抖

本文介绍了一种基于Linux的按键驱动设计,包括去抖动处理、异步通知机制及定时器使用等关键技术点。通过具体代码实现,展示了如何有效地管理和响应按键事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于按键是金属弹片,很容易让按键产生多个状态值,这就是抖动。

代码如下:

buttons_drv.c

/*
 *功能:中断驱动,在按键驱动基础上修改,原理图见按键驱动,增加poll机制,增加异步通知机制 +按键中断去抖
 */
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>

#include <linux/device.h> 

#include <linux/irq.h>
#include <linux/interrupt.h>
//#include <mach/regs-gpio.h>  
//#include <mach/hardware.h>
#include <mach/gpio.h>  


#include <mach/hardware.h>  
#include <linux/sched.h>
#include <linux/poll.h>

#include <asm/atomic.h>

int major;
const char *major_name = "sixth_drv";
const char *minor_name = "button";

static struct class *sixthdrv_class;
static struct class_device *sixthdrv_class_dev;

volatile unsigned long *gpncon = NULL;
volatile unsigned long *gpndat = NULL;

//声明一个按键的等待队列
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

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

//异步
static struct fasync_struct *button_fasync;

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

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

//定时器相关
static struct timer_list buttons_timer;
static struct pin_desc *irq_pd;

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对应GPN0,GPGN1,GPN2,GPN3
 */
 /*
 struct pin_desc pins_desc[4] = {
	{S3C64XX_GPN0_EINT0, 0x01},
	{S3C64XX_GPN1_EINT1, 0x02},
	{S3C64XX_GPN2_EINT2, 0x03},
	{S3C64XX_GPN3_EINT3, 0x04},
};
struct pin_desc pins_desc[4] = {
	{0, 0x01},
	{1, 0x02},
	{2, 0x03},
	{3, 0x04},
};
*/
struct pin_desc pins_desc[4] = {
	{S3C64XX_GPN(0), 0x01},
	{S3C64XX_GPN(1), 0x02},
	{S3C64XX_GPN(2), 0x03},
	{S3C64XX_GPN(3), 0x04},
};

/*
 * 确定按键值
 *中断处理程序,并置标志位为1,唤醒等待队列上等待的进程,注册用户中断处理函数,设置触发方式为双边沿触发
 */
static irqreturn_t button_irq(int irq,void *dev_id)
{
	irq_pd = (struct pin_desc*)dev_id;
	
	mod_timer(&buttons_timer,jiffies + (HZ)/50);
	//jiffies为系统全局变量,用cat /proc/interrupts可以查看其值一直在变化“100:    1036564   s3c-timer  S3C2410 Timer Tick”
	//jiffies + (HZ)/100:表示当前时间起延时10ms后处理(HZ=1000)
	
	return IRQ_RETVAL(IRQ_HANDLED);
}

static void buttons_timer_function(unsigned long data)
{
	struct pin_desc * pindesc = irq_pd;
	
	if(!pindesc)	//如果没有中断发生就返回
		return ;
	unsigned int pinval;
	
//	pinval = ioread32(S3C64XX_GPNDAT);
//	pinval = gpio_get_value(S3C64XX_GPN(pindesc->pin));/* 读取中断引脚的状态 */
	pinval = gpio_get_value(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_fasync,SIGIO,POLL_IN);//发信号
}
/* open函数  */
static int sixth_drv_open(struct inode *inode,struct file *file)
{
#if 0
	if(!atomic_dec_and_test(&canopen))//原子操作,相当于一个标记,打开一次就有标记变化,要再次打开就返回
	{
		atomic_inc(&canopen);
		return -EBUSY;
	}

	//获取信号量
	down(&button_lock);	
#endif
	
	if(file->f_flags & O_NONBLOCK)
	{
		//非阻塞模式
		if(down_trylock(&button_lock))//如果没有获得信号就返回
			return -EBUSY;
	}
	else
	{
		//阻塞模式
		//获取信号量
		down(&button_lock);	
	}
	request_irq(IRQ_EINT(0),button_irq,IRQ_TYPE_EDGE_BOTH,"K1",&pins_desc[0]);
	request_irq(IRQ_EINT(1),button_irq,IRQ_TYPE_EDGE_BOTH,"K2",&pins_desc[1]);
	request_irq(IRQ_EINT(2),button_irq,IRQ_TYPE_EDGE_BOTH,"K3",&pins_desc[2]);
	request_irq(IRQ_EINT(3),button_irq,IRQ_TYPE_EDGE_BOTH,"K4",&pins_desc[3]);
//	request_irq(IRQ_EINT(4),button_irq,IRQ_TYPE_EDGE_BOTH,"K4",1);
	return 0;
}

/* read函数  */
static 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 -EBUSY;
	}
	else
	{
		//阻塞模式
		/* 如果没有按键动作, 休眠 */
		wait_event_interruptible(button_waitq, ev_press);
	}
	


	/* 如果有按键动作, 返回键值 */
	copy_to_user(buf, &key_val, 1);
	ev_press = 0;//重新清0
	
	return 1;
}

static unsigned sixth_drv_poll(struct file *file,poll_table *wait)
{
	unsigned int mask = 0;
	
	/* 不会立即休眠  只是将进程挂到key_waitq队列, 并不会休眠 */
	poll_wait(file,&button_waitq,wait);
	
	if(ev_press)//如果没有中断产生
		mask |= POLLIN | POLLRDNORM;
	
//	printk("\t*******************\n\tpoll\n\t***********************\n");
	return mask;
}
//主要是卸载用户中断处理程序
int sixth_drv_close(struct inode* inode,struct file* file)
{
#if 0
	atomic_dec(&canopen);//释放原子操作
#endif

	//释放信号量
	up(&button_lock);
		
	free_irq(IRQ_EINT(0),&pins_desc[0]);
	free_irq(IRQ_EINT(1),&pins_desc[1]);
	free_irq(IRQ_EINT(2),&pins_desc[2]);
	free_irq(IRQ_EINT(3),&pins_desc[3]);
	
	return 0;
}

static int sixth_drv_fasync(int fd,struct file *file,int on)
{
	printk("dirver:sixth_drv_fasync\n");
	
	return fasync_helper(fd,file,on,&button_fasync);//初始化结构button_fasync
}
	
	/* 结构体  */
static struct file_operations sixth_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,	
};


/* 入口函数  */	
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,major_name,&sixth_drv_fops);
	if(major < 0)
	{
		printk("Can't register major number\n");
		return major;
	}
	
	/* 自动生成设备节点机制,获取系统消息,cat /proc/class会发现 first_drv */
     sixthdrv_class = class_create(THIS_MODULE, major_name);
     /* register your own device in sysfs, and this will cause udev to create corresponding device node */
     sixthdrv_class_dev = device_create( sixthdrv_class, NULL, MKDEV(major, 0),NULL, minor_name );
     
     /* 地址映射 */
	gpncon = (volatile unsigned long *)ioremap(0x7F008830,16);
	gpndat = gpncon + 1;//指针加1
	
	return 0;
}

/* 出口函数  */
static void sixth_drv_exit(void)
{
	unregister_chrdev(major,major_name);
	device_unregister(sixthdrv_class_dev);
	class_destroy(sixthdrv_class);
	iounmap(gpncon);
}

module_init(sixth_drv_init);
module_exit(sixth_drv_exit);
MODULE_LICENSE("GPL");


Makefile:

obj-m := buttons_drv.o

KER_DIR := /work/work/linux/


all:
	$(MAKE) -C $(KER_DIR) M=`pwd` modules
#	cp first_drv.ko /work/tftpboot/
	cp buttons_drv.ko /work/nfsroot/
	echo $^
	
clean:
	make -C $(KER_DIR) M=`pwd` modules clean


test.c

/*
 *功能:在一定时间内没有按键中断,也返回
 */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <signal.h>


/*test 
 */


int main(int argc,char *argv[])
{
	int fd;
	int Oflags ;	
	unsigned char key_vals;
	int ret;
	
	fd = open("/dev/button",O_RDWR);
	if(fd < 0)
	{
		printf("can't opent /dev/button\n");
		return -1;
	}

	while(1)	
	{

		ret = read(fd,&key_vals,1);
		printf("key_vals = 0x%x ,ret = %d \n",key_vals,ret);
//		sleep(5);
	}
	
	return 0;
	
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值