linux添加定时器防抖,linux驱动2.3按键中断-定时器防抖
本節(jié)目標(biāo):
通過(guò)定時(shí)器來(lái)防止按鍵抖動(dòng),測(cè)試程序是使用上節(jié)的:阻塞操作的測(cè)試程序
一、引入
如下圖所示,在沒(méi)有定時(shí)器防抖情況下,按鍵沒(méi)有穩(wěn)定之前會(huì)多次進(jìn)入中斷,使得輸出多個(gè)相同信息出來(lái)
按鍵波形圖,如下所示:
問(wèn):如何消去按鍵抖動(dòng)?
答:通過(guò)定時(shí)器延時(shí)10ms,然后每當(dāng)按鍵進(jìn)入中斷時(shí)就更新定時(shí)器延時(shí)10ms,若延時(shí)10ms到了說(shuō)明已經(jīng)過(guò)了抖動(dòng)范圍,然后再打印按鍵電平信息
二、定時(shí)器結(jié)構(gòu)體和函數(shù)介紹
我們先來(lái)看看兩個(gè)全局變量:
jiffies: 是系統(tǒng)時(shí)鐘,全局變量,默認(rèn)每隔10ms加1
HZ:是每S的頻率,通過(guò)系統(tǒng)時(shí)鐘換算出來(lái),比如每隔10ms加1,那么HZ就等于100。
2.1、定時(shí)器結(jié)構(gòu)體timer_list
timer_list常用結(jié)構(gòu)體成員如下所示:
1)data //傳遞到*function超時(shí)處理函數(shù)的參數(shù),可以通過(guò)參數(shù)來(lái)獲取信息
2)expires //定時(shí)器到期的時(shí)間,當(dāng)expires小于等于jiffies時(shí),這個(gè)定時(shí)器便到期并調(diào)用定時(shí)器超時(shí)處理函數(shù),然后就不會(huì)再調(diào)用了,比如要使用10ms后到期,賦值(jiffies+HZ/100)即可
3)void (*function)(unsigned long) //定時(shí)器超時(shí)處理函數(shù)。
2.2、定時(shí)器常用函數(shù)
init_timer(struct timer_list*) //定時(shí)器初始化結(jié)構(gòu)體函數(shù),
add_timer(struct timer_list*) //往系統(tǒng)添加定時(shí)器,告訴內(nèi)核有個(gè)定時(shí)器結(jié)構(gòu)體
mod_timer(struct timer_list *, unsigned long jiffier_timerout) //修改定時(shí)器的超時(shí)時(shí)間為jiffies_timerout, 當(dāng)expires小于等于jiffies時(shí),便調(diào)用定時(shí)器超時(shí)處理函數(shù)。
timer_pending(struct timer_list *) //定時(shí)器狀態(tài)查詢,如果在系統(tǒng)的定時(shí)器列表中則返回1,否則返回0;
del_timer(struct timer_list*) //刪除定時(shí)器,在本驅(qū)動(dòng)程序出口函數(shù)sixth_drv_exit()里添加
三、修改驅(qū)動(dòng)程序?qū)崿F(xiàn)定時(shí)器消抖動(dòng)并測(cè)試
3.1、首先定義一個(gè)定時(shí)器結(jié)構(gòu)體:
static struct timer_list buttons_timer; //定義定時(shí)器結(jié)構(gòu)體
3.2、在init入口函數(shù)中初始化定時(shí)器結(jié)構(gòu)體:
init_timer(&buttons_timer); //初始化結(jié)構(gòu)體
/*成員.data未使用
不需要定時(shí)器到期時(shí)間,所以成員.expires無(wú)需初始化,默認(rèn)為0,由于小于等于jiffies,會(huì)進(jìn)入一次定時(shí)器超時(shí)函數(shù)*/
buttons_timer. function= buttons_timer_ function;
add_timer(&buttons_timer); //告訴內(nèi)核,有一個(gè)定時(shí)器
注:以上3步可以用函數(shù) setup_timer(time,func,data)代替,該函數(shù)實(shí)現(xiàn)賦值并初始化定時(shí)器,比手動(dòng)設(shè)置更方便
3.3、在exit出口函數(shù)中刪除定時(shí)器:
del_timer(&buttons_timer); //刪除定時(shí)器
3.4、定義全局變量*irq_dev_id并在中斷服務(wù)函數(shù)中獲取dev_id
struct pin_desc *irq_dev_id ; //定義全局變量獲取dev_id
并修改中斷服務(wù)函數(shù):
static irqreturn_t buttons_irq (int irq, void *dev_id) //中斷服務(wù)函數(shù)
{
irq_dev_id =(struct pin_desc *)dev_id; //獲取引腳描述結(jié)構(gòu)體
/*每產(chǎn)生一次中斷,則更新定時(shí)器10ms超時(shí) */
mod_timer(&buttons_timer, jiffies+HZ/100);
return IRQ_RETVAL(IRQ_HANDLED);
}
注意: jiffies+HZ/100 也可以直接換成 jiffies + msecs_to_jiffies(10),更加方便
3.5、超時(shí)函數(shù)
當(dāng)10ms超時(shí)到了,進(jìn)入定時(shí)器超時(shí)函數(shù),處理*irq_dev_id來(lái)判斷是哪個(gè)按鍵按下的
static void buttons_timer_function(unsigned long data) //定時(shí)器超時(shí)函數(shù)
{
unsigned int pin_val=0;
if(!irq_dev_id) //初始化時(shí),由于定時(shí)器.expires成員=0,會(huì)進(jìn)入一次,若irq_dev_id為0則退出
{
printk("expires: timer out\n");
return ;
}
pin_val=s3c2410_gpio_getpin(irq_dev_id->pin); //獲取按鍵值
if(pin_val)
{
/*按下 (下降沿),清除0x80*/
key_val=irq_dev_id->pin_status&0xef;
}
else
{
/*沒(méi)有按下(上升沿),加上0x80*/
key_val=irq_dev_id->pin_status|0x80;
}
even_press=1; //退出等待隊(duì)列
wake_up_interruptible(&button_wait); //喚醒 中斷
kill_fasync(&button_async, SIGIO, POLL_IN); //發(fā)送SIGIO信號(hào)給應(yīng)用層
}
3.6、測(cè)試效果
如下圖所示,我們運(yùn)行測(cè)試程序,來(lái)快速按下按鍵試試:
四、代碼總覽
測(cè)試程序代碼使用阻塞操作的測(cè)試程序。
驅(qū)動(dòng)程序sixth.c代碼:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static struct timer_list buttons_timer; //定義定時(shí)器結(jié)構(gòu)體
struct pin_desc *irq_dev_id ; //定義全局變量獲取dev_id
static struct class *sixthdrv_class;
static struct class_device *sixthdrv_class_devs;
/*定義互斥鎖button_lock,被用來(lái)后面的down()和up()使用 */
static DECLARE_MUTEX(button_lock);
/* 聲明等待隊(duì)列類型中斷 button_wait */
static DECLARE_WAIT_QUEUE_HEAD(button_wait);
/* 異步信號(hào)結(jié)構(gòu)體變量 */
static struct fasync_struct * button_async;
/*
* 定義中斷事件標(biāo)志
* 0:進(jìn)入等待隊(duì)列 1:退出等待隊(duì)列
*/
static int even_press=0;
/*
* 定義全局變量key_val,保存key狀態(tài)
*/
static int key_val=0;
/*
*引腳描述結(jié)構(gòu)體
*/
struct pin_desc{
unsigned int pin;
unsigned int pin_status;
};
/*
*key初始狀態(tài)(沒(méi)有按下): 0x81,0x82,0x83,0x84
*key狀態(tài)(按下): 0x01,0x02,0x03,0x04
*/
struct pin_desc pins_desc[4]={
{S3C2410_GPF0,0x01 },
{S3C2410_GPF2, 0x02 },
{S3C2410_GPG3, 0x03 },
{S3C2410_GPG11,0x04},
} ;
int sixth_drv_class(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]);
/*釋放信號(hào)量*/
up(&button_lock);
return 0;
}
/*
* 確定是上升沿還是下降沿
*/
static irqreturn_t buttons_irq (int irq, void *dev_id) //中斷服務(wù)函數(shù)
{
irq_dev_id =(struct pin_desc *)dev_id; //獲取引腳描述結(jié)構(gòu)體
/*每產(chǎn)生一次中斷,則更新定時(shí)器10ms超時(shí) */
mod_timer(&buttons_timer, jiffies+HZ/100);
return IRQ_RETVAL(IRQ_HANDLED);
}
static int sixth_drv_open(struct inode *inode, struct file *file)
{
if( file->f_flags & O_NONBLOCK ) //非阻塞操作,獲取不到則退出
{
if(down_trylock(&button_lock) )
return -1;
}
else //阻塞操作,獲取不到則進(jìn)入休眠
{
down(&button_lock);
}
request_irq(IRQ_EINT0,buttons_irq,IRQT_BOTHEDGE,"S1",&pins_desc[0]);
request_irq(IRQ_EINT2, buttons_irq,IRQT_BOTHEDGE, "S2", &pins_desc[1]);
request_irq(IRQ_EINT11, buttons_irq,IRQT_BOTHEDGE, "S3", &pins_desc[2]);
request_irq(IRQ_EINT19, buttons_irq,IRQT_BOTHEDGE, "S4", &pins_desc[3]);
return 0;
}
static int sixth_drv_read(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
if( file->f_flags & O_NONBLOCK ) //非阻塞操作,獲取不到則退出
{
if(!even_press ) //沒(méi)有按鍵按下
return -1;
}
/*阻塞操作,則直接進(jìn)入休眠狀態(tài),直到有按鍵按下為止*/
/*進(jìn)程 進(jìn)入等待隊(duì)列(休眠狀態(tài))*/
wait_event_interruptible(button_wait, even_press);
/*有按鍵按下,退出等待隊(duì)列,上傳key_val 給用戶層*/
if(copy_to_user(buf,&key_val,sizeof(key_val)))
return EFAULT;
even_press=0;
return 0;
}
static unsigned sixth_poll(struct file *file, poll_table *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_wait, wait); // 不會(huì)立即休眠
if (even_press)
mask |= POLLIN | POLLRDNORM;
return mask;
}
static int sixth_fasync (int fd, struct file *file, int on)
{
return fasync_helper(fd, file, on, & button_async); //初始化button_async結(jié)構(gòu)體,就能使用kill_fasync()了
}
static struct file_operations sixth_drv_fops={
.owner = THIS_MODULE,
.open = sixth_drv_open,
.read = sixth_drv_read,
.release=sixth_drv_class, //里面添加free_irq函數(shù),來(lái)釋放中斷服務(wù)函數(shù)
.poll = sixth_poll,
.fasync= sixth_fasync, //初始化異步信號(hào)函數(shù)
};
static void buttons_timer_function(unsigned long data) //定時(shí)器超時(shí)函數(shù)
{
unsigned int pin_val=0;
if(!irq_dev_id) //定時(shí)器.expires成員=0,會(huì)進(jìn)入一次,若irq_dev_id為0則退出
{
printk("expires: timer out\n");
return ;
}
pin_val=s3c2410_gpio_getpin(irq_dev_id->pin);
if(pin_val)
{
/* 按下 (下降沿),清除0x80*/
key_val=irq_dev_id->pin_status&0xef;
}
else
{
/*沒(méi)有按下(上升沿),加上0x80*/
key_val=irq_dev_id->pin_status|0x80;
}
even_press=1; //退出等待隊(duì)列
wake_up_interruptible(&button_wait); //喚醒 中斷
kill_fasync(&button_async, SIGIO, POLL_IN); //發(fā)送SIGIO信號(hào)給應(yīng)用層
}
volatile int sixth_major;
static int sixth_drv_init(void)
{
init_timer(&buttons_timer); //初始化定時(shí)器
buttons_timer. function= buttons_timer_function; //定時(shí)器超時(shí)函數(shù)
add_timer(&buttons_timer); //添加到內(nèi)核中
sixth_major=register_chrdev(0,"sixth_drv",&sixth_drv_fops); //創(chuàng)建驅(qū)動(dòng)
sixthdrv_class=class_create(THIS_MODULE,"sixth_dev"); //創(chuàng)建類名
sixthdrv_class_devs=class_device_create(sixthdrv_class, NULL, MKDEV(sixth_major,0), NULL,"buttons");
return 0;
}
static int sixth_drv_exit(void)
{
unregister_chrdev(sixth_major,"sixth_drv"); //卸載驅(qū)動(dòng)
class_device_unregister(sixthdrv_class_devs); //卸載類設(shè)
class_destroy(sixthdrv_class); //卸載類
del_timer(&buttons_timer); //刪除定時(shí)器
return 0;
}
module_init(sixth_drv_init);
module_exit(sixth_drv_exit);
MODULE_LICENSE("GPL v2");
}
總結(jié)
以上是生活随笔為你收集整理的linux添加定时器防抖,linux驱动2.3按键中断-定时器防抖的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Linux端口6888被占用怎么解决,z
- 下一篇: java equ,Java equals