cancel
Showing results for 
Search instead for 
Did you mean: 

STM32H725 NVIC priority and subprioririty

Aleks
Associate III

Hi!

I try to set IRQ priority to 3 and subpriority to 2.

HAL_NVIC_SetPriority(EXTI9_5_IRQn, 3, 2);

I suppose that correspond NVIC IPR [23] now is 0x32. But it is 0x30. 

I read AIRCR PRIGROUP (it is 3).

uint32_t PriorityGroup = NVIC_GetPriorityGrouping();

According PM0253 if PRIGROUP == 3, than 

Binary point is bxxxx.yyyy,

Number of group priorities = 16,

Number of subpriorities = 16

Futhermore I try to use with different input parameters and never get expected results.

PreemptSubPrior = NVIC_EncodePriority(PriorityGroup, PreemptPriority, SubPriority);

So, so...

I am totaly confused with source code of HAL and CMSIS ...

NVIC_SetPriority(IRQn, NVIC_EncodePriority(prioritygroup, PreemptPriority, SubPriority));

__STATIC_INLINE uint32_t NVIC_EncodePriority (uint32_t PriorityGroup, uint32_t PreemptPriority, uint32_t SubPriority)
 
{
 uint32_t PriorityGroupTmp = (PriorityGroup & (uint32_t)0x07UL);  /* only values 0..7 are used     */
 uint32_t PreemptPriorityBits;
 uint32_t SubPriorityBits;
 
 PreemptPriorityBits = ((7UL - PriorityGroupTmp) > (uint32_t)(__NVIC_PRIO_BITS)) ? (uint32_t)(__NVIC_PRIO_BITS) : (uint32_t)(7UL - PriorityGroupTmp);
 SubPriorityBits   = ((PriorityGroupTmp + (uint32_t)(__NVIC_PRIO_BITS)) < (uint32_t)7UL) ? (uint32_t)0UL : (uint32_t)((PriorityGroupTmp - 7UL) + (uint32_t)(__NVIC_PRIO_BITS));
 return (
      ((PreemptPriority & (uint32_t)((1UL << (PreemptPriorityBits)) - 1UL)) << SubPriorityBits) |
      ((SubPriority   & (uint32_t)((1UL << (SubPriorityBits  )) - 1UL)))
     );
}

According this code it is not possible to set up Priority = 3 and Subpriority to 2. For PriorityGroup = 3, __NVIC_PRIO_BITS = 4.

Than "Encoded" proirity shifted left in SetPriority func. So I get 0x30 not 0x32.

__STATIC_INLINE void __NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority)
{
  if ((int32_t)(IRQn) >= 0)
  {
    NVIC->IP[((uint32_t)IRQn)]                = (uint8_t)((priority << (8U - __NVIC_PRIO_BITS)) & (uint32_t)0xFFUL);
  }
  else
  {
    SCB->SHPR[(((uint32_t)IRQn) & 0xFUL)-4UL] = (uint8_t)((priority << (8U - __NVIC_PRIO_BITS)) & (uint32_t)0xFFUL);
  }
}

Please give some additional explaination about priority and subpriority in STM32H725.

1 ACCEPTED SOLUTION

Accepted Solutions
gbm
Lead III

First, the real need for subpriorities is a very rare case in real world.

The TOTAL number of priority bits implemented is 4. Normally one uses 4 main priority bits and no subpriorities. If you use 3 bits for main priority ("grouping") then one bit is left for subpriority.

Generally: avoid differentiating priorities and using subpriorities unless you are sure you need it.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice

View solution in original post

2 REPLIES 2
gbm
Lead III

First, the real need for subpriorities is a very rare case in real world.

The TOTAL number of priority bits implemented is 4. Normally one uses 4 main priority bits and no subpriorities. If you use 3 bits for main priority ("grouping") then one bit is left for subpriority.

Generally: avoid differentiating priorities and using subpriorities unless you are sure you need it.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
Aleks
Associate III

Thank a lot, I read similar advice https://community.arm.com/arm-community-blogs/b/embedded-blog/posts/cutting-through-the-confusion-with-arm-cortex-m-interrupt-priorities But I try to understand step by step and to write portable code.

So I need not to use HAL_NVIC_SetPriority(EXTI9_5_IRQn, 3, 2); to set IPR to 0x30. Better use 0x3 << 4. (The same trick used in the source code that I support).

In other words this MCU has only 4 MSB bits for priority&subpriority, 4 LSBs bits is not implemented and must not be used in the 8bit IPR registers. If I need it is possible to set 2 bits for priority and 2 for subpriority (default is 4 bit for priority).

Kind regards Aleks.