In this lesson, you will learn how to direct the processor to the exact memory address where your software begins execution immediately after a hardware reset.
The Reset Vector Concept
When a microcontroller powers up or experiences a hardware reset, it does not know where your main() function is located. Instead, the CPU hardware is hard-wired to fetch the address of its first instruction from a fixed location known as the Reset Vector.
This vector is essentially a pointer stored at a specific memory address (usually at the very beginning of the flash memory). By placing the address of your startup routine (often called Reset_Handler) at this location, you ensure that the processor jumps exactly where you need it to go to initialize registers, set up the stack, and eventually call your C code.
Defining the Entry Point in the Linker Script
While you can handle this with assembly directives, the most robust way to manage this in a modern toolchain is through your linker script. You must ensure that the section containing your vector table is placed at the exact starting address expected by the hardware.
If your architecture expects the vector table at 0x08000000, your linker script should define that region and ensure the vector table is the first object placed there:
MEMORY
{
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 512K
}
SECTIONS
{
.vectors :
{
KEEP(*(.isr_vector)) /* Ensures the vector table stays at the start */
*(.text*)
} > FLASH
}
By using the KEEP command, you prevent the linker from discarding the vector table during optimization. The symbol placed at the very first word of this section becomes the "Reset Vector." If the CPU performs a power-on reset, it reads the address stored at 0x08000000, loads it into the Program Counter, and your system begins its execution sequence. If you miss this alignment, the processor will simply fetch invalid data, leading to a hard fault or a system hang before your code ever runs.
Key Takeaway: The Reset Vector acts as the critical bridge between hardware power-on and software execution, requiring the linker script to explicitly place the startup routine at the memory address dictated by the processor’s architecture.