0
votes

I am trying to read the time using LWIP and the SNTP application on a Nucleo-F429ZI dev board using STM32Cube, the documentation for LWIP lists methods for initialisation etc but gives nothing on how you actually read the time. I am guessing something runs on a thread in the background, but without reading and understanding the LWIP stack, this is beyond me.

Any pointers on how to simply read the time? Then I can simply store this into the RTC once a day.

1

1 Answers

0
votes

The LwIP SNTP app works by periodically fetching time from the server and saving it to the system time provided by the user, in your case the RTC.

1. To do this you'll first need to provide your own function to the SNTP app to set the RTC time, this can be done like the following in sntp.c:

.
.
#include "your_rtc_driver.h"
.
.
/* Provide your function declaration */
static void sntp_set_system_time_us(u32_t t, u32_t us);
.
.
/* This is the macro that will be used by the SNTP app to set the time every time it contacts the server */
#define SNTP_SET_SYSTEM_TIME_NTP(sec, us)  sntp_set_system_time_us(sec, us)
.
.
/* Provide your function definition */
static void sntp_set_system_time_us(sec, us)
{
  your_rtc_driver_set_time(sec, us);
}

2. Now to use SNTP in your app, make sure to Enable the following SNTP defines in your lwipopts.h file like the following:

#define SNTP_SUPPORT      1
#define SNTP_SERVER_DNS   1
#define SNTP_UPDATE_DELAY 86400

3. Then in your user code:

#include "lwip/apps/sntp.h"
.
.
.
/* Configure and start the SNTP client */
sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_setservername(0, "pool.ntp.org");
sntp_init();
.
.
.
/* Now if you read the RTC you'll find the date and time set by the SNTP client */
read_date_time_from_rtc();

That's it, now every SNTP_UPDATE_DELAY ms, the SNTP app will read time from the server and save it to RTC, and all you need to do in your code is to start the SNTP app and read from the RTC.