# FreeRTOS Interrupt Priorities vs. MCU Interrupt Priorities

**URL:** <https://forums.freertos.org/t/freertos-interrupt-priorities-vs-mcu-interrupt-priorities/16345>\
**Category:** Kernel\
**Created:** [December 8, 2022, 6:39am UTC](https://forums.freertos.org/t/freertos-interrupt-priorities-vs-mcu-interrupt-priorities/16345 "2022-12-08T06:39:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![migmel](https://avatars.discourse-cdn.com/v4/letter/m/4da419/32.png) [@migmel](https://forums.freertos.org/u/migmel)\
**Post date:** [December 8, 2022, 6:39am UTC](https://forums.freertos.org/t/freertos-interrupt-priorities-vs-mcu-interrupt-priorities/16345/1 "2022-12-08T06:39:37Z")

</div>

Hello guys, I am new to FreeRTOS, and for the time being I have a big confusion about interrupt priorities. Here is the issue:  
FreeRTOS interrupt priorities are numerically proportional to the priority level. Example: 5 has a lower priority than 95. On the other hand, MCU priorities are the opposite, i.e. 0 has a higher priority than 15. Until now everything is fine.  
Now I’m reading this Richard Barry’s authored book: **“Using the FreeRTOS Real Time Kernel”** from where I took this graphic:  
 ![image](https://d2v6vdsk2p900z.cloudfront.net/original/2X/2/2a2336fb9ef92ef9a04657ca759eacaf687131c1.png)

Now, I am looking at the FreeRTOSConfig.h header, from where I copied this part:  
\*\*  
/\* Use the system definition, if there is one _/  
 #ifdef \_\_NVIC\_PRIO\_BITS  
 #define configPRIO\_BITS \_\_NVIC\_PRIO\_BITS  
 #else  
 #define configPRIO\_BITS 4 /_ 15 priority levels \*/  
#endif

#define configLIBRARY\_LOWEST\_INTERRUPT\_PRIORITY 15  
#define configLIBRARY\_MAX\_SYSCALL\_INTERRUPT\_PRIORITY 5

/\* The lowest priority. _/  
 #define configKERNEL\_INTERRUPT\_PRIORITY ( configLIBRARY\_LOWEST\_INTERRUPT\_PRIORITY \<\< (8 - configPRIO\_BITS) )  
/_ Priority 5, or 95 as only the top four bits are implemented. _/  
/_ !!! configMAX\_SYSCALL\_INTERRUPT\_PRIORITY must not be set to zero !!!  
See [RTOS for ARM Cortex-M](http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html). \*/  
#define configMAX\_SYSCALL\_INTERRUPT\_PRIORITY ( configLIBRARY\_MAX\_SYSCALL\_INTERRUPT\_PRIORITY \<\< (8 - configPRIO\_BITS) )  
\*\*  
According to the book I am reading, if I want to use Interrupt safe API functions and nest them, I should use interrupts between **configKERNEL\_INTERRUPT\_PRIORITY** and **configMAX\_SYSCALL\_INTERRUPT\_PRIORITY** values inclusive.  
Now, in the graphic attached, the reference is MCU priorities, meaning that priority 1 is a higher level than priority 7, which makes the configKERNEL\_INTERRUPT\_PRIORITY higher than configMAX\_SYSCALL\_INTERRUPT\_PRIORITY, which incidentally has a MAX word, meaning that it is the maximum priority (in FreeRTOS reference). So, first question: shouldn’t configMAX\_SYSCALL\_INTERRUPT\_PRIORITY be always numerically higher than configKERNEL\_INTERRUPT\_PRIORITY ? If the answer is yes, shouldn’t the graphic priority assignment be the opposite, that is: configMAX\_SYSCALL\_INTERRUPT\_PRIORITY=1 and configKERNEL\_INTERRUPT\_PRIORITY=3 considering that the reference is MCU priority ?  
Because we have 2 different references, how do I know which is the highest priority? The lower in MCU(0) or the higest in FreeRTOS(95 in this case)? I mean, for the MCU, 0 will be the highest and for FreeRTOS, would be 95 right?  
Coming back to FreeRTOSConfig.h: Is it posible to modify this header, so I can put all the priorities in the same range? (0~15) and within this range plainly assign values to configKERNEL\_INTERRUPT\_PRIORITY and configMAX\_SYSCALL\_INTERRUPT\_PRIORITY so i will have a clear understanding of what is going on?  
I’m sorry for the confusing text, but is the same all inside my head.

---

<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:** [December 8, 2022, 7:38am UTC](https://forums.freertos.org/t/freertos-interrupt-priorities-vs-mcu-interrupt-priorities/16345/2 "2022-12-08T07:38:02Z")

</div>

I’d recommend to also read the FreeRTOS docs regarding [RTOS for ARM Cortex-M](https://www.freertos.org/RTOS-Cortex-M3-M4.html?_ga=2.145622967.284839241.1669971288-1295978378.1669191309) and  
e.g. [Understanding priority levels of ISR and FreeRTOS APIs - #16 by aggarg](https://forums.freertos.org/t/understanding-priority-levels-of-isr-and-freertos-apis/10453/16) which contains a pretty good explanation of this rather confusing topic.  
There are much more posts regarding interrupt priorities in the forum (search: configMAX\_SYSCALL\_INTERRUPT\_PRIORITY).

---

<div class="post-metadata">

**Author:** ![cmh](https://avatars.discourse-cdn.com/v4/letter/c/3da27b/32.png) [@cmh](https://forums.freertos.org/u/cmh)\
**Post date:** [December 8, 2022, 9:17am UTC](https://forums.freertos.org/t/freertos-interrupt-priorities-vs-mcu-interrupt-priorities/16345/3 "2022-12-08T09:17:15Z")

</div>

The assigning of numeric values to abstracted priorities varies according to MCU architecture. The “lower number is higher priority” convention is adopted by ARM Cortex, but I suspect the book was written with “higher number is higher priority” in mind.

---

<div class="post-metadata">

**Author:** ![migmel](https://avatars.discourse-cdn.com/v4/letter/m/4da419/32.png) [@migmel](https://forums.freertos.org/u/migmel)\
**Post date:** [December 8, 2022, 9:22am UTC](https://forums.freertos.org/t/freertos-interrupt-priorities-vs-mcu-interrupt-priorities/16345/4 "2022-12-08T09:22:30Z")

</div>

Yes, I figure it was intended that way

---

<div class="post-metadata">

**Author:** ![kstribrn](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/kstribrn/32/2085_2.png) [@kstribrn](https://forums.freertos.org/u/kstribrn)\
**Post date:** [December 12, 2022, 7:36pm UTC](https://forums.freertos.org/t/freertos-interrupt-priorities-vs-mcu-interrupt-priorities/16345/5 "2022-12-12T19:36:29Z")

</div>

**Question 1 Part 1**

> [@migmel](#):
>
> shouldn’t configMAX\_SYSCALL\_INTERRUPT\_PRIORITY be always numerically higher than configKERNEL\_INTERRUPT\_PRIORITY ?

In this case, yes. This is because in this model (currently also in Chapter 6.8 of _Mastering The FreeRTOS Kernel_) the priority is the logical priority and not the numerical priority.

Below this graphic should be the explanation that…

_The relationship between an interrupt’s numeric priority and logical priority is dependent on the  
processor architecture; on some processors, the higher the numeric priority assigned to an  
interrupt the higher that interrupt’s logical priority will be, while on other processor architectures  
the higher the numeric priority assigned to an interrupt the lower that interrupt’s logical priority  
will be._

**Question 1 Part 2**

> [@migmel](#):
>
> If the answer is yes, shouldn’t the graphic priority assignment be the opposite, that is: configMAX\_SYSCALL\_INTERRUPT\_PRIORITY=1 and configKERNEL\_INTERRUPT\_PRIORITY=3 considering that the reference is MCU priority ?  
> Because we have 2 different references, how do I know which is the highest priority? The lower in MCU(0) or the higest in FreeRTOS(95 in this case)? I mean, for the MCU, 0 will be the highest and for FreeRTOS, would be 95 right?

This diagram, and the explanation that comes with it, is written with the logical priority in mind. Using logical priority in the explanation allows this to apply to any processor. For example, in your case the lower MCU ISR number has the higher priority than a higher numbered ISR. For other chips it may be the other way around. In both of these cases however, the highest logical priority will correspond to either the lower number (if lower number is highest priority) OR to the highest number (if highest number is highest priority).

* * *

**Question 2**

> [@migmel](#):
>
> Because we have 2 different references, how do I know which is the highest priority? The lower in MCU(0) or the higest in FreeRTOS(95 in this case)? I mean, for the MCU, 0 will be the highest and for FreeRTOS, would be 95 right?

Interrupt Service Routines (ISRs) will always have a higher priority than any FreeRTOS task. AFAIK the only FreeRTOS task which operates with an ISR priority level is the timer tick, which operates at the lower priority level (also covered in the chapter).

* * *

**Question 3**

> [@migmel](#):
>
> Coming back to FreeRTOSConfig.h: Is it posible to modify this header, so I can put all the priorities in the same range? (0~15) and within this range plainly assign values to configKERNEL\_INTERRUPT\_PRIORITY and configMAX\_SYSCALL\_INTERRUPT\_PRIORITY so i will have a clear understanding of what is going on?

I can see how the _or 95 as only the top four bits are implemented_ can be confusing. I n this case 95 would be the decimal value of the byte (aka 0x5F) after the shift. The lowest priority would have a value of 255 (aka 0xFF) after the shift. This exact scenario is explained on page 232 of [Mastering FreeRTOS](https://freertos.org/Documentation/161204_Mastering_the_FreeRTOS_Real_Time_Kernel-A_Hands-On_Tutorial_Guide.pdf)

This port should already have the priorities in the same range - but feel free to play around with the code.

---

<div class="post-metadata">

**Author:** ![kstribrn](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/kstribrn/32/2085_2.png) [@kstribrn](https://forums.freertos.org/u/kstribrn)\
**Post date:** [December 12, 2022, 7:37pm UTC](https://forums.freertos.org/t/freertos-interrupt-priorities-vs-mcu-interrupt-priorities/16345/6 "2022-12-12T19:37:31Z")

</div>

I’d also very much advise looking into the resources provided by @hs2. The links can help further explain interrupt priorities.

---

<div class="post-metadata">

**Author:** ![migmel](https://avatars.discourse-cdn.com/v4/letter/m/4da419/32.png) [@migmel](https://forums.freertos.org/u/migmel)\
**Post date:** [December 15, 2022, 1:38am UTC](https://forums.freertos.org/t/freertos-interrupt-priorities-vs-mcu-interrupt-priorities/16345/7 "2022-12-15T01:38:45Z")

</div>

Thank you Mr. Kody for the explanation. I guess with time I will get used to this rather confusing subject. For now, I have managed to make it work fine using the **vTaskNotifyGiveFromISR** API. A lot of testing needs to be done though

---

<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:** [December 15, 2022, 4:48am UTC](https://forums.freertos.org/t/freertos-interrupt-priorities-vs-mcu-interrupt-priorities/16345/8 "2022-12-15T04:48:41Z")

</div>

> [@migmel](#):
>
> A lot of testing needs to be done though

If you define configASSERT, all the incorrect priority configurations are caught. Therefore, it is strongly recommended to define configASSERT during development.

---

<div class="post-metadata">

**Author:** ![migmel](https://avatars.discourse-cdn.com/v4/letter/m/4da419/32.png) [@migmel](https://forums.freertos.org/u/migmel)\
**Post date:** [December 15, 2022, 8:15am UTC](https://forums.freertos.org/t/freertos-interrupt-priorities-vs-mcu-interrupt-priorities/16345/9 "2022-12-15T08:15:24Z")

</div>

Thank you Mr. Gaurav for the tip
