Andrew Boyson / clock

Dependents:   oldheating gps motorhome heating

Files at this revision

API Documentation at this revision

Comitter:
andrewboyson
Date:
Tue Dec 04 12:26:27 2018 +0000
Parent:
39:5b594b1b6a0a
Child:
41:8cd859cd1475
Commit message:
Renamed timer to hrtimer; added clktimer to handle utc based times.

Changed in this revision

clktimer/clktimer.c Show annotated file Show diff for this revision Revisions of this file
clktimer/clktimer.h Show annotated file Show diff for this revision Revisions of this file
clock.txt Show annotated file Show diff for this revision Revisions of this file
clock/clktime.c Show annotated file Show diff for this revision Revisions of this file
clock/clock.c Show annotated file Show diff for this revision Revisions of this file
hrtimer/hrtimer.c Show annotated file Show diff for this revision Revisions of this file
hrtimer/hrtimer.h Show annotated file Show diff for this revision Revisions of this file
mstimer/mstimer.c Show annotated file Show diff for this revision Revisions of this file
mstimer/mstimer.h Show annotated file Show diff for this revision Revisions of this file
scan/scan.c Show annotated file Show diff for this revision Revisions of this file
timer/timer.c Show diff for this revision Revisions of this file
timer/timer.h Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/clktimer/clktimer.c	Tue Dec 04 12:26:27 2018 +0000
@@ -0,0 +1,26 @@
+#include <stdint.h>
+#include <stdbool.h>
+
+#include "clock.h"
+
+int64_t ClkTimerSinceRepetitive(int64_t* pLastCount)
+{
+    int64_t thisCount = ClockTime();
+    int64_t period = thisCount - *pLastCount;    
+    *pLastCount = thisCount;
+    return period;
+}
+int64_t ClkTimerSince(int64_t lastCount)
+{
+    return ClockTime() - lastCount; 
+}
+
+bool ClkTimerRepetitiveTick(int64_t* pLastCount, int64_t interval)
+{
+    if (ClockTime() - *pLastCount >= interval)
+    {
+        *pLastCount += interval;
+        return true;
+    }
+    return false;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/clktimer/clktimer.h	Tue Dec 04 12:26:27 2018 +0000
@@ -0,0 +1,8 @@
+#include <stdint.h>
+#include <stdbool.h>
+
+extern bool    ClkTimerRepetitiveTick (int64_t* pLastCount, int64_t interval);
+
+extern int64_t ClkTimerSinceRepetitive(int64_t* pLastCount);
+extern int64_t ClkTimerSince          (int64_t   lastCount);
+extern int64_t ClkTimerSinceMs        (int64_t   lastCount);
--- a/clock.txt	Mon Dec 03 12:50:10 2018 +0000
+++ b/clock.txt	Tue Dec 04 12:26:27 2018 +0000
@@ -1,28 +1,34 @@
 The clock library provides a number of separate functions:
-* "timer" A high resolution timer which wraps around after 44 seconds from which all the time is derived.
-* "ticks" A low resolution timer to use for elapsed time (configurable but currently set up at 20ms with wrap around every year
-* "scan"  Calculates the max, min and average scan times.
-* "rtc"   A real time clock to provide backup
-* "tm"    Routines to manipulate struct tm local and utc times
-* "clock" A clock which is synchronised to an external source
+* "hrtimer"  An unsigned 32bit high resolution timer which wraps around every 44 seconds from which all the time is derived.
+* "mstimer"  An unsigned 32bit  low resolution timer which wraps around every 49 days
+* "clktimer" A    signed 64bit      utc linked timer which doesn't wrap (or not until 2242 when clock time breaks)
+* "scan"    Calculates the max, min and average scan times.
+* "rtc"     A real time clock to provide backup
+* "tm"      Routines to manipulate struct tm local and utc times
+* "clock"   A clock which is synchronised to an external source
 
 High resolution timer
 =====================
-timer.c uses TIM0 as a 32bit timer which counts at the cpu frequency 96MHz and rolls over after about 44s.
+hrtimer.c uses TIM0 as a 32bit timer which counts at the cpu frequency 96MHz and rolls over after about 44s.
 It has an init routine called from ClockInit to start it, thereafter it free runs.
 No dependancies.
 
-Low resolution elapsed time
-===========================
+Millisecond timer
+=================
 mstimer.c uses the high resolution timer to count the number of ms since power up. Its unsigned 32bit count rolls over after about 49 days.
 It has a main routine called from ClockMain.
 Depends on timer.
 
+Clock timer
+===========
+clktimer.h uses the signed 64 bit clock time.
+Depends on clock and hence hrtimer.
+
 Scan times
 ==========
 scan.c uses the high resolution timer to calculate the max, min and average scan times. 
 It has a main routine called from ClockMain.
-Depends on timer.
+Depends on hrtimer.
 
 Real time clock
 ===============
--- a/clock/clktime.c	Mon Dec 03 12:50:10 2018 +0000
+++ b/clock/clktime.c	Tue Dec 04 12:26:27 2018 +0000
@@ -6,7 +6,7 @@
 #include "clock.h"
 #include "clktime.h"
 #include "clkstate.h"
-#include "timer.h"
+#include "hrtimer.h"
 #include "led.h"
 #include "log.h"
 
@@ -20,13 +20,13 @@
 
 int64_t TimeNow()
 {
-    return tickCount + slewCount + TimerMultiplyFractionalPart(TIME_ONE_SECOND + ClockPpb + ClockSlew, TimerSinceCount(secondBaseCount), TIMER_COUNT_PER_SECOND);
+    return tickCount + slewCount + HrTimerMultiplyFractionalPart(TIME_ONE_SECOND + ClockPpb + ClockSlew, HrTimerSince(secondBaseCount), HR_TIMER_COUNT_PER_SECOND);
 }
 
 void TimeSet(int64_t extClock)
 {    
-     int64_t timerCountSinceLastSecond = TimerSinceCount(secondBaseCount);
-     int64_t fraction = (timerCountSinceLastSecond << TIME_ONE_SECOND_SHIFT) / TIMER_COUNT_PER_SECOND;
+     int64_t timerCountSinceLastSecond = HrTimerSince(secondBaseCount);
+     int64_t fraction = (timerCountSinceLastSecond << TIME_ONE_SECOND_SHIFT) / HR_TIMER_COUNT_PER_SECOND;
      int64_t    ticks = extClock - fraction;
 
     __disable_irq();
@@ -52,18 +52,18 @@
 
 void TimeSaveSnapshot()
 {
-     timerSnapshot = TimerSinceCount(secondBaseCount);
+     timerSnapshot = HrTimerSince(secondBaseCount);
       tickSnapshot = tickCount;
       slewSnapshot = slewCount;
 }
 void TimesGetFromSnapshot(int64_t* pInt, int64_t* pAbs)
 {
-    *pInt = tickSnapshot                + TimerMultiplyFractionalPart(TIME_ONE_SECOND + ClockPpb,             timerSnapshot, TIMER_COUNT_PER_SECOND);
-    *pAbs = tickSnapshot + slewSnapshot + TimerMultiplyFractionalPart(TIME_ONE_SECOND + ClockPpb + ClockSlew, timerSnapshot, TIMER_COUNT_PER_SECOND);
+    *pInt = tickSnapshot                + HrTimerMultiplyFractionalPart(TIME_ONE_SECOND + ClockPpb,             timerSnapshot, HR_TIMER_COUNT_PER_SECOND);
+    *pAbs = tickSnapshot + slewSnapshot + HrTimerMultiplyFractionalPart(TIME_ONE_SECOND + ClockPpb + ClockSlew, timerSnapshot, HR_TIMER_COUNT_PER_SECOND);
 }
 void TimesGet(int64_t* pInt, int64_t* pAbs)
 {   
-    uint32_t timerCount = TimerSinceCount(secondBaseCount);
-    *pInt = tickCount             + TimerMultiplyFractionalPart(TIME_ONE_SECOND + ClockPpb,             timerCount, TIMER_COUNT_PER_SECOND);
-    *pAbs = tickCount + slewCount + TimerMultiplyFractionalPart(TIME_ONE_SECOND + ClockPpb + ClockSlew, timerCount, TIMER_COUNT_PER_SECOND);
+    uint32_t timerCount = HrTimerSince(secondBaseCount);
+    *pInt = tickCount             + HrTimerMultiplyFractionalPart(TIME_ONE_SECOND + ClockPpb,             timerCount, HR_TIMER_COUNT_PER_SECOND);
+    *pAbs = tickCount + slewCount + HrTimerMultiplyFractionalPart(TIME_ONE_SECOND + ClockPpb + ClockSlew, timerCount, HR_TIMER_COUNT_PER_SECOND);
 }
\ No newline at end of file
--- a/clock/clock.c	Mon Dec 03 12:50:10 2018 +0000
+++ b/clock/clock.c	Tue Dec 04 12:26:27 2018 +0000
@@ -7,7 +7,7 @@
 #include  "clksync.h"
 #include    "clktm.h"
 #include      "rtc.h"
-#include    "timer.h"
+#include  "hrtimer.h"
 #include  "mstimer.h"
 #include     "scan.h"
 #include      "led.h"
@@ -34,7 +34,7 @@
 void ClockInit()
 {
     RtcInit();
-    TimerInit();
+    HrTimerInit();
     ClockStateInit();
 }
 
@@ -44,7 +44,7 @@
 
     static uint32_t secondsBaseCount = 0;
     
-    bool hadSecond = TimerRepetitiveTick(&secondsBaseCount, TIMER_COUNT_PER_SECOND);
+    bool hadSecond = HrTimerRepetitiveTick(&secondsBaseCount, HR_TIMER_COUNT_PER_SECOND);
     
     //Update the times whenever there has been a system second
     if (hadSecond) TimeIncrementByOneSecond(secondsBaseCount);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hrtimer/hrtimer.c	Tue Dec 04 12:26:27 2018 +0000
@@ -0,0 +1,61 @@
+#include <stdint.h>
+#include <stdbool.h>
+
+#include   "hrtimer.h"
+
+#define TCR  (*((volatile unsigned *) 0x40004004))
+#define TC   (*((volatile unsigned *) 0x40004008))
+#define PR   (*((volatile unsigned *) 0x4000400C))
+#define MCR  (*((volatile unsigned *) 0x40004014))
+#define CTCR (*((volatile unsigned *) 0x40004070))
+
+uint32_t HrTimerCount()
+{
+    return TC;
+}
+uint32_t HrTimerSinceRepetitive(uint32_t* pLastCount)
+{
+    uint32_t thisCount = TC;
+    uint32_t period = thisCount - *pLastCount;    
+    *pLastCount = thisCount;
+    return period;
+}
+uint32_t HrTimerSince(uint32_t lastCount)
+{
+    return TC - lastCount; 
+}
+uint32_t HrTimerSinceMs(uint32_t lastCount)
+{
+    uint32_t count = TC - lastCount;
+    return count / (HR_TIMER_COUNT_PER_SECOND / 1000);
+}
+
+bool HrTimerRepetitiveTick(uint32_t* pLastCount, uint32_t interval)
+{
+    if (TC - *pLastCount >= interval) //All unsigned wrap around arithmetic
+    {
+        *pLastCount += interval;
+        return true;
+    }
+    return false;
+}
+
+int32_t HrTimerMultiplyFractionalPart(int32_t value, uint32_t part, uint32_t interval)
+{
+    int64_t fraction;
+    
+    fraction = part;
+    fraction <<= 32;
+    fraction /= interval;
+              
+    return (value * fraction) >> 32;
+}
+
+void HrTimerInit()
+{    
+    TCR     =     2; // 21.6.2 Timer Control Register - Reset TC and PC.
+    CTCR    =     0; // 21.6.3 Count Control Register - Timer mode
+    PR      =     0; // 21.6.5 Prescale register      - Don't prescale 96MHz clock (divide by PR+1).
+    MCR     =     0; // 21.6.8 Match Control Register - no interrupt or reset
+    TCR     =     1; // 21.6.2 Timer Control Register - Enable TC and PC
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hrtimer/hrtimer.h	Tue Dec 04 12:26:27 2018 +0000
@@ -0,0 +1,15 @@
+#include <stdint.h>
+#include <stdbool.h>
+
+extern uint32_t HrTimerCount(void);
+
+extern bool     HrTimerRepetitiveTick (uint32_t* pLastCount, uint32_t interval);
+
+extern uint32_t HrTimerSinceRepetitive(uint32_t* pLastCount);
+extern uint32_t HrTimerSince          (uint32_t   lastCount);
+extern uint32_t HrTimerSinceMs        (uint32_t   lastCount);
+
+extern int32_t  HrTimerMultiplyFractionalPart(int32_t value, uint32_t part, uint32_t interval);
+extern void     HrTimerInit(void);
+
+#define HR_TIMER_COUNT_PER_SECOND 96000000UL
--- a/mstimer/mstimer.c	Mon Dec 03 12:50:10 2018 +0000
+++ b/mstimer/mstimer.c	Tue Dec 04 12:26:27 2018 +0000
@@ -2,7 +2,7 @@
 #include <stdbool.h>
 
 #include "mstimer.h"
-#include   "timer.h"
+#include "hrtimer.h"
 
 uint32_t MsTimerCount = 0;
 
@@ -25,5 +25,5 @@
 {
     static uint32_t baseCount = 0;
     
-    if (TimerRepetitiveTick(&baseCount, TIMER_COUNT_PER_SECOND / 1000)) MsTimerCount++;
+    if (HrTimerRepetitiveTick(&baseCount, HR_TIMER_COUNT_PER_SECOND / 1000)) MsTimerCount++;
 }
\ No newline at end of file
--- a/mstimer/mstimer.h	Mon Dec 03 12:50:10 2018 +0000
+++ b/mstimer/mstimer.h	Tue Dec 04 12:26:27 2018 +0000
@@ -2,6 +2,9 @@
 #include <stdbool.h>
 
 extern uint32_t MsTimerCount;
-extern bool     MsTimerHasElapsed    (uint32_t   baseMsCount, uint32_t intervalMs);
+
 extern bool     MsTimerRepetitiveTick(uint32_t* pBaseMsCount, uint32_t intervalMs);
+
+extern bool     MsTimerHasElapsed    (uint32_t   baseMsCount, uint32_t intervalMs);
+
 extern void     MsTimerMain(void);
\ No newline at end of file
--- a/scan/scan.c	Mon Dec 03 12:50:10 2018 +0000
+++ b/scan/scan.c	Tue Dec 04 12:26:27 2018 +0000
@@ -1,5 +1,5 @@
 #include <stdint.h>
-#include "timer.h"
+#include "hrtimer.h"
 
 uint32_t ScanAverage = 0;
 uint32_t ScanMinimum = 10000;
@@ -12,7 +12,7 @@
 
     bool firstScan = !scanTimer;
 
-    uint32_t elapsed = TimerSinceRepetitive(&scanTimer);
+    uint32_t elapsed = HrTimerSinceRepetitive(&scanTimer);
 
     if (firstScan) return;
 
--- a/timer/timer.c	Mon Dec 03 12:50:10 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,61 +0,0 @@
-#include <stdint.h>
-#include <stdbool.h>
-
-#include   "timer.h"
-
-#define TCR  (*((volatile unsigned *) 0x40004004))
-#define TC   (*((volatile unsigned *) 0x40004008))
-#define PR   (*((volatile unsigned *) 0x4000400C))
-#define MCR  (*((volatile unsigned *) 0x40004014))
-#define CTCR (*((volatile unsigned *) 0x40004070))
-
-uint32_t TimerCount()
-{
-    return TC;
-}
-uint32_t TimerSinceRepetitive(uint32_t* pLastCount)
-{
-    uint32_t thisCount = TC;
-    uint32_t period = thisCount - *pLastCount;    
-    *pLastCount = thisCount;
-    return period;
-}
-uint32_t TimerSinceCount(uint32_t lastCount)
-{
-    return TC - lastCount; 
-}
-uint32_t TimerSinceMs(uint32_t lastCount)
-{
-    uint32_t count = TC - lastCount;
-    return count / TIMER_COUNT_PER_MS;
-}
-
-bool TimerRepetitiveTick(uint32_t* pLastCount, uint32_t interval)
-{
-    if (TC - *pLastCount >= interval) //All unsigned wrap around arithmetic
-    {
-        *pLastCount += interval;
-        return true;
-    }
-    return false;
-}
-
-int32_t TimerMultiplyFractionalPart(int32_t value, uint32_t countSinceLast, uint32_t interval)
-{
-    int64_t fraction;
-    
-    fraction = countSinceLast;
-    fraction <<= 32;
-    fraction /= interval;
-              
-    return (value * fraction) >> 32;
-}
-
-void TimerInit()
-{    
-    TCR     =     2; // 21.6.2 Timer Control Register - Reset TC and PC.
-    CTCR    =     0; // 21.6.3 Count Control Register - Timer mode
-    PR      =     0; // 21.6.5 Prescale register      - Don't prescale 96MHz clock (divide by PR+1).
-    MCR     =     0; // 21.6.8 Match Control Register - no interrupt or reset
-    TCR     =     1; // 21.6.2 Timer Control Register - Enable TC and PC
-}
--- a/timer/timer.h	Mon Dec 03 12:50:10 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,15 +0,0 @@
-#include <stdint.h>
-#include <stdbool.h>
-
-extern uint32_t TimerCount(void);
-extern uint32_t TimerSinceRepetitive(uint32_t* pLastCount);
-extern uint32_t TimerSinceCount(uint32_t lastCount);
-extern uint32_t TimerSinceMs(uint32_t lastCount);
-extern bool     TimerRepetitiveTick(uint32_t* pLastCount, uint32_t interval);
-
-extern int32_t  TimerMultiplyFractionalPart(int32_t value, uint32_t timerCountSinceLast, uint32_t interval);
-extern void     TimerInit(void);
-
-#define TIMER_COUNT_PER_SECOND 96000000UL
-#define TIMER_COUNT_PER_MS     (TIMER_COUNT_PER_SECOND / 1000);
-#define TIMER_COUNT_PER_US     (TIMER_COUNT_PER_MS     / 1000);