This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

When call the app_timer_start(),return error_code = 0x08,NRF_ERROR_INVALID_STATE?

At the start of programm, I create a timer_id used the follow code: app_timer_create(&m_sec_req_timer_id,APP_TIMER_MODE_SINGLE_SHOT,sec_req_timeout_handler);

when the device peered the iphone(ancs),send the QQ message to the iphone,the device could respond,then disconnected the device, after a time,connect the device,the device would call the function app_timer_start(m_sec_req_timer_id,SECURITY_REQUEST_DELAY,NULL),it will generate the error_code = 0x0000008.

So I trace this error_code,it stopped at the line about 1095 in file app_timer.c,it displayed mp_nodes[timer_id].state = STATE_FREE. I have created the timer_id at the start of programm,why it displayed STATE_FREE. How to solve this problem?Thank you.

image description image description

  • FormerMember
    0 FormerMember

    From the code in app_timer_start(..), it will return NRF_ERROR_INVALID_STATE if the timer_id is not properly initialized or if there is no timeout handler:

    uint32_t app_timer_start(app_timer_id_t timer_id, uint32_t timeout_ticks, void * p_context)
    {
    uint32_t timeout_periodic;
    timer_node_t * p_node = (timer_node_t*)timer_id;
    
    // Check state and parameters
    VERIFY_MODULE_INITIALIZED();
    
    if (timer_id == 0)
    {
        return NRF_ERROR_INVALID_STATE;
    }
    if (timeout_ticks < APP_TIMER_MIN_TIMEOUT_TICKS)
    {
        return NRF_ERROR_INVALID_PARAM;
    }
    if (p_node->p_timeout_handler == NULL)
    {
        return NRF_ERROR_INVALID_STATE;
    }
    
    // Schedule timer start operation
    timeout_periodic = (p_node->mode == APP_TIMER_MODE_REPEATED) ? timeout_ticks : 0;
    
    return timer_start_op_schedule(p_node,
                                   timeout_ticks,
                                   timeout_periodic,
                                   p_context);
    }
    

    Could you run the chip in debug mode and set a breakpoint in app_timer_start(..) when your device is about to re-connect to the iPhone? When the program hits app_timer_start(..), you can see why it fails.

  • Thank you for your reply,I used SDK9.0.The function app_timer_start() as follows:

    1、uint32_t app_timer_start(app_timer_id_t timer_id, uint32_t timeout_ticks, void * p_context)

    2、{

    3、uint32_t timeout_periodic;

    4、if (mp_nodes == NULL)

    5、{

    6、 return NRF_ERROR_INVALID_STATE;

    7、}

    8、if( (timer_id >= m_node_array_size) || (timeout_ticks < APP_TIMER_MIN_TIMEOUT_TICKS))

    9、{

    10、 return NRF_ERROR_INVALID_PARAM;

    11、}

    12、if (mp_nodes[timer_id].state != STATE_ALLOCATED)

    13、{

    14、 return NRF_ERROR_INVALID_STATE;

    15、}

    // Schedule timer start operation timeout_periodic = (mp_nodes[timer_id].mode == APP_TIMER_MODE_REPEATED) ? timeout_ticks : 0;

    return timer_start_op_schedule(user_id_get(), timer_id, timeout_ticks, timeout_periodic, p_context); }

    When I run the chip in debug mode,the programm stop at the line 14 as the above shows.I cannot understand why the state of m_sec_req_timer_id is not STATE_ALLOCATED.

  • FormerMember
    0 FormerMember in reply to FormerMember

    When the timer is being created, it's added to mp_nodes with the state "STATE_ALLOCATED". When starting the timer, app_timer checks mp_nodes, to check if the given timer has state "STATE_ALLOCATED". When you call app_timer_start(..), does timer_id matches the id given in app_timer_create(..)?

  • So what was the issue in the end?

Related