</>EmbeddedInterview

What is the vector table?

EasyEmbedded C

Short answer: The vector table is an array of addresses that the CPU uses to find handlers for exceptions and interrupts. On Cortex-M it is at address 0 by default (relocatable with SCB->VTOR): entry 0 is the initial main stack pointer, entry 1 is the reset handler, then the system exception handlers (NMI, HardFault, MemManage, BusFault, UsageFault, SVCall, DebugMonitor, PendSV, SysTick), followed by one entry per peripheral interrupt (IRQ0, IRQ1, ...).

Offset Entry Exception number
0x00 Initial MSP value –
0x04 Reset 1
0x08 NMI 2
0x0C HardFault 3
0x10–0x18 MemManage, BusFault, UsageFault (not on M0/M0+) 4–6
0x1C–0x28 SecureFault (v8-M) / reserved 7–10
0x2C SVCall 11
0x30 DebugMonitor 12
0x38 PendSV 14
0x3C SysTick 15
0x40 + 4·n IRQn (peripheral interrupts) 16 + n

In C (GCC):

extern uint32_t _estack;
void Reset_Handler(void);
void Default_Handler(void) { for (;;) { } }
void NMI_Handler(void)       __attribute__((weak, alias("Default_Handler")));
void HardFault_Handler(void) __attribute__((weak, alias("Default_Handler")));
void USART2_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));

__attribute__((section(".isr_vector"), used))
void (*const vector_table[])(void) = {
    (void (*)(void))&_estack,
    Reset_Handler,
    NMI_Handler,
    HardFault_Handler,
    /* ... */
};

Details

  • Handler addresses have bit 0 set (Thumb state); the compiler/linker does this automatically for function addresses.
  • VTOR alignment: the table must be aligned to a power of two at least as large as the table (e.g. 512 bytes for up to 128 entries); check the core's rules.
  • Relocating the table to RAM lets you change handlers at run time and is needed by bootloaders (the application sets VTOR to its own table).
  • Cortex-A uses a different scheme: a table of instructions (branches) per exception type, located via VBAR_ELx, and a separate interrupt controller (GIC) provides the interrupt number.