2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
#include "sys/clock.h"
|
2008-10-14 19:06:51 +00:00
|
|
|
#include "dev/clock-avr.h"
|
2006-06-17 22:41:10 +00:00
|
|
|
#include "sys/etimer.h"
|
|
|
|
|
|
|
|
#include <avr/io.h>
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
|
2010-06-18 17:30:30 +00:00
|
|
|
static volatile clock_time_t count;
|
|
|
|
static volatile uint8_t scount;
|
|
|
|
volatile unsigned long seconds;
|
|
|
|
|
|
|
|
/* Set RADIOSTATS to monitor radio on time (must also be set in the radio driver) */
|
2010-02-26 21:38:57 +00:00
|
|
|
#if RF230BB && WEBSERVER
|
|
|
|
#define RADIOSTATS 1
|
|
|
|
#endif
|
2010-02-12 14:37:50 +00:00
|
|
|
#if RADIOSTATS
|
2010-06-18 17:30:30 +00:00
|
|
|
static volatile uint8_t rcount;
|
|
|
|
volatile unsigned long radioontime;
|
2010-02-26 21:38:57 +00:00
|
|
|
extern uint8_t RF230_receive_on;
|
2010-02-12 14:37:50 +00:00
|
|
|
#endif
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2010-06-18 17:30:30 +00:00
|
|
|
/*
|
|
|
|
CLOCK_SECOND is the number of ticks per second.
|
|
|
|
It is defined through CONF_CLOCK_SECOND in the contiki-conf.h for each platform.
|
|
|
|
The usual AVR default is ~125 ticks per second, counting a prescaler the CPU clock
|
|
|
|
using the 8 bit timer0.
|
|
|
|
|
|
|
|
As clock_time_t is an unsigned 16 bit data type, intervals up to 524 seconds
|
|
|
|
can be measured with 8 millisecond precision.
|
|
|
|
For longer intervals a 32 bit global is incremented every second.
|
|
|
|
|
|
|
|
clock-avr.h contains the specific setup code for each mcu.
|
|
|
|
*/
|
2006-06-17 22:41:10 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-10-14 19:06:51 +00:00
|
|
|
//SIGNAL(SIG_OUTPUT_COMPARE0)
|
|
|
|
ISR(AVR_OUTPUT_COMPARE_INT)
|
2006-06-17 22:41:10 +00:00
|
|
|
{
|
2009-04-06 13:08:42 +00:00
|
|
|
count++;
|
|
|
|
if(++scount == CLOCK_SECOND) {
|
|
|
|
scount = 0;
|
|
|
|
seconds++;
|
|
|
|
}
|
2010-02-12 14:37:50 +00:00
|
|
|
#if RADIOSTATS
|
2010-02-26 21:38:57 +00:00
|
|
|
if (RF230_receive_on) {
|
2010-02-12 14:37:50 +00:00
|
|
|
if (++rcount == CLOCK_SECOND) {
|
|
|
|
rcount=0;
|
|
|
|
radioontime++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2006-06-17 22:41:10 +00:00
|
|
|
if(etimer_pending()) {
|
|
|
|
etimer_request_poll();
|
|
|
|
}
|
|
|
|
}
|
2006-12-22 17:04:38 +00:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
clock_init(void)
|
|
|
|
{
|
|
|
|
cli ();
|
2008-10-14 19:06:51 +00:00
|
|
|
OCRSetup();
|
2010-06-18 17:30:30 +00:00
|
|
|
//scount = count = 0;
|
2006-12-22 17:04:38 +00:00
|
|
|
sei ();
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
2006-12-22 17:04:38 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
clock_time_t
|
|
|
|
clock_time(void)
|
|
|
|
{
|
2009-04-06 13:08:42 +00:00
|
|
|
clock_time_t tmp;
|
|
|
|
do {
|
|
|
|
tmp = count;
|
|
|
|
} while(tmp != count);
|
|
|
|
return tmp;
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-12-22 17:04:38 +00:00
|
|
|
/**
|
|
|
|
* Delay the CPU for a multiple of TODO
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
clock_delay(unsigned int i)
|
|
|
|
{
|
2007-01-24 16:28:51 +00:00
|
|
|
for (; i > 0; i--) { /* Needs fixing XXX */
|
|
|
|
unsigned j;
|
|
|
|
for (j = 50; j > 0; j--)
|
|
|
|
asm volatile("nop");
|
|
|
|
}
|
2006-12-22 17:04:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* Wait for a multiple of 1 / 125 sec = 0.008 ms.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
clock_wait(int i)
|
|
|
|
{
|
|
|
|
clock_time_t start;
|
|
|
|
|
|
|
|
start = clock_time();
|
|
|
|
while(clock_time() - start < (clock_time_t)i);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
clock_set_seconds(unsigned long sec)
|
|
|
|
{
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long
|
|
|
|
clock_seconds(void)
|
|
|
|
{
|
2009-04-06 13:08:42 +00:00
|
|
|
unsigned long tmp;
|
|
|
|
do {
|
|
|
|
tmp = seconds;
|
|
|
|
} while(tmp != seconds);
|
|
|
|
return tmp;
|
2006-12-22 17:04:38 +00:00
|
|
|
}
|