# NVIC priority vs FreeRTOS priority

**URL:** <https://forums.freertos.org/t/nvic-priority-vs-freertos-priority/19108>\
**Category:** Kernel\
**Created:** [February 2, 2024, 2:25pm UTC](https://forums.freertos.org/t/nvic-priority-vs-freertos-priority/19108 "2024-02-02T14:25:19Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcastle](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@mcastle](https://forums.freertos.org/u/mcastle)\
**Post date:** [February 2, 2024, 2:25pm UTC](https://forums.freertos.org/t/nvic-priority-vs-freertos-priority/19108/1 "2024-02-02T14:25:20Z")

</div>

Hello,

I am confused about priorities configuration and I am not sure if my configuration is good. In my project I am using LPC1767 microcontroller and set the same priority (7) for my interrupts:

```auto
  NVIC_SetPriority( UART0_IRQn, 7);
  NVIC_SetPriority( UART1_IRQn, 7);
  NVIC_SetPriority( UART2_IRQn, 7);
  NVIC_SetPriority( UART3_IRQn, 7);  
  NVIC_SetPriority( TIMER1_IRQn, 7);
  NVIC_SetPriority( TIMER3_IRQn, 7);
  NVIC_SetPriority( ENET_IRQn, configEMAC_INTERRUPT_PRIORITY); //priority 5

```

Could you please confirm that all my NVIC interrupts have correct priority set according to configMAX\_SYSCALL\_INTERRUPT\_PRIORITY?

If I correctly understand, all my used NVIC interrupts must have lower (higher number for NVIC\_SetPriority()) priority compared to configMAX\_SYSCALL\_INTERRUPT\_PRIORITY.

Part of my FreeRTOSConfig.h:

```auto
#ifdef __NVIC_PRIO_BITS
#define configPRIO_BITS __NVIC_PRIO_BITS
#else
#define configPRIO_BITS 5 /* 32 priority levels */
#endif

/* The lowest priority. */
#define configKERNEL_INTERRUPT_PRIORITY ( 31 << (8 - configPRIO_BITS) )
/* Priority 5, or 160 as only the top three bits are implemented. */
/* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!
See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
#define configMAX_SYSCALL_INTERRUPT_PRIORITY ( 5 << (8 - configPRIO_BITS) )

/* Priorities passed to NVIC_SetPriority() do not require shifting as the
function does the shifting itself. Note these priorities need to be equal to
or lower than configMAX_SYSCALL_INTERRUPT_PRIORITY - therefore the numeric
value needs to be equal to or greater than 5 (on the Cortex-M3 the lower the
numeric value the higher the interrupt priority). */
#define configEMAC_INTERRUPT_PRIORITY 5
#define configUSB_INTERRUPT_PRIORITY 6

/*-----------------------------------------------------------
 * Macros required to setup the timer for the run time stats.
 *-----------------------------------------------------------*/
#ifdef __ICCARM__
#include "LPC17xx.h"
extern void vConfigureTimerForRunTimeStats( void );
#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() vConfigureTimerForRunTimeStats()
#define portGET_RUN_TIME_COUNTER_VALUE() TIM0->TC
#endif

```

---

<div class="post-metadata">

**Author:** ![jefftenney](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jefftenney/32/1314_2.png) [@jefftenney](https://forums.freertos.org/u/jefftenney)\
**Post date:** [February 2, 2024, 5:04pm UTC](https://forums.freertos.org/t/nvic-priority-vs-freertos-priority/19108/2 "2024-02-02T17:04:32Z")

</div>

Looks ok to me. However, the following comment is not correct:

> [@mcastle](#):
>
> `/* Priority 5, or 160 as only the top three bits are implemented. */`

The LPC1767 appears to implement 5 bits (not 3), so interrupt priority 5 becomes 5 \<\< (8-5) or 40.

---

<div class="post-metadata">

**Author:** ![mcastle](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@mcastle](https://forums.freertos.org/u/mcastle)\
**Post date:** [February 2, 2024, 6:59pm UTC](https://forums.freertos.org/t/nvic-priority-vs-freertos-priority/19108/3 "2024-02-02T18:59:14Z")

</div>

Thank you for your answer. I wonder why my program hangs in vListInsert() loop. So it seems this problem does not related to my interrupt priorities.

---

<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:** [February 2, 2024, 7:02pm UTC](https://forums.freertos.org/t/nvic-priority-vs-freertos-priority/19108/4 "2024-02-02T19:02:52Z")

</div>

Does the issue still occur when you correct the priority settings? An incorrect setting will break critical section handling, resulting in corruption (sometimes), which can manifest itself in any number of ways. For example, corrupting a member of a list.

Some years back [I created a page](https://freertos.org/RTOS-Cortex-M3-M4.html) that attempts (?) to explain the priority settings on Cortex-M devices because, at that time, most support requests on those devices were eventually tracked to incorrect priority settings.

---

<div class="post-metadata">

**Author:** ![jefftenney](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jefftenney/32/1314_2.png) [@jefftenney](https://forums.freertos.org/u/jefftenney)\
**Post date:** [February 2, 2024, 8:52pm UTC](https://forums.freertos.org/t/nvic-priority-vs-freertos-priority/19108/5 "2024-02-02T20:52:16Z")

</div>

If you have `configASSERT()` defined to something useful, and if you are running kernel v10 or newer, then `configASSERT()` statements in the Cortex-M3 port will tell you if you have an issue in your interrupt-priority configuration.

One common cause is forgetting to set the priority of an interrupt. By default, the priority is zero (the highest priority, and too high to make FreeRTOS API calls).

---

<div class="post-metadata">

**Author:** ![mcastle](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@mcastle](https://forums.freertos.org/u/mcastle)\
**Post date:** [February 2, 2024, 9:11pm UTC](https://forums.freertos.org/t/nvic-priority-vs-freertos-priority/19108/6 "2024-02-02T21:11:03Z")

</div>

For interrupts that I use in my program (uart, timers and ethernet) I set priority to 7, which is lower than syscall 5. Priorities for these interrups are set at initialization, before NVIC\_Enable. In some cases my program Disable and Enable these interrupts. Do I need to set priorities every time after I Disable interrupt? What about Watchdog and Hardfault interrupts, whose I also enabled, but priority left default and using for debugging?

---

<div class="post-metadata">

**Author:** ![jefftenney](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jefftenney/32/1314_2.png) [@jefftenney](https://forums.freertos.org/u/jefftenney)\
**Post date:** [February 2, 2024, 9:18pm UTC](https://forums.freertos.org/t/nvic-priority-vs-freertos-priority/19108/7 "2024-02-02T21:18:05Z")

</div>

> [@mcastle](#):
>
> Do I need to set priorities every time after I Disable interrupt?

No.

> [@mcastle](#):
>
> What about Watchdog and Hardfault interrupts, whose I also enabled, but priority left default and using for debugging?

No worries about those handlers. They don’t call FreeRTOS APIs.

How is `configASSERT()` defined, and what version of the kernel are you running?

---

<div class="post-metadata">

**Author:** ![mcastle](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@mcastle](https://forums.freertos.org/u/mcastle)\
**Post date:** [February 2, 2024, 9:25pm UTC](https://forums.freertos.org/t/nvic-priority-vs-freertos-priority/19108/8 "2024-02-02T21:25:22Z")

</div>

My version 9… I will think about upgrading to 10.  
Strange is that my program ran without this issue for couple of years… Also I use heap 2 and I have enabled stack overflow checking, but no overflow occurs.

---

<div class="post-metadata">

**Author:** ![mcastle](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@mcastle](https://forums.freertos.org/u/mcastle)\
**Post date:** [February 3, 2024, 9:41am UTC](https://forums.freertos.org/t/nvic-priority-vs-freertos-priority/19108/9 "2024-02-03T09:41:32Z")

</div>

> [@mcastle](#):
>
> ```auto
> #ifdef __NVIC_PRIO_BITS
> #define configPRIO_BITS __NVIC_PRIO_BITS
> #else
> #define configPRIO_BITS 5 /* 32 priority levels */
> #endif
> 
> /* The lowest priority. */
> #define configKERNEL_INTERRUPT_PRIORITY ( 31 << (8 - configPRIO_BITS) )
> /* Priority 5, or 160 as only the top three bits are implemented. */
> /* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!
> See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
> #define configMAX_SYSCALL_INTERRUPT_PRIORITY ( 5 << (8 - configPRIO_BITS) )
> 
> /* Priorities passed to NVIC_SetPriority() do not require shifting as the
> function does the shifting itself. Note these priorities need to be equal to
> or lower than configMAX_SYSCALL_INTERRUPT_PRIORITY - therefore the numeric
> value needs to be equal to or greater than 5 (on the Cortex-M3 the lower the
> numeric value the higher the interrupt priority). */
> #define configEMAC_INTERRUPT_PRIORITY 5
> #define configUSB_INTERRUPT_PRIORITY 6
> 
> ```

If configMAX\_SYSCALL\_INTERRUPT\_PRIORITY after bits shifting result is 40, does it mean my Uart, Timer, Ethernet interrupt priorities have to be set to \>=40?

---

<div class="post-metadata">

**Author:** ![jefftenney](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jefftenney/32/1314_2.png) [@jefftenney](https://forums.freertos.org/u/jefftenney)\
**Post date:** [February 3, 2024, 3:25pm UTC](https://forums.freertos.org/t/nvic-priority-vs-freertos-priority/19108/10 "2024-02-03T15:25:20Z")

</div>

No, your own interrupt priorities are “pre-shift”, as long as you set the priorities with `NVIC_SetPriority()`. Many FreeRTOSConfig.h files include an unshifted version of `configMAX_SYSCALL_INTERRUPT_PRIORITY` as a coding convenience. By convention it is called `configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY`. In your case it would be 5.

---

<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:** [February 5, 2024, 6:43am UTC](https://forums.freertos.org/t/nvic-priority-vs-freertos-priority/19108/11 "2024-02-05T06:43:57Z")

</div>

> [@mcastle](#):
>
> I will think about upgrading to 10.

You can try the latest version 11 too.
