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

Parents
  • 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]

Reply
  • 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]

Children
No Data
Related