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

[bugreport] app_timer_stop() resets RTC1 counter when APP_TIMER_KEEPS_RTC_ACTIVE is set

My setup is as follows: nRF5 SDK 13.1 with s132 5.1.0 nRF52832_AA Keil 5.25pre

I use RTC1 counter for timestamping along with app_timer timers. My get_timestamp() function retrieves it's time value by calling app_timer_cnt_get() and then uses app_timer_cnt_diff_compute() to check how much time passed since it was called. Result is used to increment monotonous timer tick counter which is then processed to obtain timestamp:

  uint64_t get_timestamp()
  {
    static uint32_t last_ticks=0;
	static uint64_t timestamp_ticks=0;
	uint32_t curr_ticks=app_timer_cnt_get();

	timestamp_ticks+=app_timer_cnt_diff_compute(curr_ticks, last_ticks);
	last_ticks=curr_ticks;

	return timestamp_ticks;
  }

It is working well in few applications but I've noticed that when I use app_timer_stop() timestamp jumps by large value. It turns out that calling this function on last running timer resets RTC1 counter by calling timer_list_remove() which contains following code snippet:

// Timer is the first in the list
    if (p_previous == p_current)
    {
        mp_timer_id_head = mp_timer_id_head->next;

        // No more timers in the list. Reset RTC1 in case Start timer operations are present in the queue.
        if (mp_timer_id_head == NULL)
        {
            NRF_RTC1->TASKS_CLEAR = 1;
            m_ticks_latest        = 0;
            m_rtc1_reset          = true;
            nrf_delay_us(MAX_RTC_TASKS_DELAY);
        }
    }

IMHO this is a bug because setting APP_TIMER_KEEPS_RTC_ACTIVE=1 in sdk_config.h according to infocenter documentation should result in always running RTC1 which can be used 'for timestamping'. Introducing large and unpredictable jumps in counter values contradicts this.

As a workaround I've started always running recurring timer which does nothing. This works but it's not ideal since nRF will wake from sleep to process timeout events for this timer.

In attempt to have more reliable fix I've patched snippet above so it looks like this:

    // Timer is the first in the list
    if (p_previous == p_current)
    {
        mp_timer_id_head = mp_timer_id_head->next;

        // No more timers in the list. Reset RTC1 in case Start timer operations are present in the queue.
        if (mp_timer_id_head == NULL)
        {
//do not reset RTC counter when it's used for timestamp
#if (APP_TIMER_KEEPS_RTC_ACTIVE == 0)
            NRF_RTC1->TASKS_CLEAR = 1;
            m_ticks_latest        = 0;
            m_rtc1_reset          = true;
            nrf_delay_us(MAX_RTC_TASKS_DELAY);
#else
			m_rtc1_reset          = false;
#endif

        }
    }

I'd appreciate If someone from Nordic could take a look and check if this won't break something or propose different solution.

  • @hungbui the code you've provided solves the problem. Thanks for providing support. as for nrfx this looks very promising for integration with RTOS I'm doing for another project. Do you have any rough estimate when SDK15 will be out so I can try SDK libraries which are using it?

  • Glad to hear it fixed the problem. Unfortunately we can't talk about timeline here but it shouldn't take too long to be released. I would say 1 2 months.

  • Realise this is old hat, but was there a workaround within SDK_14.2.0_17b948a ? (we are stuck with this older version for our product)
    The supplied file app_timer2 is significantly different from app_timer..c and I am not sure from the above discussion whether to replace the whole file or just a few lines

  • Hi Nick, 

    You can either take the official app timer in SDK v15.x and back port it to SDK v14.2 or you can use apptime2 that I provided earlier. You would need to replace the whole file, use it as a different module to the original app timer. 

  • HungBui - Thanks for the rapid response.
    sorry about this, but I do not completely understand what you mean

    The function timer_list_remove() in apptimer.c file in SDK15 looks identical to the one in 14.2 -  so I don't know how porting SDK15 timer library helps.

    The apptimer2.c file in my copy of nRF5_SDK_15.3.0_59ac345 is in a subdirectory called "experimental."

    Please clarify - am I supposed to replace apptimer.c in my project source list with apptimer2.c from experimental subdirectory in 15.3?

    Are there any other files need replacing at the same time?

    Are there other things required such as #define APP_TIMER_V2 and if so, where to define that?

    What side effects of this "experimental"  version are known since release? 

    Thanks

    Edit: addition - trying to put the experimental version from 15.3  back into my 14.2 project falls over heavily on the lack of nrfx.  Please advise before I get too deep into this.

Related