Contiki-NG
Loading...
Searching...
No Matches
stimer.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2004, 2008, 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 * Author: Adam Dunkels <adam@sics.se>, Nicolas Tsiftes <nvt@sics.se>
32 *
33 */
34
35/**
36 * \file
37 * Timer of seconds library implementation.
38 * \author
39 * Adam Dunkels <adam@sics.se>, Nicolas Tsiftes <nvt@sics.se>
40 */
41
42/**
43 * \addtogroup stimer
44 * @{
45 */
46
47#include "contiki.h"
48#include "sys/clock.h"
49#include "sys/stimer.h"
50
51#define SCLOCK_GEQ(a, b) ((unsigned long)((a) - (b)) < \
52 ((unsigned long)(~((unsigned long)0)) >> 1))
53
54/*---------------------------------------------------------------------------*/
55/**
56 * Reset the timer with the same interval.
57 *
58 * This function resets the timer with the same interval that was
59 * given to the stimer_set() function. The start point of the interval
60 * is the exact time that the timer last expired. Therefore, this
61 * function will cause the timer to be stable over time, unlike the
62 * stimer_restart() function. If this is executed before the
63 * timer expired, this function has no effect.
64 *
65 * \param t A pointer to the timer.
66 *
67 * \sa stimer_restart()
68 */
69void
71{
72 if(stimer_expired(t)) {
73 t->start += t->interval;
74 }
75}
76/*---------------------------------------------------------------------------*/
77/**
78 * Restart the timer from the current point in time
79 *
80 * This function restarts a timer with the same interval that was
81 * given to the stimer_set() function. The timer will start at the
82 * current time.
83 *
84 * \note A periodic timer will drift if this function is used to reset
85 * it. For periodic timers, use the stimer_reset() function instead.
86 *
87 * \param t A pointer to the timer.
88 *
89 * \sa stimer_reset()
90 */
91void
93{
94 t->start = clock_seconds();
95}
96/*---------------------------------------------------------------------------*/
97/**
98 * Check if a timer has expired.
99 *
100 * This function tests if a timer has expired and returns true or
101 * false depending on its status.
102 *
103 * \param t A pointer to the timer
104 *
105 * \return True if the timer has expired.
106 *
107 */
108bool
110{
111 return SCLOCK_GEQ(clock_seconds(), t->start + t->interval);
112}
113/*---------------------------------------------------------------------------*/
114/**
115 * The time elapsed since the timer started
116 *
117 * This function returns the time elapsed.
118 *
119 * \param t A pointer to the timer
120 *
121 * \return The time elapsed since the last start of the timer
122 *
123 */
124unsigned long
126{
127 return clock_seconds() - t->start;
128}
129
130/*---------------------------------------------------------------------------*/
131
132/** @} */
unsigned long clock_seconds(void)
Get the current value of the platform seconds.
Definition clock.c:130
void stimer_reset(struct stimer *t)
Reset the timer with the same interval.
Definition stimer.c:70
void stimer_restart(struct stimer *t)
Restart the timer from the current point in time.
Definition stimer.c:92
unsigned long stimer_elapsed(struct stimer *t)
The time elapsed since the timer started.
Definition stimer.c:125
bool stimer_expired(struct stimer *t)
Check if a timer has expired.
Definition stimer.c:109
Second timer library header file.
A timer.
Definition stimer.h:83