Andrew Boyson / clock

Dependents:   oldheating gps motorhome heating

Files at this revision

API Documentation at this revision

Comitter:
andrewboyson
Date:
Thu Jan 18 18:07:30 2018 +0000
Parent:
19:e537bacd1478
Child:
21:48fe75bcde84
Commit message:
Corrected an error in the Tick Set routine

Changed in this revision

clock.c Show annotated file Show diff for this revision Revisions of this file
clock.h Show annotated file Show diff for this revision Revisions of this file
counter.c Show diff for this revision Revisions of this file
counter.h Show diff for this revision Revisions of this file
tick.c Show annotated file Show diff for this revision Revisions of this file
tick.h Show annotated file Show diff for this revision Revisions of this file
--- a/clock.c	Wed Jan 17 20:42:14 2018 +0000
+++ b/clock.c	Thu Jan 18 18:07:30 2018 +0000
@@ -79,8 +79,6 @@
     TickInit();
 }
 
-int ClockTicked = 0; //Set to true for one scan each time the second changes
-
 uint32_t ClockScanAverage = 0;
 uint32_t ClockScanMinimum = 10000;
 uint32_t ClockScanMaximum = 0;
@@ -127,16 +125,11 @@
          
     //Record the time the clock started
     if (SyncedTime && SyncedRate) refNs = nowNs;
-
-    //Set a one shot memory for having had a tick
-    static time_t lastT = 0;
-    ClockTicked = lastT > 0 && lastT != nowT;
-    lastT = nowT;
         
     if (TickIsSet())
     {
         //Save the time to the RTC on the second
-        if (ClockTicked)
+        if (TickTicked)
         {
             struct tm tm;
             ClockTmUtc(&tm);
--- a/clock.h	Wed Jan 17 20:42:14 2018 +0000
+++ b/clock.h	Thu Jan 18 18:07:30 2018 +0000
@@ -22,7 +22,6 @@
 extern void    ClockTmLocal(struct tm* ptm);
 extern void    ClockTmUtc  (struct tm* ptm);
 
-extern bool     ClockTicked;
 extern uint32_t ClockScanAverage;
 extern uint32_t ClockScanMinimum;
 extern uint32_t ClockScanMaximum;
--- a/counter.c	Wed Jan 17 20:42:14 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,73 +0,0 @@
-#include <stdint.h>
-
-#include "peripherals.h"
-
-
-#define SECONDS_PER_TICK 45;
-#define TICK_COUNT_PER_SECOND 96000000
-
-
-static int32_t ppb = 0;
-static int32_t ns = 0;
-static int64_t freqCount = 0; //1 second = 96,000,000 counts
-static int64_t slewCount = 0; //1 ns = 96 counts
-
-int64_t CounterGetI() //Call this from an interrupt or if interrupts are already disabled
-{
-    static uint32_t lastCount = 0;
-    static uint32_t ticks = 0;
-    
-    uint32_t thisCount = LPC_TIM1->TC;
-    if (thisCount < lastCount)
-    {
-        ++ticks;
-        freqCount += ppb * SECONDS_PER_TICK;
-        slewCount +=  ns * SECONDS_PER_TICK;
-        ns = 0;
-    }
-        
-    lastCount = thisCount;
-
-    return ((int64_t)ticks << 32) | thisCount;
-}
-int64_t CounterGet() //Call this from the main thread
-{
-    __disable_irq();
-        int64_t count = CounterGetI();
-    __enable_irq();
-
-    return count;
-}
-
-static volatile int64_t nsTickCount;
-static volatile int64_t nsFreqCount;
-static volatile int64_t nsTimeCount;
-
-uint32_t CounterGetFractionOfSecond()
-{
-    static uint32_t lastCall = 0;
-    uint32_t sinceLastCall = LPC_TIM1->TC - lastCall;
-    if (sinceLastCall > TICK_COUNT_PER_SECOND)
-    {
-             lastCall += TICK_COUNT_PER_SECOND;
-        sinceLastCall -= TICK_COUNT_PER_SECOND;
-    }
-    return sinceLastCall;
-}
-
-void CounterInit(void)
-{    
-    LPC_SC->PCONP    |=     4; //  4.8.9 Power Control for Peripherals register - Timer1 Power On
-    LPC_TIM1->TCR     =     2; // 21.6.2 Timer Control Register - Reset TC and PC.
-    LPC_TIM1->CTCR    =     0; // 21.6.3 Count Control Register - Timer mode
-    LPC_TIM1->PR      =     0; // 21.6.5 Prescale register      - Don't prescale 96MHz clock (divide by PR+1).
-    LPC_TIM1->MCR     =     0; // 21.6.8 Match Control Register - interrupt on match
-    LPC_TIM1->TCR     =     1; // 21.6.2 Timer Control Register - Enable TC and PC
-}
-uint32_t CounterSinceLastCall(uint32_t* pLastTime)
-{
-    uint32_t thisTime = LPC_TIM1->TC;
-    uint32_t elapsed = thisTime - *pLastTime; //unsigned subtraction
-    *pLastTime = thisTime;
-    return elapsed;
-}
--- a/counter.h	Wed Jan 17 20:42:14 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,7 +0,0 @@
-#include <stdint.h>
-
-extern int64_t CounterGetI(void);
-extern int64_t CounterGet(void);
-extern void    CounterInit(void);
-extern int32_t CounterSinceLastCall(int32_t* pLastTime);
-
--- a/tick.c	Wed Jan 17 20:42:14 2018 +0000
+++ b/tick.c	Thu Jan 18 18:07:30 2018 +0000
@@ -13,16 +13,8 @@
 static volatile int64_t nsTickCount;
 static volatile int64_t nsFreqCount;
 static volatile int64_t nsTimeCount;
-
 static bool nsCountIsSet = false;
 bool  TickIsSet() { return nsCountIsSet; }
-void TickSet(int64_t extClock)
-{
-    nsTickCount  = 0;
-    nsFreqCount  = 0;
-    nsTimeCount  = extClock - LPC_TIM1->TC;
-    nsCountIsSet = true;
-}
 
 static volatile int32_t slew = 0; //ns     - up to +/- 2.147s of slew
 int32_t TickGetSlew() { return slew; }
@@ -33,19 +25,58 @@
 void    TickSetPpb (int32_t value) { ppb  = value; LPC_RTC->GPREG0 = ppb; }
 void    TickAddPpb (int32_t value) { ppb += value; LPC_RTC->GPREG0 = ppb; }
 
-static uint32_t lastCall = 0;
+bool TickTicked = false;
+
+/*
++---------+---------------+
+| Seconds |   Base count  |
++---------+---------------+
+|    0    |             0 |
+|    1    |    96,000,000 |
+|   ...   |       ...     |
+|   44    | 4,224,000,000 |
+|   44.74 |          2^32 |
+|   45    |    25,032,704 |
+|   ...   |       ...     |
++---------+---------------+
+*/
+static uint32_t secondsBaseCount = 0;
+
+void TickSet(int64_t extClock)
+{
+    uint32_t       tc = LPC_TIM1->TC;
+    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
+    uint32_t fractionNs = fraction / 96 * 1000;           //0 to 1,000,000,000 which fits into 32 bits
+     int64_t       ns = extClock - fractionNs;
+     
+    __disable_irq();
+             nsTickCount = ns;
+             nsFreqCount = 0;
+             nsTimeCount = 0;
+        secondsBaseCount = base;
+    __enable_irq();
+    
+    nsCountIsSet = true;
+}
 void TickMain()
 {
-    uint32_t sinceLastCall = LPC_TIM1->TC - lastCall;
-    if (sinceLastCall > TICK_COUNT_PER_SECOND)
+    uint32_t sincesecondsBaseCount = LPC_TIM1->TC - secondsBaseCount;
+    if (sincesecondsBaseCount > TICK_COUNT_PER_SECOND)
     { 
         __disable_irq();
-           lastCall += TICK_COUNT_PER_SECOND;
-        nsTickCount += ONE_BILLION;
-        nsFreqCount += ppb;
-        nsTimeCount += slew;
-               slew  = 0;
+        secondsBaseCount += TICK_COUNT_PER_SECOND;
+             nsTickCount += ONE_BILLION;
+             nsFreqCount += ppb;
+             nsTimeCount += slew;
         __enable_irq();
+        slew  = 0;
+        TickTicked = true;
+    }
+    else
+    {
+        TickTicked = false;
     }
 }
 static volatile  int64_t nsTickSnapshot;
@@ -55,7 +86,7 @@
 
 void TickSaveSnapshotI()
 {
-     timerSnapshot = LPC_TIM1->TC - lastCall;
+     timerSnapshot = LPC_TIM1->TC - secondsBaseCount;
     nsTickSnapshot = nsTickCount;
     nsFreqSnapshot = nsFreqCount;
     nsTimeSnapshot = nsTimeCount;
@@ -92,12 +123,10 @@
      int64_t freqNs;
      int64_t timeNs;
     
-    __disable_irq();
-        timerCount = LPC_TIM1->TC - lastCall;
-         tickNs    = nsTickCount;
-         freqNs    = nsFreqCount;
-         timeNs    = nsTimeCount;
-    __enable_irq();
+    timerCount = LPC_TIM1->TC - secondsBaseCount;
+     tickNs    = nsTickCount;
+     freqNs    = nsFreqCount;
+     timeNs    = nsTimeCount;
     
     makeTimesFromCounts(timerCount, tickNs, freqNs, timeNs, pNsInt, pNsAbs);
 }
--- a/tick.h	Wed Jan 17 20:42:14 2018 +0000
+++ b/tick.h	Thu Jan 18 18:07:30 2018 +0000
@@ -15,6 +15,8 @@
 extern uint32_t TickTimerCount(uint32_t startCount);
 extern uint32_t TickTimerMs   (uint32_t startCount);
 
+extern bool     TickTicked;
+
 extern void     TickInit(void);
 extern void     TickMain(void);