Synergy Software Package User's Manual
NetX/NetX Duo SNTP Client

NetX/NetX Duo SNTP Client Introduction

The Simple Network Time Protocol (SNTP) is a protocol designed for synchronizing clocks over the Internet. SNTP Version 4 is a simplified protocol based on the Network Time Protocol (NTP) and utilizes User Datagram Protocol (UDP) services to perform time updates in a simple, stateless protocol. On the internet, the SNTP provides accuracies of 1-50 milliseconds, depending on the characteristics of the synchronization source and network paths. The SNTP has many options to provide reliability of receiving time updates. Ability to switch to alternative servers, applying back-off polling algorithms and automatic time server discovery are just a few of the means for an SNTP Client to handle a variable internet time service environment. What it lacks in precision it makes up for in simplicity and ease of implementation. The SNTP is intended primarily for providing comprehensive mechanisms to access national time and frequency dissemination for NTP Server services.

Note
Except for internal processing, the NetX Duo™ SNTP Client is identical in the application, set up and running of an SNTP thread as the NetX™ SNTP Client. This document will clearly identify any differences in use between the NetX and NetX Duo SNTP Client.

Unsupported Features

Broadcast has not been tested in this version of SSP.

Multiple network interface has not been tested in this version of SSP. 

NetX/NetX Duo SNTP Client Module Features

  • Supports the SNTP standard to obtain time over the internet
  • The NetX SNTP client is compliant with RFC4330 Simple Network Time Protocol (SNTP) Version 4 for IPv4, IPv6 and OSI and related RFCs
  • Supports both unicast and broadcast SNTP messaging
  • Utilities for converting NTP time into date and time format.
  • Performs sanity checks for valid time reception
  • Support for a SNTP Client on secondary interfaces
  • Callbacks to be notified of receiving time updates
nx_sntp_client_BD.png
NetX/NetX Duo SNTP Client Module Block Diagram
Note
In the figure above, the NetX (or NetX Duo) Network Driver modules has multiple implementation options available. See the description just after the module stack figure in Including the NetX/NetX Duo SNTP Client Module in an Application for additional details.

NetX/NetX Duo SNTP Client Module APIs Overview

The NetX SNTP Client module defines APIs for creating, deleting, generating response packets, response sending and getting information from a received packet. A complete list of the available APIs, an example API call and a short description of each can be found in the following table. A table of status return values follows the API summary table.

NetX/NetX Duo SNTP Client Module API Summary

Function Name Example API Call and Description
nx_sntp_client_create nx_sntp_client_create(&demo_client, iface_index,&client_ip,
&client_packet_pool, leap_second_handler, kiss_of_death_handler,NULL // no random_number_generator callback  );
This service creates an SNTP Client instance.
nx_sntp_client_delete nx_sntp_client_delete(&demo_client);
Delete the SNTP Client.
nx_sntp_client_get_local_time nx_sntp_client_get_local_time(&demo_client, &base_seconds,&base_milliseconds, NX_NULL);
Get SNTP Client local time.
nx_sntp_client_initialize_broadcast nx_sntp_client_initialize_broadcast(&demo_client,0x0, NX_NULL, IP_ADDRESS(192,2,2,255);
Initialize Client for IPv4 broadcast operation.
**nxd_sntp_client_initialize_broadcast nxd_sntp_client_initialize_broadcast(&demo_client,0x0, NX_NULL, &broadcast_server)
Initialize Client for IPv6 or IPv4 broadcast operation.
nx_sntp_client_initialize_unicast nx_sntp_client_initialize_unicast(&demo_client, IP_ADDRESS(192,2,2,1));
Initialize Client for IPv4 unicast operation.
**nxd_sntp_client_initialize_unicast nxd_sntp_client_initialize_unicast(&demo_client, *unicast_server);
Initialize Client for IPv4 or IPv6 unicast operation.
nx_sntp_client_receiving_updates nx_sntp_client_receiving_updates(&demo_client, &receive_status);
Client is currently receiving valid SNTP updates.
nx_sntp_client_request_unicast_time nx_sntp_client_request_unicast_time(&demo_client, 400);
Send a request asynchronously to NTP server.
nx_sntp_client_run_broadcast nx_sntp_client_run_broadcast(&demo_client);
Receive time updates from server.
nx_sntp_client_run_unicast nx_sntp_client_run_unicast(&demo_client);
Send requests and receive time updates from server.
nx_sntp_client_set_local_time nx_sntp_client_set_local_time(&demo_client, base_seconds, base_fraction);
Set SNTP Client initial local time.
nx_sntp_client_set_time_update_notify nx_sntp_client_set_time_update_notify(&demo_client, time_update_cb);
Sets callback to notify the application when the SNTP Client receives a valid time update.
nx_sntp_client_stop nx_sntp_client_stop(&demo_client);
Stop the SNTP Client thread.
nx_sntp_client_utility_display_date_and_time nx_sntp_client_utility_display_date_time(&demo_client, buffer, sizeof(buffer));
Display NTP time in date and time format
nx_sntp_client_utililty_msecs_to_fraction nx_sntp_client_utility_msecs_to_fraction(milliseconds, &fraction);
Convert milliseconds to NTP fraction component.
Note
For details on operation and definitions for the function data structures, typedefs, defines, API data, API structures, and function variables, review the associated Azure RTOS User's Manual in the References section.

**This API is only available in NetX Duo SNTP Client. Please refer to the NetX Duo User Guide for the Renesas Synergy™ Platform for definition of NetX Duo specific data types.

Status Return Values

Name Description
NX_SUCCESS API Call Successful
NX_SNTP_INSUFFICIENT_PACKET_PAYLOAD Invalid non-pointer input
NX_PTR_ERROR ** Invalid pointer input
NX_CALLER_ERROR** Invalid caller of service
NX_INVALID_INTERFACE Invalid network interface
NX_INVALID_PARAMETERS Invalid non-pointer input
NX_SNTP_PARAM_ERROR Invalid non-pointer input
NX_SNTP_CLIENT_NOT_STARTED SNTP Client not running
NX_SNTP_CLIENT_ALREADY_STARTED Client already running
NX_SNTP_CLIENT_NOT_INITIALIZED Client not initialized
NX_SNTP_ERROR_CONVERTING_DATETIME NX_SNTP_CURRENT_YEAR not defined or no local time established
NX_SNTP_INVALID_DATETIME_BUFFER Insufficient buffer length
NX_SNTP_OVERFLOW_ERROR Error converting time to a date
NX_SNTP_INVALID_TIME Invalid SNTP data input
Note
Lower-level drivers may return common error codes. Refer to the SSP User's Manual API References for the associated module for a definition of all relevant status return values.
  • These are error codes which are only returned if error checking is enabled. Refer to the NetX User Guide for the Renesas Synergy™ Platform or NetX Duo User's Guide for the Renesas Synergy™ Platform for more details on error-checking services in NetX and NetX Duo, respectively.

NetX/NetX Duo SNTP Client Module Operational Overview

The NetX/NetX Duo SNTP Client module creates an IP instance and packet pool. In addition, the UDP is enabled on that IP instance and a UDP socket is created and bound to the well-known port 123 for sending time update requests to and receiving time data from an SNTP Server, although alternative ports will work as well. Broadcast clients should bind the UDP port when their broadcast server is sending on, usually 123. The NetX/NetX Duo SNTP Client application requires a server address to initialize a SNTP Client thread task for either unicast or broadcast operations. 

The NetX/NetX Duo SNTP Client can operate in one of two basic modes, unicast or broadcast, to obtain time over the internet. In unicast mode, the Client polls its SNTP Server on regular intervals and waits to receive a reply from that server. When one is received, the client verifies that the reply contains a valid time update by applying a set of sanity checks recommended by RFC 4330. The client then applies the time difference locally, if there is a time difference between the server clock and the local clock. In broadcast mode, the client merely listens for time update broadcasts and maintains its local clock after applying a similar set of sanity checks to verify the update time data.

Before the client can run in either mode, the application can get an estimate of the absolute time.  In NTP, that means the number of seconds since Jan 1, 1970 at midnight.  For example:

    base_seconds = 0xd2c96b90; /* This is the number of seconds since 1970                   
                                    on Jan 24, 2012 UTC */
    base_fraction = 0xa132db1e; /* between 0-1 seconds, not so critical */

It is not required, but if this time is registered with the SNTP Client (using the nx_sntp_client_set_local_time service), it gives the SNTP Client a means to gauge if the first received time update is reasonably accurate. NTP time keepers are more accurate than all but the most demanding time-based applications, but there is the need to check for invalid or rogue SNTP packets.  If obtaining this baseline time is not possible, and no local time is set before starting the SNTP Client, the SNTP Client will accept the time updates at face value.

Now the application can prepare the SNTP Client for receiving time updates.  This is done by calling either nx_sntp_client_initialize_unicast, or nx_sntp_client_initialize_broadcast for unicast or broadcast modes.  (These are limited to IPv4 networks only.)  If using the NetX Duo SNTP Client Module, nxd_sntp_client_initialize_broadcast and nxd_sntp_client_initialize_unicast are available services which support both IPv4 and IPv6 networks. If these initialization service calls succeed, the application can start the SNTP Client by calling the nx_sntp_client_run_broadcast or nx_sntp_client_run_unicast services.

The application can set the following parameters to fine-tune SNTP Client operation:

  • The maximum time adjustment to make the local clock, which is the Maximum time adjustment allowed to local clock time (milliseconds) property.  If there is no notion of local time, the Ignore maximum time adjust limit at startup property can be enabled to skip this check on the first time update. 
  • The unicast polling rate which is the Starting poll interval for unicast update request (seconds) property. This is the rate at which the SNTP Client polls the NTP time server for an update if running in unicast mode.
  • The maximum allowed interval between time requests sent to the server, which is the Maximum time lapse without valid update (seconds) property.  If a time request goes unanswered, the SNTP Client doubles the polling interval up to this maximum interval.
  • The maximum number of invalid SNTP packets received from a server before marking the server invalid, which is the Invalid message limit to mark server invalid property.

If these limits are exceeded, the SNTP Client continues to run but sets the current SNTP Server status to invalid. The application can call the nx_sntp_client_receiving_updates service periodically to check for valid server status.  If this service indicates no updates received, the application should stop the SNTP Client thread using the nx_sntp_client_stop service and use another SNTP Server.  To restart the SNTP Client, call either of the two initialize services, nx_sntp_client_initialize_broadcast or nx_sntp_client_initialize_unicast for IPv4 networks (or nxd_sntp_client_initialize_broadcast or nxd_sntp_client_initialize_unicast), then restart the SNTP Client with the nx_sntp_client_run_broadcast or nx_sntp_client_run_unicast services.

Additional details on the topics of local clock operation, SNTP sanity checks, and SNTP asynchronous and unicast requests are described in detail in the NetX Simple Network Time Protocol (SNTP) Client User Guide for the Renesas Synergy™ Platform document described in the introduction to this document.

NetX/NetX Duo SNTP Client Module Important Operational Notes and Limitations

NetX/NetX Duo SNTP Client Module Operational Notes

  • The application can be notified of time updates if it registers a callback with the SNTP Client. To do so, use the _nx_sntp_client_set_time_update_notify service to specify the callback.
  • Once the SNTP Client begins receiving time updates, the application can query the SNTP Client for status on time updates.  It can receive the NTP time directly using the nx_sntp_client_get_local_time service, or it can receive a formatted date time output using the _nx_sntp_client_utility_display_date_time service.  In the latter case, the application must set the Current calendar year property of the SNTP Client module for SNTP Client to process NTP time data correctly.
  • RFC 4330 recommends that SNTP clients should operate only at the highest stratum of their local network and preferably in configurations where no NTP or SNTP Client is dependent on them for synchronization. Stratum level reflects the host position in the NTP time hierarchy where stratum 1 is the highest level (a root time server) and 15 is the lowest allowed level (per client). The SNTP Client default minimum stratum is 2.

NetX/NetX Duo SNTP Client Module Limitations

  • Precision in local time representation in NTP time updates handled by the SNTP Client API is limited to millisecond resolution.
  • The NetX/NetX Duo SNTP Client only holds a single SNTP Server address at any time. If that server appears to be no longer valid, the application must stop the SNTP Client task and reinitialize it with another SNTP server address, using either broadcast or unicast SNTP communication.
  • The NetX/NetX Duo SNTP Client does not support many cast.
  • The NetX/NetX Duo SNTP Client does not support authentication mechanisms for verifying received packet data.
  • Refer to the most recent SSP Release Notes for any additional operational limitations for this module.

Including the NetX/NetX Duo SNTP Client Module in an Application

This section describes how to include either or both the NetX and NetX Duo SNTP Client module in an application using the SSP configurator.

Note
It is assumed you are familiar with creating a project, adding threads, adding a stack to a thread, and configuring a block within the stack. If you are unfamiliar with any of these items, refer to the first few chapters of the SSP User's Manual to learn how to manage each of these important steps in creating SSP-based applications.

To add the NetX/NetX Duo SNTP Client module to an application, simply add it to a thread using the stacks selection sequence given in the following table.

NetX/NetX Duo SNTP Client Module Selection Sequence

Resource ISDE Tab Stacks Selection Sequence
g_sntp_client0 NetX SNTP Client Threads New Stack> X-Ware> NetX> Protocols> NetX SNTP Client
g_sntp_client0 NetX Duo SNTP Client Threads New Stack> X-Ware> NetX Duo> Protocols> NetX Duo SNTP Client

When the NetX and/or NetX Duo SNTP Client module is added to the thread stack as shown in the following figure, the configurator automatically adds any needed lower‑level modules. Any modules needing additional configuration information have the box text highlighted in Red. Modules with a Gray band are individual modules that stand alone. Modules with a Blue band are shared or common; they need only be added once and can be used by multiple stacks. Modules with a Pink band can require the selection of lower-level modules; these are either optional or recommended. (This is indicated in the block with the inclusion of this text.) If the addition of lower-level modules is required, the module description include Add in the text. Clicking on any Pink banded modules brings up the New icon and displays possible choices.

nx_sntp_client_MS.png
NetX/NetX Duo SNTP Client Module Stack

In the stack above, the NetX Network Driver (or NetX Duo Network Driver in a NetX Duo stack) has not been populated yet. There are multiple possible selections for the Network Driver; they are not all provided so as not to needlessly complicate the figure and the following configuration tables. The available options depend on the MCU target, but some typical options include:

  • NetX Duo Port using PPP on nxd_ppp
  • NetX Port ETHER on sf_el_nx
  • NetX Port using Cellular Framework on sf_cellular_nsal_nx
  • NetX Port using PPP on nx_ppp
  • NetX Port using Wi-Fi Framework on sf_wifi_nsal_nx

Configuring the NetX/NetX Duo SNTP Client Module

The NetX/NetX Duo SNTP Client module must be configured by the user for the desired operation. The SSP configuration window automatically identifies (by highlighting the block in red) any required configuration selections, such as interrupts or operating modes, which must be configured for lower-level modules for successful operation. Only properties that can be changed without causing conflicts are available for modification. Other properties are locked and not available for changes and are identified with a lock icon for the locked property in the Properties window in the ISDE. This approach simplifies the configuration process and makes it much less error-prone than previous manual approaches to configuration. The available configuration settings and defaults for all the user-accessible properties are given in the Properties tab within the SSP Configurator and are shown in the following tables for easy reference.

Note
You may want to open your ISDE, create the module and explore the property settings in parallel with looking over the following configuration table values. This helps to orient you and can be a useful hands-on approach to learning the ins and outs of developing with SSP.

Configuration Settings for the NetX/NetX Duo SNTP Client Module

ISDE Property Value Description
Internal thread stack size (bytes) 2048 Internal thread stack size selection
SNTP client thread time slicing interval (ticks) TX_NO_TIME_SLICE SNTP client thread time slicing interval selection
Internal thread priority 2 Internal thread priority selection
UDP socket name SNTP Client socket UDP socket name selection
UDP port number 123 UDP port number selection
Server UDP port 123 Server UPD port selection
Time to live 128 Time to live selection
Maximum UDP packets queue depth (units) 5 Maximum UDP packets queue depth selection
Packet allocation timeout (seconds) 1 Packet allocation timeout selection
SNTP version to use 3 SNTP version to use selection
NTP minimum version 3 NTP minimum version selection
Lowest level server stratum client accepts 2 Lowest level server stratum client accepts selection
Minimum time difference that triggers adjustment (milliseconds) 10 Minimum time difference that triggers adjustment selection
Maximum time adjustment allowed to local clock time (milliseconds) 180000 Maximum time adjustment allowed to local clock time selection
Ignore maximum time adjust limit at startup True, False

Default: True
Ignore maximum time adjust limit at startup selection
Maximum time lapse without valid update (seconds) 7200 Maximum time lapse without valid update selection
Update time remaining timer update interval (seconds) 1 Update time remaining timer update interval selection
Starting poll interval for unicast update request (seconds) 3600 Starting poll interval for unicast update request selection
Poll interval increment after failed time update 2 Poll interval increment after failed time update selection
Calculate round trip time of messages True, False

Default: False
Calculate round trip time of messages selection
**Maximum server clock inaccuracy to accept (to disable set to 0) 50000 Maximum server clock inaccuracy to accept selection
Invalid message limit to mark server invalid 3 Invalid message limit to mark server invalid selection
Randomize update request interval on startup True, False

Default: False
Randomize update request interval on startup selection
Internal Task sleep interval (ticks) 1 Internal task sleep interval selection
Current calendar year 2016 Current calendar year selection
Name g_sntp_client0 Module name
Index to SNTP Network Interface 0 Index to SNTP network interface selection
Name of Leap Second Handler leap_second_handler Name of leap second handler selection
Name of Kiss of Death Handler kiss_of_death_handler Name of kiss of death handler selection
Name of Random Number Generator Function (optional) NULL Name of random number generator function selection
Name of generated initialization function sntp_client_init0 Name of generated initialization function selection
Auto Initialization Enable, Disable

Default: Enable
Auto initialization selection
Note
The example settings and defaults are for a project using the S7G2 Synergy MCU Group. Other MCUs may have different default values and available configuration settings.

** Indicates properties that are only available in NetX Duo.

In some cases, settings other than the defaults for stack modules can be desirable. For example, it might be useful to select different addresses for the Ethernet port. The configurable properties for the lower-level stack modules are given in the following sections for completeness and as a reference. 

Note:  Most of the property settings for lower-level modules are intuitive and usually can be determined by inspection of the associated properties window from the SSP configurator.

Configuration Settings for the NetX/NetX Duo SNTP Client Lower-Level Modules

Only a small number of settings must be modified from the default for the IP layer and lower-level drivers as indicated via the red text in the thread stack block. Notice that some of the configuration properties must be set to a certain value for proper framework operation and are locked to prevent user modification. The following table identifies all the settings within the properties section for the module:

Configuration Settings for the NetX/NetX Duo IP Instance

ISDE Property Value Description
Name g_ip0 Module name
IPv4 Address (use commas for separation) 0,0,0,0 IPv4 Address selection
Subnet Mask (use commas for separation) 255,255,255,0 Subnet Mask selection
**IPv6 Global Address (use commas for separation) 0x2001, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1 IPv6 global address selection
**IPv6 Link Local Address (use commas for separation, All zeros means use MAC address) 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 IPv6 link local address selection
IP Helper Thread Stack Size (bytes) 2048 IP Helper Thread Stack Size (bytes) selection
IP Helper Thread Priority 3 IP Helper Thread Priority selection
ARP Enable ARP selection
ARP Cache Size in Bytes 512 ARP Cache Size in Bytes selection
Reverse ARP Enable, Disable

Default: Disable
Reverse ARP selection
TCP Enable, Disable

Default: Enable
TCP selection
UDP Enable, Disable

Default: Enable
UDP selection
ICMP Enable, Disable

Default: Enable
ICMP selection
IGMP Enable, Disable

Default: Enable
IGMP selection
IP fragmentation Enable, Disable

Default: Disable
IP fragmentation selection
Name of generated initialization function ip_init0 Name of generated initialization function selection
Auto Initialization Enable, Disable

Default: Enable
Auto initialization selection
Note
The example settings and defaults are for a project using the Synergy S7G2 MCU Group. Other MCUs may have different default values and available configuration settings.

** Indicates properties that are only available in NetX Duo.

Configuration Settings for the NetX/NetX Duo Common Instance

ISDE Property Value Description
Name of generated initialization function nx_common_init0 Name of generated initialization function selection
Auto Initialization Enable, Disable

Default: Enable
Auto initialization selection
Note
The example settings and defaults are for a project using the S7G2 Synergy MCU Group. Other MCUs may have different default values and available configuration settings.

Configuration Settings for the NetX/NetX Duo Packet Pool Instance

ISDE Property Value Description
Name g_packet_pool0 Module name
Packet Size in Bytes 640 Packet size selection
Number of Packets in Pool 16 Number of packets in pool selection
Name of generated initialization function packet_pool_init0 Name of generated initialization function selection
Auto Initialization Enable, Disable

Default: Enable
Auto initialization selection
Note
The example settings and defaults are for a project using the S7G2 Synergy MCU Group. Other MCUs may have different default values and available configuration settings.

NetX/NetX Duo SNTP Client Module Clock Configuration

The ETHERC peripheral module uses PCLKA as its clock source. The PCLKA frequency is set using the SSP configurator clock tab prior to a build, or by using the CGC interface at run-time.

NetX/NetX Duo SNTP Client Module Pin Configuration

The ETHERC peripheral module uses pins on the MCU device to communicate to external devices. I/O pins must be selected and configured by the external device as required.  The following table illustrates the method for selecting the pins within the SSP configuration window and the subsequent table illustrates an example selection for the I2C pins.

Note
The selected operation mode determines the peripheral signals available and the MCU pins required.

Pin Selection for the ETHERC Module

Resource ISDE Tab Pin selection Sequence
ETHERC Pins Select Peripherals > Connectivity:ETHERC > ETHERC1.RMII
Note
The selection sequence assumes ETHERC1 is the desired hardware target for the driver.

Pin Configuration Settings for the ETHERC1

Property Value Description
Operation Mode Disabled, Custom, RMII

Default: Disabled
Select RMII as the Operation Mode for ETHERC1
Pin Group Selection Mixed, _A only

Default: _A only
Pin group selection
REF50CK P701 REF50CK Pin
TXD0 P700 TXD0 Pin
TXD1 P406 TXD1 Pin
TXD_EN P405 TXD_EN Pin
RXD0 P702 RXD0 Pin
RXD1 P703 RXD1 Pin
RX_ER P704 RX_ER Pin
CRS_DV P705 CRS_DV Pin
MDC P403 MDC Pin
MDIO P404 MDIO Pin
Note
The example settings are for a project using the S7G2 Synergy MCU and the SK-S7G2 Kit. Other Synergy MCUs and other Synergy Kits may have different available pin configuration settings.

Using the NetX/NetX Duo SNTP Client Module in an Application

The steps in using the NetX/NetX Duo SNTP Client module in a typical application are:

  1. Wait for a valid IP address using the nx_ip_status_check API.
  2. Set the mode for access to the SNTP Server with nx_sntp_client_initialize_unicast or nx_sntp_client_initialize_broadcast API, or if using NetX Duo nxd_sntp_client_initialize_unicast or nxd_sntp_client_initialize_broadcast API.
  3. Set the local time with the nx_sntp_client_set_local_time API (optional).
  4. Run the client using the nx_sntp_client_run_unicast or nx_sntp_client_run_broadcast API.
  5. Check if the SNTP is valid and running with the nx_sntp_client_receiving_updates API (recommended).
  6. Get the local time with the nx_sntp_client_get_local_time API or get the local time in date time format with the nx_sntp_client_utility_display_date_time API (optional).
  7. Stop the SNTP service with the nx_sntp_client_stop API.

The following figure illustrates common steps in a typical operational flow diagram:

nx_sntp_client_TA.png
Flow Diagram of a Typical NetX/NetX Duo SNTP Client Module Application