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: clock-ntp.c
- Revision:
- 17:927fc1eceb9d
- Parent:
- 11:6efacb859c93
- Child:
- 18:207dd1474cd9
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/clock-ntp.c Thu Jan 11 17:39:36 2018 +0000
@@ -0,0 +1,43 @@
+#include <stdint.h>
+
+
+#define SECONDS_BETWEEN_1900_AND_1970 2208988800ULL
+#define ONE_BILLION 1000000000LL
+
+#define ERA_BASE_LOW 1 //Adjust this from 0 to 1 between 1968 and 2036; 1 to 2 between 2104 and 2168
+#define ERA_BASE_HGH 0 //Adjust this from 0 to 1 between 2036 and 2104; 1 to 2 between 2172 and 2240
+
+#define ERA_LOW_SECS ERA_BASE_LOW * (1LL << 32)
+#define ERA_HGH_SECS ERA_BASE_HGH * (1LL << 32)
+#define ERA_LOW_NS ERA_LOW_SECS * ONE_BILLION
+#define ERA_HGH_NS ERA_HGH_SECS * ONE_BILLION
+
+uint64_t ClockNtpTimeStampFromNs(int64_t ns)
+{
+ uint64_t timestamp = ns << 2;
+ timestamp /= 1000; timestamp <<= 10;
+ timestamp /= 1000; timestamp <<= 10;
+ timestamp /= 1000; timestamp <<= 10;
+
+ timestamp += SECONDS_BETWEEN_1900_AND_1970 << 32; //This should just wrap around the unsigned timestamp removing the unwanted era.
+
+ return timestamp;
+}
+int64_t ClockNtpTimeStampToNs(uint64_t timestamp)
+{
+ int isHigh = (timestamp & (1ULL << 63)) != 0;
+
+ int64_t ns = timestamp - (SECONDS_BETWEEN_1900_AND_1970 << 32);
+ ns >>= 10; ns *= 1000;
+ ns >>= 10; ns *= 1000;
+ ns >>= 10; ns *= 1000;
+
+ ns >>= 2;
+
+ //Correct for era
+ if (isHigh) ns += ERA_HGH_NS;
+ else ns += ERA_LOW_NS;
+
+ return ns;
+}
+