Contiki-NG
Toggle main menu visibility
Loading...
Searching...
No Matches
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 <inttypes.h>
59
#include <stdbool.h>
60
61
/*---------------------------------------------------------------------------*/
62
63
/** \brief The rtimer size (in bytes) */
64
#ifdef RTIMER_CONF_CLOCK_SIZE
65
#define RTIMER_CLOCK_SIZE RTIMER_CONF_CLOCK_SIZE
66
#else
/* RTIMER_CONF_CLOCK_SIZE */
67
/* Default: 32bit rtimer*/
68
#define RTIMER_CLOCK_SIZE 4
69
#endif
/* RTIMER_CONF_CLOCK_SIZE */
70
71
#if RTIMER_CLOCK_SIZE == 2
72
/* 16-bit rtimer */
73
typedef
uint16_t rtimer_clock_t;
74
#define RTIMER_CLOCK_DIFF(a,b) ((int16_t)((a)-(b)))
75
#define RTIMER_PRI PRIu16
76
77
#elif RTIMER_CLOCK_SIZE == 4
78
/* 32-bit rtimer */
79
typedef
uint32_t rtimer_clock_t;
80
#define RTIMER_CLOCK_DIFF(a, b) ((int32_t)((a) - (b)))
81
#define RTIMER_PRI PRIu32
82
83
#elif RTIMER_CLOCK_SIZE == 8
84
/* 64-bit rtimer */
85
typedef
uint64_t rtimer_clock_t;
86
#define RTIMER_CLOCK_DIFF(a,b) ((int64_t)((a)-(b)))
87
#define RTIMER_PRI PRIu64
88
89
#else
90
#error Unsupported rtimer size (check RTIMER_CLOCK_SIZE)
91
#endif
92
93
#define RTIMER_CLOCK_MAX ((rtimer_clock_t)-1)
94
#define RTIMER_CLOCK_LT(a, b) (RTIMER_CLOCK_DIFF((a),(b)) < 0)
95
96
#include "rtimer-arch.h"
97
98
99
/*
100
* RTIMER_GUARD_TIME is the minimum amount of rtimer ticks between
101
* the current time and the future time when a rtimer is scheduled.
102
* Necessary to avoid accidentally scheduling a rtimer in the past
103
* on platforms with fast rtimer ticks. Should be >= 2.
104
*/
105
#ifdef RTIMER_CONF_GUARD_TIME
106
#define RTIMER_GUARD_TIME RTIMER_CONF_GUARD_TIME
107
#else
/* RTIMER_CONF_GUARD_TIME */
108
#define RTIMER_GUARD_TIME (RTIMER_ARCH_SECOND >> 14)
109
#endif
/* RTIMER_CONF_GUARD_TIME */
110
111
/*---------------------------------------------------------------------------*/
112
113
/**
114
* Number of rtimer ticks for 1 second.
115
*/
116
#define RTIMER_SECOND RTIMER_ARCH_SECOND
117
118
/**
119
* \brief Initialize the real-time scheduler.
120
*
121
* This function initializes the real-time scheduler and
122
* must be called at boot-up, before any other functions
123
* from the real-time scheduler is called.
124
*
125
* \hideinitializer
126
*/
127
#define rtimer_init() rtimer_arch_init()
128
129
struct
rtimer
;
130
typedef
void (* rtimer_callback_t)(
struct
rtimer
*t,
void
*ptr);
131
132
/**
133
* \brief Representation of a real-time task
134
*
135
* This structure represents a real-time task and is used
136
* by the real-time module and the architecture specific
137
* support module for the real-time module.
138
*/
139
struct
rtimer
{
140
rtimer_clock_t time;
141
rtimer_callback_t func;
142
void
*ptr;
143
};
144
145
/**
146
* TODO: we need to document meanings of these symbols.
147
*/
148
enum
{
149
RTIMER_OK
,
/**< rtimer task is scheduled successfully */
150
RTIMER_ERR_FULL,
151
RTIMER_ERR_TIME,
152
RTIMER_ERR_ALREADY_SCHEDULED,
153
};
154
155
/**
156
* \brief Post a real-time task.
157
* \param task A pointer to the task variable allocated somewhere.
158
* \param time The time when the task is to be executed.
159
* \param duration Unused argument.
160
* \param func A function to be called when the task is executed.
161
* \param ptr An opaque pointer that will be supplied as an argument to the callback function.
162
* \return RTIMER_OK if the task could be scheduled. Any other value indicates
163
* the task could not be scheduled.
164
*
165
* This function schedules a real-time task at a specified
166
* time in the future.
167
*
168
*/
169
int
rtimer_set
(
struct
rtimer
*task, rtimer_clock_t time,
170
rtimer_clock_t duration, rtimer_callback_t func,
void
*ptr);
171
172
/**
173
* \brief Execute the next real-time task and schedule the next task, if any
174
*
175
* This function is called by the architecture dependent
176
* code to execute and schedule the next real-time task.
177
*
178
*/
179
void
rtimer_run_next
(
void
);
180
181
/**
182
* \brief Get the current clock time
183
* \return The current time
184
*
185
* This function returns what the real-time module thinks
186
* is the current time. The current time is used to set
187
* the timeouts for real-time tasks.
188
*
189
* \hideinitializer
190
*/
191
#define RTIMER_NOW() rtimer_arch_now()
192
193
/**
194
* \brief Get the time that a task last was executed
195
* \param task The task
196
* \return The time that a task last was executed
197
*
198
* This function returns the time that the task was last
199
* executed. This typically is used to get a periodic
200
* execution of a task without clock drift.
201
*
202
* \hideinitializer
203
*/
204
#define RTIMER_TIME(task) ((task)->time)
205
206
/** \brief Busy-wait until a condition. Start time is t0, max wait time is max_time */
207
#ifndef RTIMER_BUSYWAIT_UNTIL_ABS
208
#define RTIMER_BUSYWAIT_UNTIL_ABS(cond, t0, max_time) \
209
({ \
210
bool c; \
211
while(!(c = cond) && RTIMER_CLOCK_LT(RTIMER_NOW(), (t0) + (max_time))); \
212
c; \
213
})
214
#endif
/* RTIMER_BUSYWAIT_UNTIL_ABS */
215
216
/** \brief Busy-wait until a condition for at most max_time */
217
#define RTIMER_BUSYWAIT_UNTIL(cond, max_time) \
218
({ \
219
rtimer_clock_t t0 = RTIMER_NOW(); \
220
RTIMER_BUSYWAIT_UNTIL_ABS(cond, t0, max_time); \
221
})
222
223
/** \brief Busy-wait for a fixed duration */
224
#define RTIMER_BUSYWAIT(duration) RTIMER_BUSYWAIT_UNTIL(0, duration)
225
226
/*---------------------------------------------------------------------------*/
227
228
/**
229
* \name Architecture-dependent symbols
230
*
231
* The functions declared in this section must be defined in
232
* architecture-dependent implementation of rtimer. Alternatively,
233
* they can be defined as macros in rtimer-arch.h.
234
*
235
* In addition, the architecture-dependent header (rtimer-arch.h)
236
* must define the following macros.
237
*
238
* - RTIMER_ARCH_SECOND
239
* - US_TO_RTIMERTICKS(us)
240
* - RTIMERTICKS_TO_US(t)
241
* - RTIMERTICKS_TO_US_64(t)
242
*
243
* @{
244
*/
245
246
/**
247
* Initialized the architecture-dependent part of rtimer.
248
*/
249
void
rtimer_arch_init
(
void
);
250
251
/**
252
* \brief Schedules an rtimer task to be triggered at time t
253
* \param t The time when the task will need executed.
254
*
255
* \e t is an absolute time, in other words the task will be executed AT
256
* time \e t, not IN \e t rtimer ticks.
257
*
258
*/
259
void
rtimer_arch_schedule
(rtimer_clock_t t);
260
261
/*
262
* Return the current time in rtimer ticks.
263
*
264
* Currently rtimer-arch.h needs to define:
265
*
266
* rtimer_clock_t rtimer_arch_now(void);
267
*/
268
269
/** @} */
270
271
#endif
/* RTIMER_H_ */
272
273
/** @} */
274
/** @} */
rtimer_set
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:57
rtimer_arch_init
void rtimer_arch_init(void)
Initialized the architecture-dependent part of rtimer.
Definition
rtimer-arch.c:59
rtimer_run_next
void rtimer_run_next(void)
Execute the next real-time task and schedule the next task, if any.
Definition
rtimer.c:78
rtimer_arch_schedule
void rtimer_arch_schedule(rtimer_clock_t t)
Schedules an rtimer task to be triggered at time t.
Definition
rtimer-arch.c:65
RTIMER_OK
@ RTIMER_OK
rtimer task is scheduled successfully
Definition
rtimer.h:149
rtimer
Representation of a real-time task.
Definition
rtimer.h:139
os
sys
rtimer.h
Generated on
for Contiki-NG by
1.17.0