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
Diff: sync.c
- Revision:
- 33:b9e3c06e7dab
- Parent:
- 32:f915ccb1ece3
diff -r f915ccb1ece3 -r b9e3c06e7dab sync.c
--- a/sync.c Thu Nov 29 18:43:27 2018 +0000
+++ b/sync.c Fri Nov 30 18:28:04 2018 +0000
@@ -1,9 +1,9 @@
#include <stdlib.h>
#include <stdbool.h>
-#include "log.h"
-#include "time.h"
-#include "clock.h"
+#include "log.h"
+#include "clktime.h"
+#include "clkstate.h"
bool SyncTrace = false;
@@ -14,17 +14,17 @@
int64_t absDiff = llabs(diff);
int64_t limit = ClockSyncedLimitNs;
int64_t hysterisis = ClockSyncedHysterisNs;
-
+
if (absDiff < limit - hysterisis) SyncedTime = true;
if (absDiff > limit + hysterisis) SyncedTime = false;
}
static void setSyncedRate(int64_t diff)
{
-
+
int64_t absDiff = llabs(diff);
int64_t limit = ClockSyncedLimitPpb;
int64_t hysterisis = ClockSyncedHysterisPpb;
-
+
if (absDiff < limit - hysterisis) SyncedRate = true;
if (absDiff > limit + hysterisis) SyncedRate = false;
}
@@ -33,25 +33,25 @@
{
int64_t slew = -diff / ClockSlewDivisor;
int32_t slewMaxTicks = ClockSlewMaxMs << 20;
-
+
if (slew > slewMaxTicks) slew = slewMaxTicks;
if (slew < -slewMaxTicks) slew = -slewMaxTicks;
-
- TimeSetSlew(slew);
-
- if (SyncTrace) LogTimeF("Sync setSlew diff %lld gives slew %lld gives TickSlew %ld\r\n", diff, slew, TimeGetSlew());
+
+ ClockSlew = slew;
+
+ if (SyncTrace) LogTimeF("Sync setSlew diff %lld gives slew %lld gives TickSlew %ld\r\n", diff, slew, ClockSlew);
}
static void adjustPpb(int64_t diff)
-{
+{
int64_t toAdd = diff / ClockPpbDivisor;
int32_t maxAdd = ClockPpbChangeMax;
if (toAdd > maxAdd) toAdd = maxAdd;
if (toAdd < -maxAdd) toAdd = -maxAdd;
-
- TimeAddPpb(-toAdd);
-
- if (SyncTrace) LogTimeF("Sync setPpb diff %lld gives toAdd %lld gives TickPpb %ld\r\n", diff, toAdd, TimeGetPpb());
+
+ ClockAddPpb(-toAdd);
+
+ if (SyncTrace) LogTimeF("Sync setPpb diff %lld gives toAdd %lld gives TickPpb %ld\r\n", diff, toAdd, ClockPpb);
}
static int64_t lastIntClock = -1; //-1 indicates invalid value. 0 is a valid value.
@@ -59,14 +59,14 @@
static void reset(int64_t thisExtClock)
{
TimeSet(thisExtClock);
- TimeSetPpb(0);
+ ClockSetPpb(0);
lastIntClock = 0;
lastExtClock = 0;
}
static void sync(int64_t thisExtClock)
{
-
+
if (!TimeIsSet()) //Cold start - only ever true if the RTC was not set.
{
LogTimeF("Sync - cold start of clock so resetting\r\n");
@@ -86,22 +86,22 @@
LogTimeF("Sync - offset is greater than %d seconds so resetting\r\n", ClockMaxOffsetSecs);
reset(thisExtClock);
return;
- }
+ }
setSlew(absDiff);
setSyncedTime(absDiff);
-
+
//Calculate the rate error
if (lastExtClock > -1)
{
int64_t extPeriod = thisExtClock - lastExtClock;
-
+
int64_t intPeriod = thisIntClock - lastIntClock;
int64_t periodDiff = intPeriod - extPeriod;
-
+
int64_t ppb;
if (extPeriod == TIME_ONE_SECOND) ppb = periodDiff; //This saves a 64bit shift and division for PPS
else ppb = (periodDiff << TIME_ONE_SECOND_SHIFT) / extPeriod;
-
+
adjustPpb(ppb);
setSyncedRate(ppb);
}
@@ -112,4 +112,4 @@
}
void SyncPpsI ( ) { TimeSaveSnapshot(); }
void SyncPpsN (time_t t ) { int64_t ticks = (int64_t)t << TIME_ONE_SECOND_SHIFT; sync(ticks); }
-void SyncTicks(int64_t ticks) { TimeSaveSnapshot(); sync(ticks); }
+void SyncTicks(int64_t ticks) { TimeSaveSnapshot(); sync(ticks); }
\ No newline at end of file