Andrew Boyson / clock

Dependents:   oldheating gps motorhome heating

tick.c

Committer:
andrewboyson
Date:
2018-01-20
Revision:
23:07b19cd5f6d0
Parent:
22:df0b906bda26
Child:
24:6c9833e2a049

File content as of revision 23:07b19cd5f6d0:

#include <stdint.h>
#include <stdbool.h>

#include "peripherals.h"
#include  "rtc.h"
#include "time.h"
#include "tick.h"
#include "led.h"
#include "log.h"

#define ONE_BILLION 1000000000

static volatile int64_t nsTickCount;
static volatile int64_t nsSlewCount;
static bool nsCountIsSet = false;
bool  TickIsSet() { return nsCountIsSet; }

static volatile int32_t slew = 0; //ns     - up to +/- 2.147s of slew
int32_t TickGetSlew() { return slew; }
void    TickSetSlew(int32_t value) { slew = value; }

static volatile int32_t ppb  = 0;
int32_t TickGetPpb () { return ppb;  }
void    TickSetPpb (int32_t value) { ppb  = value; LPC_RTC->GPREG0 = ppb; }
void    TickAddPpb (int32_t value) { ppb += value; LPC_RTC->GPREG0 = ppb; }

bool TickTicked = false;

/*
+---------+---------------+
| Seconds |   Base count  |
+---------+---------------+
|    0    |             0 |
|    1    |    96,000,000 |
|   ...   |       ...     |
|   44    | 4,224,000,000 |
|   44.74 |          2^32 |
|   45    |    25,032,704 |
|   ...   |       ...     |
+---------+---------------+
*/
static uint32_t secondsBaseCount = 0;

void TickSet(int64_t extClock)
{
    uint32_t       tc = LPC_TIM0->TC;
    uint32_t  seconds =       tc / TICK_COUNT_PER_SECOND; //0 to 44
    uint32_t     base =  seconds * TICK_COUNT_PER_SECOND; //0 to 2^32
    uint32_t fraction =       tc % TICK_COUNT_PER_SECOND; //0 to 96,000,000
    uint32_t fractionNs = fraction / 96 * 1000;           //0 to 1,000,000,000 which fits into 32 bits
     int64_t       ns = extClock - fractionNs;
     
    __disable_irq();
             nsTickCount = ns;
             nsSlewCount = 0;
        secondsBaseCount = base;
    __enable_irq();
    
    nsCountIsSet = true;
}
void TickMain()
{
    uint32_t sincesecondsBaseCount = LPC_TIM0->TC - secondsBaseCount;
    if (sincesecondsBaseCount > TICK_COUNT_PER_SECOND)
    { 
        __disable_irq();
        secondsBaseCount += TICK_COUNT_PER_SECOND;
             nsTickCount += ONE_BILLION + ppb;
             nsSlewCount += slew;
        __enable_irq();
        slew  = 0;
    }
}
static volatile  int64_t nsTickSnapshot;
static volatile  int64_t nsSlewSnapshot;
static volatile uint32_t  timerSnapshot;

void TickSaveSnapshot()
{
     timerSnapshot = LPC_TIM0->TC - secondsBaseCount;
    nsTickSnapshot = nsTickCount;
    nsSlewSnapshot = nsSlewCount;
}
static void makeTimesFromCounts(uint32_t timerCount, int64_t tickNs, int64_t slewNs, int64_t* pNsInt, int64_t* pNsAbs)
{
    int64_t   fraction = timerCount;
              fraction <<= 32;
              fraction /= TICK_COUNT_PER_SECOND;

    int64_t nsFraction;
    
    int64_t nsBase     = tickNs;
    int64_t nsPerTick  = ONE_BILLION + ppb;
    nsFraction = (nsPerTick * fraction) >> 32;
    *pNsInt = nsBase + nsFraction;

    nsBase    += slewNs;
    nsPerTick += slew;
    nsFraction = (nsPerTick * fraction) >> 32;
    *pNsAbs = nsBase + nsFraction;
}
void TickGetTimesFromSnapshot(int64_t* pNsInt, int64_t* pNsAbs)
{
    makeTimesFromCounts(timerSnapshot, nsTickSnapshot, nsSlewSnapshot, pNsInt, pNsAbs);
}
void TickGetTimes(int64_t* pNsInt, int64_t* pNsAbs)
{   
    makeTimesFromCounts(LPC_TIM0->TC - secondsBaseCount, nsTickCount, nsSlewCount, pNsInt, pNsAbs);
}

void TickInit(void)
{
    nsTickCount = 0;
    nsSlewCount = 0;
    
    ppb  = LPC_RTC->GPREG0; //This is saved each time Tickppb is updated
    slew = 0;
    nsCountIsSet = false;
    
    LPC_TIM0->TCR     =     2; // 21.6.2 Timer Control Register - Reset TC and PC.
    LPC_TIM0->CTCR    =     0; // 21.6.3 Count Control Register - Timer mode
    LPC_TIM0->PR      =     0; // 21.6.5 Prescale register      - Don't prescale 96MHz clock (divide by PR+1).
    LPC_TIM0->MCR     =     0; // 21.6.8 Match Control Register - no interrupt or reset
    LPC_TIM0->TCR     =     1; // 21.6.2 Timer Control Register - Enable TC and PC
}
uint32_t TickElapsed(uint32_t* pLastTime)
{
    uint32_t thisTime = LPC_TIM0->TC;
    uint32_t elapsed = thisTime - *pLastTime;    
    *pLastTime = thisTime;
    return elapsed;
}
uint32_t TickTimerStart()
{
    return LPC_TIM0->TC;
}
uint32_t TickTimerCount(uint32_t startCount)
{
    uint32_t thisCount = LPC_TIM0->TC;
    return thisCount - startCount; 
}
uint32_t TickTimerMs(uint32_t startCount)
{
    uint32_t count = TickTimerCount(startCount);
    return count / 96000;
}