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

nrfx_timer example code?

The timer example with SDK 15.0.0 uses the old timer driver. Is there any example code showing how to use the current nrfx_timer?

I'm just trying to generate a periodic 100us interrupt so I can (fairly) accurately count how many tenths of a millisecond have passed - should be pretty simple, but the documentation doesn't do a very good job of describing how to use the API, and I'm not certain if I need to be using it in timer mode or compare mode.

Thanks!

Parents
  • Hi,

    The migration guide in SDK 15.0.0 describe how to move from legacy to nrfx drivers. Do you have any specific issues?

     Also, I'm not sure I understand your statement:

    I'm not certain if I need to be using it in timer mode or compare mode.

    The timer can operate in Timer or Counter mode. Compare is not a mode, but something you use to decide when the timer should generate an event. If you set the compare register to 5, an event will be generated when the timer reach 5. If you want to generate interrupts every 100 us, you should use Timer mode.

    Best regards,
    Jørgen

Reply
  • Hi,

    The migration guide in SDK 15.0.0 describe how to move from legacy to nrfx drivers. Do you have any specific issues?

     Also, I'm not sure I understand your statement:

    I'm not certain if I need to be using it in timer mode or compare mode.

    The timer can operate in Timer or Counter mode. Compare is not a mode, but something you use to decide when the timer should generate an event. If you set the compare register to 5, an event will be generated when the timer reach 5. If you want to generate interrupts every 100 us, you should use Timer mode.

    Best regards,
    Jørgen

Children
  • Hi Jørgen - thanks for the quick reply.

    Is there some documentation that actually describes how to use these drivers to someone who doesn't already know? All I am finding in the infocenter SDK documentation (Hardware Drivers -> nEF52840 Drivers) just looks like it came out of something like Doxygen - at first glance it appears to be useful documentation, but on inspection it's just a prettied-up version of what I can read in nrfx_timer.h.

    There's no narrative along the lines of "first, you must initialize the module and here are the various options and why you would choose one option VS another. Next you must enable the module..." Without a functioning example, I just have to try things blindly until something works.

    EDIT: Ok after a lot of trial and error I got something that hits my ISR every 100us. In case anyone else can use it, here is the code:

    static volatile uint32_t m_us_counter = 0; // System time in microseconds

    static const nrfx_timer_t m_timer2 = NRFX_TIMER_INSTANCE(2);

    //== Function prototypes =====================================================
    nrfx_timer_event_handler_t Timer_2_Interrupt_Handler(void);

    //============================================================================


    nrfx_timer_event_handler_t Timer_2_Interrupt_Handler(void)
    {
    m_us_counter += 100;
    }


    void Timer_Initialize(void)
    {
    ret_code_t err_code;

    nrfx_timer_config_t tmr_config = NRFX_TIMER_DEFAULT_CONFIG;
    tmr_config.frequency = (nrf_timer_frequency_t)NRF_TIMER_FREQ_1MHz;
    tmr_config.mode = (nrf_timer_mode_t)NRF_TIMER_MODE_TIMER;
    tmr_config.bit_width = (nrf_timer_bit_width_t)NRF_TIMER_BIT_WIDTH_8;

    err_code = nrfx_timer_init(&m_timer2, &tmr_config, Timer_2_Interrupt_Handler);
    APP_ERROR_CHECK(err_code);

    nrfx_timer_extended_compare(&m_timer2, NRF_TIMER_CC_CHANNEL0, 100, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);

    nrfx_timer_enable(&m_timer2);
    }

  • Thanks Glen!  That was very helpful.  I was having an error here and it was solved (I had forgotten to initialize the tmr_config structure). Cheers, Gil.

Related