From 2feed121d23bc5a0eb22f9016a4b2960b30cb68c Mon Sep 17 00:00:00 2001 From: David Kopf Date: Wed, 17 Aug 2011 16:51:22 -0400 Subject: [PATCH] Account for 16 bit rtimer wraparound, else ENERGEST way underreports AVR times. --- core/sys/energest.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/core/sys/energest.h b/core/sys/energest.h index 8929b3547..550f5eac9 100644 --- a/core/sys/energest.h +++ b/core/sys/energest.h @@ -91,7 +91,22 @@ extern energest_t energest_leveldevice_current_leveltime[ENERGEST_CONF_LEVELDEVI energest_current_time[type] = RTIMER_NOW(); \ energest_current_mode[type] = 1; \ } while(0) +#ifdef __AVR__ +/* Handle 16 bit rtimer wraparound */ +#define ENERGEST_OFF(type) if(energest_current_mode[type] != 0) do { \ + if (RTIMER_NOW() < energest_current_time[type]) energest_total_time[type].current += RTIMER_ARCH_SECOND; \ + energest_total_time[type].current += (rtimer_clock_t)(RTIMER_NOW() - \ + energest_current_time[type]); \ + energest_current_mode[type] = 0; \ + } while(0) +#define ENERGEST_OFF_LEVEL(type,level) do { \ + if (RTIMER_NOW() < energest_current_time[type]) energest_total_time[type].current += RTIMER_ARCH_SECOND; \ + energest_leveldevice_current_leveltime[level].current += (rtimer_clock_t)(RTIMER_NOW() - \ + energest_current_time[type]); \ + energest_current_mode[type] = 0; \ + } while(0) +#else #define ENERGEST_OFF(type) if(energest_current_mode[type] != 0) do { \ energest_total_time[type].current += (rtimer_clock_t)(RTIMER_NOW() - \ energest_current_time[type]); \ @@ -103,6 +118,7 @@ extern energest_t energest_leveldevice_current_leveltime[ENERGEST_CONF_LEVELDEVI energest_current_time[type]); \ energest_current_mode[type] = 0; \ } while(0) +#endif #else /* ENERGEST_CONF_ON */