Adjust sleep time by ticks instead of seconds, proposed by Ivan Delamer

This commit is contained in:
David Kopf 2011-09-10 12:44:49 -04:00
parent ab0c04b041
commit f721f646fa
3 changed files with 46 additions and 20 deletions

View File

@ -6,9 +6,27 @@
#include <avr/io.h>
#include <avr/interrupt.h>
/*
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 defaults are 128 or 125 ticks per second, counting a prescaled CPU clock
using the 8 bit timer0.
As clock_time_t is an unsigned 16 bit data type, intervals up to 512 or 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.
*/
/* count is a 16 bit tick counter that wraps every ~10 minutes, returned by clock_time() */
static volatile clock_time_t count;
/* scount is the 8 bit counter that counts ticks modulo CLOCK_SECONDS */
static volatile uint8_t scount;
/* seconds is the number of seconds since startup, returned by clock_seconds() */
volatile unsigned long seconds;
/* sleepseconds is the number of seconds sleeping since startup, available globally */
long sleepseconds;
/* Set RADIOSTATS to monitor radio on time (must also be set in the radio driver) */
@ -32,19 +50,7 @@ extern volatile uint8_t rf230_calibrate;
static uint8_t calibrate_interval;
#endif
/*
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.
*/
#if 0
/*---------------------------------------------------------------------------*/
/* This routine can be called to add seconds to the clock after a sleep
* of an integral number of seconds.
@ -52,10 +58,28 @@ static uint8_t calibrate_interval;
void clock_adjust_seconds(uint8_t howmany) {
seconds += howmany;
sleepseconds +=howmany;
count += howmany * CLOCK_SECOND;
#if RADIOSTATS
if (RF230_receive_on) radioontime += howmany;
#endif
}
#endif
/*---------------------------------------------------------------------------*/
/* This routine can be called to add ticks to the clock after a sleep.
*/
void clock_adjust_ticks(uint16_t howmany) {
count += howmany;
scount += howmany;
while(scount >= CLOCK_SECOND) {
scount -= CLOCK_SECOND;
seconds++;
sleepseconds++;
#if RADIOSTATS
if (RF230_receive_on) radioontime += 1;
#endif
}
}
/*---------------------------------------------------------------------------*/
//SIGNAL(SIG_OUTPUT_COMPARE0)
ISR(AVR_OUTPUT_COMPARE_INT)
@ -123,6 +147,7 @@ clock_time(void)
} while(tmp != count);
return tmp;
}
#if 0
/*---------------------------------------------------------------------------*/
/**
* Delay the CPU for a multiple of TODO
@ -139,7 +164,7 @@ clock_delay(unsigned int i)
/*---------------------------------------------------------------------------*/
/**
* Wait for a multiple of 1 / 125 sec = 0.008 ms.
* Wait for a number of clock ticks.
*
*/
void
@ -154,8 +179,9 @@ clock_wait(int i)
void
clock_set_seconds(unsigned long sec)
{
// TODO
seconds = sec;
}
#endif
unsigned long
clock_seconds(void)
@ -253,4 +279,4 @@ ISR( _VECTOR(76)) {while (1) x++;}
ISR( _VECTOR(77)) {while (1) x++;}
ISR( _VECTOR(78)) {while (1) x++;}
ISR( _VECTOR(79)) {while (1) x++;}
#endif
#endif

View File

@ -224,8 +224,8 @@ void micro_sleep(uint8_t howlong)
sleep_mode(); // Sleep
/* Adjust clock.c for the time spent sleeping */
extern void clock_adjust_seconds(uint8_t howmany);
clock_adjust_seconds(howlong);
extern void clock_adjust_ticks(uint16_t howmany);
clock_adjust_ticks(howlong * CLOCK_SECOND);
// if (TIMSK2&(1<<OCIE2A)) break; // Exit sleep if not awakened by TIMER2
PRINTF(".");

View File

@ -215,8 +215,8 @@ void micro_sleep(uint8_t howlong)
sleep_mode(); // Sleep
/* Adjust clock.c for the time spent sleeping */
extern void clock_adjust_seconds(uint8_t howmany);
clock_adjust_seconds(howlong);
extern void clock_adjust_ticks(uint16_t howmany);
clock_adjust_ticks(howlong * CLOCK_SECOND);
// if (TIMSK2&(1<<OCIE2A)) break; // Exit sleep if not awakened by TIMER2
PRINTF(".");