Contiki 2.5
clock.c
1 
2 #include "sys/clock.h"
3 #include "dev/clock-avr.h"
4 #include "sys/etimer.h"
5 
6 #include <avr/io.h>
7 #include <avr/interrupt.h>
8 
9 /*
10  CLOCK_SECOND is the number of ticks per second.
11  It is defined through CONF_CLOCK_SECOND in the contiki-conf.h for each platform.
12  The usual AVR defaults are 128 or 125 ticks per second, counting a prescaled CPU clock
13  using the 8 bit timer0.
14 
15  As clock_time_t is an unsigned 16 bit data type, intervals up to 512 or 524 seconds
16  can be measured with ~8 millisecond precision.
17  For longer intervals a 32 bit global is incremented every second.
18 
19  clock-avr.h contains the specific setup code for each mcu.
20 
21 */
22 
23 /* count is a 16 bit tick counter that wraps every ~10 minutes, returned by clock_time() */
24 static volatile clock_time_t count;
25 /* scount is the 8 bit counter that counts ticks modulo CLOCK_SECONDS */
26 static volatile uint8_t scount;
27 /* seconds is the number of seconds since startup, returned by clock_seconds() */
28 volatile unsigned long seconds;
29 /* sleepseconds is the number of seconds sleeping since startup, available globally */
30 long sleepseconds;
31 
32 /* Set RADIOSTATS to monitor radio on time (must also be set in the radio driver) */
33 #if RF230BB && AVR_WEBSERVER
34 #define RADIOSTATS 1
35 #endif
36 
37 #if RADIOSTATS
38 static volatile uint8_t rcount;
39 volatile unsigned long radioontime;
40 extern uint8_t RF230_receive_on;
41 #endif
42 
43 /* Set RADIO_CONF_CALIBRATE_INTERVAL for periodic calibration of the PLL during extended radio on time.
44  * The RF230 data sheet suggests every 5 minutes if the temperature is fluctuating.
45  * At present the specified interval is ignored, and an 8 bit counter gives 256 second intervals.
46  * Actual calibration is done by the driver on the next transmit request.
47  */
48 #if RADIO_CONF_CALIBRATE_INTERVAL
49 extern volatile uint8_t rf230_calibrate;
50 static uint8_t calibrate_interval;
51 #endif
52 
53 #if 0
54 /*---------------------------------------------------------------------------*/
55 /* This routine can be called to add seconds to the clock after a sleep
56  * of an integral number of seconds.
57  */
58 void clock_adjust_seconds(uint8_t howmany) {
59  seconds += howmany;
60  sleepseconds +=howmany;
61  count += howmany * CLOCK_SECOND;
62 #if RADIOSTATS
63  if (RF230_receive_on) radioontime += howmany;
64 #endif
65 }
66 #endif
67 
68 /*---------------------------------------------------------------------------*/
69 /* This routine can be called to add ticks to the clock after a sleep.
70  */
71 void clock_adjust_ticks(uint16_t howmany) {
72  count += howmany;
73  scount += howmany;
74  while(scount >= CLOCK_SECOND) {
75  scount -= CLOCK_SECOND;
76  seconds++;
77  sleepseconds++;
78 #if RADIOSTATS
79  if (RF230_receive_on) radioontime += 1;
80 #endif
81  }
82 }
83 /*---------------------------------------------------------------------------*/
84 //SIGNAL(SIG_OUTPUT_COMPARE0)
85 ISR(AVR_OUTPUT_COMPARE_INT)
86 {
87  count++;
88  if(++scount == CLOCK_SECOND) {
89  scount = 0;
90  seconds++;
91  }
92 #if RADIO_CONF_CALIBRATE_INTERVAL
93  if (++calibrate_interval==0) {
94  rf230_calibrate=1;
95  }
96 #endif
97 #if RADIOSTATS
98  if (RF230_receive_on) {
99  if (++rcount == CLOCK_SECOND) {
100  rcount=0;
101  radioontime++;
102  }
103  }
104 #endif
105 
106 #if 1
107 /* gcc will save all registers on the stack if an external routine is called */
108  if(etimer_pending()) {
110  }
111 #else
112 /* doing this locally saves 9 pushes and 9 pops, but these etimer.c and process.c variables have to lose the static qualifier */
113  extern struct etimer *timerlist;
114  extern volatile unsigned char poll_requested;
115 
116 #define PROCESS_STATE_NONE 0
117 #define PROCESS_STATE_RUNNING 1
118 #define PROCESS_STATE_CALLED 2
119 
120  if (timerlist) {
121  if(etimer_process.state == PROCESS_STATE_RUNNING ||
122  etimer_process.state == PROCESS_STATE_CALLED) {
123  etimer_process.needspoll = 1;
124  poll_requested = 1;
125  }
126  }
127 #endif
128 }
129 
130 /*---------------------------------------------------------------------------*/
131 void
133 {
134  cli ();
135  OCRSetup();
136 //scount = count = 0;
137  sei ();
138 }
139 
140 /*---------------------------------------------------------------------------*/
141 unsigned short
142 clock_fine(void)
143 {
144  return AVR_CLOCK_COUNTER;
145 }
146 
147 /*---------------------------------------------------------------------------*/
148 int
149 clock_fine_max(void)
150 {
151  return AVR_CLOCK_MAX;
152 }
153 
154 /*---------------------------------------------------------------------------*/
155 clock_time_t
157 {
158  clock_time_t tmp;
159  do {
160  tmp = count;
161  } while(tmp != count);
162  return tmp;
163 }
164 
165 /*---------------------------------------------------------------------------*/
166 /**
167  * Delay the CPU for a multiple of TODO
168  */
169 void
170 clock_delay(unsigned int i)
171 {
172  for (; i > 0; i--) { /* Needs fixing XXX */
173  unsigned j;
174  for (j = 5; j > 0; j--)
175  asm volatile("nop");
176  }
177 }
178 #if 0
179 
180 /*---------------------------------------------------------------------------*/
181 /**
182  * Wait for a number of clock ticks.
183  *
184  */
185 void
186 clock_wait(int 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  seconds = sec;
198 }
199 #endif
200 
201 unsigned long
202 clock_seconds(void)
203 {
204  unsigned long tmp;
205  do {
206  tmp = seconds;
207  } while(tmp != seconds);
208  return tmp;
209 }
210 
211 #ifdef HANG_ON_UNKNOWN_INTERRUPT
212 /* Useful for diagnosing unknown interrupts that reset the mcu.
213  * Currently set up for 12mega128rfa1.
214  * For other mcus, enable all and then disable the conflicts.
215  */
216 static volatile uint8_t x;
217 ISR( _VECTOR(0)) {while (1) x++;}
218 ISR( _VECTOR(1)) {while (1) x++;}
219 ISR( _VECTOR(2)) {while (1) x++;}
220 ISR( _VECTOR(3)) {while (1) x++;}
221 ISR( _VECTOR(4)) {while (1) x++;}
222 ISR( _VECTOR(5)) {while (1) x++;}
223 ISR( _VECTOR(6)) {while (1) x++;}
224 ISR( _VECTOR(7)) {while (1) x++;}
225 ISR( _VECTOR(8)) {while (1) x++;}
226 ISR( _VECTOR(9)) {while (1) x++;}
227 ISR( _VECTOR(10)) {while (1) x++;}
228 ISR( _VECTOR(11)) {while (1) x++;}
229 ISR( _VECTOR(12)) {while (1) x++;}
230 ISR( _VECTOR(13)) {while (1) x++;}
231 ISR( _VECTOR(14)) {while (1) x++;}
232 ISR( _VECTOR(15)) {while (1) x++;}
233 ISR( _VECTOR(16)) {while (1) x++;}
234 ISR( _VECTOR(17)) {while (1) x++;}
235 ISR( _VECTOR(18)) {while (1) x++;}
236 ISR( _VECTOR(19)) {while (1) x++;}
237 //ISR( _VECTOR(20)) {while (1) x++;}
238 //ISR( _VECTOR(21)) {while (1) x++;}
239 ISR( _VECTOR(22)) {while (1) x++;}
240 ISR( _VECTOR(23)) {while (1) x++;}
241 ISR( _VECTOR(24)) {while (1) x++;}
242 //ISR( _VECTOR(25)) {while (1) x++;}
243 ISR( _VECTOR(26)) {while (1) x++;}
244 //ISR( _VECTOR(27)) {while (1) x++;}
245 ISR( _VECTOR(28)) {while (1) x++;}
246 ISR( _VECTOR(29)) {while (1) x++;}
247 ISR( _VECTOR(30)) {while (1) x++;}
248 ISR( _VECTOR(31)) {while (1) x++;}
249 //ISR( _VECTOR(32)) {while (1) x++;}
250 ISR( _VECTOR(33)) {while (1) x++;}
251 ISR( _VECTOR(34)) {while (1) x++;}
252 ISR( _VECTOR(35)) {while (1) x++;}
253 //ISR( _VECTOR(36)) {while (1) x++;}
254 ISR( _VECTOR(37)) {while (1) x++;}
255 //ISR( _VECTOR(38)) {while (1) x++;}
256 ISR( _VECTOR(39)) {while (1) x++;}
257 ISR( _VECTOR(40)) {while (1) x++;}
258 ISR( _VECTOR(41)) {while (1) x++;}
259 ISR( _VECTOR(42)) {while (1) x++;}
260 ISR( _VECTOR(43)) {while (1) x++;}
261 ISR( _VECTOR(44)) {while (1) x++;}
262 ISR( _VECTOR(45)) {while (1) x++;}
263 ISR( _VECTOR(46)) {while (1) x++;}
264 ISR( _VECTOR(47)) {while (1) x++;}
265 ISR( _VECTOR(48)) {while (1) x++;}
266 ISR( _VECTOR(49)) {while (1) x++;}
267 ISR( _VECTOR(50)) {while (1) x++;}
268 ISR( _VECTOR(51)) {while (1) x++;}
269 ISR( _VECTOR(52)) {while (1) x++;}
270 ISR( _VECTOR(53)) {while (1) x++;}
271 ISR( _VECTOR(54)) {while (1) x++;}
272 ISR( _VECTOR(55)) {while (1) x++;}
273 ISR( _VECTOR(56)) {while (1) x++;}
274 //ISR( _VECTOR(57)) {while (1) x++;}
275 //ISR( _VECTOR(58)) {while (1) x++;}
276 //ISR( _VECTOR(59)) {while (1) x++;}
277 //ISR( _VECTOR(60)) {while (1) x++;}
278 ISR( _VECTOR(61)) {while (1) x++;}
279 ISR( _VECTOR(62)) {while (1) x++;}
280 ISR( _VECTOR(63)) {while (1) x++;}
281 ISR( _VECTOR(64)) {while (1) x++;}
282 ISR( _VECTOR(65)) {while (1) x++;}
283 ISR( _VECTOR(66)) {while (1) x++;}
284 ISR( _VECTOR(67)) {while (1) x++;}
285 ISR( _VECTOR(68)) {while (1) x++;}
286 ISR( _VECTOR(69)) {while (1) x++;}
287 ISR( _VECTOR(70)) {while (1) x++;}
288 ISR( _VECTOR(71)) {while (1) x++;}
289 ISR( _VECTOR(72)) {while (1) x++;}
290 ISR( _VECTOR(73)) {while (1) x++;}
291 ISR( _VECTOR(74)) {while (1) x++;}
292 ISR( _VECTOR(75)) {while (1) x++;}
293 ISR( _VECTOR(76)) {while (1) x++;}
294 ISR( _VECTOR(77)) {while (1) x++;}
295 ISR( _VECTOR(78)) {while (1) x++;}
296 ISR( _VECTOR(79)) {while (1) x++;}
297 #endif