Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs

nRF52840 timer that counts to a specific value and wraps up

I'm trying to implement a timer with 1ms resolution that my application can use externally using a callback function.

The idea is to 

  • Implement the timer as a 32-bit counter that counts up starting at 0x00000000 and wraps back to 0 after reaching 0xffffffff.
  • Implement "Atomic" function to return 1ms counter.
  • This atomic function simply returns an uint32_t type variable containing the current value of the counter variable.

I'm a bit lost trying to set up the timer, the SDK examples did not really much help. Could you kindly help me out here with something?

Parents Reply Children
  • Hi  , 

    Thanks for the response, I checked it out. However because of limitations with the project I'm not using zephyr or even the nRFConnect SDK, the development is being done bare metal on the PCA10056 board of the nRF52840, using nRF_SDK_15.3.0

    Accordingly, I'd only be able to use the nRF SDK 15.3.0 files to implement this funcitonability.
    I figure, I can set up a timer of 1ms, that as a timer event increments a variable and I can just read the variable and return it. I dont understand the NRF_TIMER_EVENT_COMPAREX modes and also the NRF_TIMER_SHORT_COMPAREX_CLEAR_MASK modes.

    Would there be an easier method of implementing what I require using the SDK files only?

  • This atomic function simply returns an uint32_t type variable containing the current value of the counter variable.

    I have not worked with the nRF5 SDK for 2-3 years, but I tried to take a look.

    In the nRF Connect SDK, th function counter_get_value() would call get_value()-->read()-->nrf_timer_cc_get()

    The equivalent function in nRF5 SDK would be nrf_drv_timer_capture()-->nrfx_timer_capture()-->nrf_timer_cc_read()

    uint32_t nrfx_timer_capture(nrfx_timer_t const * const p_instance,
                                nrf_timer_cc_channel_t     cc_channel)
    {
        NRFX_ASSERT(m_cb[p_instance->instance_id].state != NRFX_DRV_STATE_UNINITIALIZED);
        NRFX_ASSERT(cc_channel < p_instance->cc_channel_count);
    
        nrf_timer_task_trigger(p_instance->p_reg,
            nrf_timer_capture_task_get(cc_channel));
        return nrf_timer_cc_read(p_instance->p_reg, cc_channel);
    }

    You can find nrf_drv_timer_capture in nRF5_SDK_17.1.0\integration\nrfx\legacy\nrf_drv_timer.h

    Oh, I see that you're using 15.3.0, but it seems like this function is present there as well: https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v15.3.0/group__nrf__drv__timer.html?cp=8_5_2_6_9_0_35_0_1#gab47bc0277a884dc727fe4d4b7f378398

Related