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

Timer scheduler

Hello: I have detected a problem with a timer on my program. I can solve it, but I would like to understand what is happening exactly.

My program starts to scan BLE signal, and, if it find a known advertising signal, it starts a timer inside the ble_event_handler function. If the nrf detects again the signal, it stops the timer, and starts it again (reset the timer to initial value). If the nrf doesn't detect a new known advertising signal, the timer expires, and go to interruption.

I have init the timer:

 uint32_t err_code;
// Initialize timer module
APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);

//Create a general timer
err_code = app_timer_create(&general_timer_id, APP_TIMER_MODE_SINGLE_SHOT,  general_timer_handler);
APP_ERROR_CHECK(err_code);

And inside the ble_event_handler function I have the following code:

static void on_ble_evt(ble_evt_t * p_ble_evt)
{
....
			err_code = app_timer_stop(general_timer_id);
			APP_ERROR_CHECK (err_code);

			err_code = app_timer_start(general_timer_id, APP_TIMER_SCAN ,NULL);
			APP_ERROR_CHECK (err_code);
....
}

My problem is that sometimes, it gives me an error of full queue (error 04), and it is because Im acumulating more instructions than APP_TIMER_OP_QUEUE_SIZE (2, on my code).

If I make APP_TIMER_OP_QUEUE_SIZE great than 2, it solves the problem, but I have some questions:

  • Can I restart the timer, or have I to stop and start it?

If the APP_TIMER_OP_QUEUE_SIZE is 1, when I stop timer (first instruction), it goes ok, but when I start timer (second instruction), I get the error 04. If I stop the timer, the queue must be 0 when the instruction has been done, why get I the error 04 on the next instruction? Is it because it is executing inside the ble_event_handler? Is a problem of priorities?

I don't know if you have enough information to answer this question, but... why, despite config APP_TIMER_OP_QUEUE_SIZE to 2, I get the error 04 sometimes?

How can I know how many tasks are on the queue of the timer?

Thank you.

Parents
  • Hi.

    From the app_timer documentation:

    When calling app_timer_start() or app_timer_stop(), the timer operation is just queued, and the software interrupt is triggered. The actual timer start/stop operation is executed by the SWI0 interrupt handler. Since the SWI0 interrupt is running in APP_LOW, if the application code calling the timer function is running in APP_LOW or APP_HIGH, the timer operation will not be performed until the application handler has returned.

    This means that both the call to app_timer_stop and app_timer_start is queued, and then executed when you exit the on_ble_evt function.

    If you get the error with queue size = 2, it is probably because an app_timer is being used somewhere else in your code.

  • There is no API call for checking how full the queue is. You can always initialize the queue with a slightly higher number than what you know you will need. This will of course impact you RAM usage a bit. You can check if the queue is full by checking if app_timer_start/stop returns NRF_ERROR_NO_MEM (0x4).

Reply Children
No Data
Related