Expand description
NPU — ST Neural-ART accelerator (“ATON”) of the STM32N6.
The Neural-ART NPU is a data-flow machine: convolution accelerators,
activation/pooling/arithmetic units and DMA “streaming engines” connected
by a stream switch. For inference, ST’s Edge AI tooling compiles a network
into epochs — self-contained hardware schedules. This driver runs both
schedules the compiler emits, mirroring ST’s LL_ATON runtime
(LL_ATON_RT_RunEpochBlock + ATON_STD_IRQHandler) with an async API.
§Epoch-controller epochs
With the (default, recommended) --enable-epoch-controller option, each
hardware epoch is emitted as a blob: a command stream that the NPU’s
built-in epoch controller unit executes autonomously, programming all
units and raising an interrupt when the epoch completes. The CPU’s job is:
- load (and optionally relocate/patch) the blobs — see
ecloader, - point the epoch controller at a blob and start it,
- sleep until the end-of-epoch interrupt,
- run any interleaved software epochs (CPU-side operators emitted by the tool, e.g. a final dequantize) and cache maintenance callbacks,
- repeat for the next epoch block.
§Streaming-engine epochs
Without --enable-epoch-controller, the compiler emits no blob. Each
hardware epoch is a generated pre-op (LL_ATON_Start_EpochBlock_*)
that programs the units over the control port and enables the output
streaming engines, and a post-op (LL_ATON_End_EpochBlock_*) that
disables them and performs cache maintenance. The epoch is complete when
every output engine in the generated wait mask has raised end-of-transfer.
Npu::run_streaming_epoch arms that mask, calls the pre-op, and sleeps
until those engines finish.
Unit programming stays in the generated pre-op, called as the epoch’s
start function. This driver does not write conv, activation, arithmetic,
pool, or stream-switch registers. Do not also link ST’s
ATON_STD_IRQHandler or call LL_ATON_RT_RunEpochBlock: both claim NPU0
and the interrupt controller, which this driver owns.
§Prerequisites
System-level setup that the driver deliberately leaves to board/startup code, matching the split in ST’s examples:
- NPU RAMs (AXISRAM3..6) powered and clocked, if the network’s memory
pools live there (
RCC.MEMENR, RAMCFG). - When running with TrustZone / RIF: the NPU (and NPU cache) bus masters granted access to the memories holding weights and activations.
- Input/output/blob buffers placed in NPU-visible memory, respecting the cache rules below.
- The NPU cache should be enabled (
cache::npu_cache_enable) when weights live in cacheable external memory (flash/PSRAM).
§Caches
Buffers shared between CPU and NPU need explicit maintenance; the
cache submodule provides both MCU-side (Cortex-M55 D-cache) and
NPU-side (CACHEAXI) operations. ST Edge AI generates the required calls as
per-epoch callbacks — port them into the epoch’s start/end functions.
§Example
use embassy_stm32::{bind_interrupts, npu, peripherals};
use embassy_stm32::npu::ecloader::EcBinary;
bind_interrupts!(struct Irqs {
NPU0 => npu::InterruptHandler<peripherals::NPU>;
});
// EC binary generated by ST Edge AI (`<network>_ecblobs.h`, here included
// as a u64 array) and a writable, NPU-visible copy of its blob.
static EC_BIN: &[u64] = &[/* ... */];
static BLOB: StaticCell<[u64; 3800]> = StaticCell::new();
let p = embassy_stm32::init(Default::default());
let mut npu = npu::Npu::new(p.NPU, Irqs);
// Network init: load the blob, relocate its "user input" symbol to the
// actual input buffer (mirrors LL_ATON_EC_Network/Inference_Init_*).
let bin = EcBinary::new(EC_BIN).unwrap();
let blob = BLOB.init([0; 3800]);
bin.load_blob(blob).unwrap();
let mut prev_base = 0;
bin.reloc_by_id(blob, "_user_io_input_0", input_buf.as_ptr() as u32, &mut prev_base).unwrap();
// Inference: run the epoch list of the network.
npu.run_epoch_blob(blob).await.unwrap();A streaming-engine epoch (compiled without --enable-epoch-controller)
passes the generated pre-op and post-op to EpochBlock::streaming, with
the output-engine wait mask from the generated epoch list. Bit 0 is
streaming engine 0. Npu::run_network arms the wait before calling the
pre-op, so the pre-op is what enables the engines:
extern "C" {
fn LL_ATON_Start_EpochBlock_0();
fn LL_ATON_End_EpochBlock_0();
}
fn pre_op() { unsafe { LL_ATON_Start_EpochBlock_0() } }
fn post_op() { unsafe { LL_ATON_End_EpochBlock_0() } }
// Output engines 0 and 2 signal the end of this epoch.
let epochs = [
npu::EpochBlock::streaming(pre_op, Some(post_op), 1 << 0 | 1 << 2),
];
npu.run_network(&epochs).await.unwrap();For multi-epoch networks, Npu::run_network executes a list of
EpochBlocks (epoch-controller blobs, streaming-engine epochs, and
software epochs), which is the direct equivalent of the generated
EpochBlock_ItemTypeDef array.
Modules§
- cache
- Cache maintenance for NPU inference.
- ecloader
- Loader for ST Edge AI Epoch Controller binaries.
- epoch
- CPU-side software epochs for the tail of a network (softmax, dequantization,
argmax). Requires the
npu-nnfeature. Software epochs: the CPU-side tail of a network, runningembedded-nnkernels.
Structs§
- Epoch
Block - One entry of a network’s epoch list — the Rust equivalent of the generated
EpochBlock_ItemTypeDef. - Interrupt
Handler - Interrupt handler for ATON interrupt line 0 (NVIC
NPU0). - Npu
- Neural-ART NPU driver.
Enums§
- Error
- NPU error.
Traits§
- Instance
- NPU instance.