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 Reply Children
  • Hi ,Edvin

    Thanks

    The program stops in the function nrf_drv_timer_init().

    here is the function and log:

    log:

    TIMER:INFO:3
    TIMER:INFO:Function: nrf_drv_timer_init, error code: NRF_SUCCESS.

    We can see the uart can't printf "TIMER:INFO:4"

    function

    ret_code_t nrf_drv_timer_init(nrf_drv_timer_t const * const p_instance,
                                  nrf_drv_timer_config_t const * p_config,
                                  nrf_timer_event_handler_t timer_event_handler)
    {
        timer_control_block_t * p_cb = &m_cb[p_instance->instance_id];
        ASSERT(((p_instance->p_reg == NRF_TIMER0) && TIMER0_ENABLED) || (p_instance->p_reg != NRF_TIMER0));
        ASSERT(((p_instance->p_reg == NRF_TIMER1) && TIMER1_ENABLED) || (p_instance->p_reg != NRF_TIMER1));
        ASSERT(((p_instance->p_reg == NRF_TIMER2) && TIMER2_ENABLED) || (p_instance->p_reg != NRF_TIMER2));
    #if TIMER_COUNT == 5
        ASSERT(((p_instance->p_reg == NRF_TIMER3) && TIMER3_ENABLED) || (p_instance->p_reg != NRF_TIMER3));
        ASSERT(((p_instance->p_reg == NRF_TIMER4) && TIMER4_ENABLED) || (p_instance->p_reg != NRF_TIMER4));
    #endif //TIMER_COUNT
    #ifdef SOFTDEVICE_PRESENT
        ASSERT(p_instance->p_reg != NRF_TIMER0);
        ASSERT(p_config);
    #endif
        ret_code_t err_code;
    
        if (p_cb->state != NRF_DRV_STATE_UNINITIALIZED)
        {
            err_code = NRF_ERROR_INVALID_STATE;
            NRF_LOG_WARNING("Function: %s, error code: %s.\r\n", (uint32_t)__func__, (uint32_t)ERR_TO_STR(err_code));
            return err_code;
        }
    
        if (timer_event_handler == NULL)
        {
            err_code = NRF_ERROR_INVALID_PARAM;
            NRF_LOG_WARNING("Function: %s, error code: %s.\r\n", (uint32_t)__func__, (uint32_t)ERR_TO_STR(err_code));
            return err_code;
        }
    
        /* Warning 685: Relational operator '<=' always evaluates to 'true'"
         * Warning in NRF_TIMER_IS_BIT_WIDTH_VALID macro. Macro validate timers resolution.
         * Not necessary in nRF52 based systems. Obligatory in nRF51 based systems.
         */
    
        /*lint -save -e685 */
    
        ASSERT(NRF_TIMER_IS_BIT_WIDTH_VALID(p_instance->p_reg, p_config->bit_width));
    
        //lint -restore
    
        p_cb->handler = timer_event_handler;
        p_cb->context = p_config->p_context;
    
        uint8_t i;
        for (i = 0; i < p_instance->cc_channel_count; ++i)
        {
            nrf_timer_event_clear(p_instance->p_reg,
                nrf_timer_compare_event_get(i));
        }
    
        nrf_drv_common_irq_enable(nrf_drv_get_IRQn(p_instance->p_reg),
            p_config->interrupt_priority);
    
        nrf_timer_mode_set(p_instance->p_reg, p_config->mode);
        nrf_timer_bit_width_set(p_instance->p_reg, p_config->bit_width);
        nrf_timer_frequency_set(p_instance->p_reg, p_config->frequency);
    
        p_cb->state = NRF_DRV_STATE_INITIALIZED;
    
        err_code = NRF_SUCCESS;
    	NRF_LOG_INFO("3\r\n");
        NRF_LOG_INFO("Function: %s, error code: %s.\r\n", (uint32_t)__func__, (uint32_t)ERR_TO_STR(err_code));
    	NRF_LOG_INFO("4\r\n");
        return err_code;
    }

Related