Contiki 3.x
rtimer-arch.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, SICS Swedish ICT.
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  */
32 
33 /**
34  * \file
35  * Header file for NXP jn516x-specific rtimer code
36  * \author
37  * Beshr Al Nahas <beshr@sics.se>
38  * Atis Elsts <atis.elsts@sics.se>
39  */
40 
41 #ifndef RTIMER_ARCH_H_
42 #define RTIMER_ARCH_H_
43 
44 #include "sys/rtimer.h"
45 
46 #ifdef RTIMER_CONF_SECOND
47 # define RTIMER_ARCH_SECOND RTIMER_CONF_SECOND
48 #else
49 #if RTIMER_USE_32KHZ
50 # if JN516X_EXTERNAL_CRYSTAL_OSCILLATOR
51 # define RTIMER_ARCH_SECOND 32768
52 # else
53 # define RTIMER_ARCH_SECOND 32000
54 #endif
55 #else
56 /* 32MHz CPU clock => 16MHz timer */
57 # define RTIMER_ARCH_SECOND (F_CPU / 2)
58 #endif
59 #endif
60 
61 #if RTIMER_USE_32KHZ
62 #define US_TO_RTIMERTICKS(US) ((US) >= 0 ? \
63  (((int32_t)(US) * (RTIMER_ARCH_SECOND) + 500000) / 1000000L) : \
64  ((int32_t)(US) * (RTIMER_ARCH_SECOND) - 500000) / 1000000L)
65 
66 #define RTIMERTICKS_TO_US(T) ((T) >= 0 ? \
67  (((int32_t)(T) * 1000000L + ((RTIMER_ARCH_SECOND) / 2)) / (RTIMER_ARCH_SECOND)) : \
68  ((int32_t)(T) * 1000000L - ((RTIMER_ARCH_SECOND) / 2)) / (RTIMER_ARCH_SECOND))
69 
70 /* A 64-bit version because the 32-bit one cannot handle T >= 4295 ticks.
71  Intended only for positive values of T. */
72 #define RTIMERTICKS_TO_US_64(T) ((uint32_t)(((uint64_t)(T) * 1000000 + ((RTIMER_ARCH_SECOND) / 2)) / (RTIMER_ARCH_SECOND)))
73 
74 #else
75 
76 #define US_TO_RTIMERTICKS(D) ((int64_t)(D) << 4)
77 #define RTIMERTICKS_TO_US(T) ((int64_t)(T) >> 4)
78 #define RTIMERTICKS_TO_US_64(T) RTIMERTICKS_TO_US(T)
79 
80 #endif
81 
82 rtimer_clock_t rtimer_arch_now(void);
83 
84 rtimer_clock_t rtimer_arch_time_to_rtimer(void);
85 
86 void rtimer_arch_reinit(rtimer_clock_t sleep_start, rtimer_clock_t wakeup_time);
87 
88 void clock_arch_init(int is_reinitialization);
89 
90 void clock_arch_calibrate(void);
91 
92 void clock_arch_reinit(void);
93 
94 void clock_arch_schedule_interrupt(clock_time_t time_to_etimer, rtimer_clock_t ticks_to_rtimer);
95 
96 clock_t clock_arch_time_to_etimer(void);
97 
98 /* Use 20 ms: enough for TSCH with the default schedule to sleep */
99 #define JN516X_MIN_SLEEP_TIME (RTIMER_SECOND / 50)
100 /* 1 second by default: arbitrary picked value which could be increased */
101 #define JN516X_MAX_SLEEP_TIME RTIMER_SECOND
102 /* Assume conservative 10 ms maximal system wakeup time */
103 #define JN516X_SLEEP_GUARD_TIME (RTIMER_ARCH_SECOND / 100)
104 
105 #define WAKEUP_TIMER E_AHI_WAKE_TIMER_0
106 #define WAKEUP_TIMER_MASK E_AHI_SYSCTRL_WK0_MASK
107 
108 #define TICK_TIMER E_AHI_WAKE_TIMER_1
109 #define TICK_TIMER_MASK E_AHI_SYSCTRL_WK1_MASK
110 
111 #define WAIT_FOR_EDGE(edge_t) do { \
112  uint64_t start_t = u64AHI_WakeTimerReadLarge(TICK_TIMER); \
113  do { \
114  edge_t = u64AHI_WakeTimerReadLarge(TICK_TIMER); \
115  } while(edge_t == start_t); \
116  } while(0)
117 
118 #endif /* RTIMER_ARCH_H_ */
Header file for the real-time timer module.
rtimer_clock_t rtimer_arch_now(void)
Returns the current real-time clock time.
Definition: rtimer-arch.c:109