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

Calculate time until next RTC Interrupt

Greetings -

I am using (the slightly out-dated) 12.3 SDK with a nRF52840. I'm also using s132 as my softdevice.

As I've been slowly reducing my RTC interrupt frequency to cut down on power consumption, I have a growing need to be able to determine the length of time until the next RTC interrupt.

I can calculate the period from the PRESCALER register, but there is no register which counts up or down which could be used to determine how far along in the process things happen to be.

The other two RTC peripherals are being used by the soft device or I'm sure I could come up with some other way. It seems this should be a pretty basic thing. Why is it impossible!

  • Hi Julie,

    From what I understand about S132 (applies also to S140) Soft Device and nRF52:

    • BLE stack "blocks" only first RTC, all the rest are available for your app.
    • The only interrupts generated by RTC block are TICK, OVERFLOW and COMPARE. You can enable each of them independently so it typically is used either in TICK or COUNTER mode.
    • Whatever mode is used COUNTER is always incremented and you can read it any time. So e.g. your app can easily count what was the difference between last and current interrupt and by knowing all the parameters like frequency and PRESCALER you can come to the actual time measurements. I actually believe that if you need just random time keeping you might be able to read RTC0 COUNTER - because reading shouldn't make any harm to the SD (but worth checking with other Q&S on this forum and testing) - and just subtract delta from previous "time stamp".

    So what exact mode are you using now and what interrupts are we talking about? Secondly am I wrong that COUNTER is not available and used by every RTC or it could use for any kind of computation? Or is there other concern I missed?

  • Unfortunately, according to this answer -- devzone.nordicsemi.com/.../ -- RTC2 is the only RTC available if I'm using the BLE stack and app_timer(). I could probably work-around using app_timer(), but as that answer also says, Good Luck With That!

    I do like your suggestion that I look at the COUNTER register for RTC0. I'll take a look and see how it's run and see what that turns up.

  • That's different situation;) Yes, app_timer lib needs one RTC instance. But when you have app_timer why you need anything else? Are you running some RTOS with RTC systick or similar thing? If so then why you use app_timer? Anyway even if you would occupy all three RTCs with these functions there should be counter visibly ticking on each of them (which was root of your question). Or did I miss anything?

  • Based on the other answer, app_timer() seems to be pretty thoroughly infiltrated into the SDK. I also hate how it handles events, so I wrote my own RTOS. Because, everyone needs to write their own RTOS from time to time ;)

  • My solution was to keep using RTC2 the way I'm doing it today, but to fetch RTC1's COUNTER register and compare it against the COUNTER register value I saved the last time I did that, and do all that in an interrupt-safe and lockless (no critical sections) fashion. I didn't use RTC0 because I don't know if it is turned off when I turn off the radio. And I'm impatient.

Related