Contiki-NG
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 threads
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.h"
57 #include "dev/watchdog.h"
58 #include <stdbool.h>
59 
60 /** \brief The rtimer size (in bytes) */
61 #ifdef RTIMER_CONF_CLOCK_SIZE
62 #define RTIMER_CLOCK_SIZE RTIMER_CONF_CLOCK_SIZE
63 #else /* RTIMER_CONF_CLOCK_SIZE */
64 /* Default: 32bit rtimer*/
65 #define RTIMER_CLOCK_SIZE 4
66 #endif /* RTIMER_CONF_CLOCK_SIZE */
67 
68 #if RTIMER_CLOCK_SIZE == 2
69 /* 16-bit rtimer */
70 typedef uint16_t rtimer_clock_t;
71 #define RTIMER_CLOCK_DIFF(a,b) ((int16_t)((a)-(b)))
72 
73 #elif RTIMER_CLOCK_SIZE == 4
74 /* 32-bit rtimer */
75 typedef uint32_t rtimer_clock_t;
76 #define RTIMER_CLOCK_DIFF(a, b) ((int32_t)((a) - (b)))
77 
78 #elif RTIMER_CLOCK_SIZE == 8
79 /* 64-bit rtimer */
80 typedef uint64_t rtimer_clock_t;
81 #define RTIMER_CLOCK_DIFF(a,b) ((int64_t)((a)-(b)))
82 
83 #else
84 #error Unsupported rtimer size (check RTIMER_CLOCK_SIZE)
85 #endif
86 
87 #define RTIMER_CLOCK_MAX ((rtimer_clock_t)-1)
88 #define RTIMER_CLOCK_LT(a, b) (RTIMER_CLOCK_DIFF((a),(b)) < 0)
89 
90 #include "rtimer-arch.h"
91 
92 /**
93  * \brief Initialize the real-time scheduler.
94  *
95  * This function initializes the real-time scheduler and
96  * must be called at boot-up, before any other functions
97  * from the real-time scheduler is called.
98  */
99 void rtimer_init(void);
100 
101 struct rtimer;
102 typedef void (* rtimer_callback_t)(struct rtimer *t, void *ptr);
103 
104 /**
105  * \brief Representation of a real-time task
106  *
107  * This structure represents a real-time task and is used
108  * by the real-time module and the architecture specific
109  * support module for the real-time module.
110  */
111 struct rtimer {
112  rtimer_clock_t time;
113  rtimer_callback_t func;
114  void *ptr;
115 };
116 
117 enum {
118  RTIMER_OK,
119  RTIMER_ERR_FULL,
120  RTIMER_ERR_TIME,
121  RTIMER_ERR_ALREADY_SCHEDULED,
122 };
123 
124 /**
125  * \brief Post a real-time task.
126  * \param task A pointer to the task variable previously declared with RTIMER_TASK().
127  * \param time The time when the task is to be executed.
128  * \param duration Unused argument.
129  * \param func A function to be called when the task is executed.
130  * \param ptr An opaque pointer that will be supplied as an argument to the callback function.
131  * \return Non-zero (true) if the task could be scheduled, zero
132  * (false) if the task could not be scheduled.
133  *
134  * This function schedules a real-time task at a specified
135  * time in the future.
136  *
137  */
138 int rtimer_set(struct rtimer *task, rtimer_clock_t time,
139  rtimer_clock_t duration, rtimer_callback_t func, void *ptr);
140 
141 /**
142  * \brief Execute the next real-time task and schedule the next task, if any
143  *
144  * This function is called by the architecture dependent
145  * code to execute and schedule the next real-time task.
146  *
147  */
148 void rtimer_run_next(void);
149 
150 /**
151  * \brief Get the current clock time
152  * \return The current time
153  *
154  * This function returns what the real-time module thinks
155  * is the current time. The current time is used to set
156  * the timeouts for real-time tasks.
157  *
158  * \hideinitializer
159  */
160 #define RTIMER_NOW() rtimer_arch_now()
161 
162 /**
163  * \brief Get the time that a task last was executed
164  * \param task The task
165  * \return The time that a task last was executed
166  *
167  * This function returns the time that the task was last
168  * executed. This typically is used to get a periodic
169  * execution of a task without clock drift.
170  *
171  * \hideinitializer
172  */
173 #define RTIMER_TIME(task) ((task)->time)
174 
175 void rtimer_arch_init(void);
176 void rtimer_arch_schedule(rtimer_clock_t t);
177 /*rtimer_clock_t rtimer_arch_now(void);*/
178 
179 #define RTIMER_SECOND RTIMER_ARCH_SECOND
180 
181 /* RTIMER_GUARD_TIME is the minimum amount of rtimer ticks between
182  the current time and the future time when a rtimer is scheduled.
183  Necessary to avoid accidentally scheduling a rtimer in the past
184  on platforms with fast rtimer ticks. Should be >= 2. */
185 #ifdef RTIMER_CONF_GUARD_TIME
186 #define RTIMER_GUARD_TIME RTIMER_CONF_GUARD_TIME
187 #else /* RTIMER_CONF_GUARD_TIME */
188 #define RTIMER_GUARD_TIME (RTIMER_ARCH_SECOND >> 14)
189 #endif /* RTIMER_CONF_GUARD_TIME */
190 
191 /** \brief Busy-wait until a condition. Start time is t0, max wait time is max_time */
192 #define RTIMER_BUSYWAIT_UNTIL_ABS(cond, t0, max_time) \
193  ({ \
194  bool c; \
195  while(!(c = cond) && RTIMER_CLOCK_LT(RTIMER_NOW(), (t0) + (max_time))) { \
196  watchdog_periodic(); \
197  } \
198  c; \
199  })
200 
201 /** \brief Busy-wait until a condition for at most max_time */
202 #define RTIMER_BUSYWAIT_UNTIL(cond, max_time) \
203  ({ \
204  rtimer_clock_t t0 = RTIMER_NOW(); \
205  RTIMER_BUSYWAIT_UNTIL_ABS(cond, t0, max_time); \
206  })
207 
208 /** \brief Busy-wait for a fixed duration */
209 #define RTIMER_BUSYWAIT(duration) RTIMER_BUSYWAIT_UNTIL(0, duration)
210 
211 #endif /* RTIMER_H_ */
212 
213 /** @} */
214 /** @} */
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
Representation of a real-time task.
Definition: rtimer.h:111
void rtimer_run_next(void)
Execute the next real-time task and schedule the next task, if any.
Definition: rtimer.c:92
void rtimer_arch_init(void)
We don&#39;t need to explicitly initialise anything but this routine is required by the API...
Definition: rtimer-arch.c:59
void rtimer_init(void)
Initialize the real-time scheduler.
Definition: rtimer.c:61
void rtimer_arch_schedule(rtimer_clock_t t)
Schedules an rtimer task to be triggered at time t.
Definition: rtimer-arch.c:71