Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

NRF5 SDK Timer HAL: How can I get the actual value of the Timer?

Dear All,

I need a high frequency clock in order to measure the time between a couple of events.

In the RTC HAL there are functions that allow to retrieve the counter value and based on the used prescaler the time elapsed can be calculated.

But I need a clock frequency higher than the 32kHz and I am also out of RTC instances because I am using low power PWM, SoftDevice and a RTC  instance to keep track of time to my system.

So I thought of using the TIMER HAL available, but I do not see how I can get the value of the TIMER in timer mode.

What I need to do is to take the TIMER value whenever an interrupt is detected on a pin and then from those values calculate the frequency of the incoming signal.



Could you perhaps assist me with that?

Parents
  • Hello,

    The capture function shown in the code snippet below can be used to read out the TIMER counter register using the driver APIs, or you can call  nrf_timer_cc_get() if you are working directly with the HAL.

    const nrf_drv_timer_t timestamp_timer = NRF_DRV_TIMER_INSTANCE(1); // Selecting TIMER1. Make sure it's enabled in sdk_config.h    
    
    /* Driver requires callback even if timer interrupts are not enabled */
    void timer_event_handler(nrf_timer_event_t event_type, void* p_context)
    {}
    
    uint32_t timer_timestamp_get(void)
    {
        return nrf_drv_timer_capture(&timestamp_timer, <CC register index>);
    }
        
    void timer_init(void)
    {
        nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
        err_code = nrf_drv_timer_init(&timestamp_timer, &timer_cfg, timer_event_handler);
        APP_ERROR_CHECK(err_code);
    } 

    Note: for high accuracy measurements you may want to start the more accurate 64 MHz crystal oscillator (HFXO) clock. The system defaults to the HFINT clock.

    Best regards,

    Vidar

Reply
  • Hello,

    The capture function shown in the code snippet below can be used to read out the TIMER counter register using the driver APIs, or you can call  nrf_timer_cc_get() if you are working directly with the HAL.

    const nrf_drv_timer_t timestamp_timer = NRF_DRV_TIMER_INSTANCE(1); // Selecting TIMER1. Make sure it's enabled in sdk_config.h    
    
    /* Driver requires callback even if timer interrupts are not enabled */
    void timer_event_handler(nrf_timer_event_t event_type, void* p_context)
    {}
    
    uint32_t timer_timestamp_get(void)
    {
        return nrf_drv_timer_capture(&timestamp_timer, <CC register index>);
    }
        
    void timer_init(void)
    {
        nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
        err_code = nrf_drv_timer_init(&timestamp_timer, &timer_cfg, timer_event_handler);
        APP_ERROR_CHECK(err_code);
    } 

    Note: for high accuracy measurements you may want to start the more accurate 64 MHz crystal oscillator (HFXO) clock. The system defaults to the HFINT clock.

    Best regards,

    Vidar

Children
No Data
Related