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

Invalid state error when stopping app_timers

Hi All,

I have got a project running on an nRF51422 QFAC, using the lastest SDK.

I am having some problems with the app_timers, i am able to create and start them, and they work ok, but i cannot stop them.

I have a static timer declared globally, which is used by multiple functions, along with a flag to see if its running or not. they are declared as follows,

static app_timer_id_t messageTimer;

static bool messageTimerRunning = false;

I have tried two methods of starting the timer, only one works. I think i only need to create the timer once on startup and then it can be used normally, however this does not work, when i go to start it later in the app, i get error NRF_ERROR_INVALID_STATE, which appears to be because the timer id is null. Instead i find i have to create it and then start it straight away. I do this the same as i would if i was just initialising it once on startup.

	if(!messageTimerRunning){
		APP_TIMER_DEF(messageTimer);
		uint32_t err_code = app_timer_create(&messageTimer, APP_TIMER_MODE_SINGLE_SHOT, onMessageTimeoutTimer);
		APP_ERROR_CHECK(err_code);
		err_code = app_timer_start(messageTimer, APP_TIMER_TICKS( 30000, APP_TIMER_PRESCALER ),NULL);
		APP_ERROR_CHECK(err_code);
		messageTimerRunning = true;
}else{
		uint32_t err_code = app_timer_stop(messageTimer);
		APP_ERROR_CHECK(err_code);
	
		APP_TIMER_DEF(messageTimer);
		err_code = app_timer_create(&messageTimer, APP_TIMER_MODE_SINGLE_SHOT, onMessageTimeoutTimer);
		APP_ERROR_CHECK(err_code);
		err_code = app_timer_start(messageTimer, APP_TIMER_TICKS( 30000, APP_TIMER_PRESCALER ),NULL);
		APP_ERROR_CHECK(err_code);
		messageTimerRunning = true;
}

Regardless of whether I create the timer once on startup, or re create every time i use it, i will get an invalid state error when i come to stop it.

Thanks in advance, Bill.

  • Hi,

    You normally do the "APP_TIMER_DEF(messageTimer);" where you have the #defines and #includes(not in a function or in main() ), and then you do the timer and library initialization in a function like this

    static void timers_init(void)
    {
        uint32_t err_code;
    
        // Initialize timer module
        APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
    
        // Create timers
        err_code = app_timer_create(&messageTimer,
                                    APP_TIMER_MODE_SINGLE_SHOT,
                                    onMessageTimeoutTimer);
        APP_ERROR_CHECK(err_code);
    }
    

    Then you can start your timer in a function like this, and set your flag:

    static void application_timers_start(void)
    {
        uint32_t err_code;
    
        // Start application timers
        err_code = app_timer_start(messageTimer, APP_TIMER_TICKS(30000, APP_TIMER_PRESCALER ), NULL);
        APP_ERROR_CHECK(err_code);
    	messageTimerRunning = true;
    }
    
Related