機械按鍵在按下的過程中會出現抖動的情況,如下圖,這樣就會導致本來按下一次按鍵的過程會出現多次中斷,導致判斷出錯。在按鍵驅動程序中我們可以這么做:
在按鍵驅動程序中我們可以這么做來取消按鍵抖動的影響:當出現一個按鍵中斷后不會馬上去處理它,而是延時一個抖動時間(一般10ms),如果在這個時間內再次出現中斷那么再次延時10ms。這樣循環,一直到在這個10ms內只有一個按鍵中斷,那么就認為這次是真的按鍵值,然后在定時器處理函數里處理它。上述過程可以利用內核的定時器來實現。
定時器二要素:定時時間、定時時間到后做什么事情。根據這兩個要素來編寫程序,直接在sixth_drv.c的驅動程序上更改直接看到代碼:
1、定時器的創建,先建立一個定時器結構
static struct timer_list buttons_timer;//定義一個定時器
2、在模塊裝載時初始化定時器
static int sixth_drv_init(void)
{
/*增加一個定時器用于處理按鍵抖動*/
init_timer(&buttons_timer);
buttons_timer.expires = 0;//定時器的定時時間
// buttons_timer->data = (unsigned long) cs;
buttons_timer.function = buttons_timeout;//定時時間到后的處理函數
add_timer(&buttons_timer);//將定義的定時器放入定時器鏈表
sixthmajor = register_chrdev(0, 'buttons', &sixth_drv_ops);//注冊驅動程序
if(sixthmajor < 0)
printk('failes 1 buttons_drv registern');
sixth_drv_class = class_create(THIS_MODULE, 'buttons');//創建類
if(sixth_drv_class < 0)
printk('failes 2 buttons_drv registern');
sixth_drv_class_dev = class_device_create(sixth_drv_class, NULL, MKDEV(sixthmajor,0), NULL,'buttons');//創建設備節點
if(sixth_drv_class_dev < 0)
printk('failes 3 buttons_drv registern');
gpfcon = ioremap(0x56000050, 16);//重映射
gpfdat = gpfcon + 1;
gpgcon = ioremap(0x56000060, 16);//重映射
gpgdat = gpgcon + 1;
printk('register buttons_drvn');
return 0;
}
3、編寫定時器處理函數
static void buttons_timeout(unsigned long data)
{
unsigned int pin_val;
static long cnt=0;
//printk('timeout cnt : %dn',++cnt);
if(pin_des==NULL)
return;
else
{
// printk('pin_des != NULLn');
pin_val = s3c2410_gpio_getpin(pin_des->pin);
if(pin_val) //按鍵松開
key_val = 0x80 | pin_des->key_val;
else
key_val = pin_des->key_val;
wake_up_interruptible(&button_waitq); /* 喚醒休眠的進程 */
ev_press = 1;
kill_fasync(&sixth_fasync, SIGIO, POLL_IN);//發生信號給進程
}
}
4、當在卸載驅動時將定時器刪除;在中斷處理程序中直接改變定時器的超時時間,并記錄下是哪個按鍵按下的即可,其他處理都在定時器超時函數中。直接看到完整代碼:
#include <linux/module.h>
#include #include #include #include #include #include #include #include #include #include #include #include //#include static struct class *sixth_drv_class;//類 static struct class_device *sixth_drv_class_dev;//類下面的設備 static int sixthmajor; static unsigned long *gpfcon = NULL; static unsigned long *gpfdat = NULL; static unsigned long *gpgcon = NULL; static unsigned long *gpgdat = NULL; struct fasync_struct *sixth_fasync; static unsigned int key_val; struct pin_desc { unsigned int pin; unsigned int key_val; }; static struct pin_desc pins_desc[4] = { {S3C2410_GPF0,0x01}, {S3C2410_GPF2,0x02}, {S3C2410_GPG3,0x03}, {S3C2410_GPG11,0x04} }; static struct pin_desc *pin_des=NULL; static unsigned int ev_press; static DECLARE_WAIT_QUEUE_HEAD(button_waitq);//注冊一個等待隊列button_waitq static atomic_t open_flag = ATOMIC_INIT(1); //定義原子變量open_flag 并初始化為1 static DECLARE_MUTEX(button_lock); //定義互斥鎖 static struct timer_list buttons_timer;//定義一個定時器 /* *0x01、0x02、0x03、0x04表示按鍵被按下 */ /* *0x81、0x82、0x83、0x84表示按鍵被松開 */ /* *利用dev_id的值為pins_desc來判斷是哪一個按鍵被按下或松開 */ static irqreturn_t buttons_irq(int irq, void *dev_id) { pin_des = (struct pin_desc *)dev_id;//取得哪個按鍵被按下的狀態 mod_timer(&buttons_timer, jiffies+HZ/100);//10ms之后調用定時器處理函數 return IRQ_HANDLED; } static int sixth_drv_open (struct inode * inode, struct file * file) { int ret; // if(atomic_dec_and_test(&open_flag)==0)//自檢后是否為0,不為0說明已經被人調用 // { // atomic_inc(&open_flag);//原子變量+1 // return -EBUSY; // } if(file->f_flags & O_NONBLOCK)//非阻塞方式 { if(down_trylock(&button_lock))//獲取信號量失敗則返回 return -EBUSY; } else down(&button_lock);//獲得信號量 ret = request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, 's1', (void * )&pins_desc[0]); if(ret) { printk('open failed 1n'); return -1; } ret = request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, 's2', (void * )& pins_desc[1]); if(ret) { printk('open failed 2n'); return -1; } ret = request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, 's3', (void * )&pins_desc[2]); if(ret) { printk('open failed 3n'); return -1; } ret = request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, 's4', (void * )&pins_desc[3]); if(ret) { printk('open failed 4n'); return -1; } return 0; } static int sixth_drv_close(struct inode * inode, struct file * file) { // atomic_inc(&open_flag);//原子變量+1 up(&button_lock);//釋放信號量 free_irq(IRQ_EINT0 ,(void * )&pins_desc[0]); free_irq(IRQ_EINT2 ,(void * )& pins_desc[1]); free_irq(IRQ_EINT11 ,(void * )&pins_desc[2]); free_irq(IRQ_EINT19 ,(void * )&pins_desc[3]); return 0; } static ssize_t sixth_drv_read(struct file * file, char __user * userbuf, size_t count, loff_t * off) { int ret; if(count != 1) { printk('read errorn'); return -1; } if(file->f_flags & O_NONBLOCK)//非阻塞方式 { if(!ev_press)//判斷是否有按鍵按下,如果沒有直接返回 { key_val = 0; ret = copy_to_user(userbuf, &key_val, 1); return -EBUSY; } } else//如果沒有按鍵動作,直接進入休眠 wait_event_interruptible(button_waitq, ev_press);//將當前進程放入等待隊列button_waitq中 ret = copy_to_user(userbuf, &key_val, 1); ev_press = 0;//按鍵已經處理可以繼續睡眠 if(ret) { printk('copy errorn'); return -1; } return 1; } static unsigned int sixth_drv_poll(struct file *file, poll_table *wait) { unsigned int ret = 0; poll_wait(file, &button_waitq, wait);//將當前進程放到button_waitq列表 if(ev_press) ret |=POLLIN;//說明有數據被取到了 return ret; } static int sixth_drv_fasync(int fd, struct file * file, int on) { int err; printk('fansync_helpern'); err = fasync_helper(fd, file, on, &sixth_fasync);//初始化sixth_fasync if (err < 0) return err; return 0; } static struct file_operations sixth_drv_ops = { .owner = THIS_MODULE, .open = sixth_drv_open, .read = sixth_drv_read, .release = sixth_drv_close, .poll = sixth_drv_poll, .fasync = sixth_drv_fasync, }; static void buttons_timeout(unsigned long data) { unsigned int pin_val; static long cnt=0; //printk('timeout cnt : %dn',++cnt); if(pin_des==NULL) return; else { // printk('pin_des != NULLn'); pin_val = s3c2410_gpio_getpin(pin_des->pin); if(pin_val) //按鍵松開 key_val = 0x80 | pin_des->key_val; else key_val = pin_des->key_val; wake_up_interruptible(&button_waitq); /* 喚醒休眠的進程 */ ev_press = 1; kill_fasync(&sixth_fasync, SIGIO, POLL_IN);//發生信號給進程
上一篇:Linux驅動之輸入子系統簡析
下一篇:Linux驅動之同步、互斥、阻塞的應用
推薦閱讀最新更新時間:2025-06-07 23:41



設計資源 培訓 開發板 精華推薦
- 肖特激光玻璃助力實現核聚變領域又一個歷史性里程碑
- 英飛凌SEMPER? NOR閃存系列獲得ASIL-D功能安全認證
- 如何在開關模式電源中運用氮化鎵技術
- LGVL配合FreeType為可變字體設置字重-ESP32篇
- 使用樹莓派進行 ESP32 Jtag 調試
- ESP32怎么在SPIFFS里面存儲html,css,js文件,以及網頁和arduino的通訊
- ESP32 freeRTOS使用測試
- API調用小記(Touchdesigner和ESP32)
- 芯科科技Tech Talks技術培訓重磅回歸: 賦能物聯網創新,共筑智能互聯未來
- 關于ESP32/8266使用async-mqtt-client庫的一些基本介紹