Contiki 3.x
clock.c
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  * Tickless clock implementation for NXP jn516x.
36  * \author
37  * Beshr Al Nahas <beshr@sics.se>
38  * Atis Elsts <atis.elsts@sics.se>
39  *
40  */
41 
42 #include <AppHardwareApi.h>
43 #include <PeripheralRegs.h>
44 #include "contiki.h"
45 #include "sys/energest.h"
46 #include "sys/clock.h"
47 #include "sys/etimer.h"
48 #include "rtimer-arch.h"
49 #include "dev/watchdog.h"
50 
51 
52 #define DEBUG 0
53 #if DEBUG
54 #include <stdio.h>
55 #define PRINTF(...) printf(__VA_ARGS__)
56 #else
57 #define PRINTF(...)
58 #endif
59 
60 #define CLOCK_TIMER E_AHI_TIMER_1
61 #define CLOCK_TIMER_ISR_DEV E_AHI_DEVICE_TIMER1
62 
63 #define OVERFLOW_TIMER E_AHI_TIMER_0
64 #define OVERFLOW_TIMER_ISR_DEV E_AHI_DEVICE_TIMER0
65 
66 /* 16Mhz / 2^10 = 15.625 kHz */
67 #define CLOCK_PRESCALE 10
68 #define PRESCALED_TICKS_PER_SECOND 15625
69 /* 8ms tick --> overflow after ~397.7 days */
70 #define CLOCK_INTERVAL 125
71 /* Max schedulable number of ticks.
72  * Must not be more than:
73  * 0xffff / (16'000'000 / (1 << CLOCK_PRESCALE) / CLOCK_SECOND)
74  */
75 #define CLOCK_MAX_SCHEDULABLE_TICKS 520
76 /* Min guard time an etimer can be scheduled before an rtimer */
77 #define CLOCK_RTIMER_GUARD_TIME US_TO_RTIMERTICKS(16)
78 /* Clock tick expressed as rtimer ticks */
79 #define CLOCK_TICK ((1 << CLOCK_PRESCALE) * CLOCK_INTERVAL)
80 
81 #define RTIMER_OVERFLOW_PRESCALED 4194304 /* = 0x100000000 / (2^CLOCK_PRESCALE) */
82 #define RTIMER_OVERFLOW_REMAINDER 54 /* in prescaled ticks, per one overflow */
83 
84 
85 #define CLOCK_LT(a, b) ((int32_t)((a)-(b)) < 0)
86 
87 /*---------------------------------------------------------------------------*/
88 static uint32_t
89 clock(void)
90 {
91  /* same as rtimer_arch_now() */
92  return u32AHI_TickTimerRead();
93 }
94 /*---------------------------------------------------------------------------*/
95 static uint32_t
96 check_rtimer_overflow(rtimer_clock_t now)
97 {
98  static rtimer_clock_t last_rtimer_ticks;
99  static uint32_t clock_ticks_remainder;
100  static uint32_t clock_ticks_base;
101 
102  if(last_rtimer_ticks > now) {
103  clock_ticks_base += RTIMER_OVERFLOW_PRESCALED / CLOCK_INTERVAL;
104  clock_ticks_remainder += RTIMER_OVERFLOW_REMAINDER;
105  if(clock_ticks_remainder > CLOCK_INTERVAL) {
106  clock_ticks_remainder -= CLOCK_INTERVAL;
107  clock_ticks_base += 1;
108  }
109  }
110  last_rtimer_ticks = now;
111  return clock_ticks_base;
112 }
113 /*---------------------------------------------------------------------------*/
114 static void
115 check_etimers(void)
116 {
117  if(etimer_pending()) {
118  clock_time_t now = clock_time();
119  if(!CLOCK_LT(now, etimer_next_expiration_time())) {
121  }
122  }
123  process_nevents();
124 }
125 /*---------------------------------------------------------------------------*/
126 void
127 clockTimerISR(uint32 u32Device, uint32 u32ItemBitmap)
128 {
129  if(u32Device != CLOCK_TIMER_ISR_DEV && u32Device != OVERFLOW_TIMER_ISR_DEV) {
130  return;
131  }
132 
133  ENERGEST_ON(ENERGEST_TYPE_IRQ);
134 
135  if(u32Device == CLOCK_TIMER_ISR_DEV) {
136  check_etimers();
137  }
138 
139  if(u32Device == OVERFLOW_TIMER_ISR_DEV) {
140  check_rtimer_overflow(clock());
141  }
142 
143  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
144 }
145 /*---------------------------------------------------------------------------*/
146 void
147 clock_arch_calibrate(void)
148 {
149  bAHI_SetClockRate(E_AHI_XTAL_32MHZ);
150 
151  /* Wait for oscillator to stabilise */
152  while(bAHI_GetClkSource() == 1) ;
153  while(bAHI_Clock32MHzStable() == 0) ;
154 
155  vAHI_OptimiseWaitStates();
156 
157  /* Turn on SPI master */
158  vREG_SysWrite(REG_SYS_PWR_CTRL, u32REG_SysRead(REG_SYS_PWR_CTRL)
159  | REG_SYSCTRL_PWRCTRL_SPIMEN_MASK);
160 }
161 /*---------------------------------------------------------------------------*/
162 void
163 clock_arch_init(int is_reinitialization)
164 {
165  /* initialize etimer interrupt timer */
166  vAHI_TimerEnable(CLOCK_TIMER, CLOCK_PRESCALE, 0, 1, 0);
167  vAHI_TimerClockSelect(CLOCK_TIMER, 0, 0);
168 
169  vAHI_TimerConfigureOutputs(CLOCK_TIMER, 0, 1);
170  vAHI_TimerDIOControl(CLOCK_TIMER, 0);
171 
172  vAHI_Timer1RegisterCallback(clockTimerISR);
173 
174  /* initialize and start rtimer overflow timer */
175  vAHI_TimerEnable(OVERFLOW_TIMER, CLOCK_PRESCALE, 0, 1, 0);
176  vAHI_TimerClockSelect(OVERFLOW_TIMER, 0, 0);
177 
178  vAHI_TimerConfigureOutputs(OVERFLOW_TIMER, 0, 1);
179  vAHI_TimerDIOControl(OVERFLOW_TIMER, 0);
180 
181  vAHI_Timer0RegisterCallback(clockTimerISR);
182  vAHI_TimerStartRepeat(OVERFLOW_TIMER, 0, PRESCALED_TICKS_PER_SECOND * 4);
183 
184  if(is_reinitialization) {
185  /* check if the etimer has overflowed (useful when this is executed after sleep */
186  check_rtimer_overflow(clock());
187  }
188 }
189 /*---------------------------------------------------------------------------*/
190 void
192 {
193  /* gMAC_u8MaxBuffers = 2; */
194 #ifdef JENNIC_CHIP_FAMILY_JN516x
195  /* Turn off debugger */
196  *(volatile uint32 *)0x020000a0 = 0;
197 #endif
198 
199  clock_arch_calibrate();
200 
201  /* setup clock mode and interrupt handler */
202  clock_arch_init(0);
203 }
204 /*---------------------------------------------------------------------------*/
205 clock_time_t
207 {
208  uint32_t now = clock();
209  clock_time_t base = check_rtimer_overflow(now);
210  return base + now / CLOCK_TICK;
211 }
212 /*---------------------------------------------------------------------------*/
213 /**
214  * Delay the CPU for a multiple of 0.0625 us.
215  */
216 void
217 clock_delay_usec(uint16_t dt)
218 {
219  uint32_t end = clock() + dt;
220  /* Note: this does not call watchdog periodic() */
221  while(CLOCK_LT(clock(), end));
222 }
223 /*---------------------------------------------------------------------------*/
224 /**
225  * Delay the CPU for a multiple of 8 us.
226  */
227 void
228 clock_delay(unsigned int dt)
229 {
230  uint32_t end = clock() + dt * 128;
231  while(CLOCK_LT(clock(), end)) {
233  }
234 }
235 /*---------------------------------------------------------------------------*/
236 /**
237  * Wait for a multiple of 10 ms.
238  *
239  */
240 void
241 clock_wait(clock_time_t t)
242 {
243  clock_time_t end = clock_time() + t;
244  while(CLOCK_LT(clock_time(), end)) {
246  }
247 }
248 /*---------------------------------------------------------------------------*/
249 unsigned long
251 {
252  return clock_time() / CLOCK_SECOND;
253 }
254 /*---------------------------------------------------------------------------*/
255 clock_time_t
256 clock_arch_time_to_etimer(void)
257 {
258  clock_time_t time_to_etimer;
259  if(etimer_pending()) {
260  time_to_etimer = etimer_next_expiration_time() - clock_time();
261  if((int32_t)time_to_etimer < 0) {
262  time_to_etimer = 0;
263  }
264  } else {
265  /* no active etimers */
266  time_to_etimer = (clock_time_t)-1;
267  }
268  return time_to_etimer;
269 }
270 /*---------------------------------------------------------------------------*/
271 void
272 clock_arch_schedule_interrupt(clock_time_t time_to_etimer, rtimer_clock_t ticks_to_rtimer)
273 {
274  if(time_to_etimer > CLOCK_MAX_SCHEDULABLE_TICKS) {
275  time_to_etimer = CLOCK_MAX_SCHEDULABLE_TICKS;
276  }
277 
278  time_to_etimer *= CLOCK_INTERVAL;
279 
280  if(ticks_to_rtimer != (rtimer_clock_t)-1) {
281  /* if the next rtimer is close enough to the etimer... */
282  rtimer_clock_t ticks_to_etimer = time_to_etimer * (1 << CLOCK_PRESCALE);
283 
284 #if RTIMER_USE_32KHZ
285  ticks_to_rtimer = (uint64_t)ticks_to_rtimer * (F_CPU / 2) / RTIMER_SECOND;
286 #endif
287 
288  if(!CLOCK_LT(ticks_to_rtimer, ticks_to_etimer)
289  && CLOCK_LT(ticks_to_rtimer, ticks_to_etimer + CLOCK_RTIMER_GUARD_TIME)) {
290  /* ..then schedule the etimer after the rtimer */
291  time_to_etimer += 2;
292  }
293  }
294 
295  /* interrupt will not be generated if 0 is passed as the parameter */
296  if(time_to_etimer == 0) {
297  time_to_etimer = 1;
298  }
299 
300  vAHI_TimerStartSingleShot(CLOCK_TIMER, 0, time_to_etimer);
301 }
302 /*---------------------------------------------------------------------------*/
clock_time_t etimer_next_expiration_time(void)
Get next event timer expiration time.
Definition: etimer.c:237
void etimer_request_poll(void)
Make the event timer aware that the clock has changed.
Definition: etimer.c:145
int etimer_pending(void)
Check if there are any non-expired event timers.
Definition: etimer.c:231
clock_time_t clock_time(void)
Get the current clock time.
Definition: clock.c:41
Header file for the energy estimation mechanism
void clock_init()
Initialize the clock library.
Definition: clock.c:76
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:64
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
void clock_delay(unsigned int i)
Obsolete delay function but we implement it here since some code still uses it.
Definition: clock.c:170
Event timer header file.
void clock_wait(clock_time_t i)
Wait for a given number of ticks.
Definition: clock.c:162
int process_nevents(void)
Number of events waiting to be processed.
Definition: process.c:316
unsigned long clock_seconds(void)
Get the current value of the platform seconds.
Definition: clock.c:54
Header file for NXP jn516x-specific rtimer code
void clock_delay_usec(uint16_t usec)
Delay a given number of microseconds.
Definition: clock.c:94