Contiki 3.x
rtimer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005, Swedish Institute of Computer Science
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * Header file for the real-time timer module.
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  *
39  */
40 
41 /** \addtogroup sys
42  * @{ */
43 
44 /**
45  * \defgroup rt Real-time task scheduling
46  *
47  * The real-time module handles the scheduling and execution of
48  * real-time tasks (with predictable execution times).
49  *
50  * @{
51  */
52 
53 #ifndef RTIMER_H_
54 #define RTIMER_H_
55 
56 #include "contiki-conf.h"
57 
58 #ifndef RTIMER_CLOCK_DIFF
59 typedef unsigned short rtimer_clock_t;
60 #define RTIMER_CLOCK_DIFF(a,b) ((signed short)((a)-(b)))
61 #endif /* RTIMER_CLOCK_DIFF */
62 
63 #define RTIMER_CLOCK_LT(a, b) (RTIMER_CLOCK_DIFF((a),(b)) < 0)
64 
65 #include "rtimer-arch.h"
66 
67 /**
68  * \brief Initialize the real-time scheduler.
69  *
70  * This function initializes the real-time scheduler and
71  * must be called at boot-up, before any other functions
72  * from the real-time scheduler is called.
73  */
74 void rtimer_init(void);
75 
76 struct rtimer;
77 typedef void (* rtimer_callback_t)(struct rtimer *t, void *ptr);
78 
79 /**
80  * \brief Representation of a real-time task
81  *
82  * This structure represents a real-time task and is used
83  * by the real-time module and the architecture specific
84  * support module for the real-time module.
85  */
86 struct rtimer {
87  rtimer_clock_t time;
88  rtimer_callback_t func;
89  void *ptr;
90 };
91 
92 enum {
93  RTIMER_OK,
94  RTIMER_ERR_FULL,
95  RTIMER_ERR_TIME,
96  RTIMER_ERR_ALREADY_SCHEDULED,
97 };
98 
99 /**
100  * \brief Post a real-time task.
101  * \param task A pointer to the task variable previously declared with RTIMER_TASK().
102  * \param time The time when the task is to be executed.
103  * \param duration Unused argument.
104  * \param func A function to be called when the task is executed.
105  * \param ptr An opaque pointer that will be supplied as an argument to the callback function.
106  * \return Non-zero (true) if the task could be scheduled, zero
107  * (false) if the task could not be scheduled.
108  *
109  * This function schedules a real-time task at a specified
110  * time in the future.
111  *
112  */
113 int rtimer_set(struct rtimer *task, rtimer_clock_t time,
114  rtimer_clock_t duration, rtimer_callback_t func, void *ptr);
115 
116 /**
117  * \brief Execute the next real-time task and schedule the next task, if any
118  *
119  * This function is called by the architecture dependent
120  * code to execute and schedule the next real-time task.
121  *
122  */
123 void rtimer_run_next(void);
124 
125 /**
126  * \brief Get the current clock time
127  * \return The current time
128  *
129  * This function returns what the real-time module thinks
130  * is the current time. The current time is used to set
131  * the timeouts for real-time tasks.
132  *
133  * \hideinitializer
134  */
135 #define RTIMER_NOW() rtimer_arch_now()
136 
137 /**
138  * \brief Get the time that a task last was executed
139  * \param task The task
140  * \return The time that a task last was executed
141  *
142  * This function returns the time that the task was last
143  * executed. This typically is used to get a periodic
144  * execution of a task without clock drift.
145  *
146  * \hideinitializer
147  */
148 #define RTIMER_TIME(task) ((task)->time)
149 
150 void rtimer_arch_init(void);
151 void rtimer_arch_schedule(rtimer_clock_t t);
152 /*rtimer_clock_t rtimer_arch_now(void);*/
153 
154 #define RTIMER_SECOND RTIMER_ARCH_SECOND
155 
156 /* RTIMER_GUARD_TIME is the minimum amount of rtimer ticks between
157  the current time and the future time when a rtimer is scheduled.
158  Necessary to avoid accidentally scheduling a rtimer in the past
159  on platforms with fast rtimer ticks. Should be >= 2. */
160 #ifdef RTIMER_CONF_GUARD_TIME
161 #define RTIMER_GUARD_TIME RTIMER_CONF_GUARD_TIME
162 #else /* RTIMER_CONF_GUARD_TIME */
163 #define RTIMER_GUARD_TIME (RTIMER_ARCH_SECOND >> 14)
164 #endif /* RTIMER_CONF_GUARD_TIME */
165 
166 #endif /* RTIMER_H_ */
167 
168 /** @} */
169 /** @} */
void rtimer_arch_init(void)
We don't need to explicitly initialise anything but this routine is required by the API...
Definition: rtimer-arch.c:78
void rtimer_arch_schedule(rtimer_clock_t t)
Schedules an rtimer task to be triggered at time t.
Definition: rtimer-arch.c:115
int rtimer_set(struct rtimer *rtimer, rtimer_clock_t time, rtimer_clock_t duration, rtimer_callback_t func, void *ptr)
Post a real-time task.
Definition: rtimer.c:67
void rtimer_run_next(void)
Execute the next real-time task and schedule the next task, if any.
Definition: rtimer.c:92
void rtimer_init(void)
Initialize the real-time scheduler.
Definition: rtimer.c:61
Representation of a real-time task.
Definition: rtimer.h:86