Contiki-Inga 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 static volatile unsigned long seconds;
45 
46 static volatile clock_time_t count = 0;
47 /* last_tar is used for calculating clock_fine */
48 static volatile uint16_t last_tar = 0;
49 /*---------------------------------------------------------------------------*/
50 ISR(TIMERA1, timera1)
51 {
52  ENERGEST_ON(ENERGEST_TYPE_IRQ);
53 
55 
56  if(TAIV == 2) {
57 
58  /* HW timer bug fix: Interrupt handler called before TR==CCR.
59  * Occurs when timer state is toggled between STOP and CONT. */
60  while(TACTL & MC1 && TACCR1 - TAR == 1);
61 
62  /* Make sure interrupt time is future */
63  do {
64  TACCR1 += INTERVAL;
65  ++count;
66 
67  /* Make sure the CLOCK_CONF_SECOND is a power of two, to ensure
68  that the modulo operation below becomes a logical and and not
69  an expensive divide. Algorithm from Wikipedia:
70  http://en.wikipedia.org/wiki/Power_of_two */
71 #if (CLOCK_CONF_SECOND & (CLOCK_CONF_SECOND - 1)) != 0
72 #error CLOCK_CONF_SECOND must be a power of two (i.e., 1, 2, 4, 8, 16, 32, 64, ...).
73 #error Change CLOCK_CONF_SECOND in contiki-conf.h.
74 #endif
75  if(count % CLOCK_CONF_SECOND == 0) {
76  ++seconds;
77  energest_flush();
78  }
79  } while((TACCR1 - TAR) > INTERVAL);
80 
81  last_tar = TAR;
82 
83  if(etimer_pending() &&
84  (etimer_next_expiration_time() - count - 1) > MAX_TICKS) {
86  LPM4_EXIT;
87  }
88 
89  }
90  /* if(process_nevents() >= 0) {
91  LPM4_EXIT;
92  }*/
93 
94  watchdog_stop();
95 
96  ENERGEST_OFF(ENERGEST_TYPE_IRQ);
97 }
98 /*---------------------------------------------------------------------------*/
99 clock_time_t
101 {
102  clock_time_t t1, t2;
103  do {
104  t1 = count;
105  t2 = count;
106  } while(t1 != t2);
107  return t1;
108 }
109 /*---------------------------------------------------------------------------*/
110 void
111 clock_set(clock_time_t clock, clock_time_t fclock)
112 {
113  TAR = fclock;
114  TACCR1 = fclock + INTERVAL;
115  count = clock;
116 }
117 /*---------------------------------------------------------------------------*/
118 int
120 {
121  return INTERVAL;
122 }
123 /*---------------------------------------------------------------------------*/
124 unsigned short
125 clock_fine(void)
126 {
127  unsigned short t;
128  /* Assign last_tar to local varible that can not be changed by interrupt */
129  t = last_tar;
130  /* perform calc based on t, TAR will not be changed during interrupt */
131  return (unsigned short) (TAR - t);
132 }
133 /*---------------------------------------------------------------------------*/
134 void
136 {
137  dint();
138 
139  /* Select SMCLK (2.4576MHz), clear TAR */
140  /* TACTL = TASSEL1 | TACLR | ID_3; */
141 
142  /* Select ACLK 32768Hz clock, divide by 2 */
143  /* TACTL = TASSEL0 | TACLR | ID_1;*/
144 
145  /* Select ACLK 32768Hz clock */
146  /* TACTL = TASSEL0 | TACLR; */
147 
148 #if INTERVAL==32768/CLOCK_SECOND
149  TACTL = TASSEL0 | TACLR;
150 #elif INTERVAL==16384/CLOCK_SECOND
151  TACTL = TASSEL0 | TACLR | ID_1;
152 #else
153 #error NEED TO UPDATE clock.c to match interval!
154 #endif
155 
156  /* Initialize ccr1 to create the X ms interval. */
157  /* CCR1 interrupt enabled, interrupt occurs when timer equals CCR. */
158  TACCTL1 = CCIE;
159 
160  /* Interrupt after X ms. */
161  TACCR1 = INTERVAL;
162 
163  /* Start Timer_A in continuous mode. */
164  TACTL |= MC1;
165 
166  count = 0;
167 
168  /* Enable interrupts. */
169  eint();
170 
171 }
172 /*---------------------------------------------------------------------------*/
173 /**
174  * Delay the CPU for a multiple of 2.83 us.
175  */
176 void
177 clock_delay(unsigned int i)
178 {
179  while(i--) {
180  _NOP();
181  }
182 }
183 /*---------------------------------------------------------------------------*/
184 /**
185  * Wait for a multiple of 10 ms.
186  *
187  */
188 void
189 clock_wait(clock_time_t i)
190 {
191  clock_time_t start;
192 
193  start = clock_time();
194  while(clock_time() - start < (clock_time_t)i);
195 }
196 /*---------------------------------------------------------------------------*/
197 void
198 clock_set_seconds(unsigned long sec)
199 {
200  int s;
201  s = splhigh();
202  seconds = sec;
203  splx(s);
204 }
205 /*---------------------------------------------------------------------------*/
206 unsigned long
208 {
209  unsigned long t1, t2;
210  do {
211  t1 = seconds;
212  t2 = seconds;
213  } while(t1 != t2);
214  return t1;
215 }
216 /*---------------------------------------------------------------------------*/
217 rtimer_clock_t
218 clock_counter(void)
219 {
220  rtimer_clock_t t1, t2;
221  do {
222  t1 = TAR;
223  t2 = TAR;
224  } while(t1 != t2);
225  return t1;
226 }
227 /*---------------------------------------------------------------------------*/