Add missing includes
[contiki-2.x.git] / cpu / avr / dev / clock.c
blobddab18f2e32f9fcb8a7cff4e231530befdacbb4b
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 static volatile clock_time_t count;
10 static volatile uint8_t scount;
11 volatile unsigned long seconds;
13 /* Set RADIOSTATS to monitor radio on time (must also be set in the radio driver) */
14 #if RF230BB && WEBSERVER
15 #define RADIOSTATS 1
16 #endif
17 #if RADIOSTATS
18 static volatile uint8_t rcount;
19 volatile unsigned long radioontime;
20 extern uint8_t RF230_receive_on;
21 #endif
24 CLOCK_SECOND is the number of ticks per second.
25 It is defined through CONF_CLOCK_SECOND in the contiki-conf.h for each platform.
26 The usual AVR default is ~125 ticks per second, counting a prescaler the CPU clock
27 using the 8 bit timer0.
29 As clock_time_t is an unsigned 16 bit data type, intervals up to 524 seconds
30 can be measured with 8 millisecond precision.
31 For longer intervals a 32 bit global is incremented every second.
33 clock-avr.h contains the specific setup code for each mcu.
35 /*---------------------------------------------------------------------------*/
36 //SIGNAL(SIG_OUTPUT_COMPARE0)
37 ISR(AVR_OUTPUT_COMPARE_INT)
39 count++;
40 if(++scount == CLOCK_SECOND) {
41 scount = 0;
42 seconds++;
44 #if RADIOSTATS
45 if (RF230_receive_on) {
46 if (++rcount == CLOCK_SECOND) {
47 rcount=0;
48 radioontime++;
51 #endif
52 if(etimer_pending()) {
53 etimer_request_poll();
57 /*---------------------------------------------------------------------------*/
58 void
59 clock_init(void)
61 cli ();
62 OCRSetup();
63 //scount = count = 0;
64 sei ();
67 /*---------------------------------------------------------------------------*/
68 clock_time_t
69 clock_time(void)
71 clock_time_t tmp;
72 do {
73 tmp = count;
74 } while(tmp != count);
75 return tmp;
77 /*---------------------------------------------------------------------------*/
78 /**
79 * Delay the CPU for a multiple of TODO
81 void
82 clock_delay(unsigned int i)
84 for (; i > 0; i--) { /* Needs fixing XXX */
85 unsigned j;
86 for (j = 50; j > 0; j--)
87 asm volatile("nop");
91 /*---------------------------------------------------------------------------*/
92 /**
93 * Wait for a multiple of 1 / 125 sec = 0.008 ms.
96 void
97 clock_wait(int i)
99 clock_time_t start;
101 start = clock_time();
102 while(clock_time() - start < (clock_time_t)i);
104 /*---------------------------------------------------------------------------*/
105 void
106 clock_set_seconds(unsigned long sec)
108 // TODO
111 unsigned long
112 clock_seconds(void)
114 unsigned long tmp;
115 do {
116 tmp = seconds;
117 } while(tmp != seconds);
118 return tmp;