MATLAB Examples

IEEE® 1588™ Precision Time Protocol - Clock Synchronization

This example shows clock synchronization between target computers using the Precision Time Protocol (PTP) with UDP as transport protocol

Contents

Requirements

To run this example, you need to:

  1. Acquire two Speedgoat target computers equipped with the Intel® i210 Ethernet card from www.speedgoat.com. Alternatively, you can use the Intel 82574L Ethernet card.
  2. Acquire a standard network switch that has at least 4 ports, such as the Cisco® SG110D-08 8-ports network switch.

Connect and configure the devices in the network

In this example, the two target computers are named TargetPC1 and TargetPC2. TargetPC1 has one Intel® i210 network card that is used for host-target connection and PTP synchronization. TargetPC2 has a dedicated Intel® i210 card for PTP synchronization.

  1. Connect the network port of the development computer to a port of the Cisco® Ethernet switch.
  2. Connect the network port of each target computer that is dedicated to communicating with the development computer to a port of the Cisco® SG110D-08 Ethernet switch.
  3. Connect the network port of the Intel® i210 Ethernet card in TargetPC2 to a port of the the Cisco® Ethernet switch.

Note: For better PTP synchronization accuracy, consider using a network switch that support PTP in place of the Cisco® SG110D-08 switch. An example of PTP capable network swicth is the EDS-405A-PTP from www.moxa.com.

Open the models

Click on the following links to open the two models:

Open the dPTPUDPNode1 model:

openedMdl = find_system('type', 'block_diagram');
model1 = fullfile(matlabroot,'toolbox','rtw','targets','xpc','xpcdemos','dPTPUDPNode1');
model1Open = 0;

Check if the model is already opened. If not, open it.

if ~any(strcmp(model1, openedMdl))
    model1Open = 1;
    open_system(model1);
end

Open the dPTPUDPNode2 model:

model2 = fullfile(matlabroot,'toolbox','rtw','targets','xpc','xpcdemos','dPTPUDPNode2');
model2Open = 0;

Check if the model is already opened. If not, open it.

if ~any(strcmp(model2, openedMdl))
    model2Open = 1;
    open_system(model2);
end

Configure the models

The two models are configured for download on target computers TargetPC1 and TargetPC2 respectively. If one of these target computers setting, does not exist in the Simulink® Real-Time™ environment configuration on your development computer, you can create and configure it by using Simulink® Real-Time™ Explorer or MATLAB® command line. For example, to add TargetPC2 to your environment, type the following command at the MATLAB® command line:

tg2 = SimulinkRealTime.addTarget('TargetPC2');

Model dPTPUDPNode1 is configured to use the host-target connection for PTP. Configure TargetPC1 to boot using the Intel® i210 card for host-target communication. If you are using network boot, you can configure the target settings using Simulink® Real-Time™ Explorer or by typing the following commands at the MATLAB® command line:

env = SimulinkRealTime.getTargetSettings('TargetPC1');
env.TargetBoot = 'NetworkBoot';
env.TcpIpTargetDriver = 'I210';
SimulinkRealTime.createBootImage(env);

For model dPTPUDPNode2, open the mask for the IEEE 1588 UDP block and insert the required values for the PCI bus, slot and function numbers assigned to the Intel® i210 card dedicated to PTP synchronization. To obtain the bus, slot and function numbers, type the following commands at the MATLAB® command line and look for the information for the Intel® i210 Ethernet card (or Intel 82574L card if that is the card you are using for PTP):

tg2 = SimulinkRealTime.target('TargetPC2');
getPCIInfo(tg2, 'ethernet');

Each model displays on target computer scopes:

  • The PTP protocol state.
  • The PTP clock synchronization status (relevant for Slave clock only).
  • The PTP time in date format.
  • The offset from the Master (relevant for Slave clock only).
  • A signal received from the other model.

The PTP hardware clock running on the Intel® i210 Ethernet card is initialized with the target computer system time. Before you run the example, you can check the target computer system time by typing the following command at the MATLAB® command line:

tg1 = SimulinkRealTime.target('TargetPC1');
d = SimulinkRealTime.utils.getTargetSystemTime(tg1)

If the target computer system time is incorrect, you can set it to your desired value. For example, to set the target computer system time to be the same as the development computer system time in UTC time zone, type the following command at the MATLAB® command line:

SimulinkRealTime.utils.setTargetSystemTime(tg1);

Build and download the models onto the target computer

Configure for a non-verbose build.

set_param('dPTPUDPNode1','RTWVerbose','off');
set_param('dPTPUDPNode2','RTWVerbose','off');

Build and download the models onto the target computers.

rtwbuild('dPTPUDPNode1');
rtwbuild('dPTPUDPNode2');
### Starting Simulink Real-Time build procedure for model: dPTPUDPNode1
Warning: This model contains blocks that do not handle sample time
changes at runtime. To avoid incorrect results, only change
the sample time in the original model, then rebuild the model. 
### Successful completion of build procedure for model: dPTPUDPNode1
### Created MLDATX ..\dPTPUDPNode1.mldatx
### Looking for target: TargetPC1
### Download model onto target: TargetPC1

### Starting Simulink Real-Time build procedure for model: dPTPUDPNode2
### Generated code for 'dPTPUDPNode2' is up to date because no structural, parameter or code replacement library changes were found.
### Successful completion of build procedure for model: dPTPUDPNode2
### Created MLDATX ..\dPTPUDPNode2.mldatx
### Looking for target: TargetPC2
### Download model onto target: TargetPC2

Run The application

Run the two models for 50 seconds

tg1.start;
tg2.start;
pause(50);

One of The models will be the PTP Master clock and the other one the Slave clock. Since the PTP clock parameters for the two models are identical, this state decision will be made based on the MAC address of the Ethernet cards. In our setup, dPTPUDPNode1 is selected as the Slave clock.

Stop the application

tg1.stop;
tg2.stop;

Display the target computer scopes

View the target computer display.

  • Scope 1 shows the protocol state and the synchronization status. The protocol state value is equal to 9 for the Slave clock and equal to 6 for the Master clock. The synchronization status is always equal to 1 for the Master clock after the state switch to 6. For the Slave clock, the sysnchronization status is initially equal to 0, and then switch to 1 when the clock servo locks to the Master clock within the threshold specified in the block parameter. Note that when the model starts and the protocol state switches to Slave clock, a couple of seconds elapse before the clock servo locks to the master clock.
  • The offset from master value displayed on Scope 3 is relevant only for the Slave clock. For the Master clock it is always equal to 0.

For model dPTPUDPNode1 on TargetPC1, use the command:

tg1.viewTargetScreen;

For model dPTPUDPNode2 on TargetPC2, use the command:

tg2.viewTargetScreen;

Obtain and plot the offset from Master.

The following figure shows the value displayed on Scope 3 for the Slave clock when the clock servo lock to the Master clock.

figh = findobj('Name', 'PTPExampleUDP');
if isempty(figh)
  figh = figure;
  set(figh, 'Name', 'PTPExampleUDP', 'NumberTitle', 'off');
else
  figure(figh);
end
data = tg1.OutputLog;
if ~any(data(:, 1))
    data = tg2.OutputLog;
end

Find index when clock enter Slave state and servo locked

offsetIndex = find(data(:,1) ~= 0, 1, 'first');
syncIndex = offsetIndex + find(data(offsetIndex:end,2) ~= 0, 1, 'first');
offset = data(syncIndex:end, 1);
plot(offset, '.');
xlabel('Sample');
ylabel('Offset from Master clock (nanoseconds)');
title('PTP clock synchronization accuracy');
drawnow limitrate;

Close the models

Close the model if we opened them.

if (model1Open)
  bdclose(model1);
end
if (model2Open)
  bdclose(model2);
end