Andrew Boyson / clock

Dependents:   oldheating gps motorhome heating

Revision:
24:6c9833e2a049
Parent:
23:07b19cd5f6d0
Child:
25:81014a201736
diff -r 07b19cd5f6d0 -r 6c9833e2a049 tick.c
--- a/tick.c	Sat Jan 20 17:10:55 2018 +0000
+++ b/tick.c	Mon Jan 22 18:35:11 2018 +0000
@@ -2,9 +2,10 @@
 #include <stdbool.h>
 
 #include "peripherals.h"
-#include  "rtc.h"
+#include "rtc.h"
 #include "time.h"
 #include "tick.h"
+#include "timer.h"
 #include "led.h"
 #include "log.h"
 
@@ -41,9 +42,11 @@
 */
 static uint32_t secondsBaseCount = 0;
 
+int64_t TickTime = 0; //30th bit is one second
+
 void TickSet(int64_t extClock)
 {
-    uint32_t       tc = LPC_TIM0->TC;
+    uint32_t       tc = TimerNowCount();
     uint32_t  seconds =       tc / TICK_COUNT_PER_SECOND; //0 to 44
     uint32_t     base =  seconds * TICK_COUNT_PER_SECOND; //0 to 2^32
     uint32_t fraction =       tc % TICK_COUNT_PER_SECOND; //0 to 96,000,000
@@ -60,16 +63,36 @@
 }
 void TickMain()
 {
-    uint32_t sincesecondsBaseCount = LPC_TIM0->TC - secondsBaseCount;
-    if (sincesecondsBaseCount > TICK_COUNT_PER_SECOND)
+    uint32_t sinceSecondsBaseCount = TimerSinceCount(secondsBaseCount);
+    
+    //Update the times whenever there has been a system second
+    if (sinceSecondsBaseCount > TICK_COUNT_PER_SECOND)
     { 
         __disable_irq();
         secondsBaseCount += TICK_COUNT_PER_SECOND;
              nsTickCount += ONE_BILLION + ppb;
              nsSlewCount += slew;
+                    slew  = 0;
         __enable_irq();
-        slew  = 0;
+        sinceSecondsBaseCount -= TICK_COUNT_PER_SECOND;
     }
+    
+    //Update TickTime
+    int64_t   fraction = sinceSecondsBaseCount;
+              fraction <<= 32;
+              fraction /= TICK_COUNT_PER_SECOND;
+    
+    int64_t nsBase     = nsTickCount + nsSlewCount;
+    int64_t nsPerTick  = ONE_BILLION + ppb + slew;
+    int64_t nsFraction = (nsPerTick * fraction) >> 32;
+    TickTime = nsBase + nsFraction;
+    
+    //Update the ticked flag
+    static bool lastTick = false;
+    bool thisTick = TickTime & (1ULL << 30);
+    TickTicked = thisTick && !lastTick;
+    lastTick = TickTicked;
+
 }
 static volatile  int64_t nsTickSnapshot;
 static volatile  int64_t nsSlewSnapshot;
@@ -77,7 +100,7 @@
 
 void TickSaveSnapshot()
 {
-     timerSnapshot = LPC_TIM0->TC - secondsBaseCount;
+     timerSnapshot = TimerSinceCount(secondsBaseCount);
     nsTickSnapshot = nsTickCount;
     nsSlewSnapshot = nsSlewCount;
 }
@@ -105,9 +128,8 @@
 }
 void TickGetTimes(int64_t* pNsInt, int64_t* pNsAbs)
 {   
-    makeTimesFromCounts(LPC_TIM0->TC - secondsBaseCount, nsTickCount, nsSlewCount, pNsInt, pNsAbs);
+    makeTimesFromCounts(TimerSinceCount(secondsBaseCount), nsTickCount, nsSlewCount, pNsInt, pNsAbs);
 }
-
 void TickInit(void)
 {
     nsTickCount = 0;
@@ -116,31 +138,4 @@
     ppb  = LPC_RTC->GPREG0; //This is saved each time Tickppb is updated
     slew = 0;
     nsCountIsSet = false;
-    
-    LPC_TIM0->TCR     =     2; // 21.6.2 Timer Control Register - Reset TC and PC.
-    LPC_TIM0->CTCR    =     0; // 21.6.3 Count Control Register - Timer mode
-    LPC_TIM0->PR      =     0; // 21.6.5 Prescale register      - Don't prescale 96MHz clock (divide by PR+1).
-    LPC_TIM0->MCR     =     0; // 21.6.8 Match Control Register - no interrupt or reset
-    LPC_TIM0->TCR     =     1; // 21.6.2 Timer Control Register - Enable TC and PC
-}
-uint32_t TickElapsed(uint32_t* pLastTime)
-{
-    uint32_t thisTime = LPC_TIM0->TC;
-    uint32_t elapsed = thisTime - *pLastTime;    
-    *pLastTime = thisTime;
-    return elapsed;
-}
-uint32_t TickTimerStart()
-{
-    return LPC_TIM0->TC;
-}
-uint32_t TickTimerCount(uint32_t startCount)
-{
-    uint32_t thisCount = LPC_TIM0->TC;
-    return thisCount - startCount; 
-}
-uint32_t TickTimerMs(uint32_t startCount)
-{
-    uint32_t count = TickTimerCount(startCount);
-    return count / 96000;
-}
+}
\ No newline at end of file