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

App Timer problem...

Hi,

I have a problem with the app timer firing incorrectly.

I am using development board nrf52840-DK, Segger Studio 4.22, SDK 15.3.0_59.

When there has gone more than 256 seconds without any running app timers, the app timer fails and triggers right away.

Can you tell me what I have misunderstood?

I have modified the example /ble_peripheral/ble_app_blinky/ and made the following changes:

In sdk_config.h, I changed APP_TIMER_KEEPS_RTC_ACTIVE to be enabled.

In main.c outside of int main() I have added:

#include "nrf_delay.h"

APP_TIMER_DEF(app_tester_timer);

#define LOG_PRINTF(...) NRF_LOG_INFO(__VA_ARGS__); while(NRF_LOG_PROCESS()== true) {;}

volatile bool timer_triggered = false;
void timer_cb(void);
void timer_cb(void)
{
timer_triggered = true;
NRF_LOG_INFO("\nTimer cb\n");}

In main.c inside of int main() just after ble_stack_init() I have added:

#define APP_TIMER_WAIT_MS (270*1000)
#define APP_TIMER_DELAY_MS (10*1000)

ret_code_t err_code;

LOG_PRINTF("\nTimer Create\n");
err_code = app_timer_create(&app_tester_timer,APP_TIMER_MODE_SINGLE_SHOT,timer_cb);
APP_ERROR_CHECK(err_code);

// For starting app rtc timer(else it will not fail!)
err_code = app_timer_start(app_tester_timer, APP_TIMER_TICKS(50), NULL);
APP_ERROR_CHECK(err_code);

// app rtc timer started, just stop alarm.
err_code = app_timer_stop(app_tester_timer);
APP_ERROR_CHECK(err_code);

LOG_PRINTF("\nWaiting for more than 256 seconds..\n");
nrf_delay_ms(APP_TIMER_WAIT_MS);

LOG_PRINTF("\nWaiting finished\n");

timer_triggered = false;
err_code = app_timer_start(app_tester_timer, APP_TIMER_TICKS(APP_TIMER_DELAY_MS), NULL);
APP_ERROR_CHECK(err_code);

LOG_PRINTF("\nTimer Started..\n");

uint32_t time_elapsed_ms = 0;
while(timer_triggered==false)
{
nrf_delay_ms(10);
time_elapsed_ms += 10;
}

if(time_elapsed_ms < (APP_TIMER_DELAY_MS*0.9))
{
LOG_PRINTF("\nApp timer fired to fast: Elapsed time(ms)=%d, expected(ms)=%d\n", time_elapsed_ms, APP_TIMER_DELAY_MS);
}
else if(time_elapsed_ms < (APP_TIMER_DELAY_MS*0.9))
{
LOG_PRINTF("\nApp timer fired to slow: Elapsed time(ms)=%d, expected(ms)=%d\n", time_elapsed_ms, APP_TIMER_DELAY_MS);
}
else
{
LOG_PRINTF("\nApp timer fired OK : Elapsed time(ms)=%d, expected(ms)=%d\n", time_elapsed_ms, APP_TIMER_DELAY_MS);
}

The app timer always fires in less than 10ms and shows the "App Timer fired to fast...", event though it should wait 10 seconds.

Help from anyone having a clue what is going is appreciated.

Best regards Erik.

Parents
  • Hello Erik, 

    I have been able to reproduce your error. There are two possible solutions to this in sdk_config.h:

    Disable RTC Always on

    // <q> APP_TIMER_KEEPS_RTC_ACTIVE  - Enable RTC always on
     
    // <i> If option is enabled RTC is kept running even if there is no active timers.
    // <i> This option can be used when app_timer is used for timestamping.
    
    #ifndef APP_TIMER_KEEPS_RTC_ACTIVE
    #define APP_TIMER_KEEPS_RTC_ACTIVE 0
    #endif
    

    Or lower the RTC prescaler, as a high prescaler causes it to wrap around. 

    // <o> APP_TIMER_CONFIG_RTC_FREQUENCY  - Configure RTC prescaler.
     
    // <0=> 32768 Hz 
    // <1=> 16384 Hz 
    // <3=> 8192 Hz 
    // <7=> 4096 Hz 
    // <15=> 2048 Hz 
    // <31=> 1024 Hz 
    
    #ifndef APP_TIMER_CONFIG_RTC_FREQUENCY
    #define APP_TIMER_CONFIG_RTC_FREQUENCY 0
    #endif


    I've tested with RTC Always ON and prescaler = 16384 Hz, and got:
    App timer fired OK : Elapsed time(ms)=10090, expected(ms)=10000


    Let me know how that works for you.

    Kind regards,
    Øyvind

  • Hi Øyvind,

    Thanks for your answer.

    I have just verified, that setting the app timer clock to 32768/2=16384 solves the problem for delays under 256 seconds.

    But I have also verified that the problem is just moved, so it instead occurs after 512 seconds. I guees the same probably happens with other prescalers.


    Are you sure that APP_TIMER_KEEPS_RTC_ACTIVE=0 actually solves the problem, and not just moves it around?
    Setting APP_TIMER_KEEPS_RTC_ACTIVE=0, ensures that the app timer doesn't overflow while no app timers is running, but eventually the app timer will overflow and the problem could occur.

    Is the app timer designed to handle wrap arounds?

    Best regards, Erik

  • Erik_pxq said:
    Is the app timer designed to handle wrap arounds?

    Yes, it should handle wrap arounds, however, there are some issues that we are aware of, and working with. This is why we have added app_timer_v2 in components\libraries\timer\experimental. However, this is currently in experimental status, and we do not have any good examples at the moment.

    Kind regards,
    Øyvind

  • I have now tried the the experimental app timer and made some testing with it.

    It works correctly at the 256 second mark. Unfortunately, I find it fails occasionally when more than 3 timers is running at the time.

    Do you know off any thirdparty libraries which implement some of the same functionality as the app timer library?

    Best regards,

    Erik

Reply Children
Related