Skip to main content
JChan.0
Associate II
May 7, 2019
Question

How can you get the time in milliseconds in reference to a particular timebase (e.g. epoch)?

  • May 7, 2019
  • 5 replies
  • 7178 views

I want the epoch time in milliseconds (or with a milliseconds field), but the main function that is suggested out there clock_gettime() ends up as an undefined reference. I have also tried functions like gettimeofday() but it always returns zero for its usec field. Does anyone have any ideas how I can get the epoch time with milliseconds resolution? (I'm using the time.h library, but if something else is required, that would be fine also).

This topic has been closed for replies.

5 replies

Tesla DeLorean
Guru
May 7, 2019

Time usually doesn't get implemented in embedded because it has hardware dependency, ie chips, frequency, etc.

And you have to initialize it to something.

I increment a millisecond resolution time count in SysTick, and have a second count I can feed to UNIX/C library code.

In system I have using GPS, I use that to set the clock, and then I can use the values I increment in FatFs so the date/time stamps are valid.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
JChan.0
JChan.0Author
Associate II
May 7, 2019

Thanks Clive,

I don't have that much experience so I'm not sure I understand everything you wrote here. Using this method, would I be able to get something like absolute time (i.e. Unix epoch time), with milliseconds resolution? If so, is there documentation or any sample code you know of that I should start from to figure this out?

Tesla DeLorean
Guru
May 7, 2019

http://www.jbox.dk/sanos/source/lib/time.c.html

Then code like this to manager the time value advancing.

extern volatile unsigned long SystemTick;
extern volatile unsigned long __unix_ms;
extern volatile unsigned long __unix_sec;
 
#define TICK_RATE 1 // in milliseconds
 
void SysTick_Handler(void)
{
 // Free running millisecond counter for timeouts
 
 SystemTick += TICK_RATE; // 1 ms tick count
 
 __unix_ms += TICK_RATE;
 
 if (__unix_ms >= 1000)
 {
 __unix_ms -= 1000;
 __unix_sec++;
 }
 
 HAL_IncTick();
}

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Bob S
Super User
May 7, 2019

I have to admit I've never tried using those time functions in an embedded system. Do you really need absolute time? Or do you just need some time reference from which you can derive relative times?

If you need absolute time, then you need an RTC (real time clock) with battery backup and/or some way to get the time from another device every time you boot. Some STM32 CPUs have this, and some do not. And even for ones that do have it, I would bet beer money that the standard C lib functions don't support it. You would most likely need to use the HAL RTC functions (or HAL LL RTC functions).

If all you need is relative time with millisecond resolution, the SysTick timer works great, via HAL_GetTick() if you are using HAL.

JChan.0
JChan.0Author
Associate II
May 7, 2019

Thanks Bob,

I do actually need absolute time of the system. I'm able to get the absolute time in seconds by calling something like time(NULL). However, I also want milliseconds resolution for that absolute time. I know there are functions like clock_gettime() that can get me the absolute time with that resolution, but I'm using SW4STM32, and doesn't seem to work on it. Looking for alternatives to it.