Linux内核定时器

        定时器是一个很常用的功能,需要周期性处理的工作都要用到定时器。 Linux 内核定时器
采用系统时钟来实现。

一.系统运行节拍数 - jiffies

        jiffies 表示系统运行的 jiffies 节拍数,所以 jiffies/HZ 就是系统运行时间,单位为秒。

        1.32位系统

        

extern unsigned long volatile __jiffy_data jiffies;

        2.64位系统

extern u64 __jiffy_data jiffies_64;

二.内核定时器结构体

struct timer_list
{
    struct list_head_entry;
    unsigned long expires;            /* 定时器超时时间,单位是节拍数 */
    struct tvec_base *base;        
    
    void (*function)(unsigned long); /* 定时处理函数 */
    unsigned long data;              /* 要传递给function函数的参数 */

    int slack;
};

三.定时器相关API函数

1.初始化timer_list类型变量 - init_timer()

功能

        初始化 timer_list 类型变量,定义了 timer_list 变量以后一定要先用此函数初始化。

        无返回值

参数

        timer:要初始化的定时器。

void init_timer(struct timer_list *timer);

2.注册定时器 - add_timer

功能

        向 Linux 内核中注册定时器,使用该函数注册后,定时器就会开始运行

        无返回值

参数

        timer:要注册的定时器

void add_timer(struct timer_list *timer);

3.删除定时器 - del_timer

功能

        用于删除一个定时器,不管定时器有没有被激活,都可以使用此函数删除。

        定时器还没被激活,返回(0);定时器已经激活,返回(1)。

参数

        timer:要删除的定时器

int del_timer(struct timer_list *timer);

4.删除定时器 - del_timer_sync

功能

        是del_timer的同步版,会等待其他处理器使用完定时器再删除,此函数不能用在中断上下文。

        定时器还没被激活,返回(0);定时器已经被激活,返回(1)。

参数

        timer :要删除的定时器

int del_timer_sync(struct timer_list *timer);

5.修改定时值 - mod_timer

功能

        修改定时值,若定时器还没有激活,调用此函数会激活定时器!

        调用mod_timer前定时器还未激活时,返回(0);调用mod_timer前定时器已经被激活时,返回(1)。

参数

        timer:要修改超时时间(定时值)的定时器。

        expires:修改后的超时时间。

6.使用示例

/* 1.定义定时器 */
struct timer_list timer;

/* 2.编写定时器回调函数 */
void function(unsigned long arg)
{
    /*
    * 1.定时处理代码
    */

    /*
    * 2.如果需要定时器周期运行,则使用mod_timer函数重新设置超时值,并启动定时器
    */
    mod_timer(&dev->timertest,jiffies + msecs_to_jiffies(2000));
}


/* 3.初始化函数 */
void init(void)
{
    /* 1.初始化定时器 */
    init_timer(&timer);        
    
    /* 2。设置定时处理函数 */
    timer.function = function;

    /* 3.设置超时时间 */
    timer.expires = jffies + msecs_to_jiffies(2000);

    /* 4.将设备结构体作为参数 */
    timer.data = (unsigned long)&dev;

    /* 5.启动定时器 */
    add_timer(&timer);
    
}


/* 4.退出函数 */
void exit(void)
{
    /* 1.删除定时器 */
    del_timer(&timer);

     /* 或者使用del_timer_sync(&timer); */
}

四.定时器实现LED闪烁程序

①.驱动程序

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <asm/mach/map.h>
#include <linux/timer.h>
#include <asm/uaccess.h>
#include <asm/io.h>


#define TIMER_CNT       1                           /* 设备号个数 */
#define TIMER_NAME      "timer"                     /* 名字 */
#define CLOSE_CMD       (_IO(0XEF,0X1))             /* 关闭定时器命令 */
#define OPEN_CMD        (_IO(0XEF,0X2))             /* 开启定时器命令 */
#define SETPERIOD_CMD   (_IO(0XEF,0X3))             /* 设置定时器周期命令 */
#define LEDON           1                           /* 开灯 */
#define LEDOFF          0                           /* 关灯 */

struct timer_dev
{
    dev_t devid;                //设备号
    struct cdev cdev;           //cdev
    struct class *class;        //类
    struct device *device;      //设备
    int major;                  //主设备号
    int minor;                  //次设备号
    struct device_node *nd;     //设备结点
    int led_gpio;               //LED所使用的GPIO编号
    int timeperiod;             //定时器周期,单位为ms
    struct timer_list timer;    //定义一个定时器
};


/* timer设备 */
struct timer_dev timerdev;


/**
 * @description:        初始化LED,在驱动入口函数中调用以初始化LED所使用的GPIO引脚
*/
static int led_init(void)
{
    int ret = 0;

    /* 从设备树找到 gpioled 这个设备结点 */
    timerdev.nd = of_find_node_by_path("/gpioled");
    if(NULL == timerdev.nd)
    {
        return -EINVAL; 
    }

    /* 获取led-gpio编号 */
    timerdev.led_gpio = of_get_named_gpio(timerdev.nd,"led-gpio",0);
    if(0 > timerdev.led_gpio)
    {
        printk("can not get led!\r\n");
        return -EINVAL;
    }

    /* 初始化led所使用的IO */
    gpio_request(timerdev.led_gpio,"led");
    ret = gpio_direction_output(timerdev.led_gpio,1);       //默认输出高电平,关闭led
    if(0 > ret)
    {
        printk("can not set gpio!\r\n");
    }

    return 0;
}


/**
 * @description:            打开设备
 * @param - inode    :      传递给驱动的inode
 * @param - filp     :      设备文件,在open的时候,将private_data指向设备结构体
 * @return           :      0 成功,其他 失败
*/
static int timer_open(struct inode *inode,struct file *filp)
{
    int ret = 0;
    filp->private_data = &timerdev;     //设置私有数据

    timerdev.timeperiod = 1000;         //默认周期为1s
    ret = led_init();                   //初始化led
    if(0 > ret)
    {
        return ret;
    }

    return 0;
}


/**
 * @description:            ioctl函数
 * @param - filp        :   文件描述符
 * @param - cmd         :   指令
 * @param - arg         :   参数(当指令为设置定时器周期时有效)
 * @return              :   0 成功,其他 失败
*/
static long timer_unlocked_ioctl(struct file *filp,unsigned int cmd,unsigned long arg)
{
    struct timer_dev *dev = (struct timer_dev *)filp->private_data;
    int timerperiod;

    switch(cmd)
    {
        /* 关闭指令 */
        case CLOSE_CMD:                     
            del_timer_sync(&dev->timer);
            break;
        /* 开启指令 */
        case OPEN_CMD:
            timerperiod = dev->timeperiod;
            mod_timer(&dev->timer,jiffies + msecs_to_jiffies(timerperiod));
            break;
        /* 设置定时器周期 */    
        case SETPERIOD_CMD:
            dev->timeperiod = arg;
            mod_timer(&dev->timer,jiffies+msecs_to_jiffies(arg));
            break;
        default:
            break;
    }

    return 0;
}


/* 绑定操作函数 */
static struct file_operations timer_fops = 
{
    .owner = THIS_MODULE,
    .open = timer_open,
    .unlocked_ioctl = timer_unlocked_ioctl,
};

/* 定时器回调函数 */
void timer_function(unsigned long arg)
{
    struct timer_dev *dev = (struct timer_dev *)arg;
    static int sta = 1;
    int timerperiod;

    sta = !sta;             //每次都取反,实现LED反转
    gpio_set_value(dev->led_gpio,sta);


    /* 重启定时器 */
    timerperiod = dev->timeperiod;
    mod_timer(&dev->timer,jiffies + msecs_to_jiffies(dev->timeperiod));

}


/**
 * @description:                驱动入口函数
*/
static int __init timer_init(void)
{
    /* 注册字符设备驱动 */
    /* 1.创建设备号 */
    if(timerdev.major)
    {
        timerdev.devid = MKDEV(timerdev.major,0);
        register_chrdev_region(timerdev.devid,TIMER_CNT,TIMER_NAME);
    }
    else
    {
        alloc_chrdev_region(&timerdev.devid,0,TIMER_CNT,TIMER_NAME);
        timerdev.major = MAJOR(timerdev.devid);
        timerdev.minor = MINOR(timerdev.devid);
    }

    /* 2.初始化cdev */
    timerdev.cdev.owner = THIS_MODULE;
    cdev_init(&timerdev.cdev,&timer_fops);

    /* 3.添加一个cdev */
    cdev_add(&timerdev.cdev,timerdev.devid,TIMER_CNT);

    /* 4.创建类 */
    timerdev.class = class_create(THIS_MODULE,TIMER_NAME);
    if(IS_ERR(timerdev.class))
    {
        return PTR_ERR(timerdev.class);
    }

    /* 5.创建设备 */
    timerdev.device = device_create(timerdev.class,NULL,timerdev.devid,NULL,TIMER_NAME);
    if(IS_ERR(timerdev.device))
    {
        return PTR_ERR(timerdev.device);
    }

    /* 6.初始化timer,设置定时器处理函数,还未设置周期,所以不会激活定时器 */
    init_timer(&timerdev.timer);
    timerdev.timer.function = timer_function;
    timerdev.timer.data = (unsigned long)&timerdev;

    return 0;
}



/**
 * @description:        驱动出口函数
*/
static void __exit timer_exit(void)
{
    gpio_set_value(timerdev.led_gpio,1);        //关闭LED
    del_timer_sync(&timerdev.timer);            //删除timer

#if 0
    del_timer(&timerdev.timer);
#endif

    /* 注销字符设备驱动 */
    cdev_del(&timerdev.cdev);
    unregister_chrdev_region(timerdev.devid,TIMER_CNT);

    device_destroy(timerdev.class,timerdev.devid);
    class_destroy(timerdev.class);
}


module_init(timer_init);
module_exit(timer_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("kaneki");

②.应用程序

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>



#define CLOSE_CMD       (_IO(0XEF,0X1))     /* 关闭定时器 */
#define OPEN_CMD        (_IO(0XEF,0X2))     /* 打开定时器 */
#define SETPERIOD_CMD   (_IO(0XEF,0X3))     /* 设置定时器周期命令 */

int main(int argc,char *argv[])
{
    int ret,fd;
    char *filename;
    unsigned int cmd;
    unsigned int arg;
    unsigned char str[100];

    if(2 != argc)
    {
        printf("Usage : <%s timer device path>\n",argv[0]);
        return -1;        
    }

    filename = argv[1];
    fd = open(filename,O_RDWR);
    if(0 > fd)
    {
        perror("open error");
        return -1;
    }

    while(1)
    {
        printf("Input CMD:\n");

        ret = scanf("%d",&cmd);
        if(1 != ret)
        {
            gets(str);
        }

        if(1 == cmd)
        {
            cmd = CLOSE_CMD;
        }
        else if(2 == cmd)
        {
            cmd = OPEN_CMD;
        }
        else if(3 == cmd)
        {
            cmd = SETPERIOD_CMD;
            printf("Input Timer Period:\n");
            ret = scanf("%d",&arg);
            if(1 != ret)
            {
                gets(str);
            }
        }

        ioctl(fd,cmd,arg);
    }

    close(fd);

    return 0;
}

  • 9
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值