Skip to content

Custom GPS Synchronized PTP/NTP Timeserver on STM32

Work in progress

This page is under construction as I find time to migrate my notes.

[TODO: picture of the hardware]

This page details a GPS-referenced Stratum 1 network time server with support for Precision Time Protocol (PTP, IEEE 1588) and NTPv4 (RFC 5905). It is implemented on a STMicro NUCLEO-H563ZI.

Initially, this project was just to get bare-metal Ethernet working on the STM32H563ZI. Once I had that working, I figured I'd use it to do something useful.

This implementation is entirely bare metal, all of the code used in this project was written from scratch by me.

View Source Code on GitHub

High-level Architecture Overview

A custom RTOS runs threads to synchronize the system time from a GPS module connected via UART and act as a network time server.

GPS Synchronization

This code was designed to use a ublox LEA-5T GPS module, although any module that provides PPS would likely work.

The GPS module provides current time and position to the microcontroller via NMEA messages over UART. However, since there is variable latency in the time is takes to send a UART message and for it to be processed in the OS, this is not a very precise way to synchronize time.

To improve precision, the GPS module also provides a PPS signal. Ideally, the rising edge of this signal occurs exactly at the second mark of global "GPS time". Since I do not have access to any atomic clocks, I can't quantify the precision of my particular GPS, but it is likely much higher than I have any real use for.

To reduce overhead as much as possible, this GPS generated PPS signal triggers a hardware interupt in the microcontroller. This interrupt signal triggers a snapshot of the timestamp timers in the Ethernet MAC. If the clocks are perfectly synchronized, the timestamped will have a subsecond value of 0 (PPS occured right at the second mark). Typically this is not the case, so instead I input the error into a PI controller that controls the clock divider that drives the Ethernet MAC's timer. Eventually, the timer will converge and the Microcontroller's clock will match the GPS's with some slight random drift.

Here is a video of the synchronization process:

sync

The yellow trace shows the PPS reference, and the blue trace shows the PPS signal generated by the microcontroller.

And here is the drift at steady state:

steady_state

A few nanoseconds doesn't seem too bad considering the hardware I'm dealing with.

Timeserver

The Ethernet MAC on teh STM32H563ZI handle layer 2 PTP completely autonomously. The packet timestamps are generated in hardware using the timer I calibrated from GPS.

Layer 2 PTP isn't very useful in my network consisting of multiple subnets. I intend to someday write a PTP driver for my RTOS, but for now I get by with NTP.

[TODO NTP Writeup]