liveBook · Manning

Chapter 4: Startup and Initialization in Bare-Metal Rust

What you will learn in chapter 4 of Embedded Software with Rust

This chapter introduction previews how Chapter 4 will build on project and build-system foundations to cover bare-metal startup, memory layout, runtime setup, and interrupt handling in no_std Rust.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduction previews how Chapter 4 will build on project and build-system foundations to cover bare-metal startup, memory layout, runtime setup, and interrupt handling in no_std Rust.

4.1 Defining the memory layout for your embedded Rust program

This section explains why a bare-metal Cortex-M program needs an explicit memory layout and identifies the linker script as the means of specifying it.

Key terms: bare-metal, linker script, interrupt vector table, memory regions

4.1.1 Memory initialization: LMA, VMA, and data movement

This section explains where program sections are stored and used, and why startup code must initialize RAM before main runs.

Key terms: Load Memory Address (LMA), Virtual Memory Address (VMA), FLASH, RAM, .text, .rodata, .data, .bss, startup code, LMA

4.1.2 Defining physical memory regions

This section explains how to define a device’s physical FLASH and RAM regions in a linker script using hardware-correct addresses and sizes.

Key terms: linker script, FLASH, RAM, ORIGIN, LENGTH

4.1.3 Mapping output sections

Explains how a linker script maps program sections to FLASH and RAM, sets up the vector table and stack pointer, and preserves required alignment.

Key terms: SECTIONS, LMA, VMA, .text, .rodata, .data, vector table, initial stack pointer, reset handler, LONG(ORIGIN(RAM) + LENGTH(RAM))

4.1.4 A Complete minimal linker script example

This subsection presents a complete linker script as a viable memory layout for a bare-metal Rust program on an STM32 Cortex-M microcontroller.

Key terms: linker script, memory layout, bare-metal, Cortex-M, STM32

4.1.5 Inspecting the memory layout

This section shows how to inspect a built program’s section mapping and recognize whether its FLASH and RAM placement supports initialization.

Key terms: headers, memory mapping, FLASH, RAM, .data, .bss

4.2 Understanding the reset sequence & processor initialization

Introduces the upcoming explanation of Cortex-M reset and processor initialization, from power-on to the start of Rust code.

4.2.1 Overview of the MCU boot sequence

This section traces the Cortex-M boot sequence from locating the vector table through reset-handler initialization, data setup, and stack setup.

Key terms: vector table, memory remapping, aliasing, BOOT0, low-level processor initialization, core clock sources, bridges, device registers, .data, .bss

4.2.2 A detailed look at reset vector fetching and execution

This section explains how Cortex-M hardware uses the first two vector-table entries to establish execution state and enter the reset handler.

Key terms: vector table, Main Stack Pointer (MSP), Program Counter (PC), reset handler, micro-coded logic

4.2.3 Manually creating a vector table in Rust

This section explains how to construct the exception-handler portion of a Cortex-M vector table in Rust and place it for correct linkage.

Key terms: vector table, union, exception handler, reserved, #[unsafe(link_section = ".vector_table.exceptions")], #[unsafe(no_mangle)], KEEP(*(.vector_table.exceptions)), FLASH

4.2.4 A side note on the vector table’s configuration

This section explains the special roles of the stack-pointer and reset entries in a Cortex-M vector table and why its first four entries must be correct.

Key terms: vector table, main stack pointer, MSP, reset handler, privileged, thread-mode, handler mode, initial MSP value, NMI, hard fault handler

4.3 Initializing Rust’s runtime in a bare-metal environment

This section explains why bare-metal Rust needs a reset handler to prepare memory before the application runs.

Key terms: reset handler, runtime environment, main Rust application, never-returning main routine, infinite loop

4.3.1 Defining the reset handler

This section defines the reset handler and explains how its address is placed in the vector table so the processor starts executing it after reset.

Key terms: reset handler, → !, undefined behavior, RESET_VECTOR, #[unsafe(link_section = ".vector_table.reset_vector")], #[unsafe(no_mangle)], vector table, FLASH

4.3.2 Writing a minimal startup routine in Rust

This section explains how a Rust reset handler prepares global-variable memory before entering an application that never returns.

Key terms: reset handler, .data, .bss, FLASH, RAM, main(), linker script, linker, symbols, extern declarations

4.3.3 Optional additional initialization steps

This subsection describes optional reset-handler setup tasks chosen according to the application and microcontroller.

Key terms: reset handler, .data, .bss, stack, VTOR, FPU

4.3.4 Linker script reference for initialization symbols

This subsection explains how linker-script symbols give the reset handler the addresses it needs to initialize memory.

Key terms: linker script, reset handler, _glob_RW, _eglob_RW, _glob_ZI, _eglob_ZI

4.4 Writing a minimal no_std Rust application

Explains why a bare-metal microcontroller application needs no_std and its own runtime infrastructure.

Key terms: freestanding, bare-metal, std, no_std, runtime infrastructure

4.4.1 Disabling the standard library and the default entry point

This subsection shows how two crate attributes remove the standard-library and default-entry-point assumptions from a bare-metal Rust program.

Key terms: #![no_std], #![no_main], core library, default entry point

4.4.2 Defining a custom entry point function

Explains how to provide a linker-recognized entry point for a bare-metal Rust program and why its declaration has particular attributes and types.

Key terms: entry point, linker, ENTRY(), _start, #[unsafe(no_mangle)], extern "C", C calling convention, -> !, divergent

4.4.3 Implementing the panic handler

Explains why a bare-metal no_std application needs its own panic handler and what a minimal handler does.

Key terms: no_std, panic handler, #[panic_handler], run-time panic

4.4.4 Structuring the application’s main function

This section separates startup work from application logic in a bare-metal program and explains why main must not return.

Key terms: reset_handler, reset, entry point, main, run-time, divergent function, bare-metal

4.4.5 Providing a custom linker script

This subsection explains why a bare-metal no_std application needs a target-appropriate linker script and what to check in the build process.

Key terms: no_std, custom linker script, linker.ld, linker.x

4.4.6 A complete minimal example

This subsection brings together the basic parts of a minimal bare-metal Rust application.

Key terms: no_std, custom entry point, panic handler, initialization, core application logic

4.4.7 Optional simplifications and use of crates

Established runtime crates can simplify bare-metal startup, but understanding the minimal pattern remains useful.

Key terms: run-time crates, cortex-m-rt, riscv-rt, minimal pattern, boilerplate code

4.5 Handling exceptions & interrupts in embedded Rust

Introduces why exception and interrupt handling matters in embedded firmware and the balance Rust offers between low-level control and safety.

Key terms: exceptions, interrupts, interrupt handlers, safety model

4.5.1 Exceptions and interrupts: Concepts and distinctions

This section distinguishes Cortex-M exceptions from interrupts and explains how the processor enters a handler and resumes interrupted code.

Key terms: exceptions, interrupts, SysTick, HardFault, vector table, handler, Nested Vectored Interrupt Controller, NVIC, preemption, Program Counter

4.5.2 The Cortex-M exception handling model

Explains how Cortex-M hardware saves processor context on an exception or interrupt before transferring execution to a handler.

Key terms: exception, interrupt, context, stack, Stack Pointer, vector table, exception stack frame, FPU, R0–R3, R12

4.5.3 Revisiting the vector table structure

Explains how a Cortex-M vector table represents handlers and where peripheral interrupt entries belong.

Key terms: vector table, union values, reserved entry, system exceptions, external interrupts, IRQ

4.5.4 Taking external interrupts in consideration

This section explains where device-specific external interrupts belong in the Cortex-M vector table and how linker placement puts their handler addresses there.

Key terms: vector table, external interrupts, NVIC IRQ line, __INTERRUPTS, #[unsafe(link_section = ".vector_table.interrupts")], KEEP(), linker script

4.5.5 Configuring and controlling interrupts with the Nested Vectored Interrupt Controller

This section introduces selective interrupt control through the NVIC and global masking of maskable interrupts through PRIMASK.

Key terms: NVIC, priorities, pending interrupt flags, PRIMASK, maskable interrupts, critical sections

4.5.6 Advanced fault handling: The hard-fault example

This subsection explains how fault registers and stacked context can help diagnose a hard-fault, and notes why the example uses unsafe Rust rather than a PAC.

Key terms: hard-fault, fault status and address registers, stacked context, Usage-Fault Status Register (UFSR), Configurable Fault Status Register (CFSR), unsafe Rust code, Peripheral Access Crate (PAC)

4.5.7 Simplifying exception and interrupt handling with a run-time crate

This section shows how cortex-m-rt simplifies handler setup and explains how to identify exception and target-specific interrupt handler names.

Key terms: cortex-m-rt, #[exception], #[interrupt], vector table, DefaultHandler, HardFault, SysTick, Peripheral Access Crate (PAC), TIM2, external interrupt handler

4.6 Sharing data and peripherals between handlers and the application

This section explains why sharing data or peripherals between the main program and interrupt handlers needs special care.

Key terms: interrupt and exception handlers, preempt, race conditions, ownership and borrowing, shared access

4.6.1 Sharing data unsafely with static mut

This section explains why sharing data with `static mut` is unsafe and when its use can be justified.

Key terms: static mut, unsafe block, race condition, exclusive access

4.6.2 Atomics for simple, lock-free data sharing

This section explains how atomics safely share simple values between main code and interrupt handlers, what memory orderings guarantee, and where atomics are insufficient.

Key terms: core::sync::atomic, atomic, SysTick, Ordering::Relaxed, Acquire, Release, AcqRel, SeqCst, read-modify-write, atomic types

4.6.3 Interior mutability: Cell, RefCell, and safe synchronization

This section explains how interior mutability supports shared access to complex data, how Cell and RefCell differ, and why interrupt-safe sharing still requires synchronization.

Key terms: interior mutability, shared reference, atomics, Cell<T>, RefCell<T>, Copy types, borrowing rules, main and interrupt contexts, exclusive access, critical sections

4.6.4 Safe shared access using cortex_m::interrupt::Mutex

This subsection shows how a Cortex-M mutex and critical section let main code and interrupt handlers share mutable data safely.

Key terms: cortex_m::interrupt::Mutex, critical section, PRIMASK, FAULTMASK, free(), CriticalSection, .borrow(cs), .borrow_mut(), RefCell, Cell<u32>

4.6.5 Sharing peripherals with mutex and interior mutability

This section explains how to share a peripheral safely between main code and interrupt handlers using a mutex, interior mutability, and one-time ownership transfer.

Key terms: Mutex<RefCell<Option<T>>>, global static, critical section, .replace(), peripheral

4.6.6 Feature flags and the role of the critical-section crate

Explains why a Cortex-M mutex needs a hardware-appropriate critical-section feature enabled.

Key terms: cortex-m::interrupt::Mutex, critical-section crate, feature flag, Cargo.toml

4.7 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 3: Project structure and configuration Chapter 5: Memory-Mapped I/O, Register Abstractions, and Low-Level Control on Cortex-M →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.