字符设备驱动程序——定时器按键消抖

实现方式
需要设置的结构体
struct timer_list {
struct list_head entry;
unsigned long expires;
void (*function)(unsigned long);
unsigned long data;
struct tvec_base *base;

}
1、static struct timer_list buttons_timer;//定义timer结构体

2、init_timer(&buttons_timer);//初始化timer
buttons_timer.function = buttons_timer_func;//定义超时处理函数
//buttons_timer.expires = 0;//入口处超时时间为0,会立马进入定时处理函数,
3、 add_timer(&buttons_timer); //加入定时器

4、mod_timer(&buttons_timer, jiffies+HZ/100);//设置超时时间
5、del_timer(&buttons_timer);//删除定时器
驱动程序
包含前面的互斥锁、异步通知、非阻塞。

#include <linux/module.h>
#include <linux/sched.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 <linux/device.h>
#include <mach/gpio.h>
#include <linux/interrupt.h>
#include <linux/poll.h>


static struct class *newdrv_class;


volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;
volatile unsigned long *gpgdat = NULL;
volatile unsigned long *gpgcon = NULL;

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

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

static struct fasync_struct *newdrv_async;

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

static struct timer_list buttons_timer;//定义timer结构体

static struct pin_desc *irq_pd;//中断引脚描述

struct pin_desc{
	unsigned int pin;//引脚
	unsigned int key_val;//键值,按下01 02 03 04;松开81 82 83 84
};

struct pin_desc pins_desc[4]={
	{S3C2410_GPF(0), 0x01},
	{S3C2410_GPF(2), 0x02},
	{S3C2410_GPG(3), 0x03},
	{S3C2410_GPG(11), 0x04},
};

/*在中断中加入延时,10ms后确定按键值*/	
static int buttons_irq (int irq, void *dev_id)
{	
	irq_pd = (struct pin_desc *)dev_id;

	/*系统每10ms产生一次中断*/
	/*jiffies+HZ  表示1s的超时时间;HZ=100表示100个jiffies*/   	
	mod_timer(&buttons_timer, jiffies+HZ/100);//当前值jiffies推迟10ms(1S/100)
	return IRQ_RETVAL(IRQ_HANDLED);//接收到了中断信号,并处理,返回1
}

static int buttons_timer_func (unsigned long data)
{
	struct pin_desc *pindesc= irq_pd;
	unsigned int pinval;

	if (!pindesc)//时间到达时,没有产生中断
		return -1;

	pinval=s3c2410_gpio_getpin(pindesc->pin);//读引脚
	if(pinval)//1 松开
	{
		key=0x80|pindesc->key_val;
	}
	else//按下
		key=pindesc->key_val;
	
	  ev_press = 1;                           /* 表示中断发生了 */
	  wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */

	  kill_fasync(&newdrv_async,SIGIO,POLL_IN);//发送信号,给newdrv_async
	  
}


static int new_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);
		}
	
			/* 中断引脚			中断函数		触发方式		名称	     传入dev―id */
	request_irq(IRQ_EINT0,  buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S2", &pins_desc[0]);
	request_irq(IRQ_EINT2,  buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S3", &pins_desc[1]);
	request_irq(IRQ_EINT11, buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S4", &pins_desc[2]);
	request_irq(IRQ_EINT19, buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S5", &pins_desc[3]);
	return 0;
}
}

static ssize_t new_drv_read(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
	if(count!=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, 1);
	ev_press=0;
	return 1;
}

static int new_drv_close(struct inode *inode, struct file *file)
{
	up(&button_lock);//释放信号量,释放后才能再次被打开进入open
	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 int new_drv_fasync (int fd, struct file *filp, int on)
{
	return fasync_helper (fd, filp, on, &newdrv_async);//初始化newdrv_async
}

static struct file_operations new_drv_fops = {
    .owner  =THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open   =new_drv_open,     
	.read   =new_drv_read,
	.release=new_drv_close,
	.fasync   =new_drv_fasync,
};

int major;
static int new_drv_init(void)
{
	init_timer(&buttons_timer);//初始化timer
	buttons_timer.function = buttons_timer_func;//定义超时处理函数
	//buttons_timer.expires  = 0;入口处超时时间为0,会立马进入定时处理函数,因此要判断if (!pindesc)
	add_timer(&buttons_timer); 

	major = register_chrdev(0, "newdrv", &new_drv_fops); // 注册, 自动分配并返回主设备号
		/*在/sys/class/下创建类目录*/
	newdrv_class = class_create(THIS_MODULE, "newdrv");
		/*根据类目录,在驱动模块初始化函数中实现设备节点的自动创建*/
	device_create(newdrv_class, NULL, MKDEV(major, 0), NULL, "newdrv"); 

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

	return 0;
}

static void new_drv_exit(void)
{
	del_timer(&buttons_timer)unregister_chrdev(major, "newdrv"); // 卸载,删除
	device_destroy(newdrv_class,MKDEV(major, 0));
	class_destroy(newdrv_class);
	iounmap(gpfcon);
	iounmap(gpgcon);
}

module_init(new_drv_init);//入口函数
module_exit(new_drv_exit);//出口函数


MODULE_LICENSE("GPL");


测试程序同异步通知测试程序。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值