Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Generate memory.x and layout constants

The build feature lets the build.rs of a firmware read its layout file. The firmware then gets the offsets and sizes of its partitions as Rust constants, and its linker regions from the same file. Build and cargo features lists every function and file.

Add the build dependency

[build-dependencies]
nanoflash = { version = "0.1", features = ["build"] }

The dependency comes from crates.io. To use the state of the main branch, which can be newer than the last release, replace version = "0.1" with git = "https://github.com/AlixANNERAUD/nanoflash". The example firmware examples/nano33ble uses a path dependency instead, so that CI builds it against the library of the same commit.

Generate the constants from a layout file

// build.rs
fn main() {
    nanoflash::build::generate("layouts/board.toml").unwrap();
}
#![allow(unused)]
fn main() {
// src/main.rs: FLASH_SIZE, and a module per partition with OFFSET, SIZE and ADDRESS
include!(concat!(env!("OUT_DIR"), "/nanoflash_layout.rs"));
}

The build script also writes nanoflash_memory.x, the MEMORY regions of the layout, to OUT_DIR and adds that directory to the linker search path.

Use the board of the package

Name the board once, in the Cargo.toml of the firmware:

[package.metadata.nanoflash]
board = "boards/board.toml"

Build::package() reads the layout of that board, so build.rs does not repeat the path:

// build.rs
fn main() {
    nanoflash::build::Build::package().generate().unwrap();
}

Without board-identifier, all boards of the manifest must use the same layout.

Write the whole memory.x

The linker script of cortex-m-rt (link.x) starts with INCLUDE memory.x and uses the regions FLASH and RAM. Build::memory adds the regions that are not partitions, such as the RAM, and writes the whole memory.x to OUT_DIR:

// build.rs
fn main() {
    nanoflash::build::Build::package()
        .memory("RAM", 0x2000_0000, 256 * 1024)
        .generate()
        .unwrap();
    // The runtime of the firmware chooses its linker script.
    println!("cargo:rustc-link-arg-bins=-Tlink.x");
}

Then:

  1. Delete the memory.x of the firmware. generate fails with an error that names the file when a memory.x is in the workspace root or in the package directory.
  2. Do not pass -Tnanoflash_memory.x. link.x includes a file named memory.x.
  3. Turn off the memory-x feature of HAL crates such as embassy-stm32 and stm32-metapac. Their memory.x can win over the generated file, and nanoflash cannot check it.
  4. Keep binaries for the host in another package. The guard script goes to every binary of the package, and a binary that does not link the generated memory.x fails with symbol not found: __nanoflash_memory_x.

A region name must use ASCII letters, digits and _, must not start with a digit, must be given once, and must not be the name of a region of the layout (FLASH for the firmware partition, the name of any other partition in upper case, or its linker region).

Troubleshooting: symbol not found: __nanoflash_memory_x

The link linked another memory.x than the generated one. rust-lld reports nanoflash_memory_guard.x:5: symbol not found: __nanoflash_memory_x, and GNU ld reports the same symbol as unresolved in an expression. Remove the other memory.x. Cargo links again only when the package changes, so the failure can wait for your next edit. How the generated memory.x is protected explains why.

Keep your own memory.x

When the layout holds every region that the firmware needs, a memory.x that includes the generated regions still works. Put it in the directory where the linker runs (the workspace root, or the package directory without a workspace), and do not call memory:

INCLUDE nanoflash_memory.x

Layouts that detect the flash size

With size = "detect", the files are written for the minimum flash of the layout. FLASH_SIZE becomes FLASH_MINIMUM_SIZE, a growing partition has MINIMUM_SIZE instead of SIZE, and a partition that moves has no OFFSET, no ADDRESS and no linker region. See Build and cargo features.

See it working

examples/nano33ble is a firmware that uses Build::package().memory(...). The tutorial Flash and monitor a Nano 33 BLE with cargo run walks through it.