Interrupt an active timer

Hi. Can I stop a timer for a time less than xTimerPeriodInTicks?

What about xTimerStop ? But I suppose you already know the provided timer API…
So what exactly do you really want to do ?

I suppose that when API xTimeStart() is called, no more code will be executed until timer is time out.

Why suppose anything ? It’s all documented :slightly_smiling_face:

xTimerStop() stops a timer that was previously started using either of the xTimerStart(), xTimerReset(),xTimerStartFromISR(), xTimerResetFromISR(), xTimerChangePeriod() and xTimerChangePeriodFromISR() API functions.

Thank you. I created a very simple example for using timer. But it seems that the call back function is never called.

#include <Arduino_FreeRTOS.h>
#include <timers.h>
#define mainAUTO_RELOAD_TIMER_PERIOD pdMS_TO_TICKS( 100 )
TimerHandle_t myTimer;
BaseType_t xTimerStarted;
BaseType_t xTimerStopped;
void setup() {
myTimer = xTimerCreate(“MyTimer”,
mainAUTO_RELOAD_TIMER_PERIOD,
pdFALSE,
0,
prvTimerCallback);
if (myTimer != NULL)
{
xTimerStarted = xTimerStart(myTimer, 0);
if (xTimerStarted == pdPASS) vTaskStartScheduler();
}
}

static void prvTimerCallback(TimerHandle_t myTimer)
{
Serial.println(“Time out.”);
}

void loop() {}

The code looks ok.
Did you stepped through the code to see why ?
I mean did you check that the timer was successfully created and started along with the scheduler ?
Did you put a breakpoint into the callback to verify that the callback is not invoked ?
(Is Serial.println thread-safe and can be called from multiple tasks concurrently ?)

I dont know what I should use instead of Serial.println. But while learning FreeRTOS, I also realized that there are some problems with this API.

I used Serial.println. It’s not printed. There is only a letter ‘T’ on the call back function printed.

Well, as opposed to your statement that the callback wasn’t invoked at all it means that the timer callback WAS invoked if something was printed, right ?
Do you have a debugger attached to step through the code, setting breakpoints, etc ? Developing application with just ‘print’ will be a loooong way to go :wink:
As you’ve possibly read in the docs be aware that configTIMER_TASK_STACK_DEPTH must cover the stack used by the callbacks.
print functions are notoriously stack hungry so maybe the timer daemon stack isn’t large enough.
I’d avoid using print in the callback. Can’t you switch on a GPIO or an LED as an initial, basic test ? Or better just use the debugger.

I use LED in call back function and the timer looks doing well. Then I add a xTimerStop() to check that the timer stops at the time I want. But it seems timer doesn’t stop.

#include <Arduino_FreeRTOS.h>
#include <timers.h>
#define mainAUTO_RELOAD_TIMER_PERIOD pdMS_TO_TICKS( 5000 )
TimerHandle_t myTimer;
BaseType_t xTimerStarted;
BaseType_t xTimerStopped;
int key = 1;
void setup() {
pinMode(13, OUTPUT);
pinMode(7, OUTPUT);
myTimer = xTimerCreate(“MyTimer”,
mainAUTO_RELOAD_TIMER_PERIOD,
pdFALSE,
0,
prvTimerCallback);
if (myTimer != NULL)
{
xTimerStarted = xTimerStart(myTimer, 0);
if (xTimerStarted == pdPASS)
vTaskStartScheduler();
if (key == 1)
{
xTimerStopped = xTimerStart(myTimer, 0);
if (xTimerStopped == pdPASS) digitalWrite(7, HIGH);
}
}
}

static void prvTimerCallback(TimerHandle_t myTimer)
{

digitalWrite(13, HIGH);
}

void loop() {}

vTaskStartScheduler doesn’t return.

and

wouldn’t stop a timer…
Try to fully understand what you’re doing and coding and make use of the documentation already recommended in the other post to get more familiar.
And use a debugger to be able to step through your code yourself to see what’s going on :+1:
Good luck !

This is my fault. It’s xTimerStop not Start.

Why do I need to check this?

As I researched, the callback function is executed by Daemon task. Daemon task is automatically created when the scheduler started. vTaskStartScheduler() will only return if there is insufficient RTOS heap available to create the idle or timer daemon tasks.