Contiki-NG
Loading...
Searching...
No Matches
clock.h
1/*
2 * Copyright (c) 2004, 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>
32 *
33 */
34
35/** \addtogroup timers
36 * @{
37 */
38
39/**
40 * \defgroup clock Clock library
41 *
42 * The clock library is the interface between Contiki and the platform
43 * specific clock functionality. The clock library defines a macro,
44 * CLOCK_SECOND, to convert seconds into the tick resolution of the platform.
45 * Typically this is 1-10 milliseconds, e.g. 4*CLOCK_SECOND could be 512.
46 * A 16 bit counter would thus overflow every 1-10 minutes.
47 * Platforms use the tick interrupt to maintain a long term count
48 * of seconds since startup.
49 *
50 * Platforms may also implement rtimers for greater time resolution
51 * and for real-time interrupts, These use a corresponding RTIMER_SECOND.
52 *
53 * \note These timers do not necessarily have a common divisor or are phase locked.
54 * One may be crystal controlled and the other may not. Low power operation
55 * or sleep will often use one for wake and disable the other, then give
56 * it a tick correction after wakeup.
57 *
58 * \note The clock library need in many cases not be used
59 * directly. Rather, the \ref timer "timer library", \ref etimer
60 * "event timers", or \ref rtimer "rtimer library" should be used.
61 *
62 * \sa \ref timer "Timer library"
63 * \sa \ref etimer "Event timers"
64 * \sa \ref rtimer "Realtime library"
65 *
66 * @{
67 */
68
69#ifndef CLOCK_H_
70#define CLOCK_H_
71
72#include <stdint.h>
73
74/** \brief The clock size (in bytes). */
75#ifdef CLOCK_CONF_SIZE
76#define CLOCK_SIZE CLOCK_CONF_SIZE
77#else /* CLOCK_CONF_SIZE */
78#define CLOCK_SIZE 8
79#endif /* CLOCK_CONF_SIZE */
80
81/* Define clock_time_t before including contiki.h, so etimer.h
82 * and other header files have clock_time_t defined. */
83#if CLOCK_SIZE == 4
84typedef uint32_t clock_time_t;
85#define CLOCK_LT(a, b) ((int32_t)((a) - (b)) < 0)
86#elif CLOCK_SIZE == 8
87typedef uint64_t clock_time_t;
88#define CLOCK_LT(a, b) ((int64_t)((a) - (b)) < 0)
89#else
90#error Unsupported clock_time_t size (check CLOCK_CONF_SIZE)
91#endif
92
93#include "contiki.h"
94
95/**
96 * A second, measured in system clock time.
97 *
98 * \hideinitializer
99 */
100#ifdef CLOCK_CONF_SECOND
101#define CLOCK_SECOND CLOCK_CONF_SECOND
102#else
103#define CLOCK_SECOND (clock_time_t)32
104#endif
105
106/**
107 * Initialize the clock library.
108 *
109 * This function initializes the clock library and should be called
110 * from the main() function of the system.
111 *
112 */
113void clock_init(void);
114
115/**
116 * Get the current clock time.
117 *
118 * This function returns the current system clock time.
119 *
120 * \return The current clock time, measured in system ticks.
121 */
122clock_time_t clock_time(void);
123
124/**
125 * Get the current value of the platform seconds.
126 *
127 * This could be the number of seconds since startup, or
128 * since a standard epoch.
129 *
130 * \return The value.
131 */
132unsigned long clock_seconds(void);
133
134/**
135 * Set the value of the platform seconds.
136 * \param sec The value to set.
137 *
138 */
139void clock_set_seconds(unsigned long sec);
140
141/**
142 * Wait for a given number of ticks.
143 * \param t How many ticks.
144 *
145 */
146void clock_wait(clock_time_t t);
147
148/**
149 * Delay a given number of microseconds.
150 * \param dt How many microseconds to delay.
151 *
152 * \note Interrupts could increase the delay by a variable amount.
153 */
154void clock_delay_usec(uint16_t dt);
155
156/**
157 * Deprecated platform-specific routines.
158 *
159 */
160int clock_fine_max(void);
161unsigned short clock_fine(void);
162void clock_delay(unsigned int delay);
163
164#endif /* CLOCK_H_ */
165
166/** @} */
167/** @} */
void clock_set_seconds(unsigned long sec)
Set the value of the platform seconds.
Definition clock.c:124
unsigned long clock_seconds(void)
Get the current value of the platform seconds.
Definition clock.c:130
void clock_init(void)
Initialize the clock library.
Definition clock.c:93
void clock_wait(clock_time_t t)
Wait for a given number of ticks.
Definition clock.c:136
void clock_delay(unsigned int delay)
Obsolete delay function but we implement it here since some code still uses it.
Definition clock.c:164
void clock_delay_usec(uint16_t dt)
Delay a given number of microseconds.
Definition clock.c:150
int clock_fine_max(void)
Deprecated platform-specific routines.
Definition clock.c:129
clock_time_t clock_time(void)
Get the current clock time.
Definition clock.c:118