2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
#include "sys/clock.h"
|
2008-10-14 21:06:51 +02:00
|
|
|
#include "dev/clock-avr.h"
|
2006-06-18 00:41:10 +02:00
|
|
|
#include "sys/etimer.h"
|
|
|
|
|
|
|
|
#include <avr/io.h>
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
|
2010-02-12 15:37:50 +01:00
|
|
|
/* Experimental: RADIOSTATS should be also be defined in the radio driver and webserver */
|
|
|
|
#define RADIOSTATS 0
|
|
|
|
#if RADIOSTATS
|
|
|
|
static volatile clock_time_t count, scount, rcount;
|
|
|
|
volatile unsigned long seconds,radioontime;
|
|
|
|
extern uint8_t RF230_radio_on;
|
|
|
|
#else
|
2009-04-06 15:08:42 +02:00
|
|
|
static volatile clock_time_t count, scount;
|
2010-02-12 15:37:50 +01:00
|
|
|
volatile unsigned long seconds;
|
|
|
|
#endif
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-10-14 21:06:51 +02:00
|
|
|
//SIGNAL(SIG_OUTPUT_COMPARE0)
|
|
|
|
ISR(AVR_OUTPUT_COMPARE_INT)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
2009-04-06 15:08:42 +02:00
|
|
|
count++;
|
|
|
|
if(++scount == CLOCK_SECOND) {
|
|
|
|
scount = 0;
|
|
|
|
seconds++;
|
|
|
|
}
|
2010-02-12 15:37:50 +01:00
|
|
|
#if RADIOSTATS
|
|
|
|
if (RF230_radio_on) {
|
|
|
|
if (++rcount == CLOCK_SECOND) {
|
|
|
|
rcount=0;
|
|
|
|
radioontime++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2006-06-18 00:41:10 +02:00
|
|
|
if(etimer_pending()) {
|
|
|
|
etimer_request_poll();
|
|
|
|
}
|
|
|
|
}
|
2006-12-22 18:04:38 +01:00
|
|
|
|
|
|
|
/* External clock source does not work ? */
|
|
|
|
#if 0
|
2006-06-18 00:41:10 +02:00
|
|
|
void
|
|
|
|
clock_init(void)
|
|
|
|
{
|
2006-12-22 18:04:38 +01:00
|
|
|
cli ();
|
|
|
|
TIMSK &= ((unsigned char)~(1 << (TOIE0)));
|
|
|
|
TIMSK &= ((unsigned char)~(1 << (OCIE0)));
|
|
|
|
/* Disable TC0 interrupt */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set Timer/Counter0 to be asynchronous
|
|
|
|
* from the CPU clock with a second external
|
|
|
|
* clock(32,768kHz)driving it
|
|
|
|
*/
|
|
|
|
ASSR |= (1 << (AS0));
|
|
|
|
TCCR0 = _BV (CS02) | _BV (CS01) | _BV (WGM1);
|
|
|
|
|
|
|
|
TCNT0 = 0;
|
|
|
|
OCR0 = 128;
|
|
|
|
|
|
|
|
TIMSK |= (1 << (OCIE0));
|
|
|
|
TIMSK |= (1 << (TOIE0));
|
|
|
|
sei ();
|
|
|
|
}
|
2006-06-18 00:41:10 +02:00
|
|
|
#endif
|
|
|
|
|
2006-12-22 18:04:38 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
clock_init(void)
|
|
|
|
{
|
|
|
|
cli ();
|
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2008-10-14 21:06:51 +02:00
|
|
|
OCRSetup();
|
2006-12-22 18:04:38 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Counts the number of ticks. Since clock_time_t is an unsigned
|
|
|
|
* 16 bit data type, time intervals of up to 524 seconds can be
|
|
|
|
* measured.
|
|
|
|
*/
|
2009-04-06 15:08:42 +02:00
|
|
|
scount = count = 0;
|
2006-12-22 18:04:38 +01:00
|
|
|
|
|
|
|
sei ();
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2006-12-22 18:04:38 +01:00
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
clock_time_t
|
|
|
|
clock_time(void)
|
|
|
|
{
|
2009-04-06 15:08:42 +02:00
|
|
|
clock_time_t tmp;
|
|
|
|
do {
|
|
|
|
tmp = count;
|
|
|
|
} while(tmp != count);
|
|
|
|
return tmp;
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-12-22 18:04:38 +01:00
|
|
|
/**
|
|
|
|
* Delay the CPU for a multiple of TODO
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
clock_delay(unsigned int i)
|
|
|
|
{
|
2007-01-24 17:28:51 +01:00
|
|
|
for (; i > 0; i--) { /* Needs fixing XXX */
|
|
|
|
unsigned j;
|
|
|
|
for (j = 50; j > 0; j--)
|
|
|
|
asm volatile("nop");
|
|
|
|
}
|
2006-12-22 18:04:38 +01: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 15:08:42 +02:00
|
|
|
unsigned long tmp;
|
|
|
|
do {
|
|
|
|
tmp = seconds;
|
|
|
|
} while(tmp != seconds);
|
|
|
|
return tmp;
|
2006-12-22 18:04:38 +01:00
|
|
|
}
|