Enable radio stats in sensor cgi as default
[contiki-2.x.git] / cpu / avr / dev / clock.c
blobf1bad63f4908dbbb473b6773c0fb27ee6dce66f3
2 #include "sys/clock.h"
3 #include "dev/clock-avr.h"
4 #include "sys/etimer.h"
6 #include <avr/io.h>
7 #include <avr/interrupt.h>
9 /* RADIOSTATS should be also be defined in the radio driver and webserver */
10 #if RF230BB && WEBSERVER
11 #define RADIOSTATS 1
12 #endif
13 #if RADIOSTATS
14 static volatile clock_time_t count, scount, rcount;
15 volatile unsigned long seconds,radioontime;
16 extern uint8_t RF230_receive_on;
17 #else
18 static volatile clock_time_t count, scount;
19 volatile unsigned long seconds;
20 #endif
22 /*---------------------------------------------------------------------------*/
23 //SIGNAL(SIG_OUTPUT_COMPARE0)
24 ISR(AVR_OUTPUT_COMPARE_INT)
26 count++;
27 if(++scount == CLOCK_SECOND) {
28 scount = 0;
29 seconds++;
31 #if RADIOSTATS
32 if (RF230_receive_on) {
33 if (++rcount == CLOCK_SECOND) {
34 rcount=0;
35 radioontime++;
38 #endif
39 if(etimer_pending()) {
40 etimer_request_poll();
44 /* External clock source does not work ? */
45 #if 0
46 void
47 clock_init(void)
49 cli ();
50 TIMSK &= ((unsigned char)~(1 << (TOIE0)));
51 TIMSK &= ((unsigned char)~(1 << (OCIE0)));
52 /* Disable TC0 interrupt */
54 /**
55 * set Timer/Counter0 to be asynchronous
56 * from the CPU clock with a second external
57 * clock(32,768kHz)driving it
59 ASSR |= (1 << (AS0));
60 TCCR0 = _BV (CS02) | _BV (CS01) | _BV (WGM1);
62 TCNT0 = 0;
63 OCR0 = 128;
65 TIMSK |= (1 << (OCIE0));
66 TIMSK |= (1 << (TOIE0));
67 sei ();
69 #endif
71 /*---------------------------------------------------------------------------*/
72 void
73 clock_init(void)
75 cli ();
78 OCRSetup();
81 * Counts the number of ticks. Since clock_time_t is an unsigned
82 * 16 bit data type, time intervals of up to 524 seconds can be
83 * measured.
85 scount = count = 0;
87 sei ();
90 /*---------------------------------------------------------------------------*/
91 clock_time_t
92 clock_time(void)
94 clock_time_t tmp;
95 do {
96 tmp = count;
97 } while(tmp != count);
98 return tmp;
100 /*---------------------------------------------------------------------------*/
102 * Delay the CPU for a multiple of TODO
104 void
105 clock_delay(unsigned int i)
107 for (; i > 0; i--) { /* Needs fixing XXX */
108 unsigned j;
109 for (j = 50; j > 0; j--)
110 asm volatile("nop");
114 /*---------------------------------------------------------------------------*/
116 * Wait for a multiple of 1 / 125 sec = 0.008 ms.
119 void
120 clock_wait(int i)
122 clock_time_t start;
124 start = clock_time();
125 while(clock_time() - start < (clock_time_t)i);
127 /*---------------------------------------------------------------------------*/
128 void
129 clock_set_seconds(unsigned long sec)
131 // TODO
134 unsigned long
135 clock_seconds(void)
137 unsigned long tmp;
138 do {
139 tmp = seconds;
140 } while(tmp != seconds);
141 return tmp;