Andrew Boyson / clock

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Thu Feb 27 13:02:18 2020 +0000
Revision:
73:286a739f7c05
Parent:
68:807c1c7b2c22
Added a method to allow the UTC offset to be changed with a corresponding adjustment to TAI so that the resulting UTC time was not changed. This was used in the clock web page.

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 }