build-nrf5340dk.sh
build-nrf5340dk.sh {build|rebuild|flash|flash-erase|build-flash} — build the…
Overview
build-nrf5340dk.sh {build|rebuild|flash|flash-erase|build-flash} — build the
Aliro NFC+UWB image for the nRF5340 DK from the self-contained ./workspace.
Run scripts/bootstrap.sh first.
Named for its board because BOARD below is hardcoded: this script builds
nrf5340dk/nrf5340/cpuapp and nothing else. The DWM3001CDK is built straight
from firmware/ by mk/cdk.mk, and the ESP32 apps by mk/esp32.mk.
Layers our modules + ISC dw3000 onto the fetched add-on via out-of-tree
overlays. Output → build/nrf5340dk (git-ignored), or build/nrf5340dk-blob
when ALIRO_SOURCE=0, so flipping that flag no longer forces a pristine rebuild.
Incremental by default — a full from-scratch (pristine) build runs only when it
has to: first build, changed build flags (UWB chip / self-test / config), or
when you ask for one. A preflight first checks the workspace is bootstrapped.
scripts/build-nrf5340dk.sh build # incremental where safe (fast)
scripts/build-nrf5340dk.sh rebuild # force a clean pristine build
PRISTINE=1 scripts/build-nrf5340dk.sh build # same as rebuild
UWB_SELFTEST=1 scripts/build-nrf5340dk.sh build # one-shot boot self-test, no iPhone (diagnostic)
PRETTY=1 scripts/build-nrf5340dk.sh build # curated/clean console (reversible; default verbose)
ALIRO_SOURCE=0 scripts/build-nrf5340dk.sh build # legacy Nordic Aliro binary fallback
UWB_CHIP=dw3720 scripts/build-nrf5340dk.sh build # select the plugged-in UWB chip (default: dw3000)
LTO=1 scripts/build-nrf5340dk.sh build # link-time optimisation (overlays/lto.conf)
DFU=1 scripts/build-nrf5340dk.sh build # MCUboot + Matter OTA (overlays/sysbuild-dfu.conf)
NOTE both default to OFF *here* and ON via make nrf-build, which is the same
split the DWM3001CDK uses: mk/ is the policy layer and decides what a plain
build means, this script only does what it is told. Call it directly and you
get neither unless you ask.
DFU=1 needs this checkout's image-signing key (make dfu-key) and refuses to
build without one, because a bootloader that trusts MCUboot's published demo
key trusts everybody. SIGN_KEY=<absolute path> overrides where it looks.
API
Flaunch()
Launch a command in the NCS toolchain environment for the configured version.
do_buildFsha()
Compute SHA-1 hash; tries shasum first (BSD/macOS), falls back to sha1sum (Linux). Filters output to the hash hex string only.
do_buildFhdr()
Print a section header to stdout: blue "==>" followed by bold text. Used to mark the start of major build phases (preflight, build, done).
do_build, preflightFok()
Print a checkmark to stdout in green followed by text. Used to mark successful completion of build steps.
do_build, preflightFkv()
Print a key-value pair indented: dim key (9 chars wide) and value. Used to display build configuration during the build phase.
do_build, resolve_snrFdie()
Print an error message to stderr and exit with status 1. First line prints the error text in red; remaining arguments are printed as indented hints (dim text with arrow prefix). Used by preflight checks and build validation to fail fast on missing prerequisites or configuration errors.
do_build, preflight, require_built, resolve_chip, resolve_snrFresolve_chip()
Resolve UWB_CHIP -> the dw3000 decadriver's chip Kconfig choice (deps/dw3000/Kconfig). Same DT node + wiring for both; only which *_device.c/dwt_driver builds changes.
do_build · calls dieFpreflight()
Verify bootstrap.sh left everything the build needs. All cheap fs/git checks.
do_build · calls die, hdr, okFdo_build()
Build the Aliro UWB firmware image. Runs preflight checks, resolves chip config, applies optional overlays (pretty console, latency diagnostics, self-test), computes a signature from all -D flags, and runs west build (pristine if config changed, incremental otherwise). Writes build signature to a cache file to detect future flag changes. Outputs merged.hex to BUILD directory.
die, hdr, kv, launch, ok, preflight, resolve_chip, shaFrequire_built()
Verify that a west build has completed in BUILD directory (build.ninja exists). Called before flash operations to fail fast if build has not run.
dieFresolve_snr()
Resolve which J-Link probe to flash, into SNR. Only nRF5340DKs (board version PCA10095 in nrfutil device list) qualify, so another attached probe (e.g. a DWM3001CDK) is never a candidate. One DK -> auto-select it; several -> prompt; none -> fail loud. The flash always names its target explicitly via --dev-id.
die, kvFwarn_if_locked()
Confirm the board we just wrote is not sitting in an APPROTECT-engaged state.
This runs AFTER the flash rather than before, because a mass erase is one of
the ways a board gets into that state: west flash --erase blanks UICR, and a
blank UICR reads as APPROTECT ENGAGED on the nRF5340 until firmware writes it
open again. So the dangerous moment is the one immediately after this command
succeeds, when everything looks like it worked.
Failing here is a warning, not a build failure: the image IS on the board and
saying so is more useful than pretending the flash did not happen. What must
never happen is silence, because a locked board does not announce itself. It
serves PARTIAL debug reads, so RAM above some address starts returning
"memory protection issue" and the board reads as physically broken.
scripts/check-approtect.sh owns the actual test, including its self-test. This
is a call site, not a second implementation.