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

measuring interrupt interval with app_timer

I have an interrupt coming in every 50-150 microsecs and I want to measure these intervals as precisely as possible.

I tried this with SDK12.2:

APP_TIMER_INIT(0, 5, false);

then in the (GPIOTE) ISR:

uint32_t this_time=app_timer_cnt_get();

I always get zero back;

Presumably I need to initialise it in a different way?

Thx for any suggestion.

Parents
  • OK. Got this after much googling.

    #define NRF_DRV_TIMER_MY_CONFIG {
        .frequency          = (nrf_timer_frequency_t) NRF_TIMER_FREQ_1MHz,
        .mode               = (nrf_timer_mode_t) TIMER_MODE_MODE_Timer,
        .bit_width          = (nrf_timer_bit_width_t) TIMER_BITMODE_BITMODE_32Bit,
        .interrupt_priority = TIMER_DEFAULT_CONFIG_IRQ_PRIORITY,
        .p_context          = NULL
    }
    
    const nrf_drv_timer_t TIMER_INPUT = NRF_DRV_TIMER_INSTANCE(3);
    
    void timer_event_handler(nrf_timer_event_t event_type, void * p_context) { 
      return;  //  needed even if not used 
    }
    

    and in main()

    nrf_drv_timer_config_t in_config_f = NRF_DRV_TIMER_MY_CONFIG;
    err_code = nrf_drv_timer_init(&TIMER_INPUT, &in_config_f, timer_event_handler); 
    APP_ERROR_CHECK(err_code);
    nrf_drv_timer_enable(&TIMER_INPUT);
    

    then in ISR

    this_time=nrf_drv_timer_capture(&TIMER_INPUT, 0);
    

    (then handle delta microseconds and overflow.)

    seems to work well enough. note that putting NULL instead of timer_event_handler causes a an immediate crash.

  • You are on the correct track. The timer driver allows one to set up timers triggered by external events via ppi and then it generates events on completion that allow you to pick up the resulting count at your leisure in code. This way the count is independent of IRQ latency. However, I would recommend just coding it directly using the ppi gpiote registers. The ppi/gpiote hardware is very useful and you will get a better understanding of how it works by avoiding the driver. Assuming time is available I can write something up Monday morning. I think it is time to post a very specific example since this question comes up a lot.

Reply
  • You are on the correct track. The timer driver allows one to set up timers triggered by external events via ppi and then it generates events on completion that allow you to pick up the resulting count at your leisure in code. This way the count is independent of IRQ latency. However, I would recommend just coding it directly using the ppi gpiote registers. The ppi/gpiote hardware is very useful and you will get a better understanding of how it works by avoiding the driver. Assuming time is available I can write something up Monday morning. I think it is time to post a very specific example since this question comes up a lot.

Children
No Data
Related