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

Repeated app_timer vs RTC with maximum prescaler

Hi for my use-case I need the RTC in the NRF51 SoC to run continuously and uninterrupted (we know running out of battery will be a concern). Keeping the NRF51 system on 24/7 will drain battery power quickly, but the NRF51 will also be connected to sensors and need to periodically check if the sensors detect any changes. So we plan to program the NRF51 to sleep for one second and then wake up the next second, and repeat this to save power. However, when Softdevice BLE is advertising I think the NRF51 has to be awake.

So we have to keep track of time for programming the NRF51 system to go to sleep as well as for recording timestamps. We thought of two ways to do this:

1. Use a repeated app_timer with normal prescale and a timeout interval of 1000 or 2000 ms, etc and increment a counter variable in the app_timer's timeout_handler. We do not see much downside to this approach, though there might be a very tiny delay between the tick and the counter variable being incremented. This approach doesn't seem to have overflow issue either. 

2. Use an app_timer that ticks with the the maximum prescale setting of 4095, so the RTC gets a period of 125 ms and overflows after 582.542 hours. The downside to this approach is we have to keep track of the RTC overflow, but compared to the first method above the app_timer will only need to repeat/reset after about 24 days.

Some of our concerns right now if there is any power advantage one has over the other? Will both methods also work without the clock timers being interrupted by Softdevice/BLE events or if the NRF system goes to sleep every other second?

And are there any other considerations we should also consider before deciding over using a repeated app_timer with a (short time interval and normal prescaler) vs RTC with maximum prescaler? 

Thank you!

Parents
  • Hi

    I made a small calendar example for the nRF52 series a while back. There are no project files for the nRF51 unfortunately, but I don't think it should be a lot of work to make it run on the nRF51 instead. 

    It allows you to use the app_timer just as you describe, and fire of an interrupt every second. Then it will update an internal variable, and use the time.h library to decode this into a time and date. 

    The example can be found here:
    https://github.com/NordicPlayground/nrf5-calendar-example

    Best regards
    Torbjørn

  • Hi Torbjørn,

    Thanks, I will check it out! But are there any significant advantages or disadvantages with using a repeated app_timer with a duration of one/two seconds over using an app_timer with maximum prescaler (4095) & maximum duration of 582.542 hours? 

  • Hi

    To me it seem the main difference between these two approaches is how often it overflows. At one point it will overflow regardless. 

    The main impact on average current consumption will be your wakeup frequency, and the choice of prescaler should not make a big difference. 

    I would suggest setting a wakeup frequency suitable for the sensor sampling and other things you need to do, and update a variable according to the wakeup frequency. 

    Best regards
    Torbjørn

  • Hi Torbjørn,

    If I use a repeated app_timer of one second, shouldn't the RTC itself never overflow?

    Using repeated app_timer I would be incrementing a counter variable every second, and an integer variable can overflow. But if the maximum unsigned integer value in C is 4294967295 then a counter variable of 4294967295 seconds would still be 136 years. If I am trying to transmit the value over BLE, then the maximum integer value is 2^(20 bytes*8 bits) - 1, which is still a lot of years before overflow. 

    Is my logic correct, or am I missing any important details? Thanks! 

  • Hi

    myNrfDoesntWork said:
    If I use a repeated app_timer of one second, shouldn't the RTC itself never overflow?

    It will overflow at one point, but the overflow is handled automatically by the app_timer library so that you don't have to think about it. The app_timer callbacks will continue to run as normal even after the overflow occurs. 

    I agree with your math. If you use a uint32 for counting seconds, and start running it now, it will not overflow until 136 years have past. 

    If you are thinking about reserving a full 20 bytes for the second counter then the sun should burn out long before your counter overflows ;)

    Best regards
    Torbjørn

Reply
  • Hi

    myNrfDoesntWork said:
    If I use a repeated app_timer of one second, shouldn't the RTC itself never overflow?

    It will overflow at one point, but the overflow is handled automatically by the app_timer library so that you don't have to think about it. The app_timer callbacks will continue to run as normal even after the overflow occurs. 

    I agree with your math. If you use a uint32 for counting seconds, and start running it now, it will not overflow until 136 years have past. 

    If you are thinking about reserving a full 20 bytes for the second counter then the sun should burn out long before your counter overflows ;)

    Best regards
    Torbjørn

Children
No Data
Related