# Am I at interrupt level?

**URL:** <https://forums.freertos.org/t/am-i-at-interrupt-level/10852>\
**Category:** Kernel\
**Created:** [October 7, 2020, 7:33pm UTC](https://forums.freertos.org/t/am-i-at-interrupt-level/10852 "2020-10-07T19:33:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rdpoor](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rdpoor/32/929_2.png) [@rdpoor](https://forums.freertos.org/u/rdpoor)\
**Post date:** [October 7, 2020, 7:33pm UTC](https://forums.freertos.org/t/am-i-at-interrupt-level/10852/1 "2020-10-07T19:33:10Z")

</div>

I’m not suggesting this is good design, but:

Assume I’ve received a callback. It could have been from an ISR _or_ from an xTimer expiration.

If I’m inside an ISR, I need to call `xTaskNotifyFromISR()`. But if it’s an xTimer notification, I believe I need to call `xTaskNotify()`.

So: is there a way to determine dynamically if I need to call `xTaskNotifyFromISR()` vs `xTaskNotify()`? Alternatively, do bad things happen if I call `xTaskNotifyFromISR()` from within an xTimer callback?

(To repeat myself: I’m not defending this as good design. But it could simplify the code design in this specific case…)

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [October 7, 2020, 7:50pm UTC](https://forums.freertos.org/t/am-i-at-interrupt-level/10852/2 "2020-10-07T19:50:37Z")

</div>

Knowing if you are in an interrupt or not is a port specific thing. Some ports provide macros that make it easier - such as [https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/master/portable/GCC/ARM\_CM4F/portmacro.h#L171](https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/master/portable/GCC/ARM_CM4F/portmacro.h#L171) - gut calling the ‘FromISR’ versions from tasks is fine too.

---

<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:** [October 7, 2020, 7:58pm UTC](https://forums.freertos.org/t/am-i-at-interrupt-level/10852/3 "2020-10-07T19:58:11Z")

</div>

For Cortex-M MCUs there is a Processor Status Register `IPSR `telling the current IRQ number which could be checked in the associated callback.  
I think I’d go this way because it’s HW managed and inherently race free (if I’d have to stick to this unfavorable design).

---

<div class="post-metadata">

**Author:** ![rdpoor](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rdpoor/32/929_2.png) [@rdpoor](https://forums.freertos.org/u/rdpoor)\
**Post date:** [October 7, 2020, 7:59pm UTC](https://forums.freertos.org/t/am-i-at-interrupt-level/10852/4 "2020-10-07T19:59:32Z")

</div>

> [@rtel](#):
>
> gut calling the ‘FromISR’ versions from tasks is fine too.

Do you mean that it’s permissable to call the `FromISR` version when I’m not in an ISR?

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [October 7, 2020, 8:02pm UTC](https://forums.freertos.org/t/am-i-at-interrupt-level/10852/5 "2020-10-07T20:02:43Z")

</div>

Answer actually depends on the port you are using. Generally, if the port you use supports interrupt nesting (such as Cortex-M, RX, PIC32, etc.) then you can call the FromISR versions from tasks. The opposite is not true - you cannot call the standard versions from interrupts. Maybe this information needs to be added to [https://www.freertos.org/FAQ\_API.html#IQRAPI](https://www.freertos.org/FAQ_API.html#IQRAPI)

---

<div class="post-metadata">

**Author:** ![rdpoor](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rdpoor/32/929_2.png) [@rdpoor](https://forums.freertos.org/u/rdpoor)\
**Post date:** [October 7, 2020, 8:19pm UTC](https://forums.freertos.org/t/am-i-at-interrupt-level/10852/6 "2020-10-07T20:19:23Z")

</div>

That’s great news! 👍

In my case, it’s Cortex-M and it’s all user-level interactions, so speed is not an issue.

And yes, adding a paragraph to the FAQ could be handy! 🙂

Many thanks.
