RA Flexible Software Package Documentation  Release v6.4.0

 
lwIP SNTP

This module provides the lwIP SNTP integration documentation.

Overview

The lwIP SNTP library can be used to synchronize system time in a network by exchanging packets regularly. The documentation for the library can be found at the following link: lwIPSNTP.

Features

Usage Notes

Note
MEMP_NUM_SYS_TIMEOUT at Infrastructure|Internal memory pools|Memp num sys timeout property of lwIP tcpip stack must be increased by one from the default value, as the SNTP module requires an additional timeout slot in the lwIP timer subsystem.

Examples

Basic Example

This is a basic example of minimal use of the lwIP SNTP APIs in an application.

Note
When using printf with e² studio, build errors may occur. Please refer to https://en-support.renesas.com/knowledgeBase/16979385 and follow Method 2, which is recommended for smooth integration and to avoid build errors.
#include <time.h>
#include "lwip/timeouts.h"
#include "lwip/ip_addr.h"
#include "lwip/init.h"
#include "lwip/apps/sntp.h"
#define LWIP_SNTP_EXAMPLE_SECONDS_PER_HOUR (60 * 60)
#define LWIP_SNTP_EXAMPLE_TIMEZONE_UTC_PLUS (7) // This may need to be updated for your timezone
#define LWIP_SNTP_EXAMPLE_RETRY_CONNECT (5U)
#define LWIP_SNTP_EXAMPLE_SERVER_NAME "time1.google.com"
#define LWIP_SNTP_EXAMPLE_SERVER_ADDRESS "216.239.35.0"
/* TODO: Set this function name in the "SNTP Set System Time" property of lwIP SNTP stack.
* This function will be called to change the system time in seconds using the timestamp received from the SNTP server.
*
* Example: lwip_sntp_example_set_time(sec)
*/
void lwip_sntp_example_set_time(u32_t sec);
uint8_t g_lwip_sntp_example_count;
bool g_lwip_sntp_example_flag;
/* LwIP SNTP example */
void lwip_sntp_example () {
struct netif netif;
ip_addr_t ipaddr;
ip_addr_t netmask;
ip_addr_t gw;
/* Initializing lwIP core. */
lwip_init();
/* Set ip address of the board. */
IP4_ADDR(&ipaddr, 192, 168, 10, 4);
IP4_ADDR(&netmask, 255, 255, 255, 0);
IP4_ADDR(&gw, 192, 168, 10, 1);
/* Initialize netif. Please pass rm_lwip_ether_init() function and the instance of rm_lwip_ether to netif_add(). */
netif_add(&netif, &ipaddr, &netmask, &gw, &g_lwip_ether0_instance, rm_lwip_ether_init, netif_input);
/* Set netif as default and enable. */
netif_set_default(&netif);
netif_set_up(&netif);
netif_set_link_up(&netif);
/* Set SNTP operation mode to Polling. */
sntp_setoperatingmode(SNTP_OPMODE_POLL);
#if SNTP_SERVER_DNS && LWIP_DNS
/* Set server as the supplied DNS. */
sntp_setservername(0, LWIP_SNTP_EXAMPLE_SERVER_NAME);
#else
ip_addr_t sntp_ip;
ipaddr_aton(LWIP_SNTP_EXAMPLE_SERVER_ADDRESS, &sntp_ip);
/* Set server as the supplied ip address. */
sntp_setserver(0, &sntp_ip);
#endif
/* Initialize the SNTP. */
sntp_init();
/* Program main loop. */
while (true)
{
sys_check_timeouts();
if (true == g_lwip_sntp_example_flag)
{
g_lwip_sntp_example_count++;
/* Stop wait when the total count is reached. */
if (LWIP_SNTP_EXAMPLE_RETRY_CONNECT == g_lwip_sntp_example_count)
{
break;
}
}
g_lwip_sntp_example_flag = false;
}
/* Stop the current SNTP. */
if (sntp_enabled())
{
sntp_stop();
}
}
/* Change the current time responded from SNTP server example */
void lwip_sntp_example_set_time (u32_t sec)
{
/* Set the flag to verify. */
g_lwip_sntp_example_flag = true;
/* Adjust clock to the your timezone. */
sec += (LWIP_SNTP_EXAMPLE_TIMEZONE_UTC_PLUS * LWIP_SNTP_EXAMPLE_SECONDS_PER_HOUR);
/* Convert Unix time to local time structure. */
time_t time = (time_t) sec;
struct tm * timeinfo;
timeinfo = localtime(&time);
char strftime_buf[32]; // NOLINT(readability-magic-numbers)
/* Format time into "YYYY-MM-DD HH:MM:SS". */
strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%d %H:%M:%S", timeinfo);
printf("Current UTC+%d time is: %s\n", LWIP_SNTP_EXAMPLE_TIMEZONE_UTC_PLUS, strftime_buf);
}