Contiki 3.x
clock.c
1 /*
2  * Copyright (c) 2005, Swedish Institute of Computer Science
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 #include "contiki.h"
33 #include "sys/energest.h"
34 #include "sys/clock.h"
35 #include "sys/etimer.h"
36 #include "rtimer-arch.h"
37 #include "dev/watchdog.h"
38 #include "isr_compat.h"
39 
40 #define INTERVAL (RTIMER_ARCH_SECOND / CLOCK_SECOND)
41 
42 #define MAX_TICKS (~((clock_time_t)0) / 2)
43 
44 #define CLOCK_LT(a, b) ((int16_t)((a)-(b)) < 0)
45 
46 static volatile unsigned long seconds;
47 
48 static volatile clock_time_t count = 0;
49 /* last_tar is used for calculating clock_fine */
50 static volatile uint16_t last_tar = 0;
51 /*---------------------------------------------------------------------------*/
52 static inline uint16_t
53 read_tar(void)
54 {
55  /* Same as clock_counter(), but can be inlined */
56  uint16_t t1, t2;
57  do {
58  t1 = TAR;
59  t2 = TAR;
60  } while(t1 != t2);
61  return t1;
62 }
63 /*---------------------------------------------------------------------------*/
64 ISR(TIMERA1, timera1)
65 {
66  ENERGEST_ON(ENERGEST_TYPE_IRQ);
67 
69 
70  if(TAIV == 2) {
71 
72  /* HW timer bug fix: Interrupt handler called before TR==CCR.
73  * Occurs when timer state is toggled between STOP and CONT. */
74  while(TACTL & MC1 && TACCR1 - read_tar() == 1);
75 
76  last_tar = read_tar();
77  /* Make sure interrupt time is future */
78  while(!CLOCK_LT(last_tar, TACCR1)) {
79  TACCR1 += INTERVAL;
80  ++count;
81 
82  /* Make sure the CLOCK_CONF_SECOND is a power of two, to ensure
83  that the modulo operation below becomes a logical and and not
84  an expensive divide. Algorithm from Wikipedia:
85  http://en.wikipedia.org/wiki/Power_of_two */
86 #if (CLOCK_CONF_SECOND & (CLOCK_CONF_SECOND - 1)) != 0
87 #error CLOCK_CONF_SECOND must be a power of two (i.e., 1, 2, 4, 8, 16, 32, 64, ...).
88 #error Change CLOCK_CONF_SECOND in contiki-conf.h.
89 #endif
90  if(count % CLOCK_CONF_SECOND == 0) {
91  ++seconds;
92  energest_flush();
93  }
94  last_tar = read_tar();
95  }
96 
97  if(etimer_pending() &&
98  (etimer_next_expiration_time() - count - 1) > MAX_TICKS) {
100  LPM4_EXIT;
101  }
102 
103  }
104  /* if(process_nevents() >= 0) {
105  LPM4_EXIT;
106  }*/
107 
108  watchdog_stop();
109 
110  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
111 }
112 /*---------------------------------------------------------------------------*/
113 clock_time_t
115 {
116  clock_time_t t1, t2;
117  do {
118  t1 = count;
119  t2 = count;
120  } while(t1 != t2);
121  return t1;
122 }
123 /*---------------------------------------------------------------------------*/
124 void
125 clock_set(clock_time_t clock, clock_time_t fclock)
126 {
127  TAR = fclock;
128  TACCR1 = fclock + INTERVAL;
129  count = clock;
130 }
131 /*---------------------------------------------------------------------------*/
132 int
134 {
135  return INTERVAL;
136 }
137 /*---------------------------------------------------------------------------*/
138 unsigned short
139 clock_fine(void)
140 {
141  unsigned short t;
142  /* Assign last_tar to local varible that can not be changed by interrupt */
143  t = last_tar;
144  /* perform calc based on t, TAR will not be changed during interrupt */
145  return (unsigned short) (TAR - t);
146 }
147 /*---------------------------------------------------------------------------*/
148 void
150 {
151  dint();
152 
153  /* Select SMCLK (2.4576MHz), clear TAR */
154  /* TACTL = TASSEL1 | TACLR | ID_3; */
155 
156  /* Select ACLK 32768Hz clock, divide by 2 */
157  /* TACTL = TASSEL0 | TACLR | ID_1;*/
158 
159  /* Select ACLK 32768Hz clock */
160  /* TACTL = TASSEL0 | TACLR; */
161 
162 #if INTERVAL==32768/CLOCK_SECOND
163  TACTL = TASSEL0 | TACLR;
164 #elif INTERVAL==16384/CLOCK_SECOND
165  TACTL = TASSEL0 | TACLR | ID_1;
166 #else
167 #error NEED TO UPDATE clock.c to match interval!
168 #endif
169 
170  /* Initialize ccr1 to create the X ms interval. */
171  /* CCR1 interrupt enabled, interrupt occurs when timer equals CCR. */
172  TACCTL1 = CCIE;
173 
174  /* Interrupt after X ms. */
175  TACCR1 = INTERVAL;
176 
177  /* Start Timer_A in continuous mode. */
178  TACTL |= MC1;
179 
180  count = 0;
181 
182  /* Enable interrupts. */
183  eint();
184 
185 }
186 /*---------------------------------------------------------------------------*/
187 /**
188  * Delay the CPU for a multiple of 2.83 us.
189  */
190 void
191 clock_delay(unsigned int i)
192 {
193  while(i--) {
194  _NOP();
195  }
196 }
197 /*---------------------------------------------------------------------------*/
198 /**
199  * Wait for a multiple of 10 ms.
200  *
201  */
202 void
203 clock_wait(clock_time_t i)
204 {
205  clock_time_t start;
206 
207  start = clock_time();
208  while(clock_time() - start < (clock_time_t)i);
209 }
210 /*---------------------------------------------------------------------------*/
211 void
212 clock_set_seconds(unsigned long sec)
213 {
214  int s;
215  s = splhigh();
216  seconds = sec;
217  splx(s);
218 }
219 /*---------------------------------------------------------------------------*/
220 unsigned long
222 {
223  unsigned long t1, t2;
224  do {
225  t1 = seconds;
226  t2 = seconds;
227  } while(t1 != t2);
228  return t1;
229 }
230 /*---------------------------------------------------------------------------*/
231 rtimer_clock_t
232 clock_counter(void)
233 {
234  rtimer_clock_t t1, t2;
235  do {
236  t1 = TAR;
237  t2 = TAR;
238  } while(t1 != t2);
239  return t1;
240 }
241 /*---------------------------------------------------------------------------*/
void watchdog_start(void)
Starts the WDT in watchdog mode if enabled by user configuration, maximum interval.
Definition: watchdog.c:49
void clock_init(void)
Initialize the clock library.
Definition: clock.c:76
Header file for the energy estimation mechanism
CCIF clock_time_t clock_time(void)
Get the current clock time.
Definition: clock.c:41
void watchdog_stop(void)
In watchdog mode, the WDT can not be stopped.
Definition: watchdog.c:58
clock_time_t etimer_next_expiration_time(void)
Get next event timer expiration time.
Definition: etimer.c:229
void etimer_request_poll(void)
Make the event timer aware that the clock has changed.
Definition: etimer.c:145
void clock_set_seconds(unsigned long sec)
Set the value of the platform seconds.
Definition: clock.c:49
CCIF unsigned long clock_seconds(void)
Get the current value of the platform seconds.
Definition: clock.c:57
int etimer_pending(void)
Check if there are any non-expired event timers.
Definition: etimer.c:223
void clock_delay(unsigned int delay)
Obsolete delay function but we implement it here since some code still uses it.
Definition: clock.c:60
void clock_wait(clock_time_t t)
Wait for a given number of ticks.
Definition: clock.c:166
Event timer header file.
int clock_fine_max(void)
Deprecated platform-specific routines.
Definition: clock.c:133