首页 > 代码库 > Contiki Rtimer 模块

Contiki Rtimer 模块

一、rtimer概述

The Contiki rtimer library provides scheduling and execution of real-time tasks (with predictable execution times)

数据结构:

struct rtimer {  rtimer_clock_t time;  rtimer_callback_t func;  void *ptr;};
typedef void (* rtimer_callback_t)(struct rtimer *t, void *ptr);

 1、rtimer_init

voidrtimer_init(void){  rtimer_arch_init();}

rtimer_arch_init是跟平台相关的rtimer_arch_init() is called by the rtimer library to initialize the rtimer architecture code.

2、rtimer_set

intrtimer_set(struct rtimer *rtimer, rtimer_clock_t time,       rtimer_clock_t duration,       rtimer_callback_t func, void *ptr){  int first = 0;  PRINTF("rtimer_set time %d\n", time);  if(next_rtimer == NULL) {//空,first置1    first = 1;  }  rtimer->func = func;//设置回调函数  rtimer->ptr = ptr;//回调函数第一个参数  rtimer->time = time;//设置  next_rtimer = rtimer;//设置next_rtimer  if(first == 1) {//如果是1    rtimer_arch_schedule(time);  }}

 

rtimer用来设置rtimer,即在特定的时间(time)执行特定的回调函数(func)。 

其中rtimer_arch_schedule函数是跟平台相关的,被底层用来确定调用rtimer_run_next的时间,rtimer_run_next会在特定的时间执行回调函数。

3、rtimer_run_next

voidrtimer_run_next(void){  struct rtimer *t;  if(next_rtimer == NULL) {    return;  }  t = next_rtimer;  next_rtimer = NULL;//重新设置为NULL  t->func(t, t->ptr);//执行回调函数  if(next_rtimer != NULL) {//在执行回调函数期间有rtimer set,在调用一次rtimer_arch_schedule    rtimer_arch_schedule(next_rtimer->time);  }  return;}

 

rtimer_run_next会调用特定的next_rtimer的回调函数。

rtimer_run_next是在底层被调用的。

 

直观的流程:

rtimer_set(设置rtimer) --->  rtimer_arch_schedule(通知底层在特定时间)    ------>   rtimer_run_next(在特定时间执行回调函数)   ------->   rtimer_set

 

注:RTIMER_NOW() RTIMER_SECOND rtimer_arch_init() rtimer_arch_now() rtimer_arch_schedule()等都是跟平台有关的。

Contiki Rtimer 模块