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

app_timer_cnt_get() always returns 0x0018000 (SDK12.1.0)

I'm trying to use app_timer_cnt_get() to implement an RTC Calendar system on a Gigawit module (nRF51822).

I have a test app_timer that has presacler=0 and period=3 seconds. The callback works as I toggle a LED, and it toggles every 3 seconds.

The code uint32_t this_count = app_timer_cnt_get(); shows a value of 0x00018000 when I set a breakpoint after the call to app_timer_cnt_get().

Why does it not increment? RTC1 and the timer is obviously working and counting, and app_timer_cnt_get() eventually return NRF_RTC1->COUNTER;

Is the counter automatically reset to zero? I can't imagine so if multiple timers are used with APP_TIMER.

I've tried enabling RTC1 in sdk_config.h, but app_timer was working without that anyway.

I've also tried enabling APP_TIMER_KEEPS_RTC_ACTIVE in sdk_config.h. I think I need this anyway for my Calendar implementation (for log timestamps), however it didn't make any difference.

Any help or suggestions greatly appreciated !!

Brendan.

  • Update: app_timer_cnt_get() is working if I call it in my main() function, but not if called in my timer_app callback routine (which is in a different file).

    Is this because it is executing in an interrupt context ?

    Would using an app_timer scheduler resolve things?

  • Maybe it is the way you are checking the value.

    You should be careful when using breakpoints as this will only stop the cpu (the code) from running and not the peripherals like the RTC. This is the reason it is not recommended to use breakpoints and then resume the code execution when doing BLE stuff (advertising/scanning/connection).

    If you get a value of 0x18000 the first time you enter the timer_app callback routine, this is correct (0x18000 is 0x8000 (32768) times 3 -> 3 seconds). When you resume the code execution do you hit the breakpoint again with the same value? If so it might be because the chip is resetting because of an error condition, see here. You can check this by adding a breakpoint at the very start of main().

    You could try to log the value using UART or RTT instead, this can be done with the log module. Enable logging with the NRF_LOG_ENABLED macro in sdk_config.h. Use NRF_LOG_... functions to print data.

  • Yes. The processor is continually resetting, even using the scheduler. If I put the global variable last_count in the live watch window of IAR, then I can see it incrementing by approx 0x18000 each time. Thanks, Brendan :)

  • app_timer_cnt_get() is working ok. Putting a breakpoint in the handler causes the processor to reset on resumption and RTC1 was being reset/reinitialised.

    Reading the timer count and storing in a global variable, and watching the variable in a live watch window, I could see the variable incrementing by 0x18000 every 3 seconds.

    Thanks Ole for the heads up.

    Brendan.

Related