Main Content

Synchronize PTP Clocks on Two Target Computers

This example shows how to synchronize clocks on two target computers by using Precision Time Protocol (PTP).

System Setup for PTP

To run the PTP daemon and build a real-time application that uses PTP, the Speedgoat I/O Blockset must be installed. For more information, see PTP Prerequisites.

With the Speedgoat I/O Blockset installed, use the Speedgoat showPtpInterfaces utility to check that PTP support is available on the selected port and the support is configured.

To check the configuration for target computers tg1 and tg2, this example uses these commands in the MATLAB Command window:

speedgoat.showPtpInterfaces('TargetObject', tg1)
         Label         Index     PTPSupport                  Configuration
     _____________    _______    __________ _________________________________________
     {'Host Link'}    {'wm0'}     {'No' }      {'IP: 192.168.7.5 Subnet: 255.255.255.0'}
     {'ETH1'     }    {'wm1'}     {'Yes'}      {'Not configured'                       }
     {'Onboard'  }    {'wm2'}     {'No' }      {'Not configured'                       }
speedgoat.showPtpInterfaces('TargetObject', tg2)
         Label         Index     PTPSupport                  Configuration
     _____________    _______    __________ __________________________________________
     {'Host Link'}    {'wm0'}     {'No' }      {'IP: 192.168.7.10 Subnet: 255.255.255.0'}
     {'ETH1'     }    {'wm1'}     {'Yes'}      {'Not configured'                        }
     {'Onboard'  }    {'wm2'}     {'No' }      {'Not configured'                        }

To use PTP, configure the ethernet interface with an IP stack by using the Speedgoat Ethernet Configuration Tool speedgoat.configureEthernet.

For this example, the ETH1 port for tg1 is set for IP address 10.10.10.21, and the ETH1 port for tg2 is set for IP address 10.10.10.22.

After configuring the ETH1 ports on the target computers, connect them to each other by using an Ethernet cable.

Start PTP Daemons Target Computers

For TargetPC1 tg1, connect to the target computer and start the PTP daemon as clock transmitter (master, -W).

tg1 = slrealtime('TargetPC1');
connect(tg1);
tg1.ptpd.Command = 'ptpd -L -K -W -b wm1';
pause(5);
start(tg1.ptpd)

For TargetPC2 tg2, connect to the target computer and start the PTP daemon as clock receiver (slave, -g).

tg2 = slrealtime('TargetPC2');
connect(tg2);
tg2.ptpd.Command = 'ptpd -L -K -g -b wm1';
pause(5);
start(tg2.ptpd)

For more information about the PTP daemon options, see the ptpd, ptpd-avb reference page.

Open Model and Build Real-Time Application

Open the PTP clock receiver model slrt_ex_ptp_offset_time_receiver and build the real-time application.

model = 'slrt_ex_ptp_offset_time_receiver';
open_system(model);
modelSTF = getSTFName(tg2);
set_param(model,"SystemTargetFile",modelSTF);
evalc('slbuild(model)');

To confirm PTP clock synchronization, load the real-time application on tg2 and run the application.

load(tg2,model);
start(tg2);

View Clock Offset Between Target Computers

Open the Simulation Data Inspector to observe the offset or difference between transmitter and receiver clocks. The display shows how the clock offset is reduced as the clocks synchronize.

Close Model

bdclose(model);