一、為啥需要按鍵去抖
按鍵所用的開關(guān)為機(jī)械彈性開關(guān),當(dāng)機(jī)械觸點(diǎn)斷開包晰、閉合時(shí),由于機(jī)械觸點(diǎn)的彈性作用炕吸,開關(guān)不會(huì)馬上穩(wěn)定地接通或斷開伐憾。因而在閉合及斷開的瞬間總是伴隨有一連串的抖動(dòng),會(huì)導(dǎo)致按鍵中斷的驅(qū)動(dòng)服務(wù)函數(shù)短時(shí)間進(jìn)入多次赫模。
二树肃、內(nèi)核定時(shí)器
linux內(nèi)核使用 struct timer_list 來描述一個(gè)定時(shí)器:
struct timer_list{
struct list_head entry;
unsigned long expires ; /* 延時(shí)時(shí)間 */
void (*function)(unsigned long); /* 超時(shí)后執(zhí)行函數(shù) */
unsigned long data;
struct tvec_base *base;
}
2.1 定時(shí)器使用流程
定義定時(shí)器變量
初始化定時(shí)器(1.init_timer 初始化 2.設(shè)置超市時(shí)間)
add_timer 注冊(cè)定時(shí)器
mod_timer 啟動(dòng)定時(shí)器
注意:定時(shí)器超時(shí)一次過后就不會(huì)自動(dòng)啟動(dòng),需要再次調(diào)用mod_timer進(jìn)行啟動(dòng)瀑罗。
三胸嘴、代碼部分
#include <linux/module.h>
#include <linux/init.h>
#include <linux/miscdevice.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/fs.h>
#define GPFCON 0x56000050
#define GPFDAT 0x56000054
struct work_struct *work1;
struct timer_list key_timer ;
unsigned int * gpio_data;
void work1_func(struct work_struct *work)
{
//printk("key down \n");
//啟動(dòng)定時(shí)器
//jiffies 是linux是全局變量,表示現(xiàn)在的時(shí)間(滴答數(shù))
mod_timer(&key_timer斩祭,jiffies + HZ/10);//HZ是1s劣像,這里超時(shí)是100ms
}
void key_timer_func(unsigned long delta)
{
unsigned short data ;
data = readw(gpio_data)&0x01 ;
if( data == 0 ){
printk("key down \n");
}
}
irqreturn_t key_int(int irq , void * dev_id)
{
//提交下半部
schedule_work(work1);
return 0;
}
void key_hw_init()
{
unsigned int * gpio_config;
unsigned short data;
gpio_config = ioremap(GPFCON,4);
data = readw(gpio_config);
data &= ~0b11;
data |= 0b10;
writew(data,gpio_config);
gpio_data = ioremap(GPFDAT,4);
}
void key_open(struct inode *node, struct file *filp)
{
return 0 ;
}
struct file_operations key_fops={
.open = key_open,
};
struct miscdevice key_miscdev = {
.minor = 200,
.name = "key",
.fops = &key_fops,
};
static int button_init()
{
misc_register(&key_miscdev);
//注冊(cè)中斷處理程序
request_irq(IRQ_EINT0,key_init,IRQF_TRIGGER_FALLING,"key",0);
//按鍵初始化
key_hw_init();
//創(chuàng)建工作
work1 = kmalloc(sizeof(struct work_struct),GFP_KERNEL);
INIT_WORK(work1,work1_func);
//初始化定時(shí)器
init_timer(&key_timer);
//key_timer.expires = 10 ;
key_timer.function = key_timer_func ;
//注冊(cè)定時(shí)器
add_timer(&key_timer);
return 0 ;
}
static void button_exit()
{
misc_deregister(&key_miscdev);
}
module_init(button_init);
module_exit(button_exit);