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