linux添加定时器防抖,linux驱动2.3按键中断-定时器防抖
本節目標:
通過定時器來防止按鍵抖動,測試程序是使用上節的:阻塞操作的測試程序
一、引入
如下圖所示,在沒有定時器防抖情況下,按鍵沒有穩定之前會多次進入中斷,使得輸出多個相同信息出來
按鍵波形圖,如下所示:
問:如何消去按鍵抖動?
答:通過定時器延時10ms,然后每當按鍵進入中斷時就更新定時器延時10ms,若延時10ms到了說明已經過了抖動范圍,然后再打印按鍵電平信息
二、定時器結構體和函數介紹
我們先來看看兩個全局變量:
jiffies: 是系統時鐘,全局變量,默認每隔10ms加1
HZ:是每S的頻率,通過系統時鐘換算出來,比如每隔10ms加1,那么HZ就等于100。
2.1、定時器結構體timer_list
timer_list常用結構體成員如下所示:
1)data //傳遞到*function超時處理函數的參數,可以通過參數來獲取信息
2)expires //定時器到期的時間,當expires小于等于jiffies時,這個定時器便到期并調用定時器超時處理函數,然后就不會再調用了,比如要使用10ms后到期,賦值(jiffies+HZ/100)即可
3)void (*function)(unsigned long) //定時器超時處理函數。
2.2、定時器常用函數
init_timer(struct timer_list*) //定時器初始化結構體函數,
add_timer(struct timer_list*) //往系統添加定時器,告訴內核有個定時器結構體
mod_timer(struct timer_list *, unsigned long jiffier_timerout) //修改定時器的超時時間為jiffies_timerout, 當expires小于等于jiffies時,便調用定時器超時處理函數。
timer_pending(struct timer_list *) //定時器狀態查詢,如果在系統的定時器列表中則返回1,否則返回0;
del_timer(struct timer_list*) //刪除定時器,在本驅動程序出口函數sixth_drv_exit()里添加
三、修改驅動程序實現定時器消抖動并測試
3.1、首先定義一個定時器結構體:
static struct timer_list buttons_timer; //定義定時器結構體
3.2、在init入口函數中初始化定時器結構體:
init_timer(&buttons_timer); //初始化結構體
/*成員.data未使用
不需要定時器到期時間,所以成員.expires無需初始化,默認為0,由于小于等于jiffies,會進入一次定時器超時函數*/
buttons_timer. function= buttons_timer_ function;
add_timer(&buttons_timer); //告訴內核,有一個定時器
注:以上3步可以用函數 setup_timer(time,func,data)代替,該函數實現賦值并初始化定時器,比手動設置更方便
3.3、在exit出口函數中刪除定時器:
del_timer(&buttons_timer); //刪除定時器
3.4、定義全局變量*irq_dev_id并在中斷服務函數中獲取dev_id
struct pin_desc *irq_dev_id ; //定義全局變量獲取dev_id
并修改中斷服務函數:
static irqreturn_t buttons_irq (int irq, void *dev_id) //中斷服務函數
{
irq_dev_id =(struct pin_desc *)dev_id; //獲取引腳描述結構體
/*每產生一次中斷,則更新定時器10ms超時 */
mod_timer(&buttons_timer, jiffies+HZ/100);
return IRQ_RETVAL(IRQ_HANDLED);
}
注意: jiffies+HZ/100 也可以直接換成 jiffies + msecs_to_jiffies(10),更加方便
3.5、超時函數
當10ms超時到了,進入定時器超時函數,處理*irq_dev_id來判斷是哪個按鍵按下的
static void buttons_timer_function(unsigned long data) //定時器超時函數
{
unsigned int pin_val=0;
if(!irq_dev_id) //初始化時,由于定時器.expires成員=0,會進入一次,若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
{
/*沒有按下(上升沿),加上0x80*/
key_val=irq_dev_id->pin_status|0x80;
}
even_press=1; //退出等待隊列
wake_up_interruptible(&button_wait); //喚醒 中斷
kill_fasync(&button_async, SIGIO, POLL_IN); //發送SIGIO信號給應用層
}
3.6、測試效果
如下圖所示,我們運行測試程序,來快速按下按鍵試試:
四、代碼總覽
測試程序代碼使用阻塞操作的測試程序。
驅動程序sixth.c代碼:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static struct timer_list buttons_timer; //定義定時器結構體
struct pin_desc *irq_dev_id ; //定義全局變量獲取dev_id
static struct class *sixthdrv_class;
static struct class_device *sixthdrv_class_devs;
/*定義互斥鎖button_lock,被用來后面的down()和up()使用 */
static DECLARE_MUTEX(button_lock);
/* 聲明等待隊列類型中斷 button_wait */
static DECLARE_WAIT_QUEUE_HEAD(button_wait);
/* 異步信號結構體變量 */
static struct fasync_struct * button_async;
/*
* 定義中斷事件標志
* 0:進入等待隊列 1:退出等待隊列
*/
static int even_press=0;
/*
* 定義全局變量key_val,保存key狀態
*/
static int key_val=0;
/*
*引腳描述結構體
*/
struct pin_desc{
unsigned int pin;
unsigned int pin_status;
};
/*
*key初始狀態(沒有按下): 0x81,0x82,0x83,0x84
*key狀態(按下): 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]);
/*釋放信號量*/
up(&button_lock);
return 0;
}
/*
* 確定是上升沿還是下降沿
*/
static irqreturn_t buttons_irq (int irq, void *dev_id) //中斷服務函數
{
irq_dev_id =(struct pin_desc *)dev_id; //獲取引腳描述結構體
/*每產生一次中斷,則更新定時器10ms超時 */
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 //阻塞操作,獲取不到則進入休眠
{
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 ) //沒有按鍵按下
return -1;
}
/*阻塞操作,則直接進入休眠狀態,直到有按鍵按下為止*/
/*進程 進入等待隊列(休眠狀態)*/
wait_event_interruptible(button_wait, even_press);
/*有按鍵按下,退出等待隊列,上傳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); // 不會立即休眠
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結構體,就能使用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函數,來釋放中斷服務函數
.poll = sixth_poll,
.fasync= sixth_fasync, //初始化異步信號函數
};
static void buttons_timer_function(unsigned long data) //定時器超時函數
{
unsigned int pin_val=0;
if(!irq_dev_id) //定時器.expires成員=0,會進入一次,若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
{
/*沒有按下(上升沿),加上0x80*/
key_val=irq_dev_id->pin_status|0x80;
}
even_press=1; //退出等待隊列
wake_up_interruptible(&button_wait); //喚醒 中斷
kill_fasync(&button_async, SIGIO, POLL_IN); //發送SIGIO信號給應用層
}
volatile int sixth_major;
static int sixth_drv_init(void)
{
init_timer(&buttons_timer); //初始化定時器
buttons_timer. function= buttons_timer_function; //定時器超時函數
add_timer(&buttons_timer); //添加到內核中
sixth_major=register_chrdev(0,"sixth_drv",&sixth_drv_fops); //創建驅動
sixthdrv_class=class_create(THIS_MODULE,"sixth_dev"); //創建類名
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"); //卸載驅動
class_device_unregister(sixthdrv_class_devs); //卸載類設
class_destroy(sixthdrv_class); //卸載類
del_timer(&buttons_timer); //刪除定時器
return 0;
}
module_init(sixth_drv_init);
module_exit(sixth_drv_exit);
MODULE_LICENSE("GPL v2");
}
總結
以上是生活随笔為你收集整理的linux添加定时器防抖,linux驱动2.3按键中断-定时器防抖的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux端口6888被占用怎么解决,z
- 下一篇: java equ,Java equals