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

Using app_timer callback

Hello, I'm trying to do a millisecond counter using app_timer but the callback is never called. I think I have missunderstood how it works. I've tried using the app_timer with or without softdevice, in debug mode and without debug mode, but still the timeout handler is not called.

What am I doing wrong?

Thank you.

My code is as follows:


static void timeout_handler(void * p_context)
{
    //UNUSED_PARAMETER(p_context);
    //battery_start();

    simple_uart_putstring((const uint8_t *)"time!\n\r");
    millis+=20;
}

int main(void)
{
simple_uart_config(RTS_PIN_NUMBER, TX_PIN_NUMBER, CTS_PIN_NUMBER, RX_PIN_NUMBER, HWFC);
simple_uart_putstring((const uint8_t *)"Start using device \n\r");

APP_TIMER_INIT(0, 4, 5, false);

  NRF_CLOCK->TASKS_LFCLKSTART = 1;

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

      
      NRF_CLOCK->TASKS_LFCLKSTART = 1;

      while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0);

        // Create timer
      err_code = app_timer_create(&test_timer_id, APP_TIMER_MODE_REPEATED, timeout_handler);
      APP_ERROR_CHECK(err_code);

      err_code =app_timer_start(test_timer_id, 100, NULL);
      APP_ERROR_CHECK(err_code);

uint8_t sd_is_enabled;
while (true)
{

	    (void)sd_softdevice_is_enabled(&sd_is_enabled);
	    if (sd_is_enabled)
	    {
	        uint32_t err_code = sd_app_event_wait();
	        APP_ERROR_CHECK(err_code);
	    }
	    else
	        __WFI()
}

}


Parents
  • This code is tested on EK board with soft device, the callback from timer is called every 500 ms.

    
    
    #define LED0	 18
    #define LED1	 19
    #define BTN0	 16
    #define BTN1	 17
    
    #define PRESCALER				0
    #define MAX_TIMERS			1
    #define OP_QUEUES_SIZE	4
    #define USE_SCHEDULER		false
    
    #define TIMER_MS				500
    #define TIMER_TICKS			APP_TIMER_TICKS(TIMER_MS, PRESCALER)
    
    
    
    // alloc timer object
    
    static app_timer_id_t			 testTimer;
    
    
    
    static void powerManager(void)
    {
    	 uint32_t error_code;
    	 error_code = sd_app_event_wait();
    	 APP_ERROR_CHECK(error_code);
    }
    
    // Timer callback
    static void timerDidEnd(void *nil)
    {
    	 // blink to say Hello!!
    	 nrf_gpio_pin_toggle(LED0);
    	 nrf_gpio_pin_toggle(LED1);
    }
    
    
    static void bleEventManager(ble_evt_t *stackEvent)
    {
    	 // stack event received
    	 // do nothing
    	 switch (stackEvent->header.evt_id)
    	{
    		 default:
    			break;
    	}
    }
    
    
    int main (void)
    {
    	 uint32_t errorCode;
    	 
    	 // I'm alive, led0 on
    	 GPIO_LED_CONFIG(LED0);
    	 GPIO_LED_CONFIG(LED1);
    	 nrf_gpio_pin_set(LED0);
    	 
    	 
    	 // stack init
    	 BLE_STACK_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM,
    													BLE_L2CAP_MTU_DEF,
    													bleEventManager,
    													false);
    
    	 // init timer module
    	 APP_TIMER_INIT(PRESCALER, MAX_TIMERS, OP_QUEUES_SIZE, USE_SCHEDULER);
    	 
    	 // create a timer
    	 errorCode = app_timer_create(&testTimer,
    																 APP_TIMER_MODE_REPEATED,
    																 timerDidEnd);
    	 
    	 APP_ERROR_CHECK(errorCode);
    	 
    	 // timer starts,
    	 errorCode = app_timer_start(testTimer,
    																TIMER_TICKS,
    																NULL);
    	 APP_ERROR_CHECK(errorCode);
    	 
    	 
    	 for(;;)
    		 {
    			 powerManager();
    	   }
    	 
    	 
    }
    
    
    

    -c

Reply
  • This code is tested on EK board with soft device, the callback from timer is called every 500 ms.

    
    
    #define LED0	 18
    #define LED1	 19
    #define BTN0	 16
    #define BTN1	 17
    
    #define PRESCALER				0
    #define MAX_TIMERS			1
    #define OP_QUEUES_SIZE	4
    #define USE_SCHEDULER		false
    
    #define TIMER_MS				500
    #define TIMER_TICKS			APP_TIMER_TICKS(TIMER_MS, PRESCALER)
    
    
    
    // alloc timer object
    
    static app_timer_id_t			 testTimer;
    
    
    
    static void powerManager(void)
    {
    	 uint32_t error_code;
    	 error_code = sd_app_event_wait();
    	 APP_ERROR_CHECK(error_code);
    }
    
    // Timer callback
    static void timerDidEnd(void *nil)
    {
    	 // blink to say Hello!!
    	 nrf_gpio_pin_toggle(LED0);
    	 nrf_gpio_pin_toggle(LED1);
    }
    
    
    static void bleEventManager(ble_evt_t *stackEvent)
    {
    	 // stack event received
    	 // do nothing
    	 switch (stackEvent->header.evt_id)
    	{
    		 default:
    			break;
    	}
    }
    
    
    int main (void)
    {
    	 uint32_t errorCode;
    	 
    	 // I'm alive, led0 on
    	 GPIO_LED_CONFIG(LED0);
    	 GPIO_LED_CONFIG(LED1);
    	 nrf_gpio_pin_set(LED0);
    	 
    	 
    	 // stack init
    	 BLE_STACK_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM,
    													BLE_L2CAP_MTU_DEF,
    													bleEventManager,
    													false);
    
    	 // init timer module
    	 APP_TIMER_INIT(PRESCALER, MAX_TIMERS, OP_QUEUES_SIZE, USE_SCHEDULER);
    	 
    	 // create a timer
    	 errorCode = app_timer_create(&testTimer,
    																 APP_TIMER_MODE_REPEATED,
    																 timerDidEnd);
    	 
    	 APP_ERROR_CHECK(errorCode);
    	 
    	 // timer starts,
    	 errorCode = app_timer_start(testTimer,
    																TIMER_TICKS,
    																NULL);
    	 APP_ERROR_CHECK(errorCode);
    	 
    	 
    	 for(;;)
    		 {
    			 powerManager();
    	   }
    	 
    	 
    }
    
    
    

    -c

Children
Related