How to run IEEE-1588 on Raspberry Pi hardware?

 IEEE-1588 (Precision Time Protocol, PTP) provides sub-microsecond clock synchronization for networked devices. Here's how to implement it on Raspberry Pi:



1. Hardware Requirements

  • Raspberry Pi 4/5 (recommended) or Pi 3B+

  • Ethernet connection (PTP works best with wired networking)

  • Optional: GPS module (for grandmaster clock functionality)


2. Software Setup

Install Required Packages

bash

sudo apt update
sudo apt install linuxptp ethtool

Configure Network Interface

Check your Ethernet interface name:

bash

ip a

(typically eth0 for wired connections)

Enable hardware timestamping (if supported):

bash

sudo ethtool -T eth0

Look for:


Hardware Transmit Timestamping: yes
Hardware Receive Timestamping: yes

If supported, enable PTP features:

bash

sudo ethtool --set-ts eth0 rx-filter ptpv2


3. Running PTP

Option A: Ordinary Clock (Slave)

bash

sudo ptp4l -i eth0 -m -S
  • -i: Interface

  • -m: Print messages to stdout

  • -S: Software timestamping (use -H for hardware if available)

Option B: Grandmaster Clock

If you want your Pi to act as the time source:

bash

sudo ptp4l -i eth0 -m -S --master_only 1

Monitor Synchronization

bash

sudo pmc -u -b 0 'GET TIME_STATUS_NP'


4. Hardware Timestamping (Pi 4/5 Only)

For best accuracy (sub-100ns):

bash

# Load module for hardware timestamping
sudo modprobe ptp_rpi
sudo ptp4l -i eth0 -m -H -f /etc/linuxptp/ptp4l.conf


5. System Time Synchronization

To sync PTP with system clock:

bash

sudo phc2sys -s eth0 -c CLOCK_REALTIME -O 0 -m


6. Verification

Check offset from master:

bash

sudo ptp4l -i eth0 -m -q -S


7. Autostart Configuration

Create a service file:

bash

sudo nano /etc/systemd/system/ptp.service

Add:


[Unit]
Description=PTP Service

[Service]
ExecStart=/usr/sbin/ptp4l -i eth0 -m -H -f /etc/linuxptp/ptp4l.conf
Restart=always

[Install]
WantedBy=multi-user.target

Then enable:

bash

sudo systemctl enable ptp
sudo systemctl start ptp


8. Expected Performance

  • Software timestamping: ±100μs accuracy

  • Hardware timestamping (Pi 4/5): ±100ns accuracy

  • With GPS grandmaster: ±50ns accuracy


Troubleshooting

  • If ptp4l fails, check kernel messages: dmesg | grep ptp

  • Ensure NTP is disabled: sudo timedatectl set-ntp false

  • For industrial applications, consider a dedicated PTP grandmaster clock

This setup turns your Raspberry Pi into a precise network time client or server, suitable for audio/video synchronization, industrial automation, or financial timestamping applications.

评论

此博客中的热门博文

What is a DSP? (Digital Signal Processor)

How To Connect Stm32 To PC?

MCU clock configuration and external crystal oscillator selection