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

Timer handler does not get called

I am trying to implement a timer using nrf_drv_timer.h and nrf_timer.h, however, the handler never gets called.

I first create a timer, then define a timer handler and finally write the init function that also enables the timer.

static const nrf_drv_timer_t m_timer = NRF_DRV_TIMER_INSTANCE(0);

static void timer_handler(nrf_timer_event_t event_type, void * p_context)
{
    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "In timer handler\n");
}

typedef enum
{
    APP_SIMPLE_TIMER_MODE_SINGLE_SHOT,   /**< The timer will expire only once. */
    APP_SIMPLE_TIMER_MODE_REPEATED       /**< The timer will restart each time it expires. */
} app_simple_timer_mode_t;

void timer_init(void) {
    ret_code_t err_code;

    nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
    timer_cfg.bit_width = NRF_TIMER_BIT_WIDTH_32;
    timer_cfg.mode = NRF_TIMER_MODE_TIMER; ///
    timer_cfg.frequency = (nrf_timer_frequency_t)4;
    err_code = nrf_drv_timer_init(&m_timer, &timer_cfg, timer_handler);
    APP_ERROR_CHECK(err_code);

       if (NRF_SUCCESS == err_code)
    {
        __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "SUCCESS\n");
    }
    else {
        __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "NO\n");
    }

    /* setup m_timer for compare event every 400ms */
    uint32_t ticks = nrf_drv_timer_ms_to_ticks(&m_timer, 400);
    nrf_drv_timer_extended_compare(&m_timer,
                                   NRF_TIMER_CC_CHANNEL0,
                                   ticks,
                                   NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK,
                                   true);
    nrf_drv_timer_enable(&m_timer);
}

Any suggestions?

Parents Reply Children
No Data
Related