Contiki-NG
rtimer-arch.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Texas Instruments Incorporated - http://www.ti.com/
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  *
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29  * OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 /**
32  * \addtogroup cc2538
33  * @{
34  *
35  * \defgroup cc2538-rtimer cc2538 rtimer
36  *
37  * Implementation of the rtimer module for the cc2538
38  *
39  * The rtimer runs on the Sleep Timer. This is a design choice, as many parts
40  * of Contiki like rtimers with a value of RTIMER_ARCH_SECOND being a power of
41  * two. The ST runs on the 32kHz clock, which can provide us with an excellent
42  * value of 32768 for RTIMER_ARCH_SECOND.
43  *
44  * Additionally, since the ST keeps running in PM2, we can do things like drop
45  * to PM2 and schedule a wake-up time through the rtimer API.
46  *
47  * \note If the 32kHz clock is running on the 32kHz RC OSC, the rtimer is
48  * not 100% accurate (the RC OSC does not run at exactly 32.768 kHz). For
49  * applications requiring higher accuracy, the 32kHz clock should be changed to
50  * use the XOSC as its source. To see which low-frequency OSC the 32kHz clock
51  * is running on, see cpu/cc2538/clock.c.
52  *
53  * \sa cpu/cc2538/clock.c
54  * @{
55  */
56 /**
57  * \file
58  * Header file for the cc2538 rtimer driver
59  */
60 #ifndef RTIMER_ARCH_H_
61 #define RTIMER_ARCH_H_
62 
63 #include "contiki.h"
64 #include "dev/gptimer.h"
65 
66 /* Do the math in 32bits to save precision.
67  * Round to nearest integer rather than truncate. */
68 #define US_TO_RTIMERTICKS(US) ((US) >= 0 ? \
69  (((int32_t)(US) * (RTIMER_ARCH_SECOND) + 500000) / 1000000L) : \
70  ((int32_t)(US) * (RTIMER_ARCH_SECOND) - 500000) / 1000000L)
71 
72 #define RTIMERTICKS_TO_US(T) ((T) >= 0 ? \
73  (((int32_t)(T) * 1000000L + ((RTIMER_ARCH_SECOND) / 2)) / (RTIMER_ARCH_SECOND)) : \
74  ((int32_t)(T) * 1000000L - ((RTIMER_ARCH_SECOND) / 2)) / (RTIMER_ARCH_SECOND))
75 
76 /* A 64-bit version because the 32-bit one cannot handle T >= 4295 ticks.
77  Intended only for positive values of T. */
78 #define RTIMERTICKS_TO_US_64(T) ((uint32_t)(((uint64_t)(T) * 1000000 + ((RTIMER_ARCH_SECOND) / 2)) / (RTIMER_ARCH_SECOND)))
79 
80 /** \sa RTIMER_NOW() */
81 rtimer_clock_t rtimer_arch_now(void);
82 
83 /**
84  * \brief Get the time of the next scheduled rtimer trigger
85  * \return The time next rtimer ISR is scheduled for
86  */
87 rtimer_clock_t rtimer_arch_next_trigger(void);
88 
89 #endif /* RTIMER_ARCH_H_ */
90 
91 /**
92  * @}
93  * @}
94  */
rtimer_clock_t rtimer_arch_now(void)
Returns the current real-time clock time.
Definition: rtimer-arch.c:115
Header file for the cc2538 General Purpose Timers.
rtimer_clock_t rtimer_arch_next_trigger()
Get the time of the next scheduled rtimer trigger.
Definition: rtimer-arch.c:105