学习笔记 --- LINUX定时器用于按键防抖实例

定时器的两个要素:定时时间,执行函数;

此例的使用函数:

init_timer(&buttons_timer);//初始化定时器
buttons_timer.function = buttons_timer_function;//给定时器添加处理函数
add_timer(&buttons_timer); //将定时器注册进内核,此时定时器启动了
mod_timer(&buttons_timer, jiffies+HZ/100);/*修改定时器的值,jiffies是一个全局变量,系统每10ms自动累加,HZ/100 表示定时10ms,HZ为100,所以jiffies后面加的值就是以10ms为单位的值,+1为10ms,+100为1s*/

 

看下实例:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/irq.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <linux/poll.h>

#define DEVICE_NAME "keys"

volatile unsigned long *gpfcon=NULL;
volatile unsigned long *gpfdat=NULL;
volatile unsigned long *gpgcon=NULL;
volatile unsigned long *gpgdat=NULL;

static struct class *keys_class;
static struct class_device *keys_class_devs;

struct pin_desc{
 unsigned int pin;
 unsigned int key_val;
};

struct pin_desc pins_desc[4] = {
 {S3C2410_GPF0, 0x01},
 {S3C2410_GPF2, 0x02},
 {S3C2410_GPG3, 0x03},
 {S3C2410_GPG11, 0x04},
};
static struct fasync_struct *button_async;
static unsigned char key_val;
static volatile int ev_press = 0;
static struct pin_desc *irq_pd;
static DECLARE_MUTEX(button_lock);
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static struct timer_list buttons_timer;//定义一个定时器
static irqreturn_t buttons_irq(int irq, void *dev_id) 
{
    irq_pd = (struct pin_desc *)dev_id;
    mod_timer(&buttons_timer, jiffies+HZ/100);//修改定时器的值
    ev_press = 1;
    return IRQ_RETVAL(IRQ_HANDLED);
}

static int s3c24xx_keys_open(struct inode *inode, struct file *file)
{
    if (file->f_flags & O_NONBLOCK)
    {
  if (down_trylock(&button_lock))
   return -EBUSY;
    }
    else
    {
     down(&button_lock);
    }
    request_irq(IRQ_EINT0,  buttons_irq, IRQT_BOTHEDGE, "S2",&pins_desc[0]);
    request_irq(IRQ_EINT2,  buttons_irq, IRQT_BOTHEDGE, "S3",&pins_desc[1]);
    request_irq(IRQ_EINT11,buttons_irq, IRQT_BOTHEDGE, "S4",&pins_desc[2]);
    request_irq(IRQ_EINT19,buttons_irq, IRQT_BOTHEDGE, "S5",&pins_desc[3]);
    return 0;
}

static int s3c24xx_keys_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
    if (filp->f_flags & O_NONBLOCK)
    {
        if (!ev_press)  
        return -EAGAIN;
    }
        else
    {
        wait_event_interruptible(button_waitq, ev_press);
    }

       copy_to_user(buff, &key_val, 1);
       ev_press = 0;
        return 1;
}


int s3c24xx_keys_close(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 int s3c24xx_keys_fasync(int fd, struct file *filp, int on)
{ 
        printk("driver: fifth_drv_fasync\n");
  return fasync_helper (fd, filp, on, &button_async);
}

static struct file_operations s3c24xx_keys_fops = {
    .owner     =   THIS_MODULE,   
    .open       =   s3c24xx_keys_open,     
    .read      = s3c24xx_keys_read,    
    .release   = s3c24xx_keys_close,
    .fasync     =  s3c24xx_keys_fasync,
};
int major;
/*这是定时器处理函数,本来中断要做的工作放在这里来做*/
static void buttons_timer_function(unsigned long data)
{
    struct pin_desc * pindesc = irq_pd;
    unsigned int pinval;

     if (!ev_press)//防止没有按键按下时进行下面的操作
  return 0;
    pinval = s3c2410_gpio_getpin(pindesc->pin);
    if (pinval)
 {  
  key_val = 0x80 | pindesc->key_val;
 }
 else
 {
  key_val = pindesc->key_val;
 }
    
    wake_up_interruptible(&button_waitq);
}
static int __init s3c24xx_keys_init(void)
{
    init_timer(&buttons_timer);//初始化定时器
    buttons_timer.function = buttons_timer_function;//给定时器添加处理函数
    add_timer(&buttons_timer); //将定时器注册进内核,此时定时器启动了
    
    major=register_chrdev(0, DEVICE_NAME, &s3c24xx_keys_fops);//auto distribute major
    keys_class = class_create(THIS_MODULE, "keys_class");  //创建类
    keys_class_devs = class_device_create(keys_class, NULL, MKDEV(major, 0), NULL, "keys"); //类下面创建设备节点 dev/keys
    gpfcon=(volatile unsigned long *)ioremap(0x56000050,16);
    gpfdat=gpfcon+1;
    gpgcon=(volatile unsigned long *)ioremap(0x56000060,16);
    gpgdat=gpgcon+1;
     printk(DEVICE_NAME " initialized\n");
     return 0;
}

static void __exit s3c24xx_keys_exit(void)
{
    unregister_chrdev(major, DEVICE_NAME);
    class_device_unregister(keys_class_devs);
    class_destroy(keys_class);
    iounmap(gpfcon);
    iounmap(gpgcon);
}
module_init(s3c24xx_keys_init);
module_exit(s3c24xx_keys_exit);
MODULE_LICENSE("GPL");


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值