cancel
Showing results for 
Search instead for 
Did you mean: 

Set RTC time from ntp timestamp

MKS
Associate II

Hi,

I am working on stm32 conntroller stm32f103rb, i am sending 8 bytes of ntp time stamp to the board over uart, i want to set RTC time according to that data, also i want to send in ntp format whenever requested from the board.

can anyone help me with this .

5 REPLIES 5

What is the format of the timestamp you are receiving? Show an example.

The F1 RTC is only a seconds counter anyway, so you extract epoch seconds from the timestamp and store them to the RTC, maybe adjusting them for a different epoch of you want.

JW

MKS
Associate II

Hi ,

Thanks for your reply, it is actual ntp time format 8 bytes the refrence link are :https://www.epochconverter.com/timezones?q=1586926232000&tz=UTC &

https://www.eecis.udel.edu/~mills/time.html.

how to extract epoch seconds.

regards,

Manjunath

Quiting from the website you linked above:

 A timestamp is a 64-bit, unsigned fixed-point number in seconds and fraction with the decimal point to the left of bit 32. 

So simply take upper 32 bits and you are done. Time will be UTC and in the NTP epoch, i.e. relative to 00:00:00 1.1.1970.

If you'd like to use the standard C library facilities, simply subtract the number of second between the NTP and UNIX epochs.

Make sure you don't run into any overflow using inappropriately short types (namely int32_t).

JW

MKS
Associate II

hi @Community member​ ,

Thanks for your reply , can you explain with an example, for converting timestamp to rtc time format and rtc time format to timestamp.

 Mean to say how epoch is calculated.

Regards,

manjunath

>>.. can you explain with an example, for converting timestamp to rtc time format and rtc time format to timestamp.

Honestly how do software engineers survive in the wild these days?

I built a large set of time/calendar support code around the Windows Epoch, the UNIX Time Epoch is similar just starting earlier, and signed.

This way I could read the 32-bit RTC value and spit out time/day, and also be able to ping an NTP/SNTP server to align the clock.

    struct ntp_message msg;
 
...
 
    if (Size)
    {
      SYSTEMTIME SystemTime[1];
      DWORD NtpTime, NtpTimeMillisecond;
      DWORD OldTime;
 
      printf("NTP Size %d\r\n",Size);
 
      DumpData(Size, Buffer);
      printf("\r\n");
 
      memcpy(&msg, Buffer, sizeof(msg));
 
      printf("Stratum : %d\r\n",msg.stratum);
 
      NtpTime             = msg.transmit_timestamp.sec;
      NtpTimeMillisecond  = msg.transmit_timestamp.sec_fraction;
 
      NtpTime             = EndianSwap(NtpTime); // Endian Magic
      NtpTimeMillisecond  = EndianSwap(NtpTimeMillisecond);
 
      printf("%08X.%08X ",NtpTime, NtpTimeMillisecond);
 
      NtpTime -= 2524435200L; // 1-Jan-1980 WinCE Epoch from NTP Epoch
 
      NtpTimeMillisecond = ((NtpTimeMillisecond >> 10) * 1000) >> 22;
 
      printf("%10u.%03u\r\n",NtpTime, NtpTimeMillisecond);
 
      OldTime = RTC_GetCounter(); // RTC 32-bit second counter
      RTC_SetCounter(NtpTime);
      printf("RTC Delta %d\n",NtpTime - OldTime);
 
      OldTime = (DWORD)SystemClock; // SysTick driven second/millisecond clock
      SystemClockMillisecond  = 0; // Avoid a potential race condition
      SystemClock             = (int)NtpTime;
      SystemClockMillisecond  = (int)NtpTimeMillisecond;
      printf("SYS Delta %d\n",NtpTime - OldTime);
 
      if (DecodeTime(NtpTime, SystemTime))
        puts(PrintSystemTime(SystemTime));
    }

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