# Detect calling context (ISR or task)

**URL:** <https://forums.freertos.org/t/detect-calling-context-isr-or-task/14939>\
**Category:** Kernel\
**Tags:** debug\
**Created:** [April 28, 2022, 5:38am UTC](https://forums.freertos.org/t/detect-calling-context-isr-or-task/14939 "2022-04-28T05:38:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![CharlesBl](https://avatars.discourse-cdn.com/v4/letter/c/f19dbf/32.png) [@CharlesBl](https://forums.freertos.org/u/CharlesBl)\
**Post date:** [April 28, 2022, 5:38am UTC](https://forums.freertos.org/t/detect-calling-context-isr-or-task/14939/1 "2022-04-28T05:38:38Z")

</div>

Hello

I need to provide a way to an external library to stop timers.  
The problems is that it call it from interruptions or not.  
In my stopTimer function, that I need to provide, if I’m calling xTimerStopFromISR or xTimerStop in the wrong context, I’m going into an error.

Would you have a solution for this?

Maybe I’m not seeing another way of doing it…

Thank you  
CBL

---

<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:** [April 28, 2022, 6:20am UTC](https://forums.freertos.org/t/detect-calling-context-isr-or-task/14939/2 "2022-04-28T06:20:10Z")

</div>

What if following the FreeRTOS convention and provide `stopTimer` and `stopTimerFromISR` ?

---

<div class="post-metadata">

**Author:** ![CharlesBl](https://avatars.discourse-cdn.com/v4/letter/c/f19dbf/32.png) [@CharlesBl](https://forums.freertos.org/u/CharlesBl)\
**Post date:** [April 28, 2022, 6:43am UTC](https://forums.freertos.org/t/detect-calling-context-isr-or-task/14939/3 "2022-04-28T06:43:22Z")

</div>

Thanks for your answer.  
Sadly, I can’t modify the library that is calling stopTimer

---

<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:** [April 28, 2022, 7:42am UTC](https://forums.freertos.org/t/detect-calling-context-isr-or-task/14939/4 "2022-04-28T07:42:48Z")

</div>

Ahh … you’ve to provide a callback with a fixed API…  
To the rescue some ports as Cortex-M3/4 provide `xPortIsInsideInterrupt()` in `portmacro.h` which can be used to detect ISR context. AFAIK some other 3rd party libs use this mechanism to provide generic APIs, too.

---

<div class="post-metadata">

**Author:** ![CharlesBl](https://avatars.discourse-cdn.com/v4/letter/c/f19dbf/32.png) [@CharlesBl](https://forums.freertos.org/u/CharlesBl)\
**Post date:** [April 28, 2022, 9:20am UTC](https://forums.freertos.org/t/detect-calling-context-isr-or-task/14939/5 "2022-04-28T09:20:42Z")

</div>

Exactly…  
Thank you, that should do the job! I am using an STM32L746 so it should be Cortex M4 but I can’t find it in my portmacro.h. I will dig into this! Thank you for your help!

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [April 28, 2022, 9:57am UTC](https://forums.freertos.org/t/detect-calling-context-isr-or-task/14939/6 "2022-04-28T09:57:55Z")

</div>

> [@CharlesBl](#):
>
> I am using an STM32L746 so it should be Cortex M4 but I can’t find it in my portmacro.h.

Here it is - [https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/portable/GCC/ARM\_CM4F/portmacro.h#L172](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/portable/GCC/ARM_CM4F/portmacro.h#L172)

---

<div class="post-metadata">

**Author:** ![CharlesBl](https://avatars.discourse-cdn.com/v4/letter/c/f19dbf/32.png) [@CharlesBl](https://forums.freertos.org/u/CharlesBl)\
**Post date:** [May 12, 2022, 8:45am UTC](https://forums.freertos.org/t/detect-calling-context-isr-or-task/14939/7 "2022-05-12T08:45:28Z")

</div>

Thank you for your help, I have included it inside my portmacro.h and it works perfectly!

---

<div class="post-metadata">

**Author:** ![simonjwright](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/simonjwright/32/4770_2.png) [@simonjwright](https://forums.freertos.org/u/simonjwright)\
**Post date:** [May 18, 2022, 7:19pm UTC](https://forums.freertos.org/t/detect-calling-context-isr-or-task/14939/8 "2022-05-18T19:19:10Z")

</div>

The identical function could be made available for the M0, if anyone wanted it enough
