System time in mbed OS

I’d like to implement an SNTP client in mbed OS. The only way I found to modify the system time is through the HAL RTC API. This only allows me to read and write the RTC time in seconds though. What does mbed OS offer to keep time with a higher (==microsecond) precision?

There are two tickers in HAL - us ticker (high precision timer) and lp ticker (low precision timer). Former - microseconds, the latter - milliseconds. There’s mbed-time module (look it up in yotta registry) which provides posix time API on top of lp ticker. Would that satisfy your needs?

1 Like

Here’s how I do it:

#include "mbed-hal/us_ticker_api.h"

static unsigned long time_ms = us_ticker_read() / 1000L;

What’s your practice to check for ticker overflows?

I just learned about the mbed-time library, just adding that to your application should be easier (getTimeval function).

It also handles overflows.

Hi,

I’m also trying to set the time on my device, which is a frdm-k64f. I’ve tried installing the mbed-time module to my project but the program then will not build because there is a clash of names. In mbed-time/source/time.cpp there is this:

extern “C”
{
static void getTimeval(struct timeval* tvp);

time_t time(time_t* timer)
{

and in mbed-drivers/source/rtc_time.c there is this:

#ifdef _cplusplus
extern “C” {
#endif
#if defined (_ICCARM
)
time_t __time32(time_t *timer)
#else
time_t time(time_t *timer)
#endif

{…

I am using the arm-none-eabi-gcc compiler, so the above comes out to the same declaration as in time.cpp above and hence the linker fails.

Is this expected, i.e. I should just continue to use rtc_time.c?

Thanks,
John