Uses Timer 0 and the RTC to keep accurate time. It can accept a PPS from an external source like a GPS or a regular time stamp from an external source like an NTP server. It also provides timer functions to 96MHz up to 44 seconds using the CPU clock.

Dependents:   oldheating gps motorhome heating

Description

The clock library provides a number of separate functions:

  • 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 timer (TAI) 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
  • clk A clock which is synchronised to an external source

High resolution timer

hrtimer 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 ClkInit to start it, thereafter it free runs.
No dependencies.

Millisecond timer

mstimer 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 ClkMain.
Depends on timer.

Clock timer

clktimer uses the signed 64 bit clock time.
Depends on clock and hence hrtimer.

Scan times

scan uses the high resolution timer to calculate the max, min and average scan times.
It has a main routine called from ClkMain.
Depends on hrtimer.

Real time clock

rtc contains routines to save and restore the time in the battery backed real time clock.
Parameters are struct tm.
No dependencies.

Local and UTC manipulation

tm contains

  • the typedef time64 which contains the count of seconds since 1970; just like time_t but based on int64_t to avoid the 2038 problem
  • a number of functions for manipulating time64 and struct tm times

No dependencies.

Clk

clk contains

  • settings
  • functions to save and restore the time to the RTC. Depends on timer, rtc and tm.

clktime increments the time by 1 each second via clk.c from timer.c.
It increments the signed 64 bit time count using the ppb and slew (governed by clkgov.c).
When the time is requested it uses its count and a proportion of the elapsed second from the high resolution timer to calculate the exact time.
See time-formats.text for the clock time format.

clkgov governs the ppb and slew to synchronise the clock time with an external source.
PPB is stored in GPREG0 whenever it is set and retrieved during initialisation.
It takes external time from either:

  • a long term source such as NTP
  • a pulse per second (PPS) such as GPS

clkntp converts clock time to NTP time and vice versa.

clktm converts clock time to struct tm and vice versa

clkutc maintains the era offset (leap seconds count).
The era offset and other information is stored in GPREG1 whenever it is set and retrieved during initialisation.
It contains:

  • the current era offset
  • for the next epoch:
    • its start month (as year and month since 1970)
    • its state: normal; waiting to leap forward; waiting to leap back; leaping forward (second 60)
  • conversion routines between tai and utc (clk time is tai)

Clock time formats

Criteria

Resolution

PPS
We get an interrupt each second which we can resolve to a microsecond. The divisor is 1000. To carry this resolution into the governor we need 1 ppb.
NTP
Suppose we are adding compensation every second, sampling every 4 hours and want to represent 3ms of error with a divisor of 10: that would need a resolution of 23 ppb.
The best temperature compensated crystal oscillators can manage about 1ppm (see Wikipedia) long term or 10 ppb short term.

Lifetime

Needs to keep going during the lifetime of this, or other related, projects. At least a century (so 2100) but more than a few centuries is likely to be pointless

Ease of transforming to NTP, time_t

A count of decimal times - ms, us, ns or ps - can only be transformed using multiplication or division by 1000s. NTP and time_t use binary fractions about a fixed decimal point.

Ease of representing ppm or ppb

A count of decimal times is best but a count of fractions is near enough as 10 bits (1024) is very close to being 1000. As long as it is only needed for a correction such as ppb the approximation would only manifest itself as a 7% error.

The version chosen

1 bit sign, 33 bits for seconds, 30 bits for fraction

+/- 272 years at 1ns or 1 ppb per second
Clock era is 1970

Advantages:

  • adequately representing the freq adjustments for pps
  • simple transformation to NTP and time_t
  • approximates to ns or, with a bit shift, to us or ms
  • adequately covers the next two centuries
  • one unit represents 1 ppb for display

Disadvantage:

  • none

Alternatives considered

1 bit sign, 43 bits for seconds, 20 bits for fraction

+/- 278,731 years at 1us or 1 ppm per second

Advantages:

  • a wide coverage
  • simple transformation to NTP and time_t
  • approximates to us or, with a bitwise shift, to ms
  • one unit represents 1 ppm for display

Disadvantage:

  • not able to reflect the freq adjustments for pps.

1 bit sign, 35bits for seconds, 28bits for fraction

+/- 1089 years at 3ns or 3ppb per second
looks like SSSS SSSS S.FFF FFFF in hex

Advantages:

  • easily represented in hex
  • a wide coverage
  • simple transformation to NTP and time_t

Disadvantage:

  • one unit doesn't approximate to anything simple

32 bits for seconds, 32 bits for fraction

Ntp time with an era of 1900
1900 to 2036 with a resolution of 250ps or 0.25 ppb

Advantages:

  • Already NTP and easily converted to time_t

Disadvantage:

  • Will rollover in 2036

Use 96MHz int64 count

+/- 3044 years with a resolution of 10ns or 10ppb per second

Advantages:

  • a wide coverage

Disadvantage:

  • cannot use simple bit shifts to transform to NTP and time_t
  • not transferable to a system with a different clock rate

Use a count of ns

+/- 292 years at 1ns or 1ppb per second

Advantages:

  • adequately representing the freq adjustments for pps
  • easily usable with ppb and ns
  • a wide coverage

Disadvantage:

  • cannot use simple bit shifts to transform to NTP and time_t
Committer:
andrewboyson
Date:
Mon Jul 27 10:30:10 2020 +0000
Revision:
76:c2035b7754fe
Parent:
73:286a739f7c05
Corrected haveFullTime in sync time PPS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 32:f915ccb1ece3 1 #include <stdint.h>
andrewboyson 32:f915ccb1ece3 2 #include <stdbool.h>
andrewboyson 68:807c1c7b2c22 3 #include <arm_compat.h> //Used for the __[en/dis]able_irq intrinsics
andrewboyson 32:f915ccb1ece3 4
andrewboyson 32:f915ccb1ece3 5 #include "rtc.h"
andrewboyson 32:f915ccb1ece3 6 #include "tm.h"
andrewboyson 46:d3d56cb47940 7 #include "clk.h"
andrewboyson 33:b9e3c06e7dab 8 #include "clktime.h"
andrewboyson 47:fd2af868c10a 9 #include "clkgov.h"
andrewboyson 40:53666b1a5848 10 #include "hrtimer.h"
andrewboyson 32:f915ccb1ece3 11 #include "led.h"
andrewboyson 32:f915ccb1ece3 12 #include "log.h"
andrewboyson 32:f915ccb1ece3 13
andrewboyson 57:4daf2e423b27 14 static clktime tickCount = 0;
andrewboyson 57:4daf2e423b27 15 static clktime slewCount = 0;
andrewboyson 57:4daf2e423b27 16 static bool countIsSet = false;
andrewboyson 32:f915ccb1ece3 17
andrewboyson 41:8cd859cd1475 18 bool ClkTimeIsSet() { return countIsSet; }
andrewboyson 33:b9e3c06e7dab 19
andrewboyson 63:28738aaad2a8 20 static uint32_t hrTimerAtLastIncrement = 0; //Set by the increment function
andrewboyson 32:f915ccb1ece3 21
andrewboyson 57:4daf2e423b27 22 clktime ClkTimeGet()
andrewboyson 33:b9e3c06e7dab 23 {
andrewboyson 63:28738aaad2a8 24 return tickCount + slewCount + HrTimerProRata(CLK_TIME_ONE_SECOND + ClkGovGetPpb() + ClkGovGetSlew(), HrTimerSince(hrTimerAtLastIncrement));
andrewboyson 33:b9e3c06e7dab 25 }
andrewboyson 32:f915ccb1ece3 26
andrewboyson 57:4daf2e423b27 27 void ClkTimeSet(clktime extClock)
andrewboyson 32:f915ccb1ece3 28 {
andrewboyson 63:28738aaad2a8 29 clktime timerCountSinceLastSecond = HrTimerSince(hrTimerAtLastIncrement);
andrewboyson 57:4daf2e423b27 30 clktime fraction = (timerCountSinceLastSecond << CLK_TIME_ONE_SECOND_SHIFT) / HR_TIMER_COUNT_PER_SECOND;
andrewboyson 57:4daf2e423b27 31 clktime ticks = extClock - fraction;
andrewboyson 32:f915ccb1ece3 32
andrewboyson 68:807c1c7b2c22 33 __disable_irq();
andrewboyson 32:f915ccb1ece3 34 tickCount = ticks;
andrewboyson 32:f915ccb1ece3 35 slewCount = 0;
andrewboyson 68:807c1c7b2c22 36 __enable_irq();
andrewboyson 32:f915ccb1ece3 37
andrewboyson 32:f915ccb1ece3 38 countIsSet = true;
andrewboyson 32:f915ccb1ece3 39 }
andrewboyson 73:286a739f7c05 40 void ClkTimeAdjustSeconds(int seconds)
andrewboyson 73:286a739f7c05 41 {
andrewboyson 73:286a739f7c05 42 __disable_irq();
andrewboyson 73:286a739f7c05 43 tickCount += (clktime)seconds << CLK_TIME_ONE_SECOND_SHIFT;
andrewboyson 73:286a739f7c05 44 __enable_irq();
andrewboyson 73:286a739f7c05 45 }
andrewboyson 41:8cd859cd1475 46 void ClkTimeIncrementByOneSecond(uint32_t startCount)
andrewboyson 34:aeb58975e61a 47 {
andrewboyson 68:807c1c7b2c22 48 __disable_irq();
andrewboyson 63:28738aaad2a8 49 hrTimerAtLastIncrement = startCount;
andrewboyson 47:fd2af868c10a 50 tickCount += CLK_TIME_ONE_SECOND + ClkGovGetPpb();
andrewboyson 47:fd2af868c10a 51 slewCount += ClkGovGetSlew();
andrewboyson 47:fd2af868c10a 52 ClkGovSetSlew(0);
andrewboyson 68:807c1c7b2c22 53 __enable_irq();
andrewboyson 33:b9e3c06e7dab 54 }
andrewboyson 32:f915ccb1ece3 55
andrewboyson 57:4daf2e423b27 56 static volatile clktime tickSnapshot;
andrewboyson 57:4daf2e423b27 57 static volatile clktime slewSnapshot;
andrewboyson 32:f915ccb1ece3 58 static volatile uint32_t timerSnapshot;
andrewboyson 32:f915ccb1ece3 59
andrewboyson 41:8cd859cd1475 60 void ClkTimeSaveSnapshot()
andrewboyson 32:f915ccb1ece3 61 {
andrewboyson 63:28738aaad2a8 62 timerSnapshot = HrTimerSince(hrTimerAtLastIncrement);
andrewboyson 32:f915ccb1ece3 63 tickSnapshot = tickCount;
andrewboyson 32:f915ccb1ece3 64 slewSnapshot = slewCount;
andrewboyson 32:f915ccb1ece3 65 }
andrewboyson 57:4daf2e423b27 66 void ClkTimesGetFromSnapshot(clktime* pInt, clktime* pAbs)
andrewboyson 32:f915ccb1ece3 67 {
andrewboyson 63:28738aaad2a8 68 *pInt = tickSnapshot + HrTimerProRata(CLK_TIME_ONE_SECOND + ClkGovGetPpb(), timerSnapshot);
andrewboyson 63:28738aaad2a8 69 *pAbs = tickSnapshot + slewSnapshot + HrTimerProRata(CLK_TIME_ONE_SECOND + ClkGovGetPpb() + ClkGovGetSlew(), timerSnapshot);
andrewboyson 32:f915ccb1ece3 70 }