RTC ith milliseconds resolution

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
ruslanpopov
Posts: 35
Joined: Sun Aug 19, 2012 9:36 pm

RTC ith milliseconds resolution

Post 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?
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: RTC ith milliseconds resolution

Post 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
ruslanpopov
Posts: 35
Joined: Sun Aug 19, 2012 9:36 pm

Re: RTC ith milliseconds resolution

Post 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.
ruslanpopov
Posts: 35
Joined: Sun Aug 19, 2012 9:36 pm

Re: RTC ith milliseconds resolution

Post 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;
}
Post Reply