Search code examples
cembeddedmicrocontrollernxp-microcontroller

What is meant by the following operation?


I cannot understand what is meant by following operation in embedded C?

NVIC_ICPR |= 1 << (vector_number%32);

From the reference manual, I found that

  • Vector number — the value stored on the stack when an interrupt is serviced.
  • IRQ number — non-core interrupt source count, which is the vector number minus 16.

But why is it modular division by 32?


Solution

  • It is basically a register with 32 bits in it.

    This removes pending state of one or more interrupts within a group of 32. Each bit represents an interrupt number from IRQ0 - IRQ31 (Vector number from 16 - 47). Writing 1 will remove the pending state. Writing 0 has no effect.

    An important point is you should use it like this

    NVIC_ICPR |= 1U << (vector_number%32);
    

    This ensures that this will be unsigned int arithmetic - it saves you from UB which arises when vector_number=31. (chux pointed this).