Contiki 3.x
clock.c
1 /*
2  * Copyright (c) 2011, 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, last_ccr might be better? */
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 = TA1R;
59  t2 = TA1R;
60  } while(t1 != t2);
61  return t1;
62 }
63 /*---------------------------------------------------------------------------*/
64 ISR(TIMER1_A1, timera1)
65 {
66  ENERGEST_ON(ENERGEST_TYPE_IRQ);
67 
68  /* watchdog_start(); */
69 
70  if(TA1IV == 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(TA1CTL & MC1 && TA1CCR1 - TA1R == 1);
75 
76  last_tar = read_tar();
77  /* Make sure interrupt time is future */
78  while(!CLOCK_LT(last_tar, TA1CCR1)) {
79  TA1CCR1 += 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  TA1R = fclock;
128  TA1CCR1 = 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) (TA1R - 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 /* TA1CTL = TASSEL0 | TACLR | ID_1; */
158 
159 #if INTERVAL==32768/CLOCK_SECOND
160  TA1CTL = TASSEL0 | TACLR;
161 #elif INTERVAL==16384/CLOCK_SECOND
162  TA1CTL = TASSEL0 | TACLR | ID_1;
163 #else
164 #error NEED TO UPDATE clock.c to match interval!
165 #endif
166 
167  /* Initialize ccr1 to create the X ms interval. */
168  /* CCR1 interrupt enabled, interrupt occurs when timer equals CCR1. */
169  TA1CCTL1 = CCIE;
170 
171  /* Interrupt after X ms. */
172  TA1CCR1 = INTERVAL;
173 
174  /* Start Timer_A in continuous mode. */
175  TA1CTL |= MC1;
176 
177  count = 0;
178 
179  /* Enable interrupts. */
180  eint();
181 
182 }
183 /*---------------------------------------------------------------------------*/
184 /**
185  * Delay the CPU for a multiple of 2.83 us.
186  */
187 void
188 clock_delay(unsigned int i)
189 {
190  while(i--) {
191  _NOP();
192  }
193 }
194 /*---------------------------------------------------------------------------*/
195 /**
196  * Wait for a multiple of 10 ms.
197  *
198  */
199 void
200 clock_wait(clock_time_t i)
201 {
202  clock_time_t start;
203 
204  start = clock_time();
205  while(clock_time() - start < (clock_time_t)i);
206 }
207 /*---------------------------------------------------------------------------*/
208 void
209 clock_set_seconds(unsigned long sec)
210 {
211  int s;
212  s = splhigh();
213  seconds = sec;
214  splx(s);
215 }
216 /*---------------------------------------------------------------------------*/
217 unsigned long
219 {
220  unsigned long t1, t2;
221  do {
222  t1 = seconds;
223  t2 = seconds;
224  } while(t1 != t2);
225  return t1;
226 }
227 /*---------------------------------------------------------------------------*/
228 rtimer_clock_t
229 clock_counter(void)
230 {
231  rtimer_clock_t t1, t2;
232  do {
233  t1 = TA1R;
234  t2 = TA1R;
235  } while(t1 != t2);
236  return t1;
237 }
238 /*---------------------------------------------------------------------------*/
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
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