0
0
Cnc-programmingHow-ToBeginner · 4 min read

How to Write Interrupt Handler in ARM Cortex-M: Simple Guide

To write an interrupt handler in ARM Cortex-M, define a function with the exact interrupt name and mark it with void return type and no parameters. Then, place this function in the vector table so the processor calls it on the interrupt event. Use the __attribute__((interrupt)) or simply define the function name matching the startup file vector table entry.
📐

Syntax

An interrupt handler in ARM Cortex-M is a function with void return type and no parameters. It must have the exact name expected by the vector table, usually defined in the startup code. The handler should be declared with void HandlerName(void). Optionally, you can use compiler-specific attributes like __attribute__((interrupt)) to indicate it is an interrupt function.

The vector table links interrupt numbers to these handler functions so the CPU calls the correct one when an interrupt occurs.

c
void TIM2_IRQHandler(void) {
    // Interrupt handling code here
}
💻

Example

This example shows a simple interrupt handler for the TIM2 timer interrupt on an ARM Cortex-M microcontroller. The handler toggles an LED each time the interrupt occurs.

c
#include "stm32f4xx.h"  // Device header, adjust for your MCU

void TIM2_IRQHandler(void) {
    if (TIM2->SR & TIM_SR_UIF) {  // Check update interrupt flag
        TIM2->SR &= ~TIM_SR_UIF;  // Clear interrupt flag
        GPIOA->ODR ^= (1 << 5);   // Toggle LED on PA5
    }
}

int main(void) {
    // Setup code for GPIOA pin 5 as output and TIM2 timer omitted for brevity
    while (1) {
        // Main loop does nothing, LED toggled by interrupt
    }
}
Output
LED on pin PA5 toggles on each TIM2 timer interrupt
⚠️

Common Pitfalls

  • Wrong function name: The handler name must exactly match the name in the vector table, or it won't be called.
  • Not clearing interrupt flags: Forgetting to clear the interrupt flag inside the handler causes repeated interrupts.
  • Using parameters or return values: Interrupt handlers must have void return type and no parameters.
  • Long or blocking code: Keep handlers short and avoid blocking calls to prevent system delays.
c
/* Wrong way: */
int TIM2_IRQHandler(int param) {
    // Incorrect signature, won't be called
    return 0;
}

/* Right way: */
void TIM2_IRQHandler(void) {
    // Correct signature
}
📊

Quick Reference

Remember these key points when writing ARM Cortex-M interrupt handlers:

  • Use void HandlerName(void) with exact vector table name.
  • Clear interrupt flags inside the handler.
  • Keep the handler code short and fast.
  • Configure NVIC to enable the interrupt.
  • Use startup file or linker script to place vector table correctly.

Key Takeaways

Interrupt handlers must have void return type and no parameters with exact vector table names.
Always clear the interrupt flag inside the handler to avoid repeated interrupts.
Keep interrupt handlers short and avoid blocking operations.
Enable the interrupt in NVIC and ensure the vector table is correctly set up.
Use the startup code or linker script to link handlers to interrupt vectors.