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.

Related