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

Are the app_timers using 32 768 Hz RTC?

Two questions:

  1. If I use Nordic's library functions such as app_timer_create() and app_timer_start(), are the timers using the low-power RTC timer running with the 32 768 Hz crystal? Our board has that crystal installed, of course. Or do they just utilize the timers running with CPU clock (16 MHz)?

  2. What is the simplest way to read milliseconds elapsed since the system started running? 16 bits would be enough, so it doesn't matter if it wraps around back to zero.

  • Hi Jarmo,

    Yes, the app_timer library uses RTC_1 for its operations. It has roughly 1 ms resolution.

    If you don't need app_timer, then one easy way to count ticks would be to manually setup RTC_1 to fire an interrupt using a capture compare register. You can set this up for roughly 1 millisecond intervals. Then in the interrupt, you can simply increment a tick counter and then exit the interrupt. You could then use this value for timing.

    If you don't actually need the tick count and instead need to execute tasks at certain intervals, you can use the app_timer library. Note that app_timer events occur in an interrupt context (SWI_0 I think). If you are accessing other resources you may want to use a flag to trigger the main loop to run the event handling instead.

    Hope this helps,

    Eric

  • Thanks, that helped a bit. So I assume app_timer library saves power by shutting down 16 MHz -side ("deep sleep") and running with the 32 kHz timer instead until the timer is fired, is that right?

    Can I somehow read a counter that runs with the 32.768 crystal? That would be a nice way to get accurate timestamp.

  • The app_timer does not use the 16 MHz clock when using an external crystal (and even without one, it will still use the internally calibrated low speed oscillator). You should be able the read the count directly from the RTC->COUNTER register. However, usually the preferred method is to use an interrupt to keep track of ticks rather than directly reading the counter register. At least, to me, that is the more power conscious option. With the app_timer library, you can also call app_timer_cnt_get which gets the current value of RTC1 specifically.

  • Yeah, that sounds good! We are tracking several sensors that cause interrupts by their own, and I just want to get accurate time stamp when such a sensor interrupt happens. So reading a hardware counter is definitely the best option also power-wise, I think. Do you agree? :)

  • Yes, I think that likely makes sense so long as 24-bit resolution is good enough for you. Based on your initial question, it seems like 24 bits is good enough.

Related