# Interrupt an active timer

**URL:** <https://forums.freertos.org/t/interrupt-an-active-timer/16095>\
**Category:** Libraries\
**Created:** [November 1, 2022, 12:53pm UTC](https://forums.freertos.org/t/interrupt-an-active-timer/16095 "2022-11-01T12:53:12Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lucy781227](https://avatars.discourse-cdn.com/v4/letter/l/45deac/32.png) [@Lucy781227](https://forums.freertos.org/u/Lucy781227)\
**Post date:** [November 1, 2022, 12:53pm UTC](https://forums.freertos.org/t/interrupt-an-active-timer/16095/1 "2022-11-01T12:53:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [November 1, 2022, 1:28pm UTC](https://forums.freertos.org/t/interrupt-an-active-timer/16095/2 "2022-11-01T13:28:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Lucy781227](https://avatars.discourse-cdn.com/v4/letter/l/45deac/32.png) [@Lucy781227](https://forums.freertos.org/u/Lucy781227)\
**Post date:** [November 1, 2022, 2:12pm UTC](https://forums.freertos.org/t/interrupt-an-active-timer/16095/3 "2022-11-01T14:12:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [November 1, 2022, 2:16pm UTC](https://forums.freertos.org/t/interrupt-an-active-timer/16095/4 "2022-11-01T14:16:58Z")

</div>

Why suppose anything ? It’s all documented 🙂

[xTimerStop](https://www.freertos.org/FreeRTOS-timers-xTimerStop.html)() stops a timer that was previously started using either of the [xTimerStart()](https://www.freertos.org/FreeRTOS-timers-xTimerStart.html), [xTimerReset()](https://www.freertos.org/FreeRTOS-timers-xTimerReset.html),[xTimerStartFromISR()](https://www.freertos.org/FreeRTOS-timers-xTimerStartFromISR.html), [xTimerResetFromISR()](https://www.freertos.org/FreeRTOS-timers-xTimerResetFromISR.html), [xTimerChangePeriod()](https://www.freertos.org/FreeRTOS-timers-xTimerChangePeriod.html) and [xTimerChangePeriodFromISR()](https://www.freertos.org/FreeRTOS-timers-xTimerChangePeriodFromISR.html) API functions.

---

<div class="post-metadata">

**Author:** ![Lucy781227](https://avatars.discourse-cdn.com/v4/letter/l/45deac/32.png) [@Lucy781227](https://forums.freertos.org/u/Lucy781227)\
**Post date:** [November 1, 2022, 3:00pm UTC](https://forums.freertos.org/t/interrupt-an-active-timer/16095/5 "2022-11-01T15:00:58Z")

</div>

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() {}

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [November 1, 2022, 3:21pm UTC](https://forums.freertos.org/t/interrupt-an-active-timer/16095/6 "2022-11-01T15:21:01Z")

</div>

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 ?)

---

<div class="post-metadata">

**Author:** ![Lucy781227](https://avatars.discourse-cdn.com/v4/letter/l/45deac/32.png) [@Lucy781227](https://forums.freertos.org/u/Lucy781227)\
**Post date:** [November 1, 2022, 3:30pm UTC](https://forums.freertos.org/t/interrupt-an-active-timer/16095/7 "2022-11-01T15:30:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Lucy781227](https://avatars.discourse-cdn.com/v4/letter/l/45deac/32.png) [@Lucy781227](https://forums.freertos.org/u/Lucy781227)\
**Post date:** [November 1, 2022, 3:31pm UTC](https://forums.freertos.org/t/interrupt-an-active-timer/16095/8 "2022-11-01T15:31:34Z")

</div>

> [@hs2](#):
>
> I mean did you check that the timer was successfully created and started along with the scheduler ?

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

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [November 1, 2022, 3:43pm UTC](https://forums.freertos.org/t/interrupt-an-active-timer/16095/9 "2022-11-01T15:43:02Z")

</div>

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 😉  
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.

---

<div class="post-metadata">

**Author:** ![Lucy781227](https://avatars.discourse-cdn.com/v4/letter/l/45deac/32.png) [@Lucy781227](https://forums.freertos.org/u/Lucy781227)\
**Post date:** [November 1, 2022, 10:28pm UTC](https://forums.freertos.org/t/interrupt-an-active-timer/16095/10 "2022-11-01T22:28:46Z")

</div>

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() {}

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [November 2, 2022, 7:00am UTC](https://forums.freertos.org/t/interrupt-an-active-timer/16095/11 "2022-11-02T07:00:27Z")

</div>

`vTaskStartScheduler` doesn’t return.

> **[The FreeRTOS vTaskStartScheduler() RTOS API function which is part of the...](https://www.freertos.org/a00132.html)**
>
> FreeRTOS is an open source, small footprint RTOS for microcontrollers. This page describes the vTaskStartScheduler() FreeRTOS API function which is part of the RTOS scheduler control API.

and

> [@Lucy781227](#):
>
> xTimerStopped = xTimerStart(myTimer, 0);

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 👍  
Good luck !

---

<div class="post-metadata">

**Author:** ![Lucy781227](https://avatars.discourse-cdn.com/v4/letter/l/45deac/32.png) [@Lucy781227](https://forums.freertos.org/u/Lucy781227)\
**Post date:** [November 2, 2022, 8:55am UTC](https://forums.freertos.org/t/interrupt-an-active-timer/16095/12 "2022-11-02T08:55:42Z")

</div>

> [@Lucy781227](#):
>
> xTimerStopped = xTimerStart(myTimer, 0);

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

---

<div class="post-metadata">

**Author:** ![Lucy781227](https://avatars.discourse-cdn.com/v4/letter/l/45deac/32.png) [@Lucy781227](https://forums.freertos.org/u/Lucy781227)\
**Post date:** [November 2, 2022, 8:56am UTC](https://forums.freertos.org/t/interrupt-an-active-timer/16095/13 "2022-11-02T08:56:01Z")

</div>

> [@hs2](#):
>
> `vTaskStartScheduler` doesn’t return.

Why do I need to check this?

---

<div class="post-metadata">

**Author:** ![Lucy781227](https://avatars.discourse-cdn.com/v4/letter/l/45deac/32.png) [@Lucy781227](https://forums.freertos.org/u/Lucy781227)\
**Post date:** [November 2, 2022, 12:45pm UTC](https://forums.freertos.org/t/interrupt-an-active-timer/16095/14 "2022-11-02T12:45:21Z")

</div>

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](https://www.freertos.org/a00111.html) available to create the idle or timer daemon tasks.
