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

How to create a timer by using nRF connect SDK on nRF52840dk board

Hi nRF support team,

By using the nRF Connect SDK, I am trying to create a timer, which I can use for incrementing a counter by 1 for each second on a timer interrupt handler.

Can you please share how to create a timer and any sample code?

Regards,
Kalyan

  • Hi nRF support team,

    For your reference, I have attached code and output log. The issue is, not receiving nrf timer event handler? Please let me know if anything I am missing here?

    main.c::

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

    //static const nrfx_timer_t m_timer = NRFX_TIMER_INSTANCE(2);
    static const nrfx_timer_t m_timer = NRFX_TIMER_INSTANCE(4);

    void timer_handler(nrf_timer_event_t event_type, void *p_context) {
        printk("timer_handler()....\n");
        m_counter += 1;
    }

    void timer_init(void) {
        nrfx_err_t err;
        nrfx_timer_config_t timer_cfg = NRFX_TIMER_DEFAULT_CONFIG;  
        timer_cfg.bit_width = NRF_TIMER_BIT_WIDTH_32;
        timer_cfg.mode = NRF_TIMER_MODE_TIMER;
        //timer_cfg.mode = NRF_TIMER_MODE_COUNTER;
     
        err = nrfx_timer_init(&m_timer, &timer_cfg, timer_handler);
        printk("nrfx_timer_init() return value [0x%x]\n", err);

        uint32_t ticks = nrfx_timer_ms_to_ticks(&m_timer, 1000);
        printk("ticks[%d]\n", ticks);
        nrfx_timer_extended_compare(&m_timer, NRF_TIMER_CC_CHANNEL0, ticks,
                    NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, false);
        nrfx_timer_enable(&m_timer);
    }

    void main(void)
    {

            timer_init();
        /* Implement notification. At the moment there is no suitable way
         * of starting delayed work so we do it here
         */
        while (1)
        {
                    printk("m_counter value [%d]\n", m_counter);
                    //Referesh actual data every two minutes
            k_sleep(K_SECONDS(2));
        }
    }

    Output::

    *** Booting Zephyr OS build v2.3.0-rc1-ncs1-2410-g7d20f2ebf259  ***
    nrfx_timer_init() return value [0xbad0000]
    ticks[16000000]
    m_counter value [0]
    m_counter value [0]
    m_counter value [0]
    m_counter value [0]
    m_counter value [0]
    m_counter value [0]
    m_counter value [0]
    m_counter value [0]

  • Hi,

     

    In order to use interrupts in your application, you have to enable it using the OS wrapper functions, like done here:

    https://github.com/nrfconnect/sdk-nrf/blob/master/samples/peripheral/radio_test/src/radio_test.c#L577-L581

     

    For your case, you need to adjust "0" to "2".

     

    Note that you can also create a kernel timer (based on the 32k RTC):

    https://docs.zephyrproject.org/latest/reference/kernel/timing/timers.html#using-a-timer-expiry-function

     

    Kind regards,

    Håkon

  • Hi Hakon,

    Thanks for the quick response and suggestion.

    I have adjusted "0" to "2", but still I am not getting any nRF timer event.

    I have tried Zephyr kernel timers (based on the 32k RTC) and it is working as expected and I am receiving timer event handler. Thanks for the suggestion.

    Regards,

    Kalyan

Related