This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

measuring elapsed time with app_timer_cnt_diff_compute()

I'd like to write some timestamps to a log variable in order to see how much time has elapsed between certain points in my code. app_timer.h has two functions that look suitable for this:

uint32_t app_timer_cnt_get(uint32_t * p_ticks);

uint32_t app_timer_cnt_diff_compute(uint32_t   ticks_to,
                                    uint32_t   ticks_from,
                                    uint32_t * p_ticks_diff);

First I take a note of the RTC1 counter value using app_timer_cnt_get(), then each time I write to my log, I call app_timer_cnt_get() again to get the current counter value, then app_timer_cnt_diff_compute() to get the difference in the number of ticks between the two.

But how long is a tick? I had thought the RTC1 counter was running at 32 KHz (LF clock), but dividing my elapsed ticks by 32,000 gives me a nonsense duration in seconds. Confused.

I'm not using the Soft device in this case, and I'm starting the LF clock like so:

void start_clock(void)
{
    NRF_CLOCK->LFCLKSRC             = (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos);
    NRF_CLOCK->EVENTS_LFCLKSTARTED  = 0;
    NRF_CLOCK->TASKS_LFCLKSTART     = 1;
    while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0)
    {
        // Do nothing.
    }
    NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
}
Parents Reply Children
Related