Skip to content

geerlingguy/time-pi

Repository files navigation

Time Pi

CI

Raspberry Pi 5 with TimeHAT V2

A Raspberry Pi stratum 1 time server.

Takes in GPS (or potentially other stratum 0 time sources), spits out NTP, PTP, etc.

Setup - Grandmaster

Hardware

To run a decent time server (with high accuracy), you need a few things:

  • A computer running Linux (all the nice tooling for network time is availabe here)
  • A high quality time source (GPS is most commonly used these days)
  • A network adapter capable of hardware timestamping (Intel and ASIX make some good NICs for time-related applications)

There are many options for each of the above items—for example, many use Adafruit's Ultimate GPS HAT or its USB equivalent for GPS acquisition, and for PTP, you can use a Compute Module 4 or 5's built-in NIC (with PPS in or out), or add on a compatible NIC on the Pi 5 with something like the uPCIty Lite.

My own hardware configuration—which is the basis for the code in this repository, consists of:

  • Computer: Raspberry Pi 5 model B
  • Time source: u-blox ZED-F9T-00B-01 (installed on TimeHAT V4)
  • NIC: Intel i226-LM (installed on TimeHAT V4)

A precise GPS signal for nanosecond-accurate time requires a decent antenna with as clear a view of the sky as possible. Some GPS receivers are better than others, but even USB receivers will do better than NTP!

I use a small active GPS antenna for testing, but for permanent installation it is best to mount a higher-quality GPS antenna outside, clear of obstructions.

Software

Make sure you have Ansible installed. Copy the following example files and customize them according to your setup:

  • example.hosts.ini to hosts.ini
  • example.config.yml to config.yml

Now run the Ansible playbook:

ansible-playbook main.yml

This playbook will configure:

  • GPSd: interface with the u-blox GPS and provide GPS data to other applications
  • Chrony: NTP server, which also sync GPS time (with Internet NTP server backup) to the system clock
  • Linux PTP: synchronize the system clock to the NIC PHC (Physical Hardware Clock), and set up the Pi as a PTP grandmaster clock

Intel i226 Notes: Currently I can't get the i226 to work with DHCP at all, so I have to manually set an IP address using nmtui. It also doesn't work at 2.5 Gbps currently, and it can't be overridden via Linux, so I make sure to plug it into a 1 Gbps port on my network.

Setup - Client Pis

For client Pis, make sure you have them listed under a [clients] heading in your hosts.ini file, then run the Ansible playbook:

ansible-playbook ptp-clients.yml

This should configure the clients to acquire PTP time from the grandmaster Pi.

Note: This playbook is still under active development. See Issue #1 for the latest.

GPS / GNSS Notes

Using u-blox GPS modules, you may encounter a baud rate mismatch. Many newer u-blox modules default to 38400 baud, while older modules default to 9600 baud. This project recommends 115200 baud for slightly faster timing updates.

# Get the protocol version ('PROTVER')
ubxtool -p MON-VER
...
UBX-MON-VER:
  swVersion EXT CORE 4.04 (7f89f7)
  hwVersion 00190000
  extension ROM BASE 0x118B2060
  extension FWVER=SPG 4.04
  extension PROTVER=32.01
...

# Set the version in ubxtool options
export UBXOPTS="-P 32.01"

# Set the baud rate to 115200
ubxtool -S 115200

# Persist the setting
ubxtool -p SAVE -P 32.01
ubxtool -p COLDBOOT -P 32.01

# If running GPSd, update the rate gpsd's config and restart gpsd
sudo nano /etc/default/gpsd
sudo systemctl restart gpsd

ubxtool is installed as part of the gpsd-clients package, which is automatically installed by this playbook.

For more on how to set the baud rate (or tweak other GPS module parameters), see millerjs.org's ubxtool page and the ubxtool examples page. You can also configure most options via pygpsclient using a GUI.

See this issue for more: Debug NEO-M9N module on TimeHAT V2.

Usage and Debugging

Some handy commands:

# GPS-related debugging
sudo systemctl status gpsd  # check gpsd status
gpsmon -n  # monitors gpsd output
cgps -s  # also monitors gps output

# PTP timestamping debugging
ethtool -T eth0  # or eth1, lists hardware clock info

# Chrony debugging
chronyc sources -v  # shows sources with documentation of fields
chronyc tracking  # shows detailed timing data

# Check on NTP service from another computer
ntpdate -q [ip of grandmaster]

# Check on PTP clocks and offsets (assuming ptp4l is running)
wget https://tsn.readthedocs.io/_downloads/f329e8dec804247b1dbb5835bd949e6f/check_clocks.c
gcc -o check_clocks check_clocks.c
sudo ./check_clocks -d eth0  # or eth1 (the interface you're using for PTP)

# Check offset between NIC PHY and system clock
sudo phc_ctl eth1 cmp  # should be nearly -37000000000ns

Much of the work that went into this project was documented in this thread on the TimeHat v2.

Slave / Client Setup

For PTP, you need to install and configure PTP for Linux on slave/client machines, and synchronize them to the master/server node as well.

An example configuration for a slave/client node is set up in ptp-client-node.yml, and further examples may be provided in the future.

Other Hobbyist Time Servers and Pi Time Builds

Other Time Resources I found Interesting

License

GPLv3 or Later

Author

Jeff Geerling, with assistance from Ahmad Byagowi and Oleg Obleukhov from Meta.

About

Raspberry Pi stratum 1 PTP and NTP timeserver configuration.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

  •