NTP implementation and how important is monotonic time? - Page 1
Forum section: electronics / general

author: peter-h
author_rank: Super Contributor
timestamp: August 13, 2021, 09:12:56 pm
content: I have a 32F417 based target with ethernet and am looking at NTP client implementation, to set the RTC.I have already done it from GPS (NMEA), with a user-settable "fix" time, on a 1hr boundary, say 0300.This may not produce a monotonic time, but AFAIK most NTP client implementations do something similar, including Windows, whose motherboard RTC implementation (an emulation of the old Hitachi 6818) doesn't have a "calibration" feature.OTOH the 32F417 does have RTC calibration.Does Windows have a truly monotonic time API? I didn't think it hammered the NTP server often enough, to be under 1 second with a ~20ppm crystal on the RTC.I will also be looking for some C source for NTP client. Won't be implementing it myself because LWIP is above my pay grade though

author: fchk
author_rank: Frequent Contributor
timestamp: August 14, 2021, 05:04:08 pm
content: You most likely never used the full NTP but only the simplified SNTP.SNTP ist easy. Just fill a structCode:typedef struct{uint8_t li_vn_mode; // Eight bits. li, vn, and mode.// li. Two bits. Leap indicator.// vn. Three bits. Version number of the protocol.// mode. Three bits. Client will pick mode 3 for client.uint8_t stratum; // Eight bits. Stratum level of the local clock.uint8_t poll; // Eight bits. Maximum interval between successive messages.uint8_t precision; // Eight bits. Precision of the local clock.uint32_t rootDelay; // 32 bits. Total round trip delay time.uint32_t rootDispersion; // 32 bits. Max error aloud from primary clock source.uint32_t refId; // 32 bits. Reference clock identifier.uint32_t refTm_s; // 32 bits. Reference time-stamp seconds.uint32_t refTm_f; // 32 bits. Reference time-stamp fraction of a second.uint32_t origTm_s; // 32 bits. Originate time-stamp seconds.uint32_t origTm_f; // 32 bits. Originate time-stamp fraction of a second.uint32_t rxTm_s; // 32 bits. Received time-stamp seconds.uint32_t rxTm_f; // 32 bits. Received time-stamp fraction of a second.uint32_t txTm_s; // 32 bits and the most important field the client cares about. Transmit time-stamp seconds.uint32_t txTm_f; // 32 bits. Transmit time-stamp fraction of a second.} ntp_packet; // Total: 384 bits or 48 bytes.memset everything to 0, then vn to 3, mode to 3 (li_vn_mode=0x1b),send the packet and get receive a packet with the time in rxTm.Seehttps://github.com/lettier/ntpclient/blob/master/source/c/main.cThe full ntp is much more complicated. As I know Windows only used the simplified SNTP, for the full NTP you need the reference implementation from David L. Mills. A Windows port is available here:https://www.meinbergglobal.com/english/sw/ntp.htm#ntp_stableThis will adjust the clock speed and correct for transmission latencies etc.fchk

author: peter-h
author_rank: Super Contributor
timestamp: August 14, 2021, 07:36:46 pm
content: This is very interesting - thank you.I had a dig around and it looks like Windows Server has the full implementationhttps://docs.microsoft.com/en-us/windows-server/networking/windows-time-service/how-the-windows-time-service-worksbut many sites state that Windows Workstation syncs the time only every 7 days so whatever implementation it has it cannot possibly deliver monotonic time, because there is no way an RTC could run to better than 1 sec accuracy over 7 days. A 20ppm xtal will be up to 12 secs out over 7 days, and calibrating it won't help much because of the tempco. One would need a TCXO to get < 1 sec. OTOH a PC could sync much more often if needed.So the decision is really whether monotonic time is really needed.

author: gmb42
author_rank: Frequent Contributor
timestamp: August 15, 2021, 01:18:33 pm
content: Using thew32tmcommand, or by direct registry editing if you're a masochist, you can set the time server and sync interval to whatever you like. Works on client and server OS's.Note that if your Windows OS is part of an Active Directory domain then time sync may well be set by a domain group policy and use the domain time server hierarchy.

author: peter-h
author_rank: Super Contributor
timestamp: August 23, 2021, 09:23:09 pm
content: If using simple NTP, could it be worth measuring the time delay and adding half of it to the obtained time - as a quick and dirty improvement?Cube IDE comes with a SNTP implementation - rather verbose as usualI had a quick look - it is some code from 2009 which doesn't even set the RTC. It is just a template with a load of #defined options.