cancel
Showing results for 
Search instead for 
Did you mean: 

LWIP SNTP, what does it actually do?

richardh
Associate II

I am looking in to implimenting a SNTP client on an embedded board, I am using the STM32CubeIDE so its a checkbox to enable SNTP, but I can find no documentation or examples on what you do from there.

I can see functions to enable/disable SNTP, but not to read anything or write anything....

Does anyone have some pointers about what/how it works? or even an example?

I have hunted the web, lots say how easy it is, just look at RFC4330 (which I have) etc, but I can only presume I am missing something completely.

14 REPLIES 14
kurta999
Senior

Yes, SNTP is easy, you send an empty packet, and get a time stamped packet back. If latency is low it is great for just setting a 1 Second RTC

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
richardh
Associate II

but how do I actually read the time the LWIP returns? I understand I have to set the operating mode and the server IP address then call sntp_init() but how is the time actually returned from LWIP? is it stored in an integer? is it written directly to the RTC? I think thats what I am missing?

Its okay saying its easy, everything is once you know the answer ;)

DMois.1
Associate III

There are two changes you need to make in sntp.c:

1) You need to redefine the macro SNTP_SET_SYSTEM_TIME to a function in your code that sets the time, or resets the tick counter. My code maintains its own tick counter, and this function resets it to what comes back from the SNTP request.

2) Your code needs to periodically call sys_check_timeouts(). This is the routine that invokes the SNTP client. My SNTP client runs under FreeRTOS, so I set a timer that invokes this routine every few minutes.

Calling sys_check_timeouts() is necessary only for NO_SYS==1. For RTOS with NO_SYS==0 the timeouts are processed by lwIP code internally.

https://www.nongnu.org/lwip/2_1_x/group__lwip__nosys.html#ga83cffdf69ab60fd0eba9d17d363f9883

DMois.1
Associate III

Yeah, confirmed. In my device, I have FreeRTOS running a timer task once a second that increments the seconds, and displays time, and another that updates sys_check_timeouts(). I commented out the latter timer, and SNTP still fires. Thanks.

QRobe.1
Associate II

Hello, I was wondering if you have succeeded in implementing a SNTP Client. I am trying to do the same and there is not so much information on internet.

I am using the STM32H745I board and I am able to ping through Ethernet, without OS. From what I have understood so far, I am supposed to redefine a macro called SNTP_SET_SERVER_TIME. Am I supposed to this in the file lwipopts.h or sntp.c ? . Then, the prototype is defined in the main.c for setting the time.

In the main.c, three functions are called :

- sntp_setoperatingmode(SNTP_OPMODE_POLL);

- sntp_setservername(0, "pool.ntp.org");

- sntp_init();

And in the main loop, there are two functions called :

- ethernetif_input(&gnetif);

- sys_check_timeouts();

It seems that the prototype of the function is never called, do you have any ideas or advice to give ?

never got it working as could never get my head around it, everyone was super helpful but didn't actually explain it well enough to get it doing anything sensible sadly.

Okay, thanks for replying ! hope someone else will read this topic again and can give some help. I actually have found two "examples" on chinese forums, if you're interested in them, I will link it to you.