linux字符设备驱动-定时器按键去抖笔记

来源:互联网 发布:华为大数据 编辑:程序博客网 时间:2024/05/17 21:59

转自 http://blog.csdn.net/cs953575/article/details/62881986

一、开发环境

1、内核:Linux 2.6.22.6;

2、JZ2440

3、ubuntu 9.10

二、原理

  机械按键按下时,弹片接触瞬间会使信号产生抖动,如果是使用中断读取按键值得话,则产生多次中断。使用定时器去抖的原理是,在按键中断函数里,初始化一个定时10ms(也可以是其他时间,一般10ms就可以了)的定时器函数。也就是说,这瞬间几次的中断都会使这个10ms的定时器初始化,重新定时10ms(这瞬间几次的中断时间间隔都是小于10ms的),直到最后一次按键中断不再使定时器初始化,可以顺利执行10ms的定时中断函数,在定时中断函数中我们把按键读出即可。这样就忽略掉了按键的前几次中断,只有最后一次中断有效了。

三、程序实现

1、定义timer_list结构体类型变量,如定义buttons_timer:static struct timer_list buttons_timer。这个结构体用于存放定时器的一些设置。

2、在驱动入口函数中增加

init_timer(&buttons_timer);//初始化定时器
buttons_timer.function = buttons_timer_function;//定时器函数,设置的时间到了的时候执行的函数。
//buttons_timer.expires  = 0;
add_timer(&buttons_timer); //向内核注册定时器

3、编写buttons_timer_function函数。在里面添加定时时间到达时想执行的语句。如:

static void buttons_timer_function(unsigned long data)

{

......

}

4、设置定时器定时时间。使用函数mod_timer,如:mod_timer(&buttons_timer, jiffies+HZ/100);

buttons_timer上面定义的结构体。jiffies是一个只读变量,里面记录的是linux系统运行以来的滴答值,本系统每10ms加1,不同的cpu和系统jiffies加1的时间是不同的和具体设置有关;HZ是个常量为100,jiffies每秒增加100次。这样就可以基于jiffies,设置定时器在jiffies+HZ/100时执行上面的buttons_timer_function函数。jiffies+HZ/100=jiffies+1,比现在的jiffies值多1,也就是定时10ms。


5、韦源码如下:


[cpp] view plain copy
  1. #include <linux/module.h>  
  2. #include <linux/kernel.h>  
  3. #include <linux/fs.h>  
  4. #include <linux/init.h>  
  5. #include <linux/delay.h>  
  6. #include <linux/irq.h>  
  7. #include <asm/uaccess.h>  
  8. #include <asm/irq.h>  
  9. #include <asm/io.h>  
  10. #include <asm/arch/regs-gpio.h>  
  11. #include <asm/hardware.h>  
  12. #include <linux/poll.h>  
  13.   
  14.   
  15. static struct class *sixthdrv_class;  
  16. static struct class_device  *sixthdrv_class_dev;  
  17.   
  18. volatile unsigned long *gpfcon;  
  19. volatile unsigned long *gpfdat;  
  20.   
  21. volatile unsigned long *gpgcon;  
  22. volatile unsigned long *gpgdat;  
  23.   
  24. static struct timer_list buttons_timer;  
  25.   
  26.   
  27. static DECLARE_WAIT_QUEUE_HEAD(button_waitq);  
  28.   
  29. /* 中断事件标志, 中断服务程序将它置1,sixth_drv_read将它清0 */  
  30. static volatile int ev_press = 0;  
  31.   
  32. static struct fasync_struct *button_async;  
  33.   
  34.   
  35. struct pin_desc{  
  36.     unsigned int pin;  
  37.     unsigned int key_val;  
  38. };  
  39.   
  40.   
  41. /* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */  
  42. /* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */  
  43. static unsigned char key_val;  
  44.   
  45. struct pin_desc pins_desc[4] = {  
  46.     {S3C2410_GPF0, 0x01},  
  47.     {S3C2410_GPF2, 0x02},  
  48.     {S3C2410_GPG3, 0x03},  
  49.     {S3C2410_GPG11, 0x04},  
  50. };  
  51.   
  52. static struct pin_desc *irq_pd;  
  53.   
  54. //static atomic_t canopen = ATOMIC_INIT(1);     //定义原子变量并初始化为1  
  55.   
  56. static DECLARE_MUTEX(button_lock);     //定义互斥锁  
  57.   
  58. /* 
  59.   * 确定按键值 
  60.   */  
  61. static irqreturn_t buttons_irq(int irq, void *dev_id)  
  62. {  
  63.     /* 10ms后启动定时器 */  
  64.     irq_pd = (struct pin_desc *)dev_id;  
  65.     mod_timer(&buttons_timer, jiffies+HZ/100);  
  66.     return IRQ_RETVAL(IRQ_HANDLED);  
  67. }  
  68.   
  69. static int sixth_drv_open(struct inode *inode, struct file *file)  
  70. {  
  71. #if 0     
  72.     if (!atomic_dec_and_test(&canopen))  
  73.     {  
  74.         atomic_inc(&canopen);  
  75.         return -EBUSY;  
  76.     }  
  77. #endif        
  78.   
  79.     if (file->f_flags & O_NONBLOCK)  
  80.     {  
  81.         if (down_trylock(&button_lock))  
  82.             return -EBUSY;  
  83.     }  
  84.     else  
  85.     {  
  86.         /* 获取信号量 */  
  87.         down(&button_lock);  
  88.     }  
  89.   
  90.     /* 配置GPF0,2为输入引脚 */  
  91.     /* 配置GPG3,11为输入引脚 */  
  92.     request_irq(IRQ_EINT0,  buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);  
  93.     request_irq(IRQ_EINT2,  buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);  
  94.     request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);  
  95.     request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);     
  96.   
  97.     return 0;  
  98. }  
  99.   
  100. ssize_t sixth_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)  
  101. {  
  102.     if (size != 1)  
  103.         return -EINVAL;  
  104.   
  105.     if (file->f_flags & O_NONBLOCK)  
  106.     {  
  107.         if (!ev_press)  
  108.             return -EAGAIN;  
  109.     }  
  110.     else  
  111.     {  
  112.         /* 如果没有按键动作, 休眠 */  
  113.         wait_event_interruptible(button_waitq, ev_press);  
  114.     }  
  115.   
  116.     /* 如果有按键动作, 返回键值 */  
  117.     copy_to_user(buf, &key_val, 1);  
  118.     ev_press = 0;  
  119.       
  120.     return 1;  
  121. }  
  122.   
  123.   
  124. int sixth_drv_close(struct inode *inode, struct file *file)  
  125. {  
  126.     //atomic_inc(&canopen);  
  127.     free_irq(IRQ_EINT0, &pins_desc[0]);  
  128.     free_irq(IRQ_EINT2, &pins_desc[1]);  
  129.     free_irq(IRQ_EINT11, &pins_desc[2]);  
  130.     free_irq(IRQ_EINT19, &pins_desc[3]);  
  131.     up(&button_lock);  
  132.     return 0;  
  133. }  
  134.   
  135. static unsigned sixth_drv_poll(struct file *file, poll_table *wait)  
  136. {  
  137.     unsigned int mask = 0;  
  138.     poll_wait(file, &button_waitq, wait); // 不会立即休眠  
  139.   
  140.     if (ev_press)  
  141.         mask |= POLLIN | POLLRDNORM;  
  142.   
  143.     return mask;  
  144. }  
  145.   
  146. static int sixth_drv_fasync (int fd, struct file *filp, int on)  
  147. {  
  148.     printk("driver: sixth_drv_fasync\n");  
  149.     return fasync_helper (fd, filp, on, &button_async);  
  150. }  
  151.   
  152.   
  153. static struct file_operations sencod_drv_fops = {  
  154.     .owner   =  THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */  
  155.     .open    =  sixth_drv_open,       
  156.     .read    =  sixth_drv_read,      
  157.     .release =  sixth_drv_close,  
  158.     .poll    =  sixth_drv_poll,  
  159.     .fasync  =  sixth_drv_fasync,  
  160. };  
  161.   
  162.   
  163. int major;  
  164.   
  165. static void buttons_timer_function(unsigned long data)  
  166. {  
  167.     struct pin_desc * pindesc = irq_pd;  
  168.     unsigned int pinval;  
  169.   
  170.     if (!pindesc)  
  171.         return;  
  172.       
  173.     pinval = s3c2410_gpio_getpin(pindesc->pin);  
  174.   
  175.     if (pinval)  
  176.     {  
  177.         /* 松开 */  
  178.         key_val = 0x80 | pindesc->key_val;  
  179.     }  
  180.     else  
  181.     {  
  182.         /* 按下 */  
  183.         key_val = pindesc->key_val;  
  184.     }  
  185.   
  186.     ev_press = 1;                  /* 表示中断发生了 */  
  187.     wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */  
  188.       
  189.     kill_fasync (&button_async, SIGIO, POLL_IN);  
  190. }  
  191.   
  192.   
  193. static int sixth_drv_init(void)  
  194. {  
  195.     init_timer(&buttons_timer);  
  196.     buttons_timer.function = buttons_timer_function;  
  197.     //buttons_timer.expires  = 0;  
  198.     add_timer(&buttons_timer);   
  199.   
  200.     major = register_chrdev(0, "sixth_drv", &sencod_drv_fops);  
  201.   
  202.     sixthdrv_class = class_create(THIS_MODULE, "sixth_drv");  
  203.   
  204.     /* 为了让mdev根据这些信息来创建设备节点 */  
  205.     sixthdrv_class_dev = class_device_create(sixthdrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */  
  206.   
  207.     gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);  
  208.     gpfdat = gpfcon + 1;  
  209.   
  210.     gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);  
  211.     gpgdat = gpgcon + 1;  
  212.   
  213.     return 0;  
  214. }  
  215.   
  216. static void sixth_drv_exit(void)  
  217. {  
  218.     unregister_chrdev(major, "sixth_drv");  
  219.     class_device_unregister(sixthdrv_class_dev);  
  220.     class_destroy(sixthdrv_class);  
  221.     iounmap(gpfcon);  
  222.     iounmap(gpgcon);  
  223.     return 0;  
  224. }  
  225.   
  226.   
  227. module_init(sixth_drv_init);  
  228.   
  229. module_exit(sixth_drv_exit);  
  230.   
  231. MODULE_LICENSE("GPL");  



这里也有一个类似的案例

linux驱动程序之定时器防按键抖动

http://blog.csdn.net/tianzhihen_wq/article/details/42125377


阅读全文
0 0
原创粉丝点击