Microseconds since start

I am using the nRF54L15 and SDK 2.9.1 in VSCode.

I need to know the microseconds since start throughout my code.

I created this function:

inline unsigned long micros(void)
{
    double value = k_uptime_ticks();
    value /= ((double)CONFIG_SYS_CLOCK_TICKS_PER_SEC / (double)1000000);
    return (unsigned long)(value);
}

The problem is that CONFIG_SYS_CLOCK_TICKS_PER_SEC is defined as 31250. That means there are fewer ticks per second than a microsecond. I understand why this is, but how can I get microseconds on nRF54L15?

Parents
  • The system timer uses RTC which is low precision timer by design for lower power.

    If you want higher precision timer than like Fischer suggested, you need HFCLK based TIMER and you can then get a better accuracy on tick.

  • I'm a bit surprised since the nRF54L platform only has GRTC, which is backed by a 32768 Hz clock, but that one cannot be queried directly as opposed to the RTC peripheral that is present in other nRF series. Instead, there is a 1 MHz clock waked up on demand, so the resolution is 1 tick per microsecond. If the high frequency clock that the GRTC uses when querying the current time is off, the solution implemented in nRF Connect is to trigger GRTC to start the high frequency clock, busy-loop the CPU up to 31 µs so that the high frequency clock can synchronise with the low frequency clock, and then return the current µs at that point.

Reply
  • I'm a bit surprised since the nRF54L platform only has GRTC, which is backed by a 32768 Hz clock, but that one cannot be queried directly as opposed to the RTC peripheral that is present in other nRF series. Instead, there is a 1 MHz clock waked up on demand, so the resolution is 1 tick per microsecond. If the high frequency clock that the GRTC uses when querying the current time is off, the solution implemented in nRF Connect is to trigger GRTC to start the high frequency clock, busy-loop the CPU up to 31 µs so that the high frequency clock can synchronise with the low frequency clock, and then return the current µs at that point.

Children
Related