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/io.h>
#include <avr/interrupt.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; static volatile clock_time_t count;
/* scount is the 8 bit counter that counts ticks modulo CLOCK_SECONDS */
static volatile uint8_t scount; static volatile uint8_t scount;
/* seconds is the number of seconds since startup, returned by clock_seconds() */
volatile unsigned long seconds; volatile unsigned long seconds;
/* sleepseconds is the number of seconds sleeping since startup, available globally */
long sleepseconds; long sleepseconds;
/* Set RADIOSTATS to monitor radio on time (must also be set in the radio driver) */ /* 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; static uint8_t calibrate_interval;
#endif #endif
/* #if 0
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.
*/
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/* This routine can be called to add seconds to the clock after a sleep /* This routine can be called to add seconds to the clock after a sleep
* of an integral number of seconds. * of an integral number of seconds.
@ -52,10 +58,28 @@ static uint8_t calibrate_interval;
void clock_adjust_seconds(uint8_t howmany) { void clock_adjust_seconds(uint8_t howmany) {
seconds += howmany; seconds += howmany;
sleepseconds +=howmany; sleepseconds +=howmany;
count += howmany * CLOCK_SECOND;
#if RADIOSTATS #if RADIOSTATS
if (RF230_receive_on) radioontime += howmany; if (RF230_receive_on) radioontime += howmany;
#endif #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) //SIGNAL(SIG_OUTPUT_COMPARE0)
ISR(AVR_OUTPUT_COMPARE_INT) ISR(AVR_OUTPUT_COMPARE_INT)
@ -123,6 +147,7 @@ clock_time(void)
} while(tmp != count); } while(tmp != count);
return tmp; return tmp;
} }
#if 0
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/** /**
* Delay the CPU for a multiple of TODO * 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 void
@ -154,8 +179,9 @@ clock_wait(int i)
void void
clock_set_seconds(unsigned long sec) clock_set_seconds(unsigned long sec)
{ {
// TODO seconds = sec;
} }
#endif
unsigned long unsigned long
clock_seconds(void) clock_seconds(void)

View File

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

View File

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