liveBook · Manning

What you will learn in Embedded Software with Rust

by David Cabanis · Manning Early Access Program (MEAP)

Chapter by chapter

Chapter 1: The Foundations of embedded Rust development

Introduces embedded systems and explains why Rust’s safety guarantees matter for dependable firmware on constrained hardware.

1.1 Why embedded Rust? Benefits and adoption · 1.1.1 Compile-time safety and reliability · 1.1.2 Constraints, Determinism, and Performance · 1.1.3 How Rust compares to other approaches · 1.2 Rust execution sequence: From reset to main · 1.2.1 Power on and reset (steps 1 to 2) · 1.2.2 Minimal runtime setup (step 2 to 4) · 1.2.3 Transfer to user code (step 5 and 6) · 1.2.4 Exceptions and interrupts at a glance (step 7) · 1.3 What is non-standard library Rust? · 1.3.1 What no_std excludes and what core provides · 1.3.2 Entry and panic requirements · 1.3.3 What minimal runtime crates initialize · 1.4 Core Rust features in embedded context · 1.4.1 Ownership and borrowing for hardware resources · 1.4.2 The singleton pattern for peripherals · 1.4.3 Type-enforced hardware correctness · 1.4.4 Concurrency preview: Interrupts, sharing, and async · 1.5 Scope and audience: architectures and examples · 1.5.1 Platform focus · 1.5.2 What transfers across architectures · 1.5.3 What is out of scope · 1.6 How this book teaches · 1.6.1 Teaching strategy · 1.6.2 Using the virtual machine and code · 1.6.3 Reader support · 1.6.4 What you will be able to do by the end · 1.7 What you need to follow along · 1.7.1 Toolchain and targets · 1.7.2 Hardware or emulator? · 1.7.3 Debug and flash path · 1.7.4 Downloadable content · 1.8 Summary

What you learn in chapter 1 →

Chapter 2: Setting up your Rust tool-chain

This chapter introduction previews the tools and workflows for setting up an embedded Rust development environment.

2.1 Setting up your embedded Rust environment · 2.1.1 A brief word about embedded Rust vs. desktop Rust · 2.1.2 Installing and using rustup · 2.1.3 Managing embedded targets · 2.1.4 Targets and target triples explained · 2.1.5 Common pitfalls in target setup · 2.1.6 Understanding rustc and compiler alternatives · 2.1.7 IDE and editor setup: VSCode · 2.2 Expanding cargo: Building, generating, and inspecting binaries · 2.2.1 Building embedded binaries with cargo · 2.2.2 Installing and using cargo-binutils · 2.3 Oldies but goodies: The GNU binary utilities · 2.3.1 Overview of key tools · 2.4 Streamlining bare-metal Rust development · 2.4.1 Installing the cargo-generate sub-command · 2.4.2 Using a project template with cargo-generate · 2.4.3 Making sense of project’s settings · 2.5 Getting your firmware onto the device and execution/testing options · 2.5.1 Common debug interfaces · 2.5.2 Installing and using OpenOCD · 2.5.3 Vendor-specific CLI alternatives · 2.5.4 Example: Running the firmware on the board · 2.5.5 Testing and execution without hardware · 2.6 A Pragmatic Bare-Metal Development Environment with the Knurling Tools · 2.6.1 Key Tools in the Knurling Ecosystem · 2.6.2 Installing and setting up Knurling tools · 2.6.3 Contrasting Knurling and traditional embedded Rust workflows · 2.6.4 Practical Example: "Hello World" with Knurling on the Blue Pill board · 2.6.5 Traditional vs Knurling workflows · 2.7 Running embedded Rust code in an emulator · 2.7.1 The value and trade-offs of emulation · 2.7.2 QEMU: The workhorse emulator for embedded Rust · 2.7.3 The Renode emulation solution · 2.7.4 The PICSimLab graphical emulation environment · 2.7.5 The Wokwi graphical emulator · 2.8 Summary

What you learn in chapter 2 →

Chapter 3: Project structure and configuration

The introduction explains why developers need to understand embedded Rust project structure beyond what a generated template provides.

3.1 Understanding Rust embedded project structure · 3.1.1 A typical embedded Rust project layout · 3.1.2 The role of Cargo.toml in embedded projects · 3.1.3 Cargo.lock: Ensuring dependency reproducibility · 3.1.4 Optional: Embed.toml and advanced tool integration · 3.2 Build system & linker configuration · 3.2.1 Defining memory layout with memory.x · 3.2.2 Optional build-time customization with build.rs · 3.2.3 The role of .cargo/config.toml in embedded Rust · 3.3 Managing features & dependencies in Cargo · 3.3.1 The importance of features in embedded Rust · 3.3.2 Defining features in Cargo.toml · 3.3.3 Target-specific dependencies · 3.3.4 Discovering and Understanding Features in Dependencies · 3.4 Configuring no_std for embedded systems · 3.4.1 What does no_std mean in embedded Rust? · 3.4.2 How to set-up a no_std project · 3.4.3 A minimal bare-metal no_std example · 3.4.4 Platform-specific aspects: Cortex-M vs. RISC-V · 3.5 Setting-up commonly used embedded crates · 3.5.1 Core architecture crates · 3.5.2 Practical example: Using Cortex-M crates together · 3.5.3 The embedded HAL ecosystem · 3.5.4 Example: Implementing an embedded-hal trait for platform agnostic drivers · 3.5.5 Peripheral access crates (PACs), Hardware Abstraction Layers (HALs), and Board Support Packages (BSPs) · 3.5.6 Expanding beyond Cortex-M and RISC-V · 3.6 Summary

What you learn in chapter 3 →

Chapter 4: Startup and Initialization in Bare-Metal 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.

4.1 Defining the memory layout for your embedded Rust program · 4.1.1 Memory initialization: LMA, VMA, and data movement · 4.1.2 Defining physical memory regions · 4.1.3 Mapping output sections · 4.1.4 A Complete minimal linker script example · 4.1.5 Inspecting the memory layout · 4.2 Understanding the reset sequence & processor initialization · 4.2.1 Overview of the MCU boot sequence · 4.2.2 A detailed look at reset vector fetching and execution · 4.2.3 Manually creating a vector table in Rust · 4.2.4 A side note on the vector table’s configuration · 4.3 Initializing Rust’s runtime in a bare-metal environment · 4.3.1 Defining the reset handler · 4.3.2 Writing a minimal startup routine in Rust · 4.3.3 Optional additional initialization steps · 4.3.4 Linker script reference for initialization symbols · 4.4 Writing a minimal no_std Rust application · 4.4.1 Disabling the standard library and the default entry point · 4.4.2 Defining a custom entry point function · 4.4.3 Implementing the panic handler · 4.4.4 Structuring the application’s main function · 4.4.5 Providing a custom linker script · 4.4.6 A complete minimal example · 4.4.7 Optional simplifications and use of crates · 4.5 Handling exceptions & interrupts in embedded Rust · 4.5.1 Exceptions and interrupts: Concepts and distinctions · 4.5.2 The Cortex-M exception handling model · 4.5.3 Revisiting the vector table structure · 4.5.4 Taking external interrupts in consideration · 4.5.5 Configuring and controlling interrupts with the Nested Vectored Interrupt Controller · 4.5.6 Advanced fault handling: The hard-fault example · 4.5.7 Simplifying exception and interrupt handling with a run-time crate · 4.6 Sharing data and peripherals between handlers and the application · 4.6.1 Sharing data unsafely with static mut · 4.6.2 Atomics for simple, lock-free data sharing · 4.6.3 Interior mutability: Cell, RefCell, and safe synchronization · 4.6.4 Safe shared access using cortex_m::interrupt::Mutex · 4.6.5 Sharing peripherals with mutex and interior mutability · 4.6.6 Feature flags and the role of the critical-section crate · 4.7 Summary

What you learn in chapter 4 →

Chapter 5: Memory-Mapped I/O, Register Abstractions, and Low-Level Control on Cortex-M

The chapter shifts from firmware startup to correctness-sensitive, day-to-day hardware interaction on Cortex-M.

5.1 What are memory-mapped I/Os in a microcontroller? · 5.1.1 The memory map · 5.1.2 What is memory-mapped? · 5.1.3 From the CPU’s perspective · 5.2 Rust’s Embedded ecosystem: Abstractions and unsafe code · 5.2.1 Unsafe code: A necessary evil · 5.2.2 Accessing registers via raw pointers · 5.2.3 Using read_volatile and write_volatile · 5.2.4 Safer MMIO with the volatile-register crate · 5.2.5 Real-World examples · 5.3 Working with Peripheral Access Crates (PACs) & Hardware Abstraction Layers (HALs) · 5.3.1 From Unsafe Code to Safe Wrappers · 5.3.2 Introducing svd2rust and auto-generated PACs · 5.3.3 Using a PAC: stm32f1xx GPIO example · 5.3.4 Moving up to the HALs · 5.3.5 The Role of embedded-hal · 5.4 Memory & data placement considerations · 5.4.1 Understanding data alignment and layout · 5.4.2 Struct layout · 5.4.3 Packed layout and alignment · 5.4.4 Section placement with #[unsafe(link_section)] · 5.4.5 Revisiting Vector Table Placement · 5.5 Writing Low-Level Code with Assembly in Rust · 5.5.1 Why Write Assembly in Embedded Rust? · 5.5.2 Safe Intrinsics via cortex-m::asm · 5.5.3 Writing Inline Assembly with asm!() · 5.5.4 Using global_asm! for Module-Level Assembly · 5.5.5 Linking External Assembly Files · 5.6 Summary

What you learn in chapter 5 →

Chapter 6: Debugging, observability, and profiling

The introduction explains why observing a bare-metal Rust program requires an explicit path for information to reach the developer.

6.1 Debugging fundamentals: methods, interfaces, and protocols · 6.1.1 Overview of debugging techniques · 6.1.2 The debugging pipeline from host to target · 6.1.3 Debug interfaces: JTAG vs. SWD · 6.1.4 Debug probes: hardware interfaces · 6.2 Printing debug messages in embedded Rust · 6.2.1 Semihosting with hprintln!() · 6.2.2 UART logging with writeln!() · 6.2.3 ITM logging with iprintln!() · 6.2.4 RTT logging with rtt-target · 6.3 Structured logging with defmt · 6.3.1 A minimal example · 6.3.2 Transport backends · 6.3.3 Linker setup · 6.3.4 Host-side tooling · 6.3.5 Configuring cargo-embed with Embed.toml · 6.3.6 Comparative summary · 6.4 Handling panics in embedded applications · 6.4.1 When and why panics occur · 6.4.2 Available panic handler crates · 6.4.3 Minimal handler implementation · 6.4.4 Logging-integrated panic handlers · 6.4.5 Conditional panic behavior with Cargo features · 6.4.6 Fault handlers and exception frame recovery · 6.5 Interactive debugging & on-chip debugging · 6.5.1 Choosing a toolchain: GDB + OpenOCD vs. probe-rs · 6.5.2 GDB for embedded systems · 6.5.3 GDB interfaces: CLI, TUI, and GUIs · 6.5.4 Common GDB command groups · 6.5.5 OpenOCD: GDB’s embedded debugging backend · 6.5.6 Launching a debugging session · 6.5.7 Automating GDB with a configuration file · 6.6 Profiling & Performance analysis in embedded Rust · 6.6.1 The DWT cycle counter · 6.6.2 Setting up DWT in embedded Rust · 6.6.3 Example: Measuring function execution time · 6.6.4 Example: SysTick-based timing for cores without DWT · 6.6.5 Platform notes · 6.7 Summary

What you learn in chapter 6 →

Chapter 7: Managing stack and heap in resource-constrained systems

The chapter introduction explains why embedded Rust programmers must make deliberate memory-placement choices despite Rust’s ownership guarantees.

7.1 Understanding memory in embedded Rust · 7.1.1 The three memory regions of an embedded Rust program · 7.1.2 What core provides and what alloc adds · 7.1.3 Manual memory management: from C to unsafe Rust · 7.2 Custom allocators for heap usage in embedded Rust · 7.2.1 Why heap allocation is an explicit choice in no_std · 7.2.2 Installing a global allocator with embedded-alloc · 7.2.3 An alternative: the buddy system · 7.2.4 Using the heap: a worked example · 7.2.5 Choosing between allocators in practice · 7.2.6 Sizing the heap and avoiding allocation pitfalls · 7.3 Alternatives to heap allocation in embedded Rust · 7.3.1 Fixed-capacity containers with heapless · 7.3.2 Lighter-weight alternative: arrayvec · 7.3.3 Hybrid containers: tinyvec and smallvec · 7.3.4 Choosing among the alternatives · 7.4 Managing the stack for memory safety · 7.4.1 How stack overflows happen · 7.4.2 Estimating stack usage with static analysis · 7.4.3 Detecting overflow at link time with flip-link · 7.4.4 Software canaries as a fallback · 7.4.5 Hardware-enforced guard regions with the MPU · 7.5 Summary

What you learn in chapter 7 →

Chapter 8: Rust-C Integration and Migration

The chapter introduction explains why integrating embedded Rust with existing C code requires care at the language boundary.

8.1 Understanding Rust FFI & ABI · 8.1.1 Setting up the bridgeheads · 8.1.2 The ABI and the procedure call standard · 8.2 Calling C from Rust · 8.2.1 Naming conventions and the ffi module · 8.2.2 Scalar type compatibility · 8.2.3 Struct layout and alignment · 8.2.4 Enums, transparent wrappers, and layout lints · 8.2.5 Encapsulating unsafe behind safe interfaces · 8.2.6 Linking C code: direct and indirect · 8.3 Calling Rust from C · 8.3.1 Exporting functions and data · 8.3.2 A minimal no_std static library · 8.3.3 Consuming the library from C · 8.3.4 Error propagation with integer codes · 8.4 Advanced FFI topics · 8.4.1 Arrays and C strings · 8.4.2 Callbacks from C into Rust · 8.4.3 Callbacks with context · 8.5 Automating the interface: bindgen and cbindgen · 8.5.1 bindgen: from C headers to Rust · 8.5.2 Automating bindgen in build.rs · 8.5.3 cbindgen: from Rust to C headers · 8.5.4 Automating cbindgen in build.rs · 8.6 Migration strategies · 8.7 Summary

What you learn in chapter 8 →

Concepts explained in this book

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.