# vPortValidateInterruptPriority() bug or feature?

**URL:** <https://forums.freertos.org/t/vportvalidateinterruptpriority-bug-or-feature/4120>\
**Category:** Kernel\
**Created:** [January 18, 2015, 7:31pm UTC](https://forums.freertos.org/t/vportvalidateinterruptpriority-bug-or-feature/4120 "2015-01-18T19:31:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 18, 2015, 7:31pm UTC](https://forums.freertos.org/t/vportvalidateinterruptpriority-bug-or-feature/4120/1 "2015-01-18T19:31:04Z")

</div>

**[stephanhd](https://sourceforge.net/u/stephanhd/)** wrote on [Sunday, January 18, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/b6702230/#b7ce):

printf(“hello world!”)

I am just on 8.2.0 (FreeRTOSV8.2.0\FreeRTOS\Source\portable\IAR\ARM\_CM3) port.

I wonder if the following code really works or if i had to continue studying…

```

// (port.c)
vPortValidateInterruptPriority( void ){
	uint32_t ulCurrentInterrupt;
	uint8_t ucCurrentPriority;
	/* Obtain the number of the currently executing interrupt. */
	__asm volatile( "mrs %0, ipsr" : "=r"( ulCurrentInterrupt ) );

	/* Is the interrupt number a user defined interrupt? */
	if( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER ){
		/* Look up the interrupt's priority. */
                    //ucCurrentPriority = pcInterruptPriorityRegisters[ulCurrentInterrupt];

[…]

```

Since **pcInterruptPriorityRegisters** is pointing to the very beginning of the NVIC\_IP table at [_#define portNVIC\_IP\_REGISTERS\_OFFSET\_16 ( 0xE000E3F0 )_], **pcInterruptPriorityRegisters[ulCurrentInterrupt]** will in my opinion point to the wrong location (by 16 Byte to short).

Using this small modification:

```

ucCurrentPriority = pcInterruptPriorityRegisters[portFIRST_USER_INTERRUPT_NUMBER + ulCurrentInterrupt];

```

_ucCurrentPriority_ will be derived from the correct IRQn.

What do you think?

Kind regards, Stephan

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 18, 2015, 8:33pm UTC](https://forums.freertos.org/t/vportvalidateinterruptpriority-bug-or-feature/4120/2 "2015-01-18T20:33:53Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Sunday, January 18, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/b6702230/#348e):

I was sure the code is right because I tested it when it first appeared in FreeRTOS, and I dont think it has changed since. But you made me doubt myself so I looked again.

I think you have made a mistake here

> Since pcInterruptPriorityRegisters is pointing to the very beginning of the  
> NVIC\_IP table at [#define portNVIC\_IP\_REGISTERS\_OFFSET\_16 ( 0xE000E3F0 )],

The table begins at 0xE000E400 so the OFFSET\_16 refers to a negative 16 from the start of the table. pcInterruptPriorityRegisters is not pointing to the very beginning of the NVIC\_IP table it is pointing 16 bytes behind the beginning of the table.

But if you thought pcInterruptPriorityRegisters pointed to the start of the table I dont see why your modification is adding portFIRST\_USER\_INTERRUPT\_NUMBER to ulCurrentInterrupt rather than subtracting portFIRST\_USER\_INTERRUPT\_NUMBER from ulCurrentInterrupt?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 18, 2015, 10:14pm UTC](https://forums.freertos.org/t/vportvalidateinterruptpriority-bug-or-feature/4120/3 "2015-01-18T22:14:40Z")

</div>

**[stephanhd](https://sourceforge.net/u/stephanhd/)** wrote on [Sunday, January 18, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/b6702230/#cd99):

Hi Dave and thanks for taking care!

please let me explain how I came to this point: (using IAR EWARM; Cortex M4 - AT91SAM4S)

1.) NVIC\_SetPriority(28, 11); // set somewhere at start; value visible in NVICIP29  
2.) then PIOB\_Irq is fired  
3.) and PIOB\_IrqHandler is called  
4.) some MessageProcessing is done and via xQueueSendToFrontFromISR() …  
5.) … in vPortValidateInterruptPriority():

```
   __asm volatile( "mrs %0, ipsr" : "=r"( ulCurrentInterrupt ) );

ulCurrentInterrupt is derived as 28 (0x1C). 
Perfect as it fits to __vector_table[28] == PIOB_IrqHandler() :)

```

6.) Status at this point in time:  
\* 0xE000E3F0: it contains (uint8\_t)0x00  
\* 0xE000E400: it contains (uint8\_t)0x00  
\* 0xE000E40C: it contains (uint8\_t)0x00 (this is pcInterruptPriorityRegisters[ulCurrentInterrupt])

**But:  
\* 0xE000E41C: contains (uint8\_t)176 (0xB0) // (11 \<\< 4) from (step 1.)**  
   
and this is exactely: 0xE000E3F0 plus 16 + 28 == 0xE000E41C  
pcInterruptPriorityRegisters[portFIRST\_USER\_INTERRUPT\_NUMBER + ulCurrentInterrupt]

This is as I could see it in the debugger …

Indeed: if portNVIC\_IP\_REGISTERS\_OFFSET\_16 **would not be** pointing to 0xE000E3F0, but to 0xE000E400 (the real start of the NVIC\_IP) then pcInterruptPriorityRegisters[ulCurrentInterrupt] would be fine as well.

Maybe you can help me to get the knot out of my brain? 😉  
Kind regards, Stephan

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 18, 2015, 11:02pm UTC](https://forums.freertos.org/t/vportvalidateinterruptpriority-bug-or-feature/4120/4 "2015-01-18T23:02:49Z")

</div>

**[stephanhd](https://sourceforge.net/u/stephanhd/)** wrote on [Sunday, January 18, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/b6702230/#cd99/b41c):

I found it by myself. Finally it was too simple 🙂 shame on me 😉

I made the mistake to set the idx of the \_\_vector\_table[] equal to the numbering of the specific IRQs like PIOB\_IRQn; which is 12 and thats \_\_vector\_table[16 + 12 == 28].

So setting NVIC\_SetPriority(28, 11) is bullShit as it set the NVIC\_IP for IRQ28 which is not equal PIOB\_IRQn…  
Use NVIC\_SetPriority((IRQn\_Type)PIOB\_IRQn, 11) instead…

Ok sessions learned; thanks a lot and good night!  
Kind regards, Stephan

p.s.  
FreeRTOS is a really cool thing!
