Andrew Boyson / clock

Dependents:   oldheating gps motorhome heating

Revision:
26:0421132e6eaf
Parent:
22:df0b906bda26
Child:
28:b4aa41fdeb68
--- a/sync.c	Mon Jan 22 18:54:23 2018 +0000
+++ b/sync.c	Thu Jan 25 07:54:54 2018 +0000
@@ -6,9 +6,6 @@
 #include  "time.h"
 #include "clock.h"
 
-#define ONE_BILLION 1000000000LL //Make sure ONE_BILLION is 64 bit by putting LL on the end
-#define ONE_MILLION    1000000
-
 bool SyncTrace = false;
 
 bool SyncedTime = false;
@@ -36,10 +33,10 @@
 static void setSlew(int64_t diff)
 {
     int64_t slew = -diff / ClockSlewDivisor;
-    int32_t slewMaxNs = ClockSlewMaxMs * ONE_MILLION;
+    int32_t slewMaxTicks = ClockSlewMaxMs << 20;
     
-    if (slew >  slewMaxNs) slew =  slewMaxNs;
-    if (slew < -slewMaxNs) slew = -slewMaxNs;
+    if (slew >  slewMaxTicks) slew =  slewMaxTicks;
+    if (slew < -slewMaxTicks) slew = -slewMaxTicks;
     
     TickSetSlew(slew);
     
@@ -85,7 +82,7 @@
 
     //Calulate the time error
     int64_t absDiff = thisAbsClock - thisExtClock;
-    if (llabs(absDiff) > ClockMaxOffsetSecs * ONE_BILLION)
+    if (llabs(absDiff) > ((int64_t)ClockMaxOffsetSecs << TICK_ONE_SECOND_SHIFT))
     {
         LogTimeF("Sync - offset is greater than %d seconds so resetting\r\n", ClockMaxOffsetSecs);
         reset(thisExtClock);
@@ -103,8 +100,8 @@
         int64_t periodDiff =   intPeriod -    extPeriod;
         
         int64_t ppb;
-        if (extPeriod == ONE_BILLION) ppb = periodDiff; //This saves a 64bit multiplication and division for PPS
-        else                          ppb = periodDiff * ONE_BILLION / extPeriod;
+        if (extPeriod == TICK_ONE_SECOND) ppb =  periodDiff; //This saves a 64bit shift and division for PPS
+        else                              ppb = (periodDiff << TICK_ONE_SECOND_SHIFT) / extPeriod;
         
         adjustPpb(ppb);
         setSyncedRate(ppb);
@@ -114,6 +111,6 @@
     lastIntClock = thisIntClock;
     lastExtClock = thisExtClock;
 }
-void SyncPpsI(          ) { TickSaveSnapshot(); }
-void SyncPpsN(time_t t  ) { int64_t ns = t * ONE_BILLION; sync(ns); }
-void SyncNs  (int64_t ns) { TickSaveSnapshot();           sync(ns); }
+void SyncPpsI (             ) { TickSaveSnapshot(); }
+void SyncPpsN (time_t  t    ) { int64_t ticks = t << TICK_ONE_SECOND_SHIFT; sync(ticks); }
+void SyncTicks(int64_t ticks) { TickSaveSnapshot();                         sync(ticks); }