Page 1 of 1

RTC ith milliseconds resolution

Posted: Thu May 22, 2014 8:15 am
by ruslanpopov
Hi.

I need to get an information about current time with milliseconds resolution. I use function rtcGetTimeTm() and see in docs that tm structure has only field for seconds, not millisecond. It makes me think that I can't get milliseconds. But suddenly I have know that my stm32f103 has an ability to get what I need. Searching in ChibiOS code gives me a definition of STM32_RTC_HAS_SUBSECONDS. But I see no abilities to get milliseconds without creating custom version of rtcGetTimeTm().

It may be implemented like this:

Code: Select all

#if STM32_RTC_HAS_SUBSECONDS
uint32_t rtcGetTimeTmMilli(RTCDriver *rtcp, struct tm *timp) {
    RTCTime timespec = {0, 0,FALSE,0};

    rtcGetTime(rtcp, &timespec);
    stm32_rtc_bcd2tm(timp, &timespec);
    return timespec.tv_msec;
}
#endif


What do you think about?

Re: RTC ith milliseconds resolution

Posted: Thu May 22, 2014 8:43 am
by Giovanni
Hi,

Good point, the RTC driver will be reworked in 3.0, such a feature could be implemented in the new driver.

Giovanni

Re: RTC ith milliseconds resolution

Posted: Thu May 22, 2014 7:26 pm
by ruslanpopov
There is a problem. The macro STM32_RTC_HAS_SUBSECONDS is defined for RTCv2, but as I remember stm32f103 has to use RTCv1.

If I am right, any ideas to get milliseconds?

Update
Yes, I am right: link to post.

Re: RTC ith milliseconds resolution

Posted: Thu May 22, 2014 8:14 pm
by ruslanpopov
The final solution:

Code: Select all

uint32_t rtcGetTimeTmMilli(RTCDriver *rtcp, struct tm *timp) {
  RTCTime timespec = {0,0};

  rtcGetTime(rtcp, &timespec);
  localtime_r((time_t *)&(timespec.tv_sec), timp);
  return timespec.tv_msec;
}