Andrew Boyson / clock

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Mon Jun 26 09:59:21 2017 +0000
Revision:
0:33686e88f09a
Child:
3:92cfc43d493a
Created as stand alone library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 0:33686e88f09a 1 #include "mbed.h"
andrewboyson 0:33686e88f09a 2 #include "rtc.h"
andrewboyson 0:33686e88f09a 3 #include "time.h"
andrewboyson 0:33686e88f09a 4 #include "state.h"
andrewboyson 0:33686e88f09a 5
andrewboyson 0:33686e88f09a 6 #define ONE_BILLION 1000000000
andrewboyson 0:33686e88f09a 7
andrewboyson 0:33686e88f09a 8 #define COUNT_PER_TICK 96000000
andrewboyson 0:33686e88f09a 9 #define SLEW_MAX_NS_PER_S 100000000
andrewboyson 0:33686e88f09a 10
andrewboyson 0:33686e88f09a 11 static int64_t nsTickCount;
andrewboyson 0:33686e88f09a 12 static int64_t nsFreqCount;
andrewboyson 0:33686e88f09a 13 static int64_t nsTimeCount;
andrewboyson 0:33686e88f09a 14
andrewboyson 0:33686e88f09a 15 int32_t TickSlew = 0; //ns - up to +/- 2.147s of slew
andrewboyson 0:33686e88f09a 16 int32_t TickPpb = 0;
andrewboyson 0:33686e88f09a 17 bool TickIsSet = false;
andrewboyson 0:33686e88f09a 18
andrewboyson 0:33686e88f09a 19 #define CLOCK_TIC 0
andrewboyson 0:33686e88f09a 20 #define CLOCK_INT 1
andrewboyson 0:33686e88f09a 21 #define CLOCK_ABS 2
andrewboyson 0:33686e88f09a 22
andrewboyson 0:33686e88f09a 23 static void tick(void) //Called from main if the timer has overflowed
andrewboyson 0:33686e88f09a 24 {
andrewboyson 0:33686e88f09a 25 //Reset the timer interrupt
andrewboyson 0:33686e88f09a 26 LPC_TIM1->IR |= 1;
andrewboyson 0:33686e88f09a 27
andrewboyson 0:33686e88f09a 28 //reset the tick timer
andrewboyson 0:33686e88f09a 29 LPC_TIM1->TCR = 2; // 21.6.2 Timer Control Register - Reset TC and PC.
andrewboyson 0:33686e88f09a 30 LPC_TIM1->TCR = 1; // 21.6.2 Timer Control Register - Enable TC and PC
andrewboyson 0:33686e88f09a 31
andrewboyson 0:33686e88f09a 32 //Calculate the new counts
andrewboyson 0:33686e88f09a 33 nsTickCount += ONE_BILLION;
andrewboyson 0:33686e88f09a 34 nsFreqCount += TickPpb;
andrewboyson 0:33686e88f09a 35 nsTimeCount += TickSlew;
andrewboyson 0:33686e88f09a 36
andrewboyson 0:33686e88f09a 37 //Feedback the amount slewed
andrewboyson 0:33686e88f09a 38 TickSlew = 0;
andrewboyson 0:33686e88f09a 39
andrewboyson 0:33686e88f09a 40 }
andrewboyson 0:33686e88f09a 41 static volatile int64_t nsTickSnapshot;
andrewboyson 0:33686e88f09a 42 static volatile int64_t nsFreqSnapshot;
andrewboyson 0:33686e88f09a 43 static volatile int64_t nsTimeSnapshot;
andrewboyson 0:33686e88f09a 44 static volatile int32_t timerSnapshot;
andrewboyson 0:33686e88f09a 45
andrewboyson 0:33686e88f09a 46 void TickSaveSnapshotI()
andrewboyson 0:33686e88f09a 47 {
andrewboyson 0:33686e88f09a 48 timerSnapshot = LPC_TIM1->TC;
andrewboyson 0:33686e88f09a 49 nsTickSnapshot = nsTickCount;
andrewboyson 0:33686e88f09a 50 nsFreqSnapshot = nsFreqCount;
andrewboyson 0:33686e88f09a 51 nsTimeSnapshot = nsTimeCount;
andrewboyson 0:33686e88f09a 52 }
andrewboyson 0:33686e88f09a 53 void TickRetrieveSnapshot(int64_t* pNsInt, int64_t* pNsAbs)
andrewboyson 0:33686e88f09a 54 {
andrewboyson 0:33686e88f09a 55 int64_t fraction = ((int64_t)timerSnapshot << 32) / COUNT_PER_TICK;
andrewboyson 0:33686e88f09a 56 int64_t nsFraction;
andrewboyson 0:33686e88f09a 57
andrewboyson 0:33686e88f09a 58 int64_t nsBase = nsTickSnapshot;
andrewboyson 0:33686e88f09a 59 int64_t nsPerTick = ONE_BILLION;
andrewboyson 0:33686e88f09a 60
andrewboyson 0:33686e88f09a 61 nsBase += nsFreqSnapshot;
andrewboyson 0:33686e88f09a 62 nsPerTick += TickPpb;
andrewboyson 0:33686e88f09a 63 nsFraction = (nsPerTick * fraction) >> 32;
andrewboyson 0:33686e88f09a 64 *pNsInt = nsBase + nsFraction;
andrewboyson 0:33686e88f09a 65
andrewboyson 0:33686e88f09a 66 nsBase += nsTimeSnapshot;
andrewboyson 0:33686e88f09a 67 nsPerTick += TickSlew;
andrewboyson 0:33686e88f09a 68 nsFraction = (nsPerTick * fraction) >> 32;
andrewboyson 0:33686e88f09a 69 *pNsAbs = nsBase + nsFraction;
andrewboyson 0:33686e88f09a 70 }
andrewboyson 0:33686e88f09a 71 static int64_t getClock(char index)
andrewboyson 0:33686e88f09a 72 {
andrewboyson 0:33686e88f09a 73 //Get the base and fractional values while protected from a tick interrupt
andrewboyson 0:33686e88f09a 74 __disable_irq();
andrewboyson 0:33686e88f09a 75 int64_t nsBase = nsTickCount;
andrewboyson 0:33686e88f09a 76 int64_t nsPerTick = ONE_BILLION;
andrewboyson 0:33686e88f09a 77 if (index >= CLOCK_INT)
andrewboyson 0:33686e88f09a 78 {
andrewboyson 0:33686e88f09a 79 nsBase += nsFreqCount;
andrewboyson 0:33686e88f09a 80 nsPerTick += TickPpb;
andrewboyson 0:33686e88f09a 81 }
andrewboyson 0:33686e88f09a 82 if (index >= CLOCK_ABS)
andrewboyson 0:33686e88f09a 83 {
andrewboyson 0:33686e88f09a 84 nsBase += nsTimeCount;
andrewboyson 0:33686e88f09a 85 nsPerTick += TickSlew;
andrewboyson 0:33686e88f09a 86 }
andrewboyson 0:33686e88f09a 87 int32_t count = LPC_TIM1->TC;
andrewboyson 0:33686e88f09a 88 __enable_irq();
andrewboyson 0:33686e88f09a 89
andrewboyson 0:33686e88f09a 90 //Now calculate everything
andrewboyson 0:33686e88f09a 91 int64_t fraction = ((int64_t)count << 32) / COUNT_PER_TICK;
andrewboyson 0:33686e88f09a 92 int64_t nsFraction = (nsPerTick * fraction) >> 32;
andrewboyson 0:33686e88f09a 93
andrewboyson 0:33686e88f09a 94 //Return the base plus the fractional part
andrewboyson 0:33686e88f09a 95 return nsBase + nsFraction;
andrewboyson 0:33686e88f09a 96
andrewboyson 0:33686e88f09a 97 }
andrewboyson 0:33686e88f09a 98 int64_t TickGetTic() {return getClock(CLOCK_TIC); }
andrewboyson 0:33686e88f09a 99 int64_t TickGetInt() {return getClock(CLOCK_INT); }
andrewboyson 0:33686e88f09a 100 int64_t TickGetAbs() {return getClock(CLOCK_ABS); }
andrewboyson 0:33686e88f09a 101
andrewboyson 0:33686e88f09a 102 void TickSet(int64_t extClock)
andrewboyson 0:33686e88f09a 103 {
andrewboyson 0:33686e88f09a 104 nsTickCount = 0;
andrewboyson 0:33686e88f09a 105 nsFreqCount = 0;
andrewboyson 0:33686e88f09a 106 nsTimeCount = extClock;
andrewboyson 0:33686e88f09a 107 LPC_TIM1->TCR = 2; // 21.6.2 Timer Control Register - Reset TC and PC.
andrewboyson 0:33686e88f09a 108 LPC_TIM1->TCR = 1; // 21.6.2 Timer Control Register - Enable TC and PC.
andrewboyson 0:33686e88f09a 109 TickIsSet = true;
andrewboyson 0:33686e88f09a 110 }
andrewboyson 0:33686e88f09a 111
andrewboyson 0:33686e88f09a 112 int TickInit(void)
andrewboyson 0:33686e88f09a 113 {
andrewboyson 0:33686e88f09a 114 nsTickCount = 0;
andrewboyson 0:33686e88f09a 115 nsFreqCount = 0;
andrewboyson 0:33686e88f09a 116 nsTimeCount = 0;
andrewboyson 0:33686e88f09a 117
andrewboyson 0:33686e88f09a 118 TickPpb = StateReadPpb();
andrewboyson 0:33686e88f09a 119 TickSlew = 0;
andrewboyson 0:33686e88f09a 120 TickIsSet = false;
andrewboyson 0:33686e88f09a 121
andrewboyson 0:33686e88f09a 122 LPC_SC->PCLKSEL0 &= ~0x20; // 4.7.3 Peripheral Clock Selection - PCLK_peripheral PCLK_TIMER1 01xxxx = CCLK - reset bit 5
andrewboyson 0:33686e88f09a 123 LPC_SC->PCLKSEL0 |= 0x10; // set bit 4
andrewboyson 0:33686e88f09a 124 LPC_SC->PCONP |= 4; // 4.8.9 Power Control for Peripherals register - Timer1 Power On
andrewboyson 0:33686e88f09a 125 LPC_TIM1->TCR = 2; // 21.6.2 Timer Control Register - Reset TC and PC.
andrewboyson 0:33686e88f09a 126 LPC_TIM1->CTCR = 0; // 21.6.3 Count Control Register - Timer mode
andrewboyson 0:33686e88f09a 127 LPC_TIM1->PR = 0; // 21.6.5 Prescale register - Don't prescale 96MHz clock (divide by PR+1).
andrewboyson 0:33686e88f09a 128 LPC_TIM1->MR0 = COUNT_PER_TICK; // 21.6.7 Match Register 0 - Match count
andrewboyson 0:33686e88f09a 129 LPC_TIM1->MCR = 1; // 21.6.8 Match Control Register - interrupt on match
andrewboyson 0:33686e88f09a 130 LPC_TIM1->TCR = 1; // 21.6.2 Timer Control Register - Enable TC and PC
andrewboyson 0:33686e88f09a 131
andrewboyson 0:33686e88f09a 132 NVIC_SetVector(TIMER1_IRQn, (uint32_t)&tick);
andrewboyson 0:33686e88f09a 133 NVIC_EnableIRQ(TIMER1_IRQn);
andrewboyson 0:33686e88f09a 134
andrewboyson 0:33686e88f09a 135 return 0;
andrewboyson 0:33686e88f09a 136 }