Andrew Boyson / clock

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Thu Oct 19 20:59:15 2017 +0000
Revision:
14:7ef557918bb1
Parent:
13:0200519c9bbf
Child:
16:933cbe190bb0
Added 'elapsed' for use with debouncing switches

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 14:7ef557918bb1 1 #include "mbed.h"
andrewboyson 14:7ef557918bb1 2 #include "rtc.h"
andrewboyson 14:7ef557918bb1 3 #include "time.h"
andrewboyson 14:7ef557918bb1 4 #include "tick.h"
andrewboyson 0:33686e88f09a 5
andrewboyson 0:33686e88f09a 6 #define ONE_BILLION 1000000000
andrewboyson 0:33686e88f09a 7
andrewboyson 0:33686e88f09a 8 static int64_t nsTickCount;
andrewboyson 0:33686e88f09a 9 static int64_t nsFreqCount;
andrewboyson 0:33686e88f09a 10 static int64_t nsTimeCount;
andrewboyson 5:a3e37ce4975d 11 static bool nsCountIsSet = false;
andrewboyson 5:a3e37ce4975d 12 bool TickIsSet() { return nsCountIsSet; }
andrewboyson 5:a3e37ce4975d 13 void TickSet(int64_t extClock)
andrewboyson 5:a3e37ce4975d 14 {
andrewboyson 5:a3e37ce4975d 15 nsTickCount = 0;
andrewboyson 5:a3e37ce4975d 16 nsFreqCount = 0;
andrewboyson 5:a3e37ce4975d 17 nsTimeCount = extClock;
andrewboyson 5:a3e37ce4975d 18 LPC_TIM1->TCR = 2; // 21.6.2 Timer Control Register - Reset TC and PC.
andrewboyson 5:a3e37ce4975d 19 LPC_TIM1->TCR = 1; // 21.6.2 Timer Control Register - Enable TC and PC.
andrewboyson 5:a3e37ce4975d 20 nsCountIsSet = true;
andrewboyson 5:a3e37ce4975d 21 }
andrewboyson 0:33686e88f09a 22
andrewboyson 5:a3e37ce4975d 23 int32_t slew = 0; //ns - up to +/- 2.147s of slew
andrewboyson 5:a3e37ce4975d 24 int32_t TickGetSlew() { return slew; }
andrewboyson 5:a3e37ce4975d 25 void TickSetSlew(int32_t value) { slew = value; }
andrewboyson 5:a3e37ce4975d 26
andrewboyson 5:a3e37ce4975d 27 int32_t ppb = 0;
andrewboyson 5:a3e37ce4975d 28 int32_t TickGetPpb () { return ppb; }
andrewboyson 5:a3e37ce4975d 29 void TickSetPpb (int32_t value) { ppb = value; LPC_RTC->GPREG0 = ppb; }
andrewboyson 5:a3e37ce4975d 30 void TickAddPpb (int32_t value) { ppb += value; LPC_RTC->GPREG0 = ppb; }
andrewboyson 0:33686e88f09a 31
andrewboyson 0:33686e88f09a 32 #define CLOCK_TIC 0
andrewboyson 0:33686e88f09a 33 #define CLOCK_INT 1
andrewboyson 0:33686e88f09a 34 #define CLOCK_ABS 2
andrewboyson 0:33686e88f09a 35
andrewboyson 9:cd30749dfa3d 36 static void tick(void)
andrewboyson 9:cd30749dfa3d 37 {//Checked the number of counts and this routine takes 46 counts or about 0.5us
andrewboyson 9:cd30749dfa3d 38
andrewboyson 0:33686e88f09a 39 //Reset the timer interrupt
andrewboyson 0:33686e88f09a 40 LPC_TIM1->IR |= 1;
andrewboyson 0:33686e88f09a 41
andrewboyson 0:33686e88f09a 42 //reset the tick timer
andrewboyson 0:33686e88f09a 43 LPC_TIM1->TCR = 2; // 21.6.2 Timer Control Register - Reset TC and PC.
andrewboyson 0:33686e88f09a 44 LPC_TIM1->TCR = 1; // 21.6.2 Timer Control Register - Enable TC and PC
andrewboyson 0:33686e88f09a 45
andrewboyson 0:33686e88f09a 46 //Calculate the new counts
andrewboyson 0:33686e88f09a 47 nsTickCount += ONE_BILLION;
andrewboyson 5:a3e37ce4975d 48 nsFreqCount += ppb;
andrewboyson 5:a3e37ce4975d 49 nsTimeCount += slew;
andrewboyson 0:33686e88f09a 50
andrewboyson 0:33686e88f09a 51 //Feedback the amount slewed
andrewboyson 5:a3e37ce4975d 52 slew = 0;
andrewboyson 0:33686e88f09a 53 }
andrewboyson 0:33686e88f09a 54 static volatile int64_t nsTickSnapshot;
andrewboyson 0:33686e88f09a 55 static volatile int64_t nsFreqSnapshot;
andrewboyson 0:33686e88f09a 56 static volatile int64_t nsTimeSnapshot;
andrewboyson 0:33686e88f09a 57 static volatile int32_t timerSnapshot;
andrewboyson 0:33686e88f09a 58
andrewboyson 0:33686e88f09a 59 void TickSaveSnapshotI()
andrewboyson 0:33686e88f09a 60 {
andrewboyson 0:33686e88f09a 61 timerSnapshot = LPC_TIM1->TC;
andrewboyson 0:33686e88f09a 62 nsTickSnapshot = nsTickCount;
andrewboyson 0:33686e88f09a 63 nsFreqSnapshot = nsFreqCount;
andrewboyson 0:33686e88f09a 64 nsTimeSnapshot = nsTimeCount;
andrewboyson 0:33686e88f09a 65 }
andrewboyson 14:7ef557918bb1 66 static void makeTimesFromCounts(int32_t timerCount, int64_t tickNs, int64_t freqNs, int64_t timeNs, int64_t* pNsInt, int64_t* pNsAbs)
andrewboyson 0:33686e88f09a 67 {
andrewboyson 14:7ef557918bb1 68 int64_t fraction = ((int64_t)timerCount << 32) / TICK_COUNT_PER_SECOND;
andrewboyson 0:33686e88f09a 69 int64_t nsFraction;
andrewboyson 0:33686e88f09a 70
andrewboyson 14:7ef557918bb1 71 int64_t nsBase = tickNs;
andrewboyson 0:33686e88f09a 72 int64_t nsPerTick = ONE_BILLION;
andrewboyson 0:33686e88f09a 73
andrewboyson 14:7ef557918bb1 74 nsBase += freqNs;
andrewboyson 5:a3e37ce4975d 75 nsPerTick += ppb;
andrewboyson 0:33686e88f09a 76 nsFraction = (nsPerTick * fraction) >> 32;
andrewboyson 0:33686e88f09a 77 *pNsInt = nsBase + nsFraction;
andrewboyson 0:33686e88f09a 78
andrewboyson 14:7ef557918bb1 79 nsBase += timeNs;
andrewboyson 5:a3e37ce4975d 80 nsPerTick += slew;
andrewboyson 0:33686e88f09a 81 nsFraction = (nsPerTick * fraction) >> 32;
andrewboyson 0:33686e88f09a 82 *pNsAbs = nsBase + nsFraction;
andrewboyson 0:33686e88f09a 83 }
andrewboyson 14:7ef557918bb1 84 void TickRetrieveSnapshot(int64_t* pNsInt, int64_t* pNsAbs)
andrewboyson 14:7ef557918bb1 85 {
andrewboyson 14:7ef557918bb1 86 makeTimesFromCounts(timerSnapshot, nsTickSnapshot, nsFreqSnapshot, nsTimeSnapshot, pNsInt, pNsAbs);
andrewboyson 14:7ef557918bb1 87 }
andrewboyson 6:d8a6235486b7 88 void TickGetTimes(int64_t* pNsInt, int64_t* pNsAbs)
andrewboyson 0:33686e88f09a 89 {
andrewboyson 14:7ef557918bb1 90 int32_t timerCount;
andrewboyson 14:7ef557918bb1 91 int64_t tickNs;
andrewboyson 14:7ef557918bb1 92 int64_t freqNs;
andrewboyson 14:7ef557918bb1 93 int64_t timeNs;
andrewboyson 14:7ef557918bb1 94
andrewboyson 0:33686e88f09a 95 __disable_irq();
andrewboyson 14:7ef557918bb1 96 timerCount = LPC_TIM1->TC;
andrewboyson 14:7ef557918bb1 97 tickNs = nsTickCount;
andrewboyson 14:7ef557918bb1 98 freqNs = nsFreqCount;
andrewboyson 14:7ef557918bb1 99 timeNs = nsTimeCount;
andrewboyson 0:33686e88f09a 100 __enable_irq();
andrewboyson 6:d8a6235486b7 101
andrewboyson 14:7ef557918bb1 102 makeTimesFromCounts(timerCount, tickNs, freqNs, timeNs, pNsInt, pNsAbs);
andrewboyson 0:33686e88f09a 103 }
andrewboyson 0:33686e88f09a 104
andrewboyson 0:33686e88f09a 105 int TickInit(void)
andrewboyson 0:33686e88f09a 106 {
andrewboyson 0:33686e88f09a 107 nsTickCount = 0;
andrewboyson 0:33686e88f09a 108 nsFreqCount = 0;
andrewboyson 0:33686e88f09a 109 nsTimeCount = 0;
andrewboyson 0:33686e88f09a 110
andrewboyson 5:a3e37ce4975d 111 ppb = LPC_RTC->GPREG0; //This is saved each time Tickppb is updated
andrewboyson 5:a3e37ce4975d 112 slew = 0;
andrewboyson 5:a3e37ce4975d 113 nsCountIsSet = false;
andrewboyson 0:33686e88f09a 114
andrewboyson 14:7ef557918bb1 115 LPC_SC->PCLKSEL0 &= ~0x20; // 4.7.3 Peripheral Clock Selection - PCLK_peripheral PCLK_TIMER1 01xxxx = CCLK - reset bit 5
andrewboyson 14:7ef557918bb1 116 LPC_SC->PCLKSEL0 |= 0x10; // set bit 4
andrewboyson 14:7ef557918bb1 117 LPC_SC->PCONP |= 4; // 4.8.9 Power Control for Peripherals register - Timer1 Power On
andrewboyson 14:7ef557918bb1 118 LPC_TIM1->TCR = 2; // 21.6.2 Timer Control Register - Reset TC and PC.
andrewboyson 14:7ef557918bb1 119 LPC_TIM1->CTCR = 0; // 21.6.3 Count Control Register - Timer mode
andrewboyson 14:7ef557918bb1 120 LPC_TIM1->PR = 0; // 21.6.5 Prescale register - Don't prescale 96MHz clock (divide by PR+1).
andrewboyson 14:7ef557918bb1 121 LPC_TIM1->MR0 = TICK_COUNT_PER_SECOND; // 21.6.7 Match Register 0 - Match count
andrewboyson 14:7ef557918bb1 122 LPC_TIM1->MCR = 1; // 21.6.8 Match Control Register - interrupt on match
andrewboyson 14:7ef557918bb1 123 LPC_TIM1->TCR = 1; // 21.6.2 Timer Control Register - Enable TC and PC
andrewboyson 0:33686e88f09a 124
andrewboyson 0:33686e88f09a 125 NVIC_SetVector(TIMER1_IRQn, (uint32_t)&tick);
andrewboyson 0:33686e88f09a 126 NVIC_EnableIRQ(TIMER1_IRQn);
andrewboyson 0:33686e88f09a 127
andrewboyson 0:33686e88f09a 128 return 0;
andrewboyson 0:33686e88f09a 129 }
andrewboyson 14:7ef557918bb1 130 int32_t TickElapsed(int32_t* pLastTime)
andrewboyson 14:7ef557918bb1 131 {
andrewboyson 14:7ef557918bb1 132 int32_t thisTime = LPC_TIM1->TC;
andrewboyson 14:7ef557918bb1 133
andrewboyson 14:7ef557918bb1 134 if (thisTime < *pLastTime) *pLastTime -= TICK_COUNT_PER_SECOND; //lastCount could be 96 000 002 and thisTime could be 00 000 100 which should give 98
andrewboyson 14:7ef557918bb1 135 int32_t elapsed = thisTime - *pLastTime;
andrewboyson 14:7ef557918bb1 136
andrewboyson 14:7ef557918bb1 137 *pLastTime = thisTime;
andrewboyson 14:7ef557918bb1 138 return elapsed;
andrewboyson 14:7ef557918bb1 139 }
andrewboyson 14:7ef557918bb1 140 bool Ticked(int32_t* pLastTime)
andrewboyson 14:7ef557918bb1 141 {
andrewboyson 14:7ef557918bb1 142 int thisTime = LPC_TIM1->TC;
andrewboyson 14:7ef557918bb1 143
andrewboyson 14:7ef557918bb1 144 bool ticked = thisTime < *pLastTime;
andrewboyson 14:7ef557918bb1 145
andrewboyson 14:7ef557918bb1 146 *pLastTime = thisTime;
andrewboyson 14:7ef557918bb1 147 return ticked;
andrewboyson 14:7ef557918bb1 148 }