内核中的时间

概述

时间

内核中的时间变量:
jiffies
include/linux/jiffies.h
/*
* 功能:获得jieffes值
*/
u64 get_jiffies_64(void);
include/linux/time.h 时间转换相关函数
/*
* 功能:获得当前的时间中的天
*/
dogettime_day();
秒数 = (jiffies(new)-jieffies(old))/HZ

频率:

include/asm-generic/param.h
    HZ = ...

延时函数

include/linux/delay.h
mdelay();
ndelay();
msleep();
ssleep();
usleep_range();

定时器

include/linux/time.h

定时器实例

定时器编程坑位:

起一个timer的流程:

  1. hrtimer_init(&blink_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2. blink_timer.function = blink_timer_hander;
  3. hrtimer_start_expires(&blink_timer,HRTIMER_MODE_REL);
  4. 定时器到期回调函数中调用hrtimer_forward_now(&poll_timer, ktime_set(0, POLL_TIMER_DELAY*1000000))修改下次的到期时间,单位ns;
  5. hrtimer_cancel(&blink_timer);

易错点:

  1. 必须先hrtimer_init(),再blink_timer.function = xxx ,否则内核直接崩溃 。因为init会清掉前面赋的值,导致找不到到期回调函数;
  2. hrtimer_cancel()之后定时器会被彻底销毁,若想再起,必须走上面的标准流程,光hrtimer_start_expires()是不行的。

工程实例:

/***********************************************************************************
 * File name    : act-led-driver.c
 * Author   : xxg
 * Creat date   : 2017-08-07
 * Description  : A module to control act led, this module is not devie file 
 * Copyright    : Copyright (c) 2008-2017   Nufront
 * Others   : 
 * Modify log   : 
***********************************************************************************/



#include <linux/module.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <linux/kernel.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/mutex.h>


#define  POLL_TIMER_DELAY    1000
#define GSE_ERR(x, ...)     pr_err( x, ##__VA_ARGS__)

static struct hrtimer poll_timer,blink_timer;
static int blink_timer_delay=500;
static int gpio;    



/* blink timer hander  */
static enum hrtimer_restart blink_timer_hander(struct hrtimer *timer)
{
    if (gpio_get_value( gpio) ) {
        gpio_set_value(gpio, 0);
    } else {
        gpio_set_value(gpio, 1);

    }
    printk(KERN_DEBUG "blink timer triger222222222222222222222222 \n");
    hrtimer_forward_now(&blink_timer, ktime_set(0, blink_timer_delay*1000000));
    return HRTIMER_RESTART;
}




/* poll timer hander  */
static enum hrtimer_restart poll_timer_hander(struct hrtimer *timer)
{
/* check condition by call bridge function */
    if (0) {
        /*start blink timer */  
        hrtimer_start(&blink_timer, ktime_set(0, 90*1000000), HRTIMER_MODE_REL);
    }
    if(0) {
        /* stop blink timer */
        hrtimer_cancel(&blink_timer);
    }
    if (0) {
        /* modify timer delay value */
        blink_timer_delay = 400;
    }
    if (0) {
        /* led on */
        gpio_set_value(gpio, 1);
    }
    if (0) {
        /* led off */
        gpio_set_value(gpio, 0);
    }

    hrtimer_forward_now(&poll_timer, ktime_set(0, POLL_TIMER_DELAY*1000000));
    static int cnt =0;
    printk(KERN_DEBUG "triger times : %d \n", cnt++);
    if (cnt == 10) {
        hrtimer_init(&blink_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
        blink_timer.function = blink_timer_hander;
        hrtimer_start_expires(&blink_timer,HRTIMER_MODE_REL);
    }
    if (cnt == 30) {
        hrtimer_cancel(&blink_timer);
    }
    if (cnt == 50) {        
        hrtimer_init(&blink_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
        blink_timer.function = blink_timer_hander;
        hrtimer_start_expires(&blink_timer,HRTIMER_MODE_REL);
    }
    return HRTIMER_RESTART;

}

int act_led_init(void)
{
    struct device_node *np = NULL;

/* 1.find gpio from dts */
    np = of_find_compatible_node(NULL, NULL, "gpio-leds");
    if(NULL == np) {
         printk(KERN_DEBUG "not fund device node gpio-leds \n");
         return -1 ;
    }
    np = of_find_node_by_name(np, "led4");
    if(NULL == np) {
         printk(KERN_DEBUG "not fund device node gpio-leds \n");
         return -1 ;
    }
    gpio = of_get_named_gpio(np, "gpios", 0);
    if (gpio < 0) {
            printk(KERN_DEBUG "get gpio failed \n");
            return -1 ;
    }
    if(gpio_is_valid(gpio)){
        GSE_ERR("get gpio sucssce! gpio = %d \n", gpio);
    }
/* 2.set default gpio voltage */
    gpio_set_value(gpio, 0);
/* 3.init poll timer  and blink timer */    
    hrtimer_init(&poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
    poll_timer.function = poll_timer_hander;

/* 4.start poll timer */
    hrtimer_start_expires(&poll_timer,HRTIMER_MODE_REL);

    return 0;

}


void act_led_exit(void)
{
    hrtimer_cancel(&poll_timer);
    hrtimer_cancel(&blink_timer);
    gpio_free(gpio);

}

module_exit(act_led_exit)

module_init(act_led_init);

MODULE_LICENSE("GPL");

代码下载:

coding码市git地址: https://git.coding.net/xxgui1992/timer-led.git

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xxgui1992

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值