Skip to main content

Interrupts in Embedded Systems: From Hardware Events to Firmware Execution

Updated 7 min readMarkdown
Interrupts in Embedded Systems: From Hardware Events to Firmware Execution

Embedded Systems Fundamentals #5

Every embedded application relies on interrupts.

Whether you're developing a UART driver, reading a sensor over SPI, handling an I²C transaction, servicing a timer, or responding to a GPIO event, interrupts are the mechanism that allows software to react to the outside world.

Yet many engineers view interrupts as simply writing an Interrupt Service Routine (ISR).

void UART0_IRQHandler(void)
{
    ...
}

What actually happens before this function executes?

How does the processor know an interrupt occurred?

Who decides which interrupt executes first?

What happens if another interrupt arrives while one is already running?

This article answers those questions by exploring the complete interrupt lifecycle—from the hardware event to returning back to the interrupted application.

Polling vs Interrupts

Imagine an application waiting for a UART character.

With polling:

while(1)
{
    if(UART_RX_READY)
    {
        read_character();
    }
}

The CPU continuously checks a status register. Most of the time, nothing has changed. The processor wastes cycles checking the same condition repeatedly.

Interrupts solve this problem.

Instead of constantly asking, "Has anything happened?", the processor continues executing useful work until hardware actively notifies it that attention is required.

Polling still has its place in simple firmware and deterministic boot code, but interrupts are generally preferred for responsive and power-efficient systems.

What Generates an Interrupt?

Interrupts originate from hardware peripherals.

Examples include:

  • UART receive complete

  • SPI transfer complete

  • I²C transaction finished

  • Timer overflow

  • GPIO edge detection

  • ADC conversion complete

  • DMA transfer complete

  • Watchdog timeout

Every peripheral contains event detection logic.

When a configured event occurs, it asserts an interrupt request.

The Interrupt Path

A simplified interrupt flow looks like:

Peripheral Event
        ↓
Peripheral Interrupt Flag
        ↓
Interrupt Controller (NVIC)
        ↓
CPU Core
        ↓
Context Save
        ↓
ISR Executes
        ↓
Context Restore
        ↓
Application Continues

Although this appears simple, several hardware components participate before your ISR begins.

Interrupt Controller (NVIC)

On ARM Cortex-M processors, interrupts are managed by the Nested Vectored Interrupt Controller (NVIC).

The NVIC is responsible for:

  • Enabling or disabling interrupts

  • Managing priorities

  • Tracking pending interrupts

  • Supporting nested interrupts

  • Dispatching the correct ISR

Instead of every peripheral communicating directly with the CPU, they signal the NVIC, which decides when each interrupt should execute.

Vector Table

How does the processor know which ISR to execute?

The answer is the interrupt vector table.

Address
-------
Reset_Handler
NMI_Handler
HardFault_Handler
Timer_IRQHandler
UART_IRQHandler
SPI_IRQHandler
GPIO_IRQHandler

Each entry contains the address of the corresponding interrupt handler.

When an interrupt occurs, the processor reads the appropriate entry and branches directly to that function.

Context Saving

One of the most important, but often overlooked steps is context preservation. Before executing an ISR, the processor automatically saves part of its current execution state.

Typically this includes:

  • Program Counter (PC)

  • Link Register (LR)

  • Program Status Register (xPSR)

  • General-purpose registers

This allows the processor to resume the interrupted code exactly where it left off.

Without context saving, interrupts would corrupt the application's execution.

Interrupt Latency

Interrupt latency is the time between:

Hardware Event
        ↓
First Instruction of ISR

Latency depends on many factors:

  • CPU frequency

  • Interrupt masking

  • Current execution state

  • Context save time

  • Memory wait states

  • Higher-priority interrupts

Reducing latency is important in real-time systems.

Interrupt Priorities

Not every interrupt has the same importance.

Example:

  • Watchdog timeout

  • Motor control timer

  • UART receive

  • Periodic LED blink

Clearly, these should not all have equal priority.

The interrupt controller allows priorities to be assigned so that critical events are serviced first. A high-priority interrupt can preempt a lower-priority interrupt if preemption is enabled.

Nested Interrupts

Consider this scenario:

A UART ISR is executing.

While it is running, a motor-control timer interrupt occurs with a higher priority.

The processor suspends the UART ISR, services the timer interrupt, and then returns to the UART ISR.

UART ISR
    ↓
Timer Interrupt
    ↓
Timer ISR
    ↓
Resume UART ISR

This is called interrupt nesting. Proper priority assignment is essential to avoid excessive latency and starvation.

Writing Good ISRs

A common mistake is placing too much work inside an interrupt handler.

Poor example:

UART Interrupt
    ↓
Parse packet
    ↓
Allocate memory
    ↓
Write flash
    ↓
Update display

The ISR now occupies the CPU for an extended period, delaying other interrupts.

A better approach is:

UART Interrupt
    ↓
Read received byte
    ↓
Store in buffer
    ↓
Set event flag
    ↓
Exit

The heavy processing occurs later in normal application context or an RTOS task.

A useful rule of thumb:

Do only the work that must be done immediately.

Everything else should be deferred.

Deferred Interrupt Processing

Many systems separate interrupt handling into two stages.

Top Half:

  • Handle the immediate hardware event.

  • Clear interrupt status.

  • Capture minimal data.

Bottom Half:

  • Parse packets.

  • Process data.

  • Update state machines.

  • Notify applications.

In bare-metal systems, the bottom half may run from the main loop.

In an RTOS, it often runs inside a dedicated worker task.

Interrupts and RTOS

Real-time operating systems integrate closely with interrupts.

An ISR may:

  • Give a semaphore

  • Send a queue message

  • Notify a task

  • Trigger a context switch

The scheduler then wakes the appropriate task to perform the heavier work.

This keeps interrupt execution short while maintaining system responsiveness.

Common Interrupt Problems

Interrupt Never Fires

Possible causes:

  • Interrupt not enabled

  • Wrong vector table entry

  • Peripheral interrupt disabled

  • Incorrect pin configuration

Interrupt Fires Continuously

Often caused by:

  • Interrupt flag not cleared

  • Level-triggered interrupt remaining active

  • Hardware fault condition

Lost Interrupts

Usually related to:

  • Disabled interrupts

  • Incorrect priority configuration

  • Buffer overruns

  • ISR execution taking too long

Stack Overflow

Deep interrupt nesting and large local variables inside ISRs can consume significant stack space.

Always consider worst-case nesting scenarios when sizing stacks.

Debugging Interrupt Issues

Useful techniques include:

  • Toggle a GPIO at ISR entry and exit to measure execution time with an oscilloscope or logic analyzer.

  • Read peripheral status registers before clearing interrupt flags.

  • Verify interrupt priorities during initialization.

  • Check pending and active interrupt states.

  • Measure latency for time-critical events.

Good debugging starts with understanding the interrupt lifecycle, not just the ISR code.

Putting Everything Together

When a UART receives a byte, the complete flow is more than just calling an ISR.

UART Receives Byte
        ↓
Peripheral Sets Interrupt Flag
        ↓
NVIC Detects Pending Interrupt
        ↓
CPU Completes Current Instruction
        ↓
Automatic Context Save
        ↓
Vector Table Lookup
        ↓
UART_IRQHandler()
        ↓
Read FIFO
        ↓
Clear Interrupt
        ↓
Notify Application/RTOS
        ↓
Automatic Context Restore
        ↓
Resume Interrupted Code

Understanding this complete sequence makes it much easier to design efficient drivers, reduce latency, and debug timing issues in complex embedded systems.

Key Takeaways

  • Interrupts allow hardware to notify software without constant polling.

  • The NVIC manages priorities, pending interrupts, and nesting.

  • Context saving and restoration preserve application execution.

  • Interrupt latency depends on both hardware and software factors.

  • Keep ISRs short and deterministic.

  • Defer expensive work to tasks or the main application.

  • Proper priority assignment is critical in real-time systems.

  • Understanding the entire interrupt path leads to better firmware design.


What's Next?

Part 6:

DMA in Embedded Systems: Moving Data Without Burdening the CPU

We'll explore:

  • Why DMA exists

  • DMA controller architecture

  • Channels and descriptors

  • Memory-to-memory vs peripheral transfers

  • Circular mode

  • Scatter-gather DMA

  • Cache coherency

  • DMA with UART, SPI and I²C

  • Common debugging techniques

  • Performance considerations

We'll see how modern embedded systems move large amounts of data efficiently while allowing the CPU to focus on application logic.

No comments yet. Be the first to comment.