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