3

I am using CMSIS API in my C program for ARM Cortex-M CPU.
CMSIS defines NVIC_DisableIRQ function as

__STATIC_INLINE void __NVIC_DisableIRQ(IRQn_Type IRQn);

IRQn_Type is a typedef enum, like

typedef enum IRQn
{

  NonMaskableInt_IRQn           = -14,     /*  2 Non Maskable Interrupt */
  HardFault_IRQn                = -13,     /*  3 HardFault Interrupt */
  .....
  Interrupt0_IRQn               =   0,
  Interrupt1_IRQn               =   1,
  Interrupt2_IRQn               =   2,
} IRQn_Type;

So, given that I would like to call NVIC_DisableIRQ, as

NVIC_DisableIRQ( (IRQn_Type )(SCB->ICSR -16);

and this fails MISRA check

note 9034: cannot assign 'unsigned32' to different essential type 'enum (IRQn)' [MISRA 2012 Rule 10.3, required]

I understand why MISRA complains, but that is a practical solution for this? That I found is to create huge switch/case , are any other solutions/hacks available ?

2
  • What's SCB->ICSR, a volatile uint32_t?
    – Lundin
    Sep 7, 2020 at 11:01
  • Your enum IRQn has to be signed for the negative values, but an uint32_t or equivalent can not store negative numbers. Sep 7, 2020 at 11:36

1 Answer 1

1

Assuming that SCB->ICSR -16 results in a uint32_t result, then you can't assign that to an enum variable (10.3) nor are you allowed to cast it to an enum (10.5). This is because enums have implementation-defined size and signedness.

I suppose there might be various dirty tricks to dodge MISRA, but that defeats the rather sound rationale of these rules, which boil down to: don't use signed operands all over your code to make it go haywire in various subtle and severe ways. Such as changing the default compiler enum type causing all interrupts to change behavior - that is not good - dangerous as can be.

The root of the problem is the enum type and CMSIS function __NVIC_DisableIRQ that requires an enum. If that one had been written differently, we could have used unsigned types. Pretty strange that sloppy non-MISRA compliant libraries are still getting rolled out in the year 2020.

I would suggest that you drop that function and write a custom one. Most of these functions are just wrappers around simple single-line assembler instructions or register writes anyway, so if you have access to the function implementation, it might not be much of an effort.

Note that MISRA-C (10.5) allows casts from enum to unsigned, so if you make an API based on uint32_t you may still be able to pass on the enum values by casting them.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.