]> begriffs open source - freertos/blob - Demo/Common/ethernet/FreeTCPIP/timer.c
Not much more than a reformatted uIP so far - this will eventually replace the FreeRT...
[freertos] / Demo / Common / ethernet / FreeTCPIP / timer.c
1 /**\r
2  * \addtogroup timer\r
3  * @{\r
4  */\r
5 \r
6 /**\r
7  * \file\r
8  * Timer library implementation.\r
9  * \author\r
10  * Adam Dunkels <adam@sics.se>\r
11  */\r
12 \r
13 /*\r
14  * Copyright (c) 2004, Swedish Institute of Computer Science.\r
15  * All rights reserved.\r
16  *\r
17  * Redistribution and use in source and binary forms, with or without\r
18  * modification, are permitted provided that the following conditions\r
19  * are met:\r
20  * 1. Redistributions of source code must retain the above copyright\r
21  *    notice, this list of conditions and the following disclaimer.\r
22  * 2. Redistributions in binary form must reproduce the above copyright\r
23  *    notice, this list of conditions and the following disclaimer in the\r
24  *    documentation and/or other materials provided with the distribution.\r
25  * 3. Neither the name of the Institute nor the names of its contributors\r
26  *    may be used to endorse or promote products derived from this software\r
27  *    without specific prior written permission.\r
28  *\r
29  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND\r
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE\r
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
39  * SUCH DAMAGE.\r
40  *\r
41  * This file is part of the Contiki operating system.\r
42  *\r
43  * Author: Adam Dunkels <adam@sics.se>\r
44  *\r
45  * $Id: timer.c,v 1.5 2009/01/24 15:20:11 adamdunkels Exp $\r
46  */\r
47 \r
48 //_RB_#include "contiki-conf.h"\r
49 #include "uip-conf.h"\r
50 #include "net/clock-arch.h"\r
51 #include "sys/clock.h"\r
52 #include "sys/timer.h"\r
53 \r
54 /*---------------------------------------------------------------------------*/\r
55 \r
56 /**\r
57  * Set a timer.\r
58  *\r
59  * This function is used to set a timer for a time sometime in the\r
60  * future. The function timer_expired() will evaluate to true after\r
61  * the timer has expired.\r
62  *\r
63  * \param t A pointer to the timer\r
64  * \param interval The interval before the timer expires.\r
65  *\r
66  */\r
67 void timer_set( struct timer *t, clock_time_t interval )\r
68 {\r
69         t->interval = interval;\r
70         t->start = clock_time();\r
71 }\r
72 \r
73 /*---------------------------------------------------------------------------*/\r
74 \r
75 /**\r
76  * Reset the timer with the same interval.\r
77  *\r
78  * This function resets the timer with the same interval that was\r
79  * given to the timer_set() function. The start point of the interval\r
80  * is the exact time that the timer last expired. Therefore, this\r
81  * function will cause the timer to be stable over time, unlike the\r
82  * timer_restart() function.\r
83  *\r
84  * \param t A pointer to the timer.\r
85  *\r
86  * \sa timer_restart()\r
87  */\r
88 void timer_reset( struct timer *t )\r
89 {\r
90         t->start += t->interval;\r
91 }\r
92 \r
93 /*---------------------------------------------------------------------------*/\r
94 \r
95 /**\r
96  * Restart the timer from the current point in time\r
97  *\r
98  * This function restarts a timer with the same interval that was\r
99  * given to the timer_set() function. The timer will start at the\r
100  * current time.\r
101  *\r
102  * \note A periodic timer will drift if this function is used to reset\r
103  * it. For preioric timers, use the timer_reset() function instead.\r
104  *\r
105  * \param t A pointer to the timer.\r
106  *\r
107  * \sa timer_reset()\r
108  */\r
109 void timer_restart( struct timer *t )\r
110 {\r
111         t->start = clock_time();\r
112 }\r
113 \r
114 /*---------------------------------------------------------------------------*/\r
115 \r
116 /**\r
117  * Check if a timer has expired.\r
118  *\r
119  * This function tests if a timer has expired and returns true or\r
120  * false depending on its status.\r
121  *\r
122  * \param t A pointer to the timer\r
123  *\r
124  * \return Non-zero if the timer has expired, zero otherwise.\r
125  *\r
126  */\r
127 int timer_expired( struct timer *t )\r
128 {\r
129         return( clock_time_t ) ( clock_time() - t->start ) >= ( clock_time_t ) t->interval;\r
130 }\r
131 \r
132 /*---------------------------------------------------------------------------*/\r
133 \r
134 /**\r
135  * The time until the timer expires\r
136  *\r
137  * This function returns the time until the timer expires.\r
138  *\r
139  * \param t A pointer to the timer\r
140  *\r
141  * \return The time until the timer expires\r
142  *\r
143  */\r
144 clock_time_t timer_remaining( struct timer *t )\r
145 {\r
146         return t->start + t->interval - clock_time();\r
147 }\r
148 \r
149 /*---------------------------------------------------------------------------*/\r
150 \r
151 /** @} */\r