What is NVIC ISPR — Manually Pending Interrupts-Embedded Systems Training in Hyderabad

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

Example 09 — Source Code Deep-Dive

NVIC ISPR — Manually Pending Interrupts

Understanding the difference between interrupt enabled state and pending state — and how to manually set either using the NVIC register set.

1. Overview

The 009_USART3_int_pend project demonstrates the NVIC interrupt lifecycle by manually setting the USART3 interrupt (IRQ39) to pending state and then enabling it, causing the ISR to fire without any actual USART hardware activity.

2. Source Code — Annotated

#include <stdint.h>
#include <stdio.h>

#define USART3_IRQNO 39   /* USART3 global interrupt = IRQ39 on STM32F407 */

int main(void)
{
    /* Step 1: Manually set the PENDING bit for IRQ39
     *
     * ISPR1: Interrupt Set-Pending Register 1
     * Address: 0xE000E204
     * ISPR0 covers IRQ0–IRQ31 (at 0xE000E200)
     * ISPR1 covers IRQ32–IRQ63 (at 0xE000E204)
     *
     * IRQ39 is in ISPR1 at bit position (39 % 32) = 7
     * Writing 1 to bit 7 of ISPR1 sets IRQ39 to PENDING state.
     * The interrupt is now "in the queue" but NOT yet taken
     * because it hasn't been enabled yet. */
    uint32_t *pISPR1 = (uint32_t*)0xE000E204;
    *pISPR1 |= (1 << (USART3_IRQNO % 32));  /* bit 7 of ISPR1 */

    /* Step 2: Enable IRQ39 in NVIC
     *
     * ISER1: Interrupt Set-Enable Register 1
     * Address: 0xE000E104
     * ISER0 covers IRQ0–IRQ31 (at 0xE000E100)
     * ISER1 covers IRQ32–IRQ63 (at 0xE000E104)
     *
     * Writing 1 to bit 7 of ISER1 enables IRQ39.
     * Since the pending bit is already set (from step 1),
     * the processor immediately takes the interrupt → USART3_IRQHandler fires. */
    uint32_t *pISER1 = (uint32_t*)0xE000E104;
    *pISER1 |= (1 << (USART3_IRQNO % 32));  /* bit 7 of ISER1 */

    for(;;);
}

/* USART3 ISR — fires immediately after IRQ39 is enabled */
void USART3_IRQHandler(void)
{
    printf("in USART3 isr\n");
}

3. Interrupt State Machine

An interrupt on the NVIC has four possible states:

StateISER bitISPR bitMeaning
Inactive0 or 10No interrupt pending, nothing to service
Pending01Event occurred but not yet serviced (not enabled — will wait)
Pending11Event occurred and enabled — will be taken as soon as priority allows
Active10 (cleared when taken)Currently being serviced by the CPU (ISR running)
Active + Pending11ISR running AND a new event occurred — will re-enter after current ISR returns
Key insight in this example: Step 1 sets PENDING before ENABLING. The interrupt is queued but not taken. Step 2 enables it. The moment bit 7 of ISER1 is set, the processor sees IRQ39 is both enabled AND pending → takes it immediately on the next instruction cycle.

4. Complete NVIC Register Map

RegisterAddressIRQ RangeAction on Write 1Action on Write 0
ISER00xE000E100IRQ0–31Enable IRQNo effect
ISER10xE000E104IRQ32–63Enable IRQNo effect
ISER20xE000E108IRQ64–95Enable IRQNo effect
ICER00xE000E180IRQ0–31Disable IRQNo effect
ICER10xE000E184IRQ32–63Disable IRQNo effect
ISPR00xE000E200IRQ0–31Set to PendingNo effect
ISPR10xE000E204IRQ32–63Set to PendingNo effect
ICPR00xE000E280IRQ0–31Clear PendingNo effect
ICPR10xE000E284IRQ32–63Clear PendingNo effect
IABR00xE000E300IRQ0–31Read-only (active status)—
IPR0–IPR590xE000E400–0xE000E4EFAll IRQsSet priority (8-bit per IRQ)—
STIR0xE000EF00Write IRQ#Software trigger—

IRQ Number Calculations

/* Generic NVIC helper functions */
#define NVIC_ISER_BASE  ((volatile uint32_t*)0xE000E100)
#define NVIC_ICER_BASE  ((volatile uint32_t*)0xE000E180)
#define NVIC_ISPR_BASE  ((volatile uint32_t*)0xE000E200)
#define NVIC_ICPR_BASE  ((volatile uint32_t*)0xE000E280)
#define NVIC_IABR_BASE  ((volatile uint32_t*)0xE000E300)
#define NVIC_IPR_BASE   ((volatile uint8_t* )0xE000E400)

void NVIC_Enable(uint8_t irq)    { NVIC_ISER_BASE[irq/32] =  (1UL << (irq%32)); }
void NVIC_Disable(uint8_t irq)   { NVIC_ICER_BASE[irq/32] =  (1UL << (irq%32)); }
void NVIC_SetPending(uint8_t irq){ NVIC_ISPR_BASE[irq/32] =  (1UL << (irq%32)); }
void NVIC_ClrPending(uint8_t irq){ NVIC_ICPR_BASE[irq/32] =  (1UL << (irq%32)); }
int  NVIC_IsActive(uint8_t irq)  { return (NVIC_IABR_BASE[irq/32] >> (irq%32)) & 1; }

void NVIC_SetPriority(uint8_t irq, uint8_t priority)
{
    /* STM32F407 uses top 4 bits of each IPR byte for priority */
    NVIC_IPR_BASE[irq] = priority & 0xF0;
}

5. STM32F407 IRQ Table (Key Peripherals)

IRQ#ISER/ISPR RegisterBit in RegisterHandler Name
0ISER0/ISPR00WWDG_IRQHandler
6ISER0/ISPR06EXTI0_IRQHandler
22ISER0/ISPR022TIM2_IRQHandler
28ISER0/ISPR028TIM2_IRQHandler (alternate)
37ISER1/ISPR15USART1_IRQHandler
38ISER1/ISPR16USART2_IRQHandler
39ISER1/ISPR17USART3_IRQHandler
56ISER1/ISPR124DMA2_Stream0_IRQHandler
67ISER2/ISPR23ADC_IRQHandler

6. Interview Questions & Answers

Q1. What is the difference between interrupt enable and interrupt pending?
Enabled (ISER bit): Allows the interrupt to be taken by the CPU when it becomes pending. An enabled interrupt with no pending bit does nothing. Disabling an interrupt (ICER) does not clear the pending bit — the event is remembered.

Pending (ISPR bit): Records that an interrupt event has occurred. The interrupt is “in the queue” waiting to be serviced. If the interrupt is not enabled, it stays pending indefinitely. When it is later enabled, it will be taken immediately.

Gate analogy: Pending = ringing the doorbell. Enabled = the door is unlocked. Both must be true for entry (interrupt to be taken).
Q2. How do you calculate which ISER/ISPR register and bit an IRQ maps to?
Formula:
• Register index: IRQ_number / 32 (integer division)
• Bit position: IRQ_number % 32 (remainder)
• Register address: BASE + (IRQ_number / 32) × 4

Examples:
• IRQ39 (USART3): 39/32=1 → ISER1/ISPR1, bit 39%32=7
• IRQ6 (EXTI0): 6/32=0 → ISER0/ISPR0, bit 6%32=6
• IRQ67 (ADC): 67/32=2 → ISER2/ISPR2, bit 67%32=3

This calculation is encapsulated in the CMSIS functions NVIC_EnableIRQ(irq), NVIC_SetPendingIRQ(irq) etc.
Q3. What is the IABR register and when is it useful?
IABR (Interrupt Active Bit Register) is a read-only register that shows which interrupts are currently active (ISR is running for that IRQ). A bit in IABR is set when the processor enters the ISR and cleared when the ISR returns (EXC_RETURN).

Useful for:
• Detecting nested interrupt situations (if an ISR sets the IABR bit of a lower-priority IRQ while its own bit is still set)
• Debugging: check IABR to see which ISRs are currently executing when stepping through code
• Detecting if an interrupt is stuck active (never cleared) which can prevent that priority level from being entered again

Note: IABR is only 1 set of registers (not separate IABR_SET/IABR_CLEAR). It reflects live hardware state and cannot be written.
Q4. Can you have an interrupt that is both active AND pending at the same time?
Yes. This is called “active-and-pending” state. It occurs when:
• ISR X is currently running (IABR bit set → active)
• Another occurrence of the same event happens (or ISPR is written → pending bit set again)

In this case, the ISR runs to completion, then the processor immediately takes the interrupt again (because it’s still pending). This is different from nesting: nesting occurs when a higher-priority IRQ preempts the current one. Active+Pending for the same IRQ means the same ISR will run back-to-back.

Practical example: If the USART3 RX ISR runs but before it reads the data register, another byte arrives — USART3 interrupt becomes pending again. After the current ISR returns, it fires again immediately.
Q5. What is the difference between ISPR (write-to-set) and ICPR (write-to-clear)?
ARM uses write-to-set and write-to-clear register pairs instead of read-modify-write for NVIC state registers:

ISPR (Interrupt Set-Pending): Writing 1 to a bit sets that interrupt pending. Writing 0 has no effect. This prevents accidental clearing of other interrupt pending bits in an RMW sequence.

ICPR (Interrupt Clear-Pending): Writing 1 to a bit clears that interrupt from pending. Writing 0 has no effect.

This design means you can safely set/clear one interrupt’s pending bit with a single 32-bit write without needing to read the register first — no risk of accidentally changing other pending bits in the same register. The same pattern applies to ISER/ICER for enable/disable.
Q6. In the example, what would happen if ISER1 was set before ISPR1?
If IRQ39 was enabled first (ISER1) and then pended (ISPR1):
1. Enabling IRQ39 (ISER1 bit7 = 1): IRQ is enabled but no pending event → nothing happens
2. Setting pending (ISPR1 bit7 = 1): IRQ39 is now both enabled and pending → processor takes it immediately on the next instruction cycle

The end result is identical. The order of setting ISER vs ISPR only affects the precise moment the interrupt fires:
• Pend first, then enable: fires the instant enable completes
• Enable first, then pend: fires the instant pend completes

In practice, the enable-first sequence is more common because it mirrors how real peripherals work: enable the IRQ in NVIC, then enable the peripheral interrupt (which sets the pending bit when the event occurs).

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *