按鍵驅(qū)動--定時器消抖
#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"
int major = 0;
static struct class *keydrv_class;
static struct class_device *keydrv_class_dev;
volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;
struct pin_desc{
};
struct pin_desc pins_desc[4] = {
};
// 鍵值: 按下時, 0x01, 0x02, 0x03, 0x04
// 鍵值: 松開時, 0x81, 0x82, 0x83, 0x84
static unsigned char key_val;
static struct timer_list buttons_timer;
static struct pin_desc *irq_pd;
static DECLARE_MUTEX(button_lock); //定義互斥鎖
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
// 中斷事件標志, 中斷服務(wù)程序?qū)⑺?,third_drv_read將它清0
static volatile int ev_press = 0;
static struct fasync_struct *button_async;
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
}
static int key_drv_open(struct inode *inode, struct file *file)
{
}
ssize_t key_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
}
int key_drv_close(struct inode *inode, struct file *file)
{
}
static unsigned key_drv_poll(struct file *file, poll_table *wait)
{
}
static int key_drv_fasync (int fd, struct file *filp, int on)
{
}
static struct file_operations key_drv_fops = {
};
static void buttons_timer_function(unsigned long data)
{
}
static int key_drv_init(void)
{
}
static void key_drv_exit(void)
{
}
module_init(key_drv_init);
module_exit(key_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"
int fd;
void my_signal_fun(int signum)
{
}
int main(int argc, char **argv)
{
}
===============================================================
解析:
1、先定義一個定時器:static struct timer_list buttons_timer;
2、再在key_drv_init函數(shù)中初始化定時器:
3、在按鍵中斷中設(shè)置定時器時間:
4、如果沒有二次中斷修改中斷時間則10ms以后就會進入定時器中斷函數(shù),處理定時器中斷。
評論