From 3e25033644b4f9ff7039df6fc4655cffaf61bcc1 Mon Sep 17 00:00:00 2001 From: mmaroti Date: Sat, 27 Mar 2010 21:27:33 +0000 Subject: [PATCH 1/1] fix rounding in the scale function (see MeasureClockC tinyos-help discussion on feb 8, 2010) Committed on the Free edition of March Hare Software CVSNT Server. Upgrade to CVS Suite for more features and support: http://march-hare.com/cvsnt/ --- tos/platforms/mica/MeasureClockC.nc | 4 ++-- tos/system/scale.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tos/platforms/mica/MeasureClockC.nc b/tos/platforms/mica/MeasureClockC.nc index fa82bbba..5518964d 100644 --- a/tos/platforms/mica/MeasureClockC.nc +++ b/tos/platforms/mica/MeasureClockC.nc @@ -93,11 +93,11 @@ implementation } async command uint32_t Atm128Calibrate.calibrateMicro(uint32_t n) { - return scale32(n + MAGIC / 2, cycles, MAGIC); + return scale32(n, cycles, MAGIC); } async command uint32_t Atm128Calibrate.actualMicro(uint32_t n) { - return scale32(n + (cycles >> 1), MAGIC, cycles); + return scale32(n, MAGIC, cycles); } async command uint8_t Atm128Calibrate.adcPrescaler() { diff --git a/tos/system/scale.h b/tos/system/scale.h index d249cf82..8e251116 100644 --- a/tos/system/scale.h +++ b/tos/system/scale.h @@ -8,7 +8,7 @@ * @param x Number to scale * @param a Numerator of scaling value * @param b Denominator of scaling value - * @return x * a/b + * @return round(x * a/b) */ inline uint32_t scale32(uint32_t x, uint32_t a, uint32_t b) { @@ -16,7 +16,7 @@ inline uint32_t scale32(uint32_t x, uint32_t a, uint32_t b) uint32_t x_mod_b = x % b; x_mod_b *= a; // on a separate line just in case some compiler goes weird - return x_over_b * a + x_mod_b / b; + return x_over_b * a + (x_mod_b + (b>>1)) / b; } #endif -- 2.39.2