This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to set interrupt priorities in CMSIS-RTOS RTX?

I have a question about the correct setting of the Cortex-M interrupt priorities in CMSIS-RTOS RTX. The CMSIS-RTX online manual (for the latest version 4.78) does not talk about this issue, and in fact, does not explain at all how to write ISRs for CMSIS-RTX.

Correct setting of the Cortex-M interrupt priorities is important, because the CMSIS-RTOS RTX kernel apparently does not disable interrupts explicitly. Instead, the atomic execution of critical sections is implemented as the SVC (Supervisor Call) exception, which runs at priority higher than any task.

Specifically, the priority of the SVC exception is set in the NVIC to the lowest possible level (0xFF), so is the priority of the PendSV and SysTick exceptions. With this setting, the exceptions cannot preempt each other, so their execution is mutually exclusive. So far so good.

But, what about the application-specific interrupts. It seems that they too need to be prioritized in the NVIC at the lowest level (0xFF), because any other setting would allow the IRQs to preempt the SVC/PendSV exceptions and could lead to corruption of the internal RTX state.

Is this observation accurate?

If so, then RTX would essentially defeat the purpose of the NVIC ("nested" interrupt controller), as the interrupts would NOT be allowed to nest.

Second, the CMSIS-RTX users must be made aware that they need to manually adjust the priorities of all their interrupts to 0xFF, by calling NVIC_SetPriority(<irq>_IRQn, 0xFFU) for all their interrupts. This is necessary, because all IRQs have priority 0 (the highest) out of reset, and with this setting they would be allowed to preempt RTX critical sections.

And finally, the whole design of CMSIS-RTOS RTX around the SVC exception makes most of the RTOS code execute in the exception context of the Cortex-M processor. This immensely hinders debugging, because you lose the call-stack context. In fact, the whole RTOS code feels like debugging a humongous ISR. This is exactly counter to the best practice to "keep your ISRs short".

Any comments about these issues would be highly appreciated.

Miro Samek
state-machine.com

Parents
  • Writing ISR's for CMSIS-RTX or even just bare metal does take some understanding of what is going on.

    There is no need to limit the Priority of any of the ISR's save the ones you mentioned. SVC,Systick and PendSV. They should be the lowest priority and any interrupt on the system.

    Looking at the RTX code you will see any (ok almost any) function that is called within an isr does not actually execute the RTX code. All it does is place an item into a circular buffer. This circular buffer is ISR safe as it uses exclusive access to the index. This is a small amount of time needed to make sure things are safe.

    It is actually one of the 3 interrupts listed above that actually run the code for these RTX items (I believe the PendSV as that makes the most sense to me - and IF all 3 of those interrupts are pending it will be the last to execute.

    Every example that I have seem Keil provide very specifically sets both the interrupt Group to be used in the system and the priorities for any used Interrupt. This is not really any different for RTX, CMSIS-RTX or bare metal programming. The User really should always set these.

    Yes, "most" of the RTX code does execute at exception level. That is clearly the intent by the way it was designed. None of the functions seem to be long at all. Not sure where this "Humongous ISR" term is comming from. My RTX code usually complies into less than 2K of code space with -o1. Since it is likely that any other interrupt is a higher priority than any executing RTX/CMSIS-RTX code (unless of course you actually DO set their priorities to 0xFF)

    I don't believe you should be spending much time at all debugging the RTX code. It is fine the way it is. There are minor issues with the CMSIS-RTX that need to be worked around, but that is not really an RTX issues, it is more an CMSIS definition that at times is somewhat ambiguous. Just try and build translation layer between CMSIS and your favorite RTOS and you will run into a few of these that will take time to ponder and the pondering may produce something that the original spec writers did not intened.

Reply
  • Writing ISR's for CMSIS-RTX or even just bare metal does take some understanding of what is going on.

    There is no need to limit the Priority of any of the ISR's save the ones you mentioned. SVC,Systick and PendSV. They should be the lowest priority and any interrupt on the system.

    Looking at the RTX code you will see any (ok almost any) function that is called within an isr does not actually execute the RTX code. All it does is place an item into a circular buffer. This circular buffer is ISR safe as it uses exclusive access to the index. This is a small amount of time needed to make sure things are safe.

    It is actually one of the 3 interrupts listed above that actually run the code for these RTX items (I believe the PendSV as that makes the most sense to me - and IF all 3 of those interrupts are pending it will be the last to execute.

    Every example that I have seem Keil provide very specifically sets both the interrupt Group to be used in the system and the priorities for any used Interrupt. This is not really any different for RTX, CMSIS-RTX or bare metal programming. The User really should always set these.

    Yes, "most" of the RTX code does execute at exception level. That is clearly the intent by the way it was designed. None of the functions seem to be long at all. Not sure where this "Humongous ISR" term is comming from. My RTX code usually complies into less than 2K of code space with -o1. Since it is likely that any other interrupt is a higher priority than any executing RTX/CMSIS-RTX code (unless of course you actually DO set their priorities to 0xFF)

    I don't believe you should be spending much time at all debugging the RTX code. It is fine the way it is. There are minor issues with the CMSIS-RTX that need to be worked around, but that is not really an RTX issues, it is more an CMSIS definition that at times is somewhat ambiguous. Just try and build translation layer between CMSIS and your favorite RTOS and you will run into a few of these that will take time to ponder and the pondering may produce something that the original spec writers did not intened.

Children