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