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
clktime.c
- Committer:
- andrewboyson
- Date:
- 2018-11-30
- Revision:
- 33:b9e3c06e7dab
- Parent:
- time.c@ 32:f915ccb1ece3
- Child:
- 34:aeb58975e61a
File content as of revision 33:b9e3c06e7dab:
#include <stdint.h>
#include <stdbool.h>
#include "rtc.h"
#include "tm.h"
#include "clock.h"
#include "clktime.h"
#include "clkstate.h"
#include "timer.h"
#include "led.h"
#include "log.h"
static int64_t tickCount = 0;
static int64_t slewCount = 0;
static bool countIsSet = false;
bool TimeIsSet() { return countIsSet; }
static uint32_t secondBaseCount = 0; //Set by the increment function
int64_t TimeNow()
{
return tickCount + slewCount + TimerMultiplyFractionalPart(TIME_ONE_SECOND + ClockPpb + ClockSlew, TimerSinceCount(secondBaseCount), TIMER_COUNT_PER_SECOND);
}
void TimeToTmUtc (int64_t ticks, struct tm* ptm)
{
time_t t = ticks >> TIME_ONE_SECOND_SHIFT;
TmUtcFromTimeT(t, ptm);
}
int64_t TimeFromTmUtc(struct tm* ptm)
{
time_t t = TmUtcToTimeT(ptm);
return t << TIME_ONE_SECOND_SHIFT;
}
void TimeSet(int64_t extClock)
{
int64_t timerCountSinceLastSecond = TimerSinceCount(secondBaseCount);
int64_t fraction = (timerCountSinceLastSecond << TIME_ONE_SECOND_SHIFT) / TIMER_COUNT_PER_SECOND;
int64_t ticks = extClock - fraction;
__disable_irq();
tickCount = ticks;
slewCount = 0;
__enable_irq();
countIsSet = true;
}
void TimeIncrementByOneSecond(uint32_t startCount)
{
__disable_irq();
secondBaseCount = startCount;
tickCount += TIME_ONE_SECOND + ClockPpb;
slewCount += ClockSlew;
ClockSlew = 0;
__enable_irq();
}
static volatile int64_t tickSnapshot;
static volatile int64_t slewSnapshot;
static volatile uint32_t timerSnapshot;
void TimeSaveSnapshot()
{
timerSnapshot = TimerSinceCount(secondBaseCount);
tickSnapshot = tickCount;
slewSnapshot = slewCount;
}
void TimesGetFromSnapshot(int64_t* pInt, int64_t* pAbs)
{
*pInt = tickSnapshot + TimerMultiplyFractionalPart(TIME_ONE_SECOND + ClockPpb, timerSnapshot, TIMER_COUNT_PER_SECOND);
*pAbs = tickSnapshot + slewSnapshot + TimerMultiplyFractionalPart(TIME_ONE_SECOND + ClockPpb + ClockSlew, timerSnapshot, TIMER_COUNT_PER_SECOND);
}
void TimesGet(int64_t* pInt, int64_t* pAbs)
{
uint32_t timerCount = TimerSinceCount(secondBaseCount);
*pInt = tickCount + TimerMultiplyFractionalPart(TIME_ONE_SECOND + ClockPpb, timerCount, TIMER_COUNT_PER_SECOND);
*pAbs = tickCount + slewCount + TimerMultiplyFractionalPart(TIME_ONE_SECOND + ClockPpb + ClockSlew, timerCount, TIMER_COUNT_PER_SECOND);
}