How to get the runtime in microseconds in MBED OS 6?

The title says it all. I would like to simply keep track of the current runtime since the last reset (for data logging).
Is there a built-in function for this? (E.g. like micropyhton’s micros())

I know of time(), but that returns a timestamp, which has not nearly the accuracy I am looking for.

The alternative would be a Timer object. However, I was hoping for something more simple.

1 Like

Hello Robert,

Although it returns milliseconds, you can use

/** A C++11 chrono TrivialClock for the kernel millisecond tick count
 *
 * @note To fit better into the chrono framework, Kernel::Clock uses
 *       std::chrono::milliseconds as its representation, which makes it signed
 *       and at least 45 bits (so it will be int64_t or equivalent).
 */

Kernel::Clock::now();

Best regards, Zoltan

2 Likes

That works, thank you.

A complete example:

using namespace std::chrono;
auto now_ms = time_point_cast<microseconds>(Kernel::Clock::now()); // Convert time_point to one in microsecond accuracy
long micros = now_ms.time_since_epoch().count();
2 Likes

You can also use us_ticker_read();

2 Likes

Thank you for the suggestion. It sounds nicer, but using it freezes my microcontroller (K64F, latest versions of DAPLink bootloader and MBED OS). It looks there is a problem with that function (at least on my platform).

Debugger says:

5 “Handler mode” received signal SIGSEGV, Segmentation fault.
[Switching to Thread 2]
0x00000598 in HardFault_Handler ()

Brief rant: how are we supposed to find a function like that? I’m missing a functional search for the API documentation.