Linux jiffies Timers and Hrtimer high-precision timers "turn"

Source: Internet
Author: User

This article was reproduced from: http://blog.csdn.net/dosculler/article/details/7932315

First, jiffies timer, hz=100, precision can only reach 10ms.

Note: Use Jiffies+msecs_to_jiffies (xx ms), can achieve MS level, but not enough precision

#include <linux/jiffies.h>//do-->jiffies Call header file
#include <linux/timer.h>//do-->timer_list Structural bodies

static struct timer_list ms_timer;//do--> define timer_list struct body

static void Ms_timer_handler (void)//do--> defines the timer handler function

{
PRINTK ("Do_debug----------->%s\n", __func__);
Ms_timer.expires=jiffies+hz;

Ms_timer.expires=jiffies+msecs_to_jiffies (10);
ms_timer.function=&ms_timer_handler;
Add_timer (&ms_timer);

}

Static int32_t xxx_init (void)
{
Hrtimer_init_module ();
Init_timer (&ms_timer); Do--> Initialization Timer
Ms_timer.expires=jiffies+msecs_to_jiffies (10); Do--> define Interrupt Time: 10ms Enter interrupt

Ms_timer.expires=jiffies+hz;
Ms_timer.data= (unsigned long) ms_timer;//distinguish between different timers, not verified
ms_timer.function=&ms_timer_handler; Do--> Defining timer Interrupt handling functions

Add_timer (&ms_timer); do--> increase the registration timer to make the timer effective

Second, Hrtimer high-precision timer, can be achieved NS level, here to do milliseconds as follows:

Note: The actual is the nanosecond level, where Ktime_set (const long secs, const unsigned long nsecs) is determined, parameters can be achieved in the nanosecond level.

#include <linux/dma-mapping.h>//do-->hrtimer contains the following three header files/* DMA APIs */
#include <linux/hrtimer.h>

#include <linux/time.h>/* struct TIMESPEC */

#define KER_PRINT (FMT, ...) printk ("<ker-driver>" FMT, # #__VA_ARGS__);
static struct Hrtimer Vibe_timer;

static struct work_struct vibe_work;
static int value = 2000; /* Note: In milliseconds ms Time out setting,2 seconds */

static enum Hrtimer_restart vibrator_timer_func (struct Hrtimer *timer)//do--> callback function, called when interrupted
{

struct TIMESPEC uptime;

Do_posix_clock_monotonic_gettime (&uptime);
Ker_print ("time:%lu.%0 2lu\n ",

(unsigned long) uptime.tv_sec,
(Uptime.tv_nsec/(nsec_per_sec/1000)));

Ker_print ("vibrator_timer_func\n");
Schedule_work (&vibe_work);
return hrtimer_norestart;

}
static void Vibe_work_func (struct work_struct *work)//do--> Task Force column function
{

Ker_print ("' Vibe_work_func '-->work\n");
Msleep (50); /* CPU sleep */

Vibe_timer.function = Vibrator_timer_func;
Hrtimer_start (&vibe_timer,

Ktime_set (value/1000, (value%) * 1000000), Hrtimer_mode_rel);
}

static void Ker_driver_init (void)//do-->hrtimer high-precision timer initialization function
{

struct TIMESPEC uptime;

Ker_print ("ker_driver_init\n");
Hrtimer_init (&vibe_timer, Clock_monotonic, Hrtimer_mode_rel); Do-->hrtimer Timer Initialization

Vibe_timer.function = Vibrator_timer_func; Do-->hrtimer Timer callback function
Hrtimer_start (&vibe_timer,

Ktime_set (value/1000, (value%) * 1000000), Hrtimer_mode_rel); Do-->hrtimer timer time initialization, where Ktime_set (sec, nanosecond)

Do_posix_clock_monotonic_gettime (&uptime); Thread settling time, used to compare (timer) at this time
Ker_print ("time:%lu.%0 2lu\n ",

(unsigned long) uptime.tv_sec,
(Uptime.tv_nsec/(nsec_per_sec/1000)));

Init_work (&vibe_work, Vibe_work_func); /* Intialize the Work Queue * *//Initialize working queues

}

Static int32_t xxxx_init (void)

{

Ker_driver_init ();

....

}

Linux jiffies Timers and Hrtimer high-precision timers "turn"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.