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

Time taken by a code snippet

Hi

I want to know the time taken(in us) by a code snippet. I had a look at app_timer functionality with the help of this post. But the timer value always shows 0. I am not sure on how to create a dummy timer. Can someone help me with this? Also which is more accurate, RTC or APP_TIMER?

Thanks in advance.

-Sai Kiran

  • Sorry. I wrote that code without testing it. I was missed the part that the tasks has to be set to 1 (like the NRF_TIMER3->TASKS_START = 1;)

    Try the following:

    void timer_init(void)
    {
        NRF_TIMER3->BITMODE         = TIMER_BITMODE_BITMODE_32Bit << TIMER_BITMODE_BITMODE_Pos;
        NRF_TIMER3->PRESCALER       = 4;
        NRF_TIMER3->SHORTS          = TIMER_SHORTS_COMPARE0_CLEAR_Disabled << TIMER3_CC_NUM;
        NRF_TIMER3->MODE            = TIMER_MODE_MODE_Timer << TIMER_MODE_MODE_Pos;
        
        NRF_TIMER3->TASKS_START = 1;
    }
    
    int main(void)
    {
        bsp_board_init(BSP_INIT_LEDS);
    
        APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
        NRF_LOG_DEFAULT_BACKENDS_INIT();
        
        timer_init();
        
        NRF_LOG_INFO("test");
        while (1)
        {
            bsp_board_led_invert(0);
            NRF_TIMER3->TASKS_CAPTURE[0] = 1;
            nrf_delay_ms(1000);
            NRF_TIMER3->TASKS_CAPTURE[1] = 1;
            NRF_LOG_INFO("diff_time: %08d µs", (NRF_TIMER3->CC[1] - NRF_TIMER3->CC[0]));
            
            NRF_LOG_FLUSH();
            __WFE();
        }
    }

    BR,

    Edvin

Related