Sample Application

Initialization

This example shows how to initialize µC/SNTPc. Note that µC/TCP-IP must be initialized without error before initializing µC/SNTPc.

#include  <Source/sntp-c.h>
#include  <sntp-c_cfg.h>
 
CPU_BOOLEAN App_SNTPc_Init (void)			
{
    SNTPc_ERR       sntp_err;
    CPU_BOOLEAN     ret_val;
                                                                
	ret_val = SNTPc_Init(SNTPc_Cfg, &sntp_err);
    
    return (ret_val);
}
  1. Prerequisite modules must be initialized before calling any uC/SNTPc functions.

  2. Prior to do any call to uC/SNTPc, the module must be initialized. This is done by calling SNTPc_Init(). If the process is successful, the SNTP client internal data structures are initialized and ready to operate.

SNTP request 

Set µC/CLK module time stamp from a SNTP request to a NTP server.

CPU_BOOLEAN App_SNTPc_SetClk (SNTPc_CFG *p_sntp_cfg)			(1)(2)
{
    SNTP_TS         ntp_ts;
    SNTP_PKT        sntp_pkt;
    SNTPc_ERR       sntp_err;
    CPU_BOOLEAN     ret_val;
                                                                
    ret_val = SNTPc_ReqRemoteTime( p_sntp_cfg,                  (3)
                                  &sntp_pkt,
                                  &sntp_err);
    if (ret_val == DEF_FAIL) {
        return (DEF_FAIL);
    }
                                                                
    ntp_ts = SNTPc_GetRemoteTime(&sntp_pkt, &sntp_err);         (4)

                                                               
    ret_val = Clk_SetTS_NTP(ntp_ts.Sec);                        (5)
    if (ret_val == DEF_FAIL) {
        return (DEF_FAIL);
    }
    return (DEF_OK);
}
  1. The SNTPc_CFG keep the information on the NTP server to connect to. It is possible to specified either an IPv4 or IPv6 address or even an hostname if µC/DNSc is properly included and initialized. Please refer to Run-time Configuration section for more details.

  2. There's is many NTP servers available on the internet for either IPv4 and IPv6 that you can find near your location. Otherwise, if µC/DNSc is included, you may also use "pool.ntp.org" which will return an address of an random NTP server near your location.

  3. Send a SNTP request to the specified NTP server. It will return sntp packet that will be used to retrieve the corrected time. 
     

  4. Get the local time from the received SNTP message packet in the form of an NTP time stamp. Note thaht SNTP_TS is two 32-bit integer: the first represent the number of seconds elapsed since 1 January 1900, the the second represent the fraction of a second on 32-bit.  
     

  5. Set the local time using uC/CLK. Since the uC/CLK time granularity is a second, only the first 32-bit of the timestamps is used.