ThreadX docsThreadX docs
ThreadX
ThreadX Modules
NetX Duo
FileX
GUIX
USBX
TraceX
LevelX
ThreadX
ThreadX Modules
NetX Duo
FileX
GUIX
USBX
TraceX
LevelX
  • NetX Duo Documentation

    • Understand NetX Duo
    • NetX Duo documentation
    • Chapter 1 - Introduction to NetX Duo
    • Chapter 2 - Installation and Use of NetX Duo
    • Chapter 3 - Functional Components of NetX Duo
    • Chapter 4 - Description of NetX Duo Services
    • Chapter 5 - NetX Duo Network Drivers
    • Appendix A - NetX Duo Services
    • Appendix B - NetX Duo Constants
    • Appendix C - NetX Duo Data Types
    • Appendix D - NetX Duo BSD-Compatible Socket API
    • Appendix E - NetX Duo ASCII Character Codes
    • Auto-IP

      • Chapter 1 - Introduction to NetX Duo AutoIP
      • Chapter 2 - Installation and use of NetX Duo AutoIP
      • Chapter 3 - Description of NetX Duo AutoIP services
    • BSD

      • Chapter 1 - Introduction to NetX Duo BSD
      • Chapter 2 - Installation and use of NetX Duo BSD
      • Chapter 3 - NetX Duo BSD Services
    • Crypto

      • Chapter 1 - Introduction to NetX Duo Crypto
      • Chapter 2 - Installation and use of NetX Duo Crypto
      • Chapter 3 - Functional description of NetX Duo Crypto
      • Chapter 4 - NetX Duo Crypto API description
      • Appendix - NetX Duo Crypto CAVS test
    • DHCP Client

      • Chapter 1 - Introduction to the NetX Duo DHCP Client
      • Chapter 2 - Installation and use of NetX Duo DHCP Client
      • Chapter 3 - Description of NetX Duo DHCP Client services
      • Appendix A - Description of the Restore state feature for NetX Duo DHCP Client services
    • DHCP Server

      • Chapter 1 - Introduction to NetX Duo DHCP Server
      • Chapter 2 - Installation and Use of the NetX Duo DHCP Server
      • Chapter 3 - Description of NetX Duo DHCP server services
    • DHCPv6 Client

      • Chapter 1 - Introduction to NetX Duo DHCPv6 Client
      • Chapter 2 - Installation and use of NetX Duo DHCPv6 Client
      • Chapter 3 - NetX Duo DHCPv6 configuration options
      • Chapter 4 - NetX Duo DHCPv6 Client services
      • Appendix A - Description of the Restore State Feature for NetX Duo DHCPv6 Client
    • DHCPv6 Server

      • Chapter 1 - Introduction to NetX Duo DHCPv6 server
      • Chapter 2 - Installation and use of NetX Duo DHCPv6 server
      • Chapter 3 - NetX Duo DHCPv6 server configuration options
      • Chapter 4 - NetX Duo DHCPv6 server services
      • Appendix A – NetX Duo DHCPv6 option codes
      • Appendix B - NetX Duo DHCPv6 server status codes
      • Appendix C - NetX Duo DHCPv6 unique identifiers (DUIDs)
      • Appendix D - NetX Duo Advanced DHCPv6 server example
    • DNS

      • Chapter 1 - Introduction to the NetX Duo DNS Client
      • Chapter 2 - Installation and Use of NetX Duo DNS Client
      • Chapter 3 - Description of NetX Duo DNS Client Services
    • FTP

      • Chapter 1 - Introduction to NetX Duo FTP
      • Chapter 2 - Installation and use of FTP
      • Chapter 3 - Description of FTP services
    • HTTP

      • Chapter 1 - Introduction to NetX Duo HTTP
      • Chapter 2 - Installation and Use of NetX Duo HTTP
      • Chapter 3 - Description of NetX Duo HTTP Services
    • iperf

      • Chapter 1 - Introduction to NetX Duo Iperf
      • Chapter 2 - Installing and using NetX Duo Iperf
      • Chapter 3 - Running the UDP Transmit Test
    • mDNS

      • Chapter 1 - Introduction to NetX Duo mDNS/DNS-SD
      • Chapter 2 - Installation and use of mDNS
      • Chapter 3 - Description of internal service cache
      • Chapter 4 - Description of mDNS services
    • mqtt

      • Chapter 1 - Introduction to NetX Duo MQTT
      • Chapter 2 - Installation and use of NetX Duo MQTT client
      • Chapter 3 - Description of NetX Duo MQTT Client Services
    • NAT

      • Chapter 1 - An introduction to Network Address Translation
      • Chapter 2 - Installation and use of NAT
      • Chapter 3 - NAT configuration options
      • Chapter 4 - Description of NAT services
    • POP3 Client

      • Chapter 1 - Introduction to NetX Duo POP3
      • Chapter 2 - Installation and use of NetX Duo POP3 Client
      • Chapter 3 - Description of POP3 Client services
    • PPP

      • Chapter 1 - Introduction to the NetX Duo Point-to-Point Protocol (PPP)
      • Chapter 2 - Installation and use of NetX Duo Point-to-Point Protocol (PPP)
      • Chapter 3 - Description of NetX Duo Point-to-Point Protocol (PPP) services
    • PTP Client

      • Chapter 1 - Introduction to NetX Duo PTP Client
      • Chapter 2 - Installation and Use of NetX Duo PTP Client
      • Chapter 3 - Description of NetX Duo PTP Client Services
    • rtp

      • Chapter 1 - Introduction to the NetX Duo RTP Sender
      • Chapter 2 - Installation and use of NetX Duo RTP Sender
      • Chapter 3 - Description of NetX Duo RTP Sender Services
    • rtsp

      • Chapter 1 - Introduction to the NetX Duo RTSP Server
      • Chapter 2 - Installation and use of NetX Duo RTSP Server
      • Chapter 3 - Description of NetX Duo RTSP Server Services
    • Secure DTLS

      • Chapter 1 - Introduction to NetX Duo Secure DTLS
      • Chapter 2 - Installation and use of NetX Duo Secure DTLS
      • Chapter 3 - Functional description of NetX Duo Secure DTLS
      • Chapter 4 - Description of NetX Duo Secure DTLS services
      • Appendix A - NetX Duo Secure DTLS return/error codes
    • Secure TLS

      • Chapter 1 - Introduction to NetX Duo Secure
      • Chapter 2 - Installation and use of NetX Duo Secure
      • Chapter 3 - Functional description of NetX Duo Secure
      • Chapter 4 - Description of NetX Duo Secure services
      • Appendix A - NetX Duo Secure return/error codes
    • SMTP Client

      • Chapter 1 - Introduction to NetX Duo SMTP client
      • Chapter 2 - Installation and use of NetX Duo SMTP client
      • Chapter 3 - Client description of SMTP Client services
    • snmp

      • Chapter 1 - Introduction to NetX Duo SNMP
      • Chapter 2 - Installation and use of the NetX Duo SNMP agent
      • Chapter 3 - Description of NetX Duo SNMP agent services
    • sntp client

      • Chapter 1 - Introduction to NetX Duo SNTP
      • Chapter 2 - Installation and Use of NetX Duo SNTP Client
      • Chapter 3 - Description of NetX Duo SNTP Client Services
      • Appendix A - NetX Duo SNTP Fatal Error Codes
    • telnet

      • Chapter 1 - Introduction to NetX Duo Telnet
      • Chapter 2 - Installation and use of NetX Duo Telnet
      • Chapter 3 - Description of NetX Duo Telnet services
    • TFTP

      • Chapter 1 - Introduction to NetX Duo TFTP
      • Chapter 2 - Installation and use of NetX Duo TFTP
      • Chapter 3 - Description of NetX Duo TFTP services
    • Web HTTP

      • Chapter 1 - Introduction to HTTP and HTTPS
      • Chapter 2 - Installation and use of HTTP and HTTPS
      • Chapter 3 - Description of HTTP services
    • About the NetX Duo User Guide

Chapter 1 - Introduction to NetX Duo PTP Client

The NetX Duo PTP implements the client part of the Precision Time Protocol (PTP) version 2, IEEE 1588-2008. It is used to synchronize clocks among MCUs on the local network by communicating each other via IPv4 or IPv6 multicast packets.

NetX Duo PTP Client Setup

In order to function properly, the PTP client package requires that a NetX Duo IP instance has already been created.

By default, the PTP client joins IPv4 multicast group. In order to run the PTP client on an IPv6 network, NX_ENABLE_IPV6_MULTICAST must be defined when building NetX Duo library.

When creating the PTP client, the application must provide a callback function to handle timestamps of incoming and outgoing packets. To achieve high resolution, we recommend applications to generate timestamps using a high resolution timer. To run the PTP on simulator, a software-based implementation nx_ptp_client_soft_clock_callback is provided.

The PTP client requires a packet pool for transmitting PTP messages. The payload size of packet pool must be no less than NX_UDP_PACKET + NX_PTP_CLIENT_PACKET_DATA_SIZE, which is 108 bytes for IPv4, and 128 bytes if IPv6 is enabled.

After creating the PTP Client, the application can start the PTP client. The synchronization is done in the PTP helper thread. An event callback function can be specified. It will be invoked when any one of the following events happen.

  • A master is selected;
  • The time is calibrated.
  • A master times out.

NetX Duo PTP Client Messages

The NetX Duo PTP client implements the delay request-response mechanism only. The PTP client opens two UDP ports. 319 for event message and 320 for general message. There are five types of message for this mechanism.

  • Announce. This is an event message. It is used for master clock selection.
  • Sync. This is an event message. It is used to send origin timestamp and calculate the path delay from master to client.
  • Follow_Up. This is a general message. It is optional and used to correct the origin timestamp in related Sync message. It is sent only when the two step bit in Sync flag is set.
  • Delay_Req. This is an event message. It is sent from PTP client to calculate the path delay from client to master, on receiving Delay_Resp.
  • Delay_Resp. This is an event message. It is sent from master to client to calculate the path delay from client to master.

Note, "best master clock" algorithm is not implemented. Only the first master clock is accepted when the PTP client is in listening state.

NetX Duo PTP Client Clock Callback

To synchronize clock from master, PTP client needs a local clock. A clock callback function is passed into PTP client during creation. The format of the clock callback routine is defined below.

UINT ptp_clock_callback(
    NX_PTP_CLIENT *client_ptr, 
    UINT operation,
    NX_PTP_TIME *time_ptr, 
    NX_PACKET *packet_ptr,
    VOID *callback_data);

The input parameters are defined as follows:

  • client_ptr points to PTP client.
  • operation specifies the callback operation, valid values are defined as shown in the list below.
    • NX_PTP_CLIENT_CLOCK_INIT Initialize clock.
    • NX_PTP_CLIENT_CLOCK_SET Set current timestamp specified by time_ptr.
    • NX_PTP_CLIENT_CLOCK_GET Return current timestamp to time_ptr.
    • NX_PTP_CLIENT_CLOCK_PACKET_TS_EXTRACT Extract timestamp from packet_ptr to time_ptr.
    • NX_PTP_CLIENT_CLOCK_ADJUST Adjust current timestamp less than 1 second.
    • NX_PTP_CLIENT_CLOCK_PACKET_TS_PREPARE Mark the packet_ptr which requires to notify PTP client the timestamp when it is transmitted.
    • NX_PTP_CLIENT_CLOCK_SOFT_TIMER_UPDATE Update soft timer. It can be ignored by hardware clock.
  • time_ptr points to timestamp.
  • packet_ptr points to packet.
  • callback_data points to opaque callback data.

The NX_PTP_TIME data type is defined as follows.

typedef struct NX_PTP_TIME_STRUCT
{
    /* The MSB of the number of seconds */
    LONG  second_high;

    /* The LSB of the number of seconds */
    ULONG second_low;

    /* The number of nanoseconds */
    LONG  nanosecond;
} NX_PTP_TIME;

NetX Duo PTP Client Event Callback

The event callback function can be setup to notify application the state of PTP client. The format of the event callback routine is defined as shown below.

UINT event_callback(
    NX_PTP_CLIENT *client_ptr, 
    UINT event, 
    VOID *event_data, 
    VOID *callback_data);

The input parameters are.

  • client_ptr points to PTP client.
  • event specifies the callback event, valid values are defined as:
    • NX_PTP_CLIENT_EVENT_MASTER A master clock is selected.
    • NX_PTP_CLIENT_EVENT_SYNC PTP client is calibrated.
    • NX_PTP_CLIENT_EVENT_TIMEOUT Master clock is timeout.
  • event_data specifies the data related to event. When event is NX_PTP_CLIENT_EVENT_MASTER, event_data points to NX_PTP_CLIENT_MASTER instance. When event is NX_PTP_CLIENT_EVENT_SYNC, event data points to NX_PTP_CLIENT_SYNC instance.

NetX Duo PTP Client Communication

As mentioned previously, NetX Duo PTP client only supports delay request-response mechanism. This mechanism measures the mean path delay between the client and the master clock as below:

  1. Client receives Announce message from master and select it as best master.
  2. Client receives Sync message from master. The timestamp t1 is the origin timestamp in this message. The timestamp t2 is read from local clock when this message is received.
  3. Client receives Follow_Up message from master. This message is optional and valid only when two step in Sync flag is set. Then the timestamp t1 is updated to the origin timestamp in this message.
  4. Client sends Delay_Req message to master. The timestamp t3 is read from local clock when the message is transmitted.
  5. Client receives Delay_Resp message from master. The timestamp t4 is the receive timestamp in this message.

The mean path delay is computed as shown below.

<meanPathDelay>=[(t2-t1)+(t4-t3)]/2

The offset from master is computed as shown below.

<offsetFromMaster>=client_clock-master_clock
                  =(t2-t1)-<meanPathDelay>

Note: The correctionField is ignored during the calculation.

Next
Chapter 2 - Installation and Use of NetX Duo PTP Client