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

nrf_delay_ms code hangs when using app_timer

Hey,

I am trying to use app_timers and nrf_delay_ms together but the system gets stuck almost everytime. I have the following code:

void timer_handler(void *p_context) 
{ 
    uint32_t err_code = app_timer_stop(timer_id); 
    APP_ERROR_CHECK(err_code); 
    func_2(); 
}

void func_1() 
{ 
    //create timer
    uint32_t err_code = app_timer_create(&timer_id, APP_TIMER_MODE_SINGLE_SHOT, timer_handler);

    APP_ERROR_CHECK(err_code); 

    err_code = app_timer_start(timer_id, TIMER_INTERVAL(TIMER_INTERVAL_S), NULL);

    APP_ERROR_CHECK(err_code); 
    return;
}


void func_2() 
{ 
    /* code/ 
    nrf_delay_ms(125); //system gets stuck here!! 

    /code*/ 
    return; 
}

for the assert call back I have this:

void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name)
{
     SEGGER_RTT_printf(0, "app_error_handler\r\n: 0x%#04x\nline_num=%u\r\nfile_name=%s",error_code, line_num,p_file_name); 
}

void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name) { 
    app_error_handler(1, line_num, p_file_name); 
}

I'm getting the following response on RTTviewer:

0> app_error_handler 0> : 0x0001 0> line_num=1371 0> file_name=src\rem.c

I'm using a custom board with nrf51822. The system gets stuck after this error and doesn't reset. If I enable wdt then I do see a reset. Why is this error occurring even after stopping the timer?

Thanks, Vandita

Parents
  • I doubt the problem is nrf_delay_ms(125) but rather that either func_1() and/or func_2() are taking too long to execute, causing some time-sensitive code inside the SoftDevice to generate that error you're seeing.

    I had problems like this once and it helped to keep a free-running clock so I could print out the current time and see just how long code was taking to execute. app_timer_cnt_get() will get you the time, but out-of-the-box it only runs when an app_timer is running. I had to make a few tweaks to app_timer.c to turn it into an always-running clock. Once I identified the problem, ultimately I had to restructure my code so that the tasks which were taking too long to execute would instead execute in tiny chunks, giving the SoftDevice control of the system in between chunks.

    static void rtc1_init(uint32_t prescaler)
    {
        ...
        // Add this at the end of rtc1_init()
        NRF_RTC1->TASKS_START = 1;
    }
    
    static void rtc1_start(void)
    {
        ....
    
        //Comment these 2 lines out    
        //NRF_RTC1->TASKS_START = 1;
        //nrf_delay_us(MAX_RTC_TASKS_DELAY);
    
        ...
    }
    
    static void rtc1_stop(void)
    {
        ...
        // Comment these 4 lines out
        //NRF_RTC1->TASKS_STOP = 1;
        //nrf_delay_us(MAX_RTC_TASKS_DELAY);
    
        //NRF_RTC1->TASKS_CLEAR = 1;
        //m_ticks_latest        = 0;
        ...		
    }
    
    static void timer_list_remove(app_timer_id_t timer_id)
    {
        ...
                //Comment these two lines out
                //NRF_RTC1->TASKS_CLEAR = 1;
                //m_ticks_latest        = 0;
                //Add this line
    	        m_ticks_latest				= NRF_RTC1->COUNTER;
        ...
    }
    
  • Hey Nick, I moved the func_2 call out of the handler and used flags in the main to call it whenever handler is called. That solved the issue. Thanks for your answer. Vandita

Reply Children
No Data
Related