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

Timer handler called immediately on app_timer_start()

On an nRF51822 without the Soft Device, I use the app timers in a driver for a GPS module that uses the UART. Each time I start reading from the UART, I also start a timer with a timeout of 2 seconds. When the timer handler is called, I set a bool to indicate that the read has timed out and the driver continues.

This works just fine for 3 to 5 minutes, but then I notice that the timer is having its handler called immediately after app_timer_start() in every case. I have no idea why.

static app_timer_id_t                  m_gps_response_timeout_timer_id;

In my gps_init(), which is called once only, at the start:

err_code = app_timer_create(&m_gps_response_timeout_timer_id, APP_TIMER_MODE_SINGLE_SHOT, gps_response_timeout_handler);
	APP_ERROR_CHECK(err_code);

Every time I read a sentence from the GPS:

err_code = app_timer_start(m_gps_response_timeout_timer_id, GPS_TIMEOUT, NULL);
APP_ERROR_CHECK(err_code);

...UART stuff, including checking for timeout, in a loop...

err_code = app_timer_stop(m_gps_response_timeout_timer_id);
APP_ERROR_CHECK(err_code);

Header macros:

#define GPS_TIMER_PRESCALER 0
#define GPS_TIMEOUT APP_TIMER_TICKS(2000, GPS_TIMER_PRESCALER)

Timeout handler:

void gps_response_timeout_handler(void * p_context)
{
    UNUSED_PARAMETER(p_context);
    m_gps_response_timed_out = true;
}

I've verified that the timeout handler function is really getting called. Why would this work for a few minutes then stop working? I do have other timers in the same code, but they all have their own timer IDs of course.

Parents
  • We've had a couple of known bugs regarding the app_timer in the latest release, which we are working on fixing. I have the app_timer source file that will be included in the next release of the nRF51 SDK. Could you try that?

    -H

    app_timer.c

  • Hi Hakon, I will try this. Also note that as a general rule in my application I always stop an app_timer before starting it, to assure any timer is started fresh if already running. Could this increase the likelihood of this issue?

    I have been able to recreate my issue but hard-coding rtc1_stop() and rtc_start() from app_timer.c in the Button_handler at a Button push. Normally a button push causes a button_timer to run to measure how long the button is held. At specific intervals of this timer I schedule events. After a specific amount of time the device is shutoff System_off via a scheduled event. However adding the above code causes an immediate shutdown and seems to crash/overflow the scheduler???

Reply
  • Hi Hakon, I will try this. Also note that as a general rule in my application I always stop an app_timer before starting it, to assure any timer is started fresh if already running. Could this increase the likelihood of this issue?

    I have been able to recreate my issue but hard-coding rtc1_stop() and rtc_start() from app_timer.c in the Button_handler at a Button push. Normally a button push causes a button_timer to run to measure how long the button is held. At specific intervals of this timer I schedule events. After a specific amount of time the device is shutoff System_off via a scheduled event. However adding the above code causes an immediate shutdown and seems to crash/overflow the scheduler???

Children
No Data
Related