Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: oldheating gps motorhome heating
tick.c@17:927fc1eceb9d, 2018-01-11 (annotated)
- Committer:
- andrewboyson
- Date:
- Thu Jan 11 17:39:36 2018 +0000
- Revision:
- 17:927fc1eceb9d
- Parent:
- tick.cpp@16:933cbe190bb0
- Child:
- 18:207dd1474cd9
Removed dependence on Mbed OS
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| andrewboyson | 17:927fc1eceb9d | 1 | #include "peripherals.h" |
| andrewboyson | 14:7ef557918bb1 | 2 | #include "rtc.h" |
| andrewboyson | 14:7ef557918bb1 | 3 | #include "time.h" |
| andrewboyson | 14:7ef557918bb1 | 4 | #include "tick.h" |
| andrewboyson | 17:927fc1eceb9d | 5 | |
| andrewboyson | 17:927fc1eceb9d | 6 | #define TRUE 1 |
| andrewboyson | 17:927fc1eceb9d | 7 | #define FALSE 0 |
| andrewboyson | 0:33686e88f09a | 8 | |
| andrewboyson | 0:33686e88f09a | 9 | #define ONE_BILLION 1000000000 |
| andrewboyson | 0:33686e88f09a | 10 | |
| andrewboyson | 16:933cbe190bb0 | 11 | static volatile int64_t nsTickCount; |
| andrewboyson | 16:933cbe190bb0 | 12 | static volatile int64_t nsFreqCount; |
| andrewboyson | 16:933cbe190bb0 | 13 | static volatile int64_t nsTimeCount; |
| andrewboyson | 16:933cbe190bb0 | 14 | |
| andrewboyson | 17:927fc1eceb9d | 15 | static int nsCountIsSet = FALSE; |
| andrewboyson | 17:927fc1eceb9d | 16 | int TickIsSet() { return nsCountIsSet; } |
| andrewboyson | 5:a3e37ce4975d | 17 | void TickSet(int64_t extClock) |
| andrewboyson | 5:a3e37ce4975d | 18 | { |
| andrewboyson | 5:a3e37ce4975d | 19 | nsTickCount = 0; |
| andrewboyson | 5:a3e37ce4975d | 20 | nsFreqCount = 0; |
| andrewboyson | 5:a3e37ce4975d | 21 | nsTimeCount = extClock; |
| andrewboyson | 5:a3e37ce4975d | 22 | LPC_TIM1->TCR = 2; // 21.6.2 Timer Control Register - Reset TC and PC. |
| andrewboyson | 5:a3e37ce4975d | 23 | LPC_TIM1->TCR = 1; // 21.6.2 Timer Control Register - Enable TC and PC. |
| andrewboyson | 17:927fc1eceb9d | 24 | nsCountIsSet = TRUE; |
| andrewboyson | 5:a3e37ce4975d | 25 | } |
| andrewboyson | 0:33686e88f09a | 26 | |
| andrewboyson | 16:933cbe190bb0 | 27 | static volatile int32_t slew = 0; //ns - up to +/- 2.147s of slew |
| andrewboyson | 5:a3e37ce4975d | 28 | int32_t TickGetSlew() { return slew; } |
| andrewboyson | 5:a3e37ce4975d | 29 | void TickSetSlew(int32_t value) { slew = value; } |
| andrewboyson | 5:a3e37ce4975d | 30 | |
| andrewboyson | 16:933cbe190bb0 | 31 | static volatile int32_t ppb = 0; |
| andrewboyson | 5:a3e37ce4975d | 32 | int32_t TickGetPpb () { return ppb; } |
| andrewboyson | 5:a3e37ce4975d | 33 | void TickSetPpb (int32_t value) { ppb = value; LPC_RTC->GPREG0 = ppb; } |
| andrewboyson | 5:a3e37ce4975d | 34 | void TickAddPpb (int32_t value) { ppb += value; LPC_RTC->GPREG0 = ppb; } |
| andrewboyson | 0:33686e88f09a | 35 | |
| andrewboyson | 0:33686e88f09a | 36 | #define CLOCK_TIC 0 |
| andrewboyson | 0:33686e88f09a | 37 | #define CLOCK_INT 1 |
| andrewboyson | 0:33686e88f09a | 38 | #define CLOCK_ABS 2 |
| andrewboyson | 0:33686e88f09a | 39 | |
| andrewboyson | 17:927fc1eceb9d | 40 | __irq void TickHandler(void) |
| andrewboyson | 9:cd30749dfa3d | 41 | {//Checked the number of counts and this routine takes 46 counts or about 0.5us |
| andrewboyson | 9:cd30749dfa3d | 42 | |
| andrewboyson | 0:33686e88f09a | 43 | //Reset the timer interrupt |
| andrewboyson | 0:33686e88f09a | 44 | LPC_TIM1->IR |= 1; |
| andrewboyson | 0:33686e88f09a | 45 | |
| andrewboyson | 0:33686e88f09a | 46 | //reset the tick timer |
| andrewboyson | 0:33686e88f09a | 47 | LPC_TIM1->TCR = 2; // 21.6.2 Timer Control Register - Reset TC and PC. |
| andrewboyson | 0:33686e88f09a | 48 | LPC_TIM1->TCR = 1; // 21.6.2 Timer Control Register - Enable TC and PC |
| andrewboyson | 0:33686e88f09a | 49 | |
| andrewboyson | 0:33686e88f09a | 50 | //Calculate the new counts |
| andrewboyson | 0:33686e88f09a | 51 | nsTickCount += ONE_BILLION; |
| andrewboyson | 5:a3e37ce4975d | 52 | nsFreqCount += ppb; |
| andrewboyson | 5:a3e37ce4975d | 53 | nsTimeCount += slew; |
| andrewboyson | 0:33686e88f09a | 54 | |
| andrewboyson | 0:33686e88f09a | 55 | //Feedback the amount slewed |
| andrewboyson | 5:a3e37ce4975d | 56 | slew = 0; |
| andrewboyson | 0:33686e88f09a | 57 | } |
| andrewboyson | 0:33686e88f09a | 58 | static volatile int64_t nsTickSnapshot; |
| andrewboyson | 0:33686e88f09a | 59 | static volatile int64_t nsFreqSnapshot; |
| andrewboyson | 0:33686e88f09a | 60 | static volatile int64_t nsTimeSnapshot; |
| andrewboyson | 0:33686e88f09a | 61 | static volatile int32_t timerSnapshot; |
| andrewboyson | 0:33686e88f09a | 62 | |
| andrewboyson | 0:33686e88f09a | 63 | void TickSaveSnapshotI() |
| andrewboyson | 0:33686e88f09a | 64 | { |
| andrewboyson | 0:33686e88f09a | 65 | timerSnapshot = LPC_TIM1->TC; |
| andrewboyson | 0:33686e88f09a | 66 | nsTickSnapshot = nsTickCount; |
| andrewboyson | 0:33686e88f09a | 67 | nsFreqSnapshot = nsFreqCount; |
| andrewboyson | 0:33686e88f09a | 68 | nsTimeSnapshot = nsTimeCount; |
| andrewboyson | 0:33686e88f09a | 69 | } |
| andrewboyson | 14:7ef557918bb1 | 70 | static void makeTimesFromCounts(int32_t timerCount, int64_t tickNs, int64_t freqNs, int64_t timeNs, int64_t* pNsInt, int64_t* pNsAbs) |
| andrewboyson | 16:933cbe190bb0 | 71 | { |
| andrewboyson | 16:933cbe190bb0 | 72 | int64_t fraction = timerCount; |
| andrewboyson | 16:933cbe190bb0 | 73 | fraction <<= 32; |
| andrewboyson | 16:933cbe190bb0 | 74 | fraction /= TICK_COUNT_PER_SECOND; |
| andrewboyson | 16:933cbe190bb0 | 75 | |
| andrewboyson | 0:33686e88f09a | 76 | int64_t nsFraction; |
| andrewboyson | 0:33686e88f09a | 77 | |
| andrewboyson | 14:7ef557918bb1 | 78 | int64_t nsBase = tickNs; |
| andrewboyson | 0:33686e88f09a | 79 | int64_t nsPerTick = ONE_BILLION; |
| andrewboyson | 16:933cbe190bb0 | 80 | |
| andrewboyson | 14:7ef557918bb1 | 81 | nsBase += freqNs; |
| andrewboyson | 5:a3e37ce4975d | 82 | nsPerTick += ppb; |
| andrewboyson | 0:33686e88f09a | 83 | nsFraction = (nsPerTick * fraction) >> 32; |
| andrewboyson | 0:33686e88f09a | 84 | *pNsInt = nsBase + nsFraction; |
| andrewboyson | 0:33686e88f09a | 85 | |
| andrewboyson | 14:7ef557918bb1 | 86 | nsBase += timeNs; |
| andrewboyson | 5:a3e37ce4975d | 87 | nsPerTick += slew; |
| andrewboyson | 0:33686e88f09a | 88 | nsFraction = (nsPerTick * fraction) >> 32; |
| andrewboyson | 0:33686e88f09a | 89 | *pNsAbs = nsBase + nsFraction; |
| andrewboyson | 0:33686e88f09a | 90 | } |
| andrewboyson | 14:7ef557918bb1 | 91 | void TickRetrieveSnapshot(int64_t* pNsInt, int64_t* pNsAbs) |
| andrewboyson | 14:7ef557918bb1 | 92 | { |
| andrewboyson | 14:7ef557918bb1 | 93 | makeTimesFromCounts(timerSnapshot, nsTickSnapshot, nsFreqSnapshot, nsTimeSnapshot, pNsInt, pNsAbs); |
| andrewboyson | 14:7ef557918bb1 | 94 | } |
| andrewboyson | 6:d8a6235486b7 | 95 | void TickGetTimes(int64_t* pNsInt, int64_t* pNsAbs) |
| andrewboyson | 0:33686e88f09a | 96 | { |
| andrewboyson | 14:7ef557918bb1 | 97 | int32_t timerCount; |
| andrewboyson | 14:7ef557918bb1 | 98 | int64_t tickNs; |
| andrewboyson | 14:7ef557918bb1 | 99 | int64_t freqNs; |
| andrewboyson | 14:7ef557918bb1 | 100 | int64_t timeNs; |
| andrewboyson | 14:7ef557918bb1 | 101 | |
| andrewboyson | 0:33686e88f09a | 102 | __disable_irq(); |
| andrewboyson | 14:7ef557918bb1 | 103 | timerCount = LPC_TIM1->TC; |
| andrewboyson | 14:7ef557918bb1 | 104 | tickNs = nsTickCount; |
| andrewboyson | 14:7ef557918bb1 | 105 | freqNs = nsFreqCount; |
| andrewboyson | 14:7ef557918bb1 | 106 | timeNs = nsTimeCount; |
| andrewboyson | 0:33686e88f09a | 107 | __enable_irq(); |
| andrewboyson | 6:d8a6235486b7 | 108 | |
| andrewboyson | 14:7ef557918bb1 | 109 | makeTimesFromCounts(timerCount, tickNs, freqNs, timeNs, pNsInt, pNsAbs); |
| andrewboyson | 0:33686e88f09a | 110 | } |
| andrewboyson | 0:33686e88f09a | 111 | |
| andrewboyson | 17:927fc1eceb9d | 112 | void 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 | 5:a3e37ce4975d | 118 | ppb = LPC_RTC->GPREG0; //This is saved each time Tickppb is updated |
| andrewboyson | 5:a3e37ce4975d | 119 | slew = 0; |
| andrewboyson | 17:927fc1eceb9d | 120 | nsCountIsSet = FALSE; |
| andrewboyson | 0:33686e88f09a | 121 | |
| andrewboyson | 14:7ef557918bb1 | 122 | LPC_SC->PCLKSEL0 &= ~0x20; // 4.7.3 Peripheral Clock Selection - PCLK_peripheral PCLK_TIMER1 01xxxx = CCLK - reset bit 5 |
| andrewboyson | 14:7ef557918bb1 | 123 | LPC_SC->PCLKSEL0 |= 0x10; // set bit 4 |
| andrewboyson | 14:7ef557918bb1 | 124 | LPC_SC->PCONP |= 4; // 4.8.9 Power Control for Peripherals register - Timer1 Power On |
| andrewboyson | 14:7ef557918bb1 | 125 | LPC_TIM1->TCR = 2; // 21.6.2 Timer Control Register - Reset TC and PC. |
| andrewboyson | 14:7ef557918bb1 | 126 | LPC_TIM1->CTCR = 0; // 21.6.3 Count Control Register - Timer mode |
| andrewboyson | 14:7ef557918bb1 | 127 | LPC_TIM1->PR = 0; // 21.6.5 Prescale register - Don't prescale 96MHz clock (divide by PR+1). |
| andrewboyson | 14:7ef557918bb1 | 128 | LPC_TIM1->MR0 = TICK_COUNT_PER_SECOND; // 21.6.7 Match Register 0 - Match count |
| andrewboyson | 14:7ef557918bb1 | 129 | LPC_TIM1->MCR = 1; // 21.6.8 Match Control Register - interrupt on match |
| andrewboyson | 14:7ef557918bb1 | 130 | LPC_TIM1->TCR = 1; // 21.6.2 Timer Control Register - Enable TC and PC |
| andrewboyson | 0:33686e88f09a | 131 | |
| andrewboyson | 17:927fc1eceb9d | 132 | NVIC->ISER[0] |= 0x04; // 6.5.1 bit2 == Interrupt set enable for timer 1. |
| andrewboyson | 0:33686e88f09a | 133 | } |
| andrewboyson | 14:7ef557918bb1 | 134 | int32_t TickElapsed(int32_t* pLastTime) |
| andrewboyson | 14:7ef557918bb1 | 135 | { |
| andrewboyson | 14:7ef557918bb1 | 136 | int32_t thisTime = LPC_TIM1->TC; |
| andrewboyson | 14:7ef557918bb1 | 137 | |
| andrewboyson | 14:7ef557918bb1 | 138 | 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 | 139 | int32_t elapsed = thisTime - *pLastTime; |
| andrewboyson | 14:7ef557918bb1 | 140 | |
| andrewboyson | 14:7ef557918bb1 | 141 | *pLastTime = thisTime; |
| andrewboyson | 14:7ef557918bb1 | 142 | return elapsed; |
| andrewboyson | 14:7ef557918bb1 | 143 | } |
| andrewboyson | 17:927fc1eceb9d | 144 | int Ticked(int32_t* pLastTime) |
| andrewboyson | 14:7ef557918bb1 | 145 | { |
| andrewboyson | 14:7ef557918bb1 | 146 | int thisTime = LPC_TIM1->TC; |
| andrewboyson | 14:7ef557918bb1 | 147 | |
| andrewboyson | 17:927fc1eceb9d | 148 | int ticked = thisTime < *pLastTime; |
| andrewboyson | 14:7ef557918bb1 | 149 | |
| andrewboyson | 14:7ef557918bb1 | 150 | *pLastTime = thisTime; |
| andrewboyson | 14:7ef557918bb1 | 151 | return ticked; |
| andrewboyson | 14:7ef557918bb1 | 152 | } |