A reset handler is the startup routine that prepares a microcontroller to run application code. On reset, the processor loads the initial stack pointer and the reset vector from the vector table, then transfers control to the handler. No operating system coordinates this sequence. section 1.2.1 section 4.2.1
Before handing control to the application, the handler prepares a predictable runtime environment:
- Copy initialized globals in .data from their load location in FLASH to their runtime location in RAM. section 4.1.3 section 4.3.2
- Clear .bss in RAM so zero-initialized globals start with the expected values. section 1.2.2
- Perform any needed early hardware setup, such as configuring clocks or memory protection, and ensure the vector table location is known. section 1.2.2
- Transfer control to the application entry point. The handler must not return; it can call a never-returning main routine or end in an infinite loop. section 1.2.3 section 4.3
In a manual Cortex-M setup, the linker script places the vector table in FLASH. Its first entry provides the initial stack pointer; its second points to the reset handler. The script also defines memory regions and section boundaries used by startup code. Incorrect vector entries can prevent the device from booting or make it behave unpredictably. section 4.1.3 section 4.2.4 section 4.3.2
#[unsafe(no_mangle)]
pub unsafe extern "C" fn reset() -> ! {
// Initialization routines here
crate::main()
}
// Define the reset vector to point to the reset handler
#[unsafe(link_section = ".vector_table.reset_vector")]
#[unsafe(no_mangle)]
pub static RESET_VECTOR: unsafe extern "C" fn() -> ! = reset;
This minimal Rust definition places a pointer to reset in the reset-vector section. The handler returns !, indicating that it never returns, and hands control to main. Memory initialization must be added before that call. section 4.3.1 section 4.3.2
On Cortex-M, reset differs from other exceptions: the processor enters privileged thread mode rather than handler mode. After startup, the application typically owns control flow through a main loop or an executor. section 4.2.4 section 1.2.3
