liveBook · Manning

Reset handler

As explained in Embedded Software with Rust

A reset handler is the startup routine executed after a processor reset to initialize execution and transfer control to application code.

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

How Embedded Software with Rust covers it

How deep it goes
Works through vector placement, linker-defined memory boundaries, data copying, and zero initialization in a manual startup routine.
Examples
Uses a Rust reset function and RESET_VECTOR static, plus a never-returning main loop.
What it assumes
Assumes embedded project structure and build-system knowledge, then develops bare-metal startup and memory initialization.
What you can do after
Build a minimal reset handler from scratch that initializes memory and hands control to a non-returning application.
How it connects
Connects reset handlers to vector tables, linker scripts, memory initialization, exceptions, and application entry points.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Embedded Software with Rust. The section links point to the parts of the book it draws on.