In this lesson, you will learn how to bridge the gap between hardware events and your C code by populating the interrupt vector table.
When a processor experiences an exception (like a division-by-zero error) or a hardware interrupt (like a timer firing), it needs a roadmap to find your handler functions. This roadmap is the Vector Table: a specific, contiguous array of memory addresses stored at a fixed location, usually at the very beginning of your Flash memory.
The Anatomy of a Vector Table
The processor expects this table to contain the starting addresses (function pointers) of your service routines. The first entry is special: it holds the initial stack pointer value. The second entry holds the address of the Reset Handler. Every subsequent entry corresponds to a specific hardware exception or peripheral interrupt.
If your linker script has correctly placed your custom .vectors section at the start of your memory (as we learned in previous lessons on memory regions), you can populate the table using a structure or an array of function pointers in your C source code:
// Define the vector table structure
typedef void (*vector_handler)(void);
__attribute__((section(".vectors")))
const vector_handler vector_table[] = {
(vector_handler)&_stack_top, // Initial Stack Pointer
Reset_Handler, // Reset entry point
NMI_Handler, // Non-Maskable Interrupt
HardFault_Handler, // System Exception
0, // Reserved
Timer0_IRQHandler // Peripheral Interrupt
};
Why This Matters
When an interrupt triggers, the hardware automatically looks at the index corresponding to that interrupt source, fetches the address stored there, and jumps to that instruction. If you fail to populate an entry or leave it as NULL, the processor will jump to an invalid memory address when that interrupt fires, leading to an immediate system crash or a "HardFault."
By carefully declaring this table and using the __attribute__((section(".vectors"))) directive, you ensure the linker places your handlers exactly where the processor’s hardware logic expects to find them during the boot sequence.
Key Takeaway: The vector table is a fixed map of function pointers that directs the CPU to the correct software handler whenever a hardware event occurs.