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
clock-ntp.c@17:927fc1eceb9d, 2018-01-11 (annotated)
- Committer:
- andrewboyson
- Date:
- Thu Jan 11 17:39:36 2018 +0000
- Revision:
- 17:927fc1eceb9d
- Parent:
- clock-ntp.cpp@11:6efacb859c93
- 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 <stdint.h> |
| andrewboyson | 17:927fc1eceb9d | 2 | |
| andrewboyson | 10:19ed22b94c99 | 3 | |
| andrewboyson | 11:6efacb859c93 | 4 | #define SECONDS_BETWEEN_1900_AND_1970 2208988800ULL |
| andrewboyson | 11:6efacb859c93 | 5 | #define ONE_BILLION 1000000000LL |
| andrewboyson | 10:19ed22b94c99 | 6 | |
| andrewboyson | 11:6efacb859c93 | 7 | #define ERA_BASE_LOW 1 //Adjust this from 0 to 1 between 1968 and 2036; 1 to 2 between 2104 and 2168 |
| andrewboyson | 11:6efacb859c93 | 8 | #define ERA_BASE_HGH 0 //Adjust this from 0 to 1 between 2036 and 2104; 1 to 2 between 2172 and 2240 |
| andrewboyson | 11:6efacb859c93 | 9 | |
| andrewboyson | 17:927fc1eceb9d | 10 | #define ERA_LOW_SECS ERA_BASE_LOW * (1LL << 32) |
| andrewboyson | 17:927fc1eceb9d | 11 | #define ERA_HGH_SECS ERA_BASE_HGH * (1LL << 32) |
| andrewboyson | 17:927fc1eceb9d | 12 | #define ERA_LOW_NS ERA_LOW_SECS * ONE_BILLION |
| andrewboyson | 17:927fc1eceb9d | 13 | #define ERA_HGH_NS ERA_HGH_SECS * ONE_BILLION |
| andrewboyson | 10:19ed22b94c99 | 14 | |
| andrewboyson | 10:19ed22b94c99 | 15 | uint64_t ClockNtpTimeStampFromNs(int64_t ns) |
| andrewboyson | 10:19ed22b94c99 | 16 | { |
| andrewboyson | 10:19ed22b94c99 | 17 | uint64_t timestamp = ns << 2; |
| andrewboyson | 10:19ed22b94c99 | 18 | timestamp /= 1000; timestamp <<= 10; |
| andrewboyson | 10:19ed22b94c99 | 19 | timestamp /= 1000; timestamp <<= 10; |
| andrewboyson | 10:19ed22b94c99 | 20 | timestamp /= 1000; timestamp <<= 10; |
| andrewboyson | 10:19ed22b94c99 | 21 | |
| andrewboyson | 11:6efacb859c93 | 22 | timestamp += SECONDS_BETWEEN_1900_AND_1970 << 32; //This should just wrap around the unsigned timestamp removing the unwanted era. |
| andrewboyson | 10:19ed22b94c99 | 23 | |
| andrewboyson | 10:19ed22b94c99 | 24 | return timestamp; |
| andrewboyson | 10:19ed22b94c99 | 25 | } |
| andrewboyson | 10:19ed22b94c99 | 26 | int64_t ClockNtpTimeStampToNs(uint64_t timestamp) |
| andrewboyson | 10:19ed22b94c99 | 27 | { |
| andrewboyson | 17:927fc1eceb9d | 28 | int isHigh = (timestamp & (1ULL << 63)) != 0; |
| andrewboyson | 11:6efacb859c93 | 29 | |
| andrewboyson | 11:6efacb859c93 | 30 | int64_t ns = timestamp - (SECONDS_BETWEEN_1900_AND_1970 << 32); |
| andrewboyson | 10:19ed22b94c99 | 31 | ns >>= 10; ns *= 1000; |
| andrewboyson | 10:19ed22b94c99 | 32 | ns >>= 10; ns *= 1000; |
| andrewboyson | 10:19ed22b94c99 | 33 | ns >>= 10; ns *= 1000; |
| andrewboyson | 10:19ed22b94c99 | 34 | |
| andrewboyson | 10:19ed22b94c99 | 35 | ns >>= 2; |
| andrewboyson | 10:19ed22b94c99 | 36 | |
| andrewboyson | 11:6efacb859c93 | 37 | //Correct for era |
| andrewboyson | 11:6efacb859c93 | 38 | if (isHigh) ns += ERA_HGH_NS; |
| andrewboyson | 11:6efacb859c93 | 39 | else ns += ERA_LOW_NS; |
| andrewboyson | 11:6efacb859c93 | 40 | |
| andrewboyson | 10:19ed22b94c99 | 41 | return ns; |
| andrewboyson | 10:19ed22b94c99 | 42 | } |
| andrewboyson | 10:19ed22b94c99 | 43 |