Add optional RS232 debugging
[contiki-2.x.git] / core / sys / stimer.c
blob4307b80220b1396b6e2521531286d4512dfdbe8a
1 /**
2 * \addtogroup stimer
3 * @{
4 */
6 /**
7 * \file
8 * Timer of seconds library implementation.
9 * \author
10 * Adam Dunkels <adam@sics.se>, Nicolas Tsiftes <nvt@sics.se>
14 * Copyright (c) 2004, 2008, Swedish Institute of Computer Science.
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. Neither the name of the Institute nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
41 * This file is part of the Contiki operating system.
43 * Author: Adam Dunkels <adam@sics.se>, Nicolas Tsiftes <nvt@sics.se>
45 * $Id: stimer.c,v 1.3 2010/03/15 15:53:57 joxe Exp $
48 #include "contiki-conf.h"
49 #include "sys/clock.h"
50 #include "sys/stimer.h"
52 #define SCLOCK_GEQ(a, b) ((unsigned long)((a) - (b)) < \
53 ((unsigned long)(~((unsigned long)0)) >> 1))
55 /*---------------------------------------------------------------------------*/
56 /**
57 * Set a timer.
59 * This function is used to set a timer for a time sometime in the
60 * future. The function stimer_expired() will evaluate to true after
61 * the timer has expired.
63 * \param t A pointer to the timer
64 * \param interval The interval before the timer expires.
67 void
68 stimer_set(struct stimer *t, unsigned long interval)
70 t->interval = interval;
71 t->start = clock_seconds();
73 /*---------------------------------------------------------------------------*/
74 /**
75 * Reset the timer with the same interval.
77 * This function resets the timer with the same interval that was
78 * given to the stimer_set() function. The start point of the interval
79 * is the exact time that the timer last expired. Therefore, this
80 * function will cause the timer to be stable over time, unlike the
81 * stimer_restart() function.
83 * \param t A pointer to the timer.
85 * \sa stimer_restart()
87 void
88 stimer_reset(struct stimer *t)
90 t->start += t->interval;
92 /*---------------------------------------------------------------------------*/
93 /**
94 * Restart the timer from the current point in time
96 * This function restarts a timer with the same interval that was
97 * given to the stimer_set() function. The timer will start at the
98 * current time.
100 * \note A periodic timer will drift if this function is used to reset
101 * it. For preioric timers, use the stimer_reset() function instead.
103 * \param t A pointer to the timer.
105 * \sa stimer_reset()
107 void
108 stimer_restart(struct stimer *t)
110 t->start = clock_seconds();
112 /*---------------------------------------------------------------------------*/
114 * Check if a timer has expired.
116 * This function tests if a timer has expired and returns true or
117 * false depending on its status.
119 * \param t A pointer to the timer
121 * \return Non-zero if the timer has expired, zero otherwise.
125 stimer_expired(struct stimer *t)
127 return SCLOCK_GEQ(clock_seconds(), t->start + t->interval);
129 /*---------------------------------------------------------------------------*/
131 * The time until the timer expires
133 * This function returns the time until the timer expires.
135 * \param t A pointer to the timer
137 * \return The time until the timer expires
140 unsigned long
141 stimer_remaining(struct stimer *t)
143 return t->start + t->interval - clock_seconds();
145 /*---------------------------------------------------------------------------*/
147 * The time elapsed since the timer started
149 * This function returns the time elapsed.
151 * \param t A pointer to the timer
153 * \return The time elapsed since the last start of the timer
156 unsigned long
157 stimer_elapsed(struct stimer *t)
159 return clock_seconds() - t->start;
162 /*---------------------------------------------------------------------------*/
164 /** @} */