Skip to content
in1004kyu edited this page Jul 16, 2013 · 3 revisions

Timer Module

Implements Timer functionality such as,

  • timer channel: periodic callback at a given time interval
  • current time: time since boot-up

Data Types

timer_channel_t

typedef enum{
	timer_sched = 0;
} timer_channel_t;

Represents a channel where 'time interval' and a callback function can be specified.

struct timeval

struct timeval{
	uint64_t tv_sec;
	uint64_t tv_usec;
};

Represents time in seconds and microseconds.

  • tv_sec : seconds
  • tv_usec : microseconds

timer_callback_t

typedef void(*timer_callback_t)(void);

Example Usage

/* Initialize the timer module 
 * This is required only once in the entire system prior to calls to other functions of Timer module.
 */
timer_init(timer_sched);

/* Set interval 10 to timer_sched channel */
timer_set_interval( timer_sched, 10 );

/* Set cb1 callback function to timer_scehd channel. */
/* cb1 call after 10 interval. */
timer_set_callback( timer_sched, &cb1 );

/* Start timer_sched channel. cb1 call after 10 interval. */
timer_start( timer_sched );
...

/* Remove the call back */
timer_remove_callback(timer_sched, &cb1);

...

void cb1(...) {
    /* the call back function body */
}

Functions

timer_init(channel)

hvmm_status_t timer_init(timer_channel_t channel);

Calling this function is required only once in the entire system prior to calls to other functions of Timer module.

timer_start(channel)

hvmm_status_t timer_start(timer_channel_t channel);

Starts the timer channel specified by 'channel'. The callback, if set, will be periodically called until it's unset or the channel stops by 'timer_stop(timer_channel)'

timer_stop(channel)

hvmm_status_t timer_stop(timer_channel_t channel);

Stops the timer channel specified by 'channel'

timer_set_interval(channel, interval)

hvmm_status_t timer_set_interval(timer_channel_t channel, uint32_t interval);

Sets time interval, in (TBD)seconds, for the timer channel. If the channel has been started and a callback function is set, it will be called in the next interval

timer_get_interval(channel)

hvmm_status_t timer_get_interval(timer_channel_t channel);

Returns the time interval for the timer channel if it was set previously. Unknown value is returned otherwise.

timer_add_callback(channel, handler)

hvmm_status_t timer_add_callback(timer_channel_t channel, timer_callback_t handler);

Adds a callback function for the timer channel.

timer_remove_callback(channel, handler)

hvmm_status_t timer_remove_callback(timer_channel_t channel, timer_callback_t handler);

Removes the callback function from the timer channel's registered callback function list if it previously has been added.

  • channel: The timer channel
  • handler: callback function pointer

timer_get_time(timeval)

void timer_get_time(struct timeval* timeval);

Returns current time since boot-up

timer_c2t(system counter)

uint64_t timer_c2t(uint64_t count);

Converts from system counter to milliseconds.

timer_t2c(milliseconds)

uint64_t timer_t2c(uint64_t time);

Converts from milliseconds to system counter.