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

timer and app_timer

hi,

I want to know why app_timer and nrf_drv_timer conflict。App_timer is used to broadcast data。When App_timer time out ,timer1 start.

Cannot broadcast data after initializing timer1 in main function。Here is the code, please help me solve this problem

#ifndef TIMER_ENABLED
#define TIMER_ENABLED 1
#endif

#ifndef TIMER1_ENABLED
#define TIMER1_ENABLED 1
#endif


void timer_sampling_event_handler(nrf_timer_event_t event_type, void* p_context)
{   
    switch(event_type)
    {
        case NRF_TIMER_EVENT_COMPARE0:
			temperature_calculate();
            break;
        
        default:
            //Do nothing.
            break;
    }    
}

const nrf_drv_timer_t TIMER_SAMPLING = NRF_DRV_TIMER_INSTANCE(1);
void my_timer_init(uint32_t time_ms)
{
    uint32_t time_ticks;
    uint32_t err_code = NRF_SUCCESS;
    
   
    //Configure TIMER_SAMPLING for generating simple light effect - leds on board will invert his state one after the other.
    err_code = nrf_drv_timer_init(&TIMER_SAMPLING, NULL, timer_sampling_event_handler);  //NULL  ĬÈÏΪ16λ¶¨Ê±Æ÷	ʱÖÓÆµÂÊΪ1M  ʱÖÓԴλ1M½µµÍ¹¦ºÄ  3-8uA
    APP_ERROR_CHECK(err_code);
    
    time_ticks = nrf_drv_timer_ms_to_ticks(&TIMER_SAMPLING, time_ms);
    
    nrf_drv_timer_extended_compare(
         &TIMER_SAMPLING, NRF_TIMER_CC_CHANNEL0, time_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);
    
    nrf_drv_timer_enable(&TIMER_SAMPLING);

}


static void timers_init(void)
{
    ret_code_t err_code;
	
	// Initialize timer module, making it use the scheduler
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
	
	
	err_code = app_timer_create(&m_adv_timer_id, APP_TIMER_MODE_REPEATED, adv_timeout_handler);
	APP_ERROR_CHECK(err_code);

	err_code  = app_timer_start(m_adv_timer_id, APP_TIMER_TICKS(broast_cycle,0), NULL); //Æô¶¯¶¨Ê±Æ÷
	APP_ERROR_CHECK(err_code);	
}


int main(void)
{
    ret_code_t err_code;
	ret_code_t poweron_flag=0;
	
    // Initialize.

    err_code = NRF_LOG_INIT(NULL);
    APP_ERROR_CHECK(err_code);
	
    leds_init();
    timers_init();			//app_timer_init
    buttons_init();
	WD3703_Init();             //temperature sampling init p0.11
	my_timer_init(SAMPLING_COMPLETED_CYCLE);			//timer1 init

    ble_stack_init();
    gap_params_init();
    services_init();
	advertising_data_set();
    conn_params_init();

    // Start execution.
    NRF_LOG_INFO("Blinky Start!\r\n");


    // Enter main loop.
    for (;;)
    {
        if (NRF_LOG_PROCESS() == false)
        {
            power_manage();
        }
    }
}

Parents
  • Can you try to define "DEBUG" in your preprocessor defines (in your project settings), and turn off optimization. Then set a breakpoint in app_error.c  around line 73 (in the first function. The name depends on what SDK version you are using). Then try to debug, and see if the breakpoint is hit.

     

    Best regards,

    Edvin

  • Hi ,Edvin

    Thanks

    I tried to debug follow your suggestion,the program stops in the function app_error_save_and_stop() at line113 while(loop)

    the program run from main() to ble_stack_init() to SOFTDEVICE_HANDLER_INIT(&clock_lf_cfg, NULL) to app_error_handler() to app_error_fault_handler(NRF_FAULT_ID_SDK_ERROR, 0, (uint32_t)(&error_info)) to app_error_save_and_stop() to while(loop),then stop at here.

    Best regards,

    Amy

     

    void app_error_save_and_stop(uint32_t id, uint32_t pc, uint32_t info)
    {
        /* static error variables - in order to prevent removal by optimizers */
        static volatile struct
        {
            uint32_t        fault_id;
            uint32_t        pc;
            uint32_t        error_info;
            assert_info_t * p_assert_info;
            error_info_t  * p_error_info;
            ret_code_t      err_code;
            uint32_t        line_num;
            const uint8_t * p_file_name;
        } m_error_data = {0};
    
        // The following variable helps Keil keep the call stack visible, in addition, it can be set to
        // 0 in the debugger to continue executing code after the error check.
        volatile bool loop = true;
        UNUSED_VARIABLE(loop);
    
        m_error_data.fault_id   = id;
        m_error_data.pc         = pc;
        m_error_data.error_info = info;
    
        switch (id)
        {
            case NRF_FAULT_ID_SDK_ASSERT:
                m_error_data.p_assert_info = (assert_info_t *)info;
                m_error_data.line_num      = m_error_data.p_assert_info->line_num;
                m_error_data.p_file_name   = m_error_data.p_assert_info->p_file_name;
                break;
    
            case NRF_FAULT_ID_SDK_ERROR:
                m_error_data.p_error_info = (error_info_t *)info;
                m_error_data.err_code     = m_error_data.p_error_info->err_code;
                m_error_data.line_num     = m_error_data.p_error_info->line_num;
                m_error_data.p_file_name  = m_error_data.p_error_info->p_file_name;
                break;
        }
    
        UNUSED_VARIABLE(m_error_data);
    
        // If printing is disrupted, remove the irq calls, or set the loop variable to 0 in the debugger.
        __disable_irq();
        while (loop);
    
        __enable_irq();
    }

Reply
  • Hi ,Edvin

    Thanks

    I tried to debug follow your suggestion,the program stops in the function app_error_save_and_stop() at line113 while(loop)

    the program run from main() to ble_stack_init() to SOFTDEVICE_HANDLER_INIT(&clock_lf_cfg, NULL) to app_error_handler() to app_error_fault_handler(NRF_FAULT_ID_SDK_ERROR, 0, (uint32_t)(&error_info)) to app_error_save_and_stop() to while(loop),then stop at here.

    Best regards,

    Amy

     

    void app_error_save_and_stop(uint32_t id, uint32_t pc, uint32_t info)
    {
        /* static error variables - in order to prevent removal by optimizers */
        static volatile struct
        {
            uint32_t        fault_id;
            uint32_t        pc;
            uint32_t        error_info;
            assert_info_t * p_assert_info;
            error_info_t  * p_error_info;
            ret_code_t      err_code;
            uint32_t        line_num;
            const uint8_t * p_file_name;
        } m_error_data = {0};
    
        // The following variable helps Keil keep the call stack visible, in addition, it can be set to
        // 0 in the debugger to continue executing code after the error check.
        volatile bool loop = true;
        UNUSED_VARIABLE(loop);
    
        m_error_data.fault_id   = id;
        m_error_data.pc         = pc;
        m_error_data.error_info = info;
    
        switch (id)
        {
            case NRF_FAULT_ID_SDK_ASSERT:
                m_error_data.p_assert_info = (assert_info_t *)info;
                m_error_data.line_num      = m_error_data.p_assert_info->line_num;
                m_error_data.p_file_name   = m_error_data.p_assert_info->p_file_name;
                break;
    
            case NRF_FAULT_ID_SDK_ERROR:
                m_error_data.p_error_info = (error_info_t *)info;
                m_error_data.err_code     = m_error_data.p_error_info->err_code;
                m_error_data.line_num     = m_error_data.p_error_info->line_num;
                m_error_data.p_file_name  = m_error_data.p_error_info->p_file_name;
                break;
        }
    
        UNUSED_VARIABLE(m_error_data);
    
        // If printing is disrupted, remove the irq calls, or set the loop variable to 0 in the debugger.
        __disable_irq();
        while (loop);
    
        __enable_irq();
    }

Children
  • Did you define "DEBUG" in the preprocessor defines?

    Can you see if you get any info on the m_error_data? If you hover your mouse over p_file_name, line_num and err_code, or add them to quick watch (name may depend on your IDE). 

     

    I suspect that you have some timer conflicts. Are you trying to use TIMER0? This timer is also used by the softdevice, so you can't use it while using the softdevice.

    But what is the err_code and function call that causes the error_handler to trigger?

     

    Best regards,

    Edvin

Related