Build and cargo features
The build feature gives the build.rs of a firmware the constants and linker regions of its
layout. The cargo feature reads the board that the Cargo.toml of a package names. The command
uses the same metadata. Generate memory.x and layout constants
shows the steps.
The board of the package
# Cargo.toml
[package.metadata.nanoflash]
board = "boards/nano33ble.toml" # the board manifest, relative to this Cargo.toml
# board-identifier = "arduino-nano-33-ble" # the board of the manifest, when it has several
| Key | Required | Value |
|---|---|---|
board | yes | The path of a board manifest (TOML or JSON, told apart by its content), relative to the directory of the Cargo.toml that holds it. |
board-identifier | no | The board of the manifest. --board-identifier of the command replaces it. |
Other keys are refused. The layout of the board is the layout file, relative to the manifest.
Without board-identifier, all boards of the manifest must use the same layout.
How the Cargo.toml is found
- The
Cargo.tomlthatCARGO_MANIFEST_PATHnames, else the one inCARGO_MANIFEST_DIR. Cargo sets both for the runner ofcargo run(CARGO_MANIFEST_PATHonly in recent versions) and keeps the working directory. - Else the nearest
Cargo.tomlfrom the working directory upwards.
The first Cargo.toml found decides. A virtual workspace, a package without the table, and a
Cargo.toml above one that has no table, all name no board. The message names the Cargo.toml
that was read and the table to add. [workspace.metadata.nanoflash] is not read.
cargo nanoflash does not set the Cargo variables, so it finds the package from the working
directory. A build script reads the Cargo.toml in CARGO_MANIFEST_DIR.
The build feature
[build-dependencies]
nanoflash = { version = "0.1", features = ["build"] }
The dependency comes from crates.io. Build::package and Build::memory exist since version
0.1.0. The example firmware examples/nano33ble uses a path dependency, so that CI builds it
against the library of the same commit.
Functions
| Function | Role |
|---|---|
nanoflash::build::generate(path) | Build::layout(path).generate(). |
Build::layout(path) | Use a layout file. A relative path is relative to the package directory. |
Build::package() | Use the layout of the board that [package.metadata.nanoflash] names in the Cargo.toml in CARGO_MANIFEST_DIR. |
Build::memory(name, origin, length) | Add a memory region that is not a partition, such as the RAM, and write the whole memory.x. Repeatable. |
Build::generate() | Write the files to OUT_DIR, print cargo:rerun-if-changed for every input, and add OUT_DIR to the linker search path. |
Build::generate_into(package_directory, output_directory) | Write the files to output_directory, without the environment and without printing. |
generate fails the build with the same errors as layout check. BuildError prints the same
text with Debug and Display, so a build script that unwraps the error shows the message of
nanoflash layout check.
Generated files
| File | Content |
|---|---|
nanoflash_layout.rs | The Rust constants, to include! from the firmware. |
nanoflash_memory.x | The MEMORY regions of the layout, for a memory.x that includes it. |
memory.x | With memory: one MEMORY block, the regions of nanoflash_memory.x, then the regions given. It ends with __nanoflash_memory_x = 1;. |
nanoflash_memory_guard.x | With memory: a linker script that fails the link when another memory.x was linked. generate prints cargo:rustc-link-arg-bins=-T<this file>. |
generate prints cargo:rerun-if-changed for the layout. With Build::package(), it also prints
it for the Cargo.toml of the package and for the board manifest. When memory is not called,
generate removes the memory.x and the guard that an earlier build left in OUT_DIR.
Constants
#![allow(unused)]
fn main() {
// src/main.rs
include!(concat!(env!("OUT_DIR"), "/nanoflash_layout.rs"));
}
The constants are u32 when every value fits, else u64. For a fixed layout:
#![allow(unused)]
fn main() {
pub const FLASH_SIZE: u32 = 0x0010_0000;
pub const FLASH_BASE_ADDRESS: u32 = 0x0000_0000;
pub mod application {
pub const OFFSET: u32 = 0x0001_0000;
pub const SIZE: u32 = 0x000E_0000;
pub const ADDRESS: u32 = 0x0001_0000;
}
}
Each partition is a module named after it, in lower case, with every character that a Rust
identifier cannot hold replaced by _. A name that starts with a digit gets the prefix
partition_, self, super and crate get the suffix _partition, and other keywords become raw
identifiers (r#type).
With size = "detect", the files describe what the firmware can count on with any flash, so they
are written for the minimum flash of the layout:
FLASH_MINIMUM_SIZEreplacesFLASH_SIZE.- A partition that grows with the flash (a
remainingor{ grow }size in the last segment) hasMINIMUM_SIZEinstead ofSIZE: its size on the minimum flash, which is the least that it has on any device. Its linker region has that length. - A partition that moves with the flash has no
OFFSET, noADDRESSand no linker region. A firmware that uses them fails to compile or to link instead of writing at the wrong place. - A partition that does both has
MINIMUM_SIZEonly. - The other partitions keep
OFFSET,SIZEandADDRESS.
See Growable partitions.
Linker regions
The region of a partition is its linker.region attribute, else FLASH for the firmware
partition, else its name in upper case with every character other than ASCII letters, digits and
_ replaced by _.
A region given to memory must have a valid name (ASCII letters, digits and _, not starting with
a digit), must be given once, and must not have the name of a region of the layout. Otherwise the
build fails and names the region (TableError::InvalidRegionName, TableError::RepeatedRegion,
TableError::DuplicateRegion).
With memory, generate refuses a memory.x in the workspace root or in the package directory
(BuildError::MemoryScriptShadowed). How the generated memory.x is
protected explains the checks.