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

Can't make app_timer work

I have following code which should start the app_timer and blink the LED_1.


#include "config.h"

#include "nrf_pwr_mgmt.h"

#include "boards.h"
#include "bsp.h"
#include "app_error.h"
#include "app_timer.h"

#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

static uint16_t leds_timer_counter = 0;

static void leds_tick_handler(void * p_context) {
  NRF_LOG_INFO("timer.");
  if (leds_timer_counter++ % 8 == 0) bsp_board_led_invert(BSP_BOARD_LED_1);
}

int main(void) {
  APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
  NRF_LOG_DEFAULT_BACKENDS_INIT();

  APP_ERROR_CHECK(app_timer_init());
  APP_TIMER_DEF(m_leds_tick_timer);
  APP_ERROR_CHECK(app_timer_create(&m_leds_tick_timer, APP_TIMER_MODE_REPEATED, leds_tick_handler));
  APP_ERROR_CHECK(app_timer_start(m_leds_tick_timer, APP_TIMER_TICKS(500), NULL));
  
  APP_ERROR_CHECK(nrf_pwr_mgmt_init());

  bsp_board_leds_init();
  bsp_board_led_on(BSP_BOARD_LED_0);

  NRF_LOG_INFO("Started.");
  
  while (1) { 
    NRF_LOG_FLUSH();
    nrf_pwr_mgmt_run();
  }
}

When flashed, the LED_0 turns on and I can see the "Started." log. But the LED_1 is not blinking and I can't see the "timer." log.

Any idea what's wrong with the code?

Thanks.

Parents
  • Hello,

    RTC does not request the low frequency clock automatically, so it needs to be started by the app prior to calling app_timer_start(). This is typically done by the softdevice or with our clock driver.

    Please try to add the following code at the beginning of main and see if you get the same result:

    int main(void)
    {

          NRF_CLOCK->TASKS_LFCLKSTART = 1;
          while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0);

           ...

Reply
  • Hello,

    RTC does not request the low frequency clock automatically, so it needs to be started by the app prior to calling app_timer_start(). This is typically done by the softdevice or with our clock driver.

    Please try to add the following code at the beginning of main and see if you get the same result:

    int main(void)
    {

          NRF_CLOCK->TASKS_LFCLKSTART = 1;
          while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0);

           ...

Children
Related