I. rtimer Overview
The contiki rtimer Library provides scheduling and execution of real-time tasks (Predictable execution times)
Data structure:
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_initIs related to the platform,Rtimer_arch_init ()Is called by the rtimer library to initialize the rtimer architecture code.
2. rtimer_set
Intrtimer_set (struct rtimer * rtimer, interval time, Interval Duration, rtimer_callback_t func, void * PTR) {int first = 0; printf ("rtimer_set time % d \ n", time ); if (next_rtimer = NULL) {// null, set first to 1 first = 1;} rtimer-> func = func; // set the callback function rtimer-> PTR = PTR; // The first parameter of the callback function, rtimer-> time = time; // sets next_rtimer = rtimer; // sets next_rtimer if (first = 1) {// if it is 1 rtimer_arch_schedule (time );}}
Rtimer is used to set rtimer, that is, to execute a specific callback function (func) at a specific time ).
The rtimer_arch_schedule function is platform-related and is used at the underlying layer to determine the time for calling rtimer_run_next. rtimer_run_next will execute the callback function at a specific time.
3. rtimer_run_next
Voidrtimer_run_next (void) {struct rtimer * t; If (next_rtimer = NULL) {return ;}t = next_rtimer; next_rtimer = NULL; // reset to null T-> func (t, t-> PTR); // execute the callback function if (next_rtimer! = NULL) {// rtimer set exists during callback function execution. rtimer_arch_schedule (next_rtimer-> time) is called once;} return ;}
Rtimer_run_next calls the specific next_rtimer callback function.
Rtimer_run_next is called at the underlying layer.
Intuitive process:
Rtimer_set (set rtimer) ---> rtimer_arch_schedule (inform the underlying layer at a specific time) ------> rtimer_run_next (execute the callback function at a specific time) -------> rtimer_set
Note: rtimer_now () rtimer_second rtimer_arch_init () rtimer_arch_now () rtimer_arch_schedule () and so on are all platform-related.
Contiki rtimer Module