Andrew Boyson / clock

Dependents:   oldheating gps motorhome heating

Revision:
23:07b19cd5f6d0
Parent:
22:df0b906bda26
Child:
24:6c9833e2a049
--- a/tick.c	Fri Jan 19 19:59:43 2018 +0000
+++ b/tick.c	Sat Jan 20 17:10:55 2018 +0000
@@ -11,8 +11,7 @@
 #define ONE_BILLION 1000000000
 
 static volatile int64_t nsTickCount;
-static volatile int64_t nsFreqCount;
-static volatile int64_t nsTimeCount;
+static volatile int64_t nsSlewCount;
 static bool nsCountIsSet = false;
 bool  TickIsSet() { return nsCountIsSet; }
 
@@ -53,8 +52,7 @@
      
     __disable_irq();
              nsTickCount = ns;
-             nsFreqCount = 0;
-             nsTimeCount = 0;
+             nsSlewCount = 0;
         secondsBaseCount = base;
     __enable_irq();
     
@@ -67,26 +65,23 @@
     { 
         __disable_irq();
         secondsBaseCount += TICK_COUNT_PER_SECOND;
-             nsTickCount += ONE_BILLION;
-             nsFreqCount += ppb;
-             nsTimeCount += slew;
+             nsTickCount += ONE_BILLION + ppb;
+             nsSlewCount += slew;
         __enable_irq();
         slew  = 0;
     }
 }
 static volatile  int64_t nsTickSnapshot;
-static volatile  int64_t nsFreqSnapshot;
-static volatile  int64_t nsTimeSnapshot;
+static volatile  int64_t nsSlewSnapshot;
 static volatile uint32_t  timerSnapshot;
 
 void TickSaveSnapshot()
 {
      timerSnapshot = LPC_TIM0->TC - secondsBaseCount;
     nsTickSnapshot = nsTickCount;
-    nsFreqSnapshot = nsFreqCount;
-    nsTimeSnapshot = nsTimeCount;
+    nsSlewSnapshot = nsSlewCount;
 }
-static void makeTimesFromCounts(uint32_t timerCount, int64_t tickNs, int64_t freqNs, int64_t timeNs, int64_t* pNsInt, int64_t* pNsAbs)
+static void makeTimesFromCounts(uint32_t timerCount, int64_t tickNs, int64_t slewNs, int64_t* pNsInt, int64_t* pNsAbs)
 {
     int64_t   fraction = timerCount;
               fraction <<= 32;
@@ -95,42 +90,28 @@
     int64_t nsFraction;
     
     int64_t nsBase     = tickNs;
-    int64_t nsPerTick  = ONE_BILLION;
-     
-    nsBase    += freqNs;
-    nsPerTick += ppb;
+    int64_t nsPerTick  = ONE_BILLION + ppb;
     nsFraction = (nsPerTick * fraction) >> 32;
     *pNsInt = nsBase + nsFraction;
 
-    nsBase    += timeNs;
+    nsBase    += slewNs;
     nsPerTick += slew;
     nsFraction = (nsPerTick * fraction) >> 32;
     *pNsAbs = nsBase + nsFraction;
 }
 void TickGetTimesFromSnapshot(int64_t* pNsInt, int64_t* pNsAbs)
 {
-    makeTimesFromCounts(timerSnapshot, nsTickSnapshot, nsFreqSnapshot, nsTimeSnapshot, pNsInt, pNsAbs);
+    makeTimesFromCounts(timerSnapshot, nsTickSnapshot, nsSlewSnapshot, pNsInt, pNsAbs);
 }
 void TickGetTimes(int64_t* pNsInt, int64_t* pNsAbs)
-{
-    uint32_t timerCount;
-     int64_t tickNs;
-     int64_t freqNs;
-     int64_t timeNs;
-    
-    timerCount = LPC_TIM0->TC - secondsBaseCount;
-     tickNs    = nsTickCount;
-     freqNs    = nsFreqCount;
-     timeNs    = nsTimeCount;
-    
-    makeTimesFromCounts(timerCount, tickNs, freqNs, timeNs, pNsInt, pNsAbs);
+{   
+    makeTimesFromCounts(LPC_TIM0->TC - secondsBaseCount, nsTickCount, nsSlewCount, pNsInt, pNsAbs);
 }
 
 void TickInit(void)
 {
     nsTickCount = 0;
-    nsFreqCount = 0;
-    nsTimeCount = 0;
+    nsSlewCount = 0;
     
     ppb  = LPC_RTC->GPREG0; //This is saved each time Tickppb is updated
     slew = 0;