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