liunx驱动开发-9_内核定时器

内核定时器简介:

定时器是一个很常用的功能,需要周期性处理的工作都要用到定时器。Linux 内核定时器
采用系统时钟来实现,并不是我们在裸机篇中讲解的 PIT 等硬件定时器。Linux 内核定时器使
用很简单,只需要提供超时时间(相当于定时值)和定时处理函数即可,当超时时间到了以后设
置的定时处理函数就会执行,和我们使用硬件定时器的套路一样,只是使用内核定时器不需要
做一大堆的寄存器初始化工作。在使用内核定时器的时候要注意一点,内核定时器并不是周期
性运行的,超时以后就会自动关闭,因此如果想要实现周期性定时,那么就需要在定时处理函
数中重新开启定时器。

Linux 内核使用全局变量 jiffies 来记录系统从启动以来的系统节拍数,系统启动的时候会
将 jiffies 初始化为 0,jiffies 定义在文件 include/linux/jiffies.h 中,定义如下:

 extern u64 __jiffy_data jiffies_64;
 extern unsigned long volatile __jiffy_data jiffies;

jiffies_64 和 jiffies 其实是同一个东西,jiffies_64 用于 64 位系统,而 jiffies 用于 32 位系统。
为了兼容不同的硬件,jiffies 其实就是 jiffies_64 的低 32 位。 HZ 表示每秒的节拍数,jiffies 表示系统运行的 jiffies 节拍数,所以 jiffies/HZ 就是系统运行时间,单位为秒。不管是 32 位还是 64 位的 jiffies,都有溢出的风险,溢出以后会重新从 0 开始计数,相当于绕回来了,因此有些资料也将这个现象也叫做绕回。假如 HZ 为最大值 1000 的时候,32 位的 jiffies 只需要 49.7 天就发生了绕回,对于 64 位的 jiffies 来说大概需要5.8 亿年才能绕回,因此 jiffies_64 的绕回忽略不计。处理 32 位 jiffies 的绕回显得尤为重要。

time_after(unkown, known)。

time_before(unkown, known)
time_after_eq(unkown, known)
time_before_eq(unkown, known)

unkown 通常为 jiffies,known 通常是需要对比的值。

如果 unkown 超过 known 的话,time_after 函数返回真,否则返回假。如果 unkown 没有超
过 known 的话 time_before 函数返回真,否则返回假。time_after_eq 函数和 time_after 函数类似,只是多了判断等于这个条件。同理,time_before_eq 函数和 time_before 函数也类似。

为了方便开发,Linux 内核提供了几个 jiffies 和 ms、us、ns 之间的转换函数:

int jiffies_to_msecs(const unsigned long j)

int jiffies_to_usecs(const unsigned long j)
u64 jiffies_to_nsecs(const unsigned long j)

将 jiffies 类型的参数 j 分别转换为对应的毫秒、微秒、纳秒。

long msecs_to_jiffies(const unsigned int m)
long usecs_to_jiffies(const unsigned int u)
unsigned long nsecs_to_jiffies(u64 n)

将毫秒、微秒、纳秒转换为 jiffies 类型。

要使用内核定时器首先要先定义一个 timer_list 变量,表示定时器,tiemr_list 结构体的
expires 成员变量表示超时时间,单位为节拍数。比如我们现在需要定义一个周期为 2 秒的定时
器,那么这个定时器的超时时间就是 jiffies+(2*HZ),因此 expires=jiffies+(2*HZ)。function 就是
定时器超时以后的定时处理函数,我们要做的工作就放到这个函数里面,需要我们编写这个定
时处理函数。


init_timer 函数负责初始化 timer_list 类型变量,当我们定义了一个 timer_list 变量以后一定
要先用 init_timer 初始化一下。init_timer 函数原型如下:

void inittimer(struct timerlist *time)

add_timer 函数用于向 Linux 内核注册定时器,使用 add_timer 函数向内核注册定时器以后,
定时器就会开始运行,函数原型如下:
void add_timer(struct timer_list *timer)

del_timer 函数用于删除一个定时器,不管定时器有没有被激活,都可以使用此函数删除。
在多处理器系统上,定时器可能会在其他的处理器上运行,因此在调用 del_timer 函数删除定时
器之前要先等待其他处理器的定时处理器函数退出。del_timer 函数原型如下:
int del_timer(struct timer_list * timer)

timer:要删除的定时器。
返回值:0,定时器还没被激活;1,定时器已经激活。

del_timer_sync 函数是 del_timer 函数的同步版,会等待其他处理器使用完定时器再删除,
del_timer_sync 不能使用在中断上下文中。del_timer_sync 函数原型如下所示:
int del_timer_sync(struct timer_list *timer)

timer:要删除的定时器。
返回值:0,定时器还没被激活;1,定时器已经激活。

mod_timer 函数用于修改定时值,如果定时器还没有激活的话,mod_timer 函数会激活定时
器!函数原型如下:
int mod_timer(struct timer_list *timer, unsigned long expires)
函数参数和返回值含义如下:
timer:要修改超时时间(定时值)的定时器。
expires:修改后的超时时间。
返回值:0,调用 mod_timer 函数前定时器未被激活;1,调用 mod_timer 函数前定时器已
被激活

接下来就是进行实验,还是点灯。

首先要在设备结构体中添加定时器

struct timer_dev{
    dev_t devid; 
    int major;
    int minor;
    struct cdev cdev;
    struct class *class;
    struct device *device;
    struct device_node *nd;
    int led_gpio;
    int timerpreiod; //周期
    struct timer_list timer;
};

函数 timer_unlocked_ioctl,对应应用程序的 ioctl 函数,应用程序调用 ioctl
函数向驱动发送控制信息,此函数响应并执行。此函数有三个参数:filp,cmd 和 arg,其中 filp
是对应的设备文件,cmd 是应用程序发送过来的命令信息,arg 是应用程序发送过来的参数,在
本章例程中 arg 参数表示定时周期。
一共有三种命令 CLOSE_CMD,OPEN_CMD 和 SETPERIOD_CMD,这三个命令分别为关
闭定时器、打开定时器、设置定时周期。这三个命令的左右如下:
CLOSE_CMD:关闭定时器命令,调用 del_timer_sync 函数关闭定时器。
OPEN_CMD:打开定时器命令,调用 mod_timer 函数打开定时器,定时周期为 timerdev 的
timeperiod 成员变量,定时周期默认是 1 秒。
SETPERIOD_CMD:设置定时器周期命令,参数 arg 就是新的定时周期,设置 timerdev 的
timeperiod 成员变量为 arg 所表示定时周期指。并且使用 mod_timer 重新打开定时器,使定时器
以新的周期运行。

#define CLOSE_CMD               _IO(0XEF, 1)//关闭 
#define OPEN_CMD                _IO(0XEF, 2)//打开
#define SETPERIOD_CMD           _IOW(0XEF, 3, int)//设置周期

static long timer_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    int ret = 0;
    int value = 0;
    struct timer_dev *dev = file->private_data;
    switch (cmd)
    {
    case CLOSE_CMD:
        del_timer_sync(&dev->timer);
        break;
    case OPEN_CMD:
        mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->timerpreiod));
        break;
    case SETPERIOD_CMD:
        ret = copy_from_user(&value, (int *)arg, sizeof(int));
        if(ret < 0){
            return -ENAVAIL;
        }
        dev->timerpreiod = value;
        mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->timerpreiod));
        break;
    default:
        break;
    }
    return ret;
}

static const struct file_operations filp = {//连接 .c 文件的open write 与 key_open key_write等操作
    .owner = THIS_MODULE,
    .open = timer_open,
    .release = timer_release,
    .unlocked_ioctl = timer_ioctl,
    .read = timer_read,
};

函数 timer_func,定时器服务函数,此函有一个参数 arg,在本例程中
arg 参数就是 timerdev 的地址,这样通过 arg 参数就可以访问到设备结构体。当定时周期到了以
后此函数就会被调用。在此函数中将 LED 灯的状态取反,实现 LED 灯闪烁的效果。因为内核
定时器不是循环的定时器,执行一次以后就结束了,因此又调用了 mod_timer 函数重
新开启定时器。


static void timer_func(unsigned long arg)
{
    struct timer_dev *dev = (struct timer_dev*)arg;
    static int sta = 1;

    sta = !sta;
    gpio_set_value(dev->led_gpio, sta);

    mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->timerpreiod));
}

函数 timer_init,驱动入口函数。初始化定时器,设置定时器的定时处理函数为 timer_func,另外设置要传递给 timer_func 函数的参数为 timerdev的地址。在此函数中调用 timer_add 函数来开启定时器,因此定时器默认是开启的。


    init_timer(&timer.timer);
    timer.timerpreiod = 500;
    timer.timer.expires = jiffies + msecs_to_jiffies(timer.timerpreiod );
    timer.timer.function = timer_func;
    timer.timer.data = (unsigned long)&timer;

    add_timer(&timer.timer);//添加到系统

调用 del_timer 函数删除定时器,也可以使用 del_timer_sync 函数。 

del_timer(&timer.timer);  

timerApp.c

把接收到的数据通过 ioctl 函数传输给驱动,然后在驱动里面比对,做出相应的改变。

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
 
#define CLOSE_CMD               _IO(0XEF, 1)//关闭 
#define OPEN_CMD                _IO(0XEF, 2)//打开
#define SETPERIOD_CMD           _IOW(0XEF, 3, int)//设置周期

int main(int argc, char *argv[])
{
    int keyvalue;
    int fd, ret;
    char *filename;
    unsigned int cmd;
    unsigned int arg;
    unsigned char str[100];
    if(argc != 2){
        printf("error usage");
        return -1;
    }

    filename = argv[1];

    fd = open(filename, O_RDWR);
    if(fd < 0){
        printf("file open error:%s",argv[1]);
        return -1;
    }

	while(1) {
        printf("input cmd:");
        ret = scanf("%d", &cmd);
        if(ret != 1){
            gets(str); //防止卡死
        }
        if(cmd == 1){
            ioctl(fd, CLOSE_CMD, &arg);
        }else if(cmd == 2){
            ioctl(fd, OPEN_CMD, &arg);
        }else if (cmd == 3){
            printf("input period:");
            ret = scanf("%d", &arg);
            if(ret != 1){
                gets(str);
            }
            ioctl(fd, SETPERIOD_CMD, &arg);
        }
	}
    ret = close(fd);
    if(ret < 0){
		printf("file %s close failed!\r\n", argv[1]);
		return -1;
	}
	return 0;
}

timer.c:

#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 <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/atomic.h>
#include <linux/jiffies.h>
#include <linux/timer.h>

#define TIMER_COUNT 1
#define TIMER_NAME  "timer"
 
#define CLOSE_CMD               _IO(0XEF, 1)//关闭 
#define OPEN_CMD                _IO(0XEF, 2)//打开
#define SETPERIOD_CMD           _IOW(0XEF, 3, int)//设置周期

struct timer_dev{
    dev_t devid; 
    int major;
    int minor;
    struct cdev cdev;
    struct class *class;
    struct device *device;
    struct device_node *nd;
    int led_gpio;
    int timerpreiod; //周期
    struct timer_list timer;
};

struct timer_dev timer;

static int timer_open(struct inode *inode, struct file *filp)
{
    int ret = 0;
    filp->private_data = &timer;
    return ret;
}

static int timer_release(struct inode *inode, struct file *filp)
{
    int ret = 0;
    return ret;
}


static long timer_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    int ret = 0;
    int value = 0;
    struct timer_dev *dev = file->private_data;
    switch (cmd)
    {
    case CLOSE_CMD:
        del_timer_sync(&dev->timer);
        break;
    case OPEN_CMD:
        mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->timerpreiod));
        break;
    case SETPERIOD_CMD:
        ret = copy_from_user(&value, (int *)arg, sizeof(int));
        if(ret < 0){
            return -ENAVAIL;
        }
        dev->timerpreiod = value;
        mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->timerpreiod));
        break;
    default:
        break;
    }
    return ret;
}

static ssize_t timer_read(struct  file *filp, char __user *buff, size_t count, loff_t *ppos)
{
    int ret = 0;
    return ret;
}

static const struct file_operations filp = {//连接 .c 文件的open write 与 key_open key_write等操作
    .owner = THIS_MODULE,
    .open = timer_open,
    .release = timer_release,
    .unlocked_ioctl = timer_ioctl,
    .read = timer_read,
};

static void timer_func(unsigned long arg)
{
    struct timer_dev *dev = (struct timer_dev*)arg;
    static int sta = 1;

    sta = !sta;
    gpio_set_value(dev->led_gpio, sta);

    mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->timerpreiod));
}

static int ledio_init(struct timer_dev *dev)
{
    int ret = 0; 

    //获取设备节点
    dev->nd = of_find_node_by_path("/gpioled");
    if(dev->nd == NULL){
        ret = -ENAVAIL;
        goto fail_inode;
    }
    //获取gpio信息

    dev->led_gpio = of_get_named_gpio(dev->nd,"led-gpios",0);
    if(dev->led_gpio < 0){
        ret = -ENAVAIL;
        goto fail_gpio;
    }

    ret = gpio_request(dev->led_gpio, "led");
    if(ret){
        ret = -EBUSY;
        printk("io %d can't request", dev->led_gpio);
        goto fail_request;
    }

    ret = gpio_direction_output(dev->led_gpio, 1);
    if(ret < 0){
        ret = -ENAVAIL;
        goto fail_set;
    }


    return ret;

fail_set:
    gpio_free(dev->led_gpio);
fail_request:
fail_gpio:
fail_inode:
    device_destroy(dev->class, dev->devid);
    return ret;
}

static int __init timer_init(void)
{
    int ret=0;

    //初始化设备号
    if(timer.major){
        timer.devid = MKDEV(timer.major, 0);
        ret = register_chrdev_region(timer.devid, TIMER_COUNT, TIMER_NAME);
    }else{
        ret = alloc_chrdev_region(&timer.devid, 0, TIMER_COUNT, TIMER_NAME);
        timer.major = MAJOR(timer.devid);
        timer.minor = MINOR(timer.devid);
    }

    if(ret < 0){
        ret = -EINVAL;
        goto fail_devid;
    }
    printk("maj:%d , min:%d",timer.major, timer.minor);
    // 初始化字符设备
    timer.cdev.owner = THIS_MODULE; 
    cdev_init(&timer.cdev, &filp);
    ret = cdev_add(&timer.cdev, timer.devid, TIMER_COUNT);
    if(ret < 0){
        ret = -ENAVAIL;
        goto fail_dev;
    }

    //创建设备节点
    timer.class = class_create(THIS_MODULE, TIMER_NAME);
    if(IS_ERR(timer.class)){
        ret = PTR_ERR(timer.class);
        goto fail_class;
    }

    //获取设备
    timer.device = device_create(timer.class, NULL, timer.devid, NULL, TIMER_NAME);
    if(IS_ERR(timer.device)){
        ret = PTR_ERR(timer.device);
        goto fail_device;
    }

    //led初始化
    ret = ledio_init(&timer);
    if(ret < 0){
        goto fail_led_init;
    }

    init_timer(&timer.timer);
    timer.timerpreiod = 500;
    timer.timer.expires = jiffies + msecs_to_jiffies(timer.timerpreiod );
    timer.timer.function = timer_func;
    timer.timer.data = (unsigned long)&timer;

    add_timer(&timer.timer);//添加到系统


    if(ret < 0){
        goto fail_device;
    }
    return ret;
fail_led_init:
fail_device:
    class_destroy(timer.class);
fail_class:
    cdev_del(&timer.cdev);
fail_dev:
    unregister_chrdev_region(timer.devid, TIMER_COUNT);
fail_devid:
    return ret;
}

static void __exit timer_exit(void)
{
    gpio_set_value(timer.led_gpio, 1);
    //删除定时器
    del_timer(&timer.timer);  

    //删除字符设备
    cdev_del(&timer.cdev);

    //注销设备
    unregister_chrdev_region(timer.devid, TIMER_COUNT);

     //删除设备
    device_destroy(timer.class, timer.devid);

    //删除类
    class_destroy(timer.class);

    //删除gpio
    gpio_free(timer.led_gpio);
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值