Contiki-Inga 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 static volatile unsigned long seconds;
45 
46 static volatile clock_time_t count = 0;
47 /* last_tar is used for calculating clock_fine, last_ccr might be better? */
48 static volatile uint16_t last_tar = 0;
49 /*---------------------------------------------------------------------------*/
50 ISR(TIMER1_A1, timera1)
51 {
52  ENERGEST_ON(ENERGEST_TYPE_IRQ);
53 
54  /* watchdog_start(); */
55 
56  if(TA1IV == 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(TA1CTL & MC1 && TA1CCR1 - TA1R == 1);
61 
62  /* Make sure interrupt time is future */
63  do {
64  TA1CCR1 += 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((TA1CCR1 - TA1R) > INTERVAL);
80 
81  last_tar = TA1R;
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  TA1R = fclock;
114  TA1CCR1 = 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) (TA1R - 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 /* TA1CTL = TASSEL0 | TACLR | ID_1; */
144 
145 #if INTERVAL==32768/CLOCK_SECOND
146  TA1CTL = TASSEL0 | TACLR;
147 #elif INTERVAL==16384/CLOCK_SECOND
148  TA1CTL = TASSEL0 | TACLR | ID_1;
149 #else
150 #error NEED TO UPDATE clock.c to match interval!
151 #endif
152 
153  /* Initialize ccr1 to create the X ms interval. */
154  /* CCR1 interrupt enabled, interrupt occurs when timer equals CCR1. */
155  TA1CCTL1 = CCIE;
156 
157  /* Interrupt after X ms. */
158  TA1CCR1 = INTERVAL;
159 
160  /* Start Timer_A in continuous mode. */
161  TA1CTL |= MC1;
162 
163  count = 0;
164 
165  /* Enable interrupts. */
166  eint();
167 
168 }
169 /*---------------------------------------------------------------------------*/
170 /**
171  * Delay the CPU for a multiple of 2.83 us.
172  */
173 void
174 clock_delay(unsigned int i)
175 {
176  while(i--) {
177  _NOP();
178  }
179 }
180 /*---------------------------------------------------------------------------*/
181 /**
182  * Wait for a multiple of 10 ms.
183  *
184  */
185 void
186 clock_wait(clock_time_t i)
187 {
188  clock_time_t start;
189 
190  start = clock_time();
191  while(clock_time() - start < (clock_time_t)i);
192 }
193 /*---------------------------------------------------------------------------*/
194 void
195 clock_set_seconds(unsigned long sec)
196 {
197  int s;
198  s = splhigh();
199  seconds = sec;
200  splx(s);
201 }
202 /*---------------------------------------------------------------------------*/
203 unsigned long
205 {
206  unsigned long t1, t2;
207  do {
208  t1 = seconds;
209  t2 = seconds;
210  } while(t1 != t2);
211  return t1;
212 }
213 /*---------------------------------------------------------------------------*/
214 rtimer_clock_t
215 clock_counter(void)
216 {
217  rtimer_clock_t t1, t2;
218  do {
219  t1 = TA1R;
220  t2 = TA1R;
221  } while(t1 != t2);
222  return t1;
223 }
224 /*---------------------------------------------------------------------------*/