# Zephyr

Under Zephyr, heliaDSP is two Zephyr modules: `helia-dsp`, which is this repository, and `cmsis-dsp`, which must be heliaDSP's copy of the qualified CMSIS-DSP commit (below, the bundled copy) in `third_party/CMSIS-DSP`. CMSIS-DSP is Arm's C library of signal-processing functions (`arm_*`, declared in `arm_math.h`); the qualified CMSIS-DSP commit is the one commit of Ambiq's upstream-tracking fork [AmbiqAI/CMSIS-DSP](https://github.com/AmbiqAI/CMSIS-DSP) that heliaDSP is qualified against. `helia-dsp` declares a dependency on a module named `cmsis-dsp` ([`zephyr/module.yml`](https://github.com/AmbiqAI/helia-dsp/blob/4a7b7f054eafd96ae50c12050ad3ef3ec1ebaea1/zephyr/module.yml)), checks that the tree Zephyr selected under that name is the qualified CMSIS-DSP commit, and adds no CMSIS-DSP sources of its own.

Placeholders on this page:

- `<helia-dsp>`: an absolute path to a heliaDSP checkout with its submodule (see [Direct CMake](https://ambiqai.github.io/helia-dsp/getting-started/cmake/), step 1).
- `<app>`: the application directory.

The commands below were run from a west workspace whose manifest is Zephyr v4.4.2.

## Why the bundled copy must be registered

A Zephyr workspace built from Zephyr's manifest already contains a `cmsis-dsp` project at `modules/lib/cmsis-dsp`, which is not Ambiq's upstream-tracking fork. Zephyr does not discover a Git submodule nested inside another module, so heliaDSP's copy must be registered explicitly. Zephyr v4.4.2 keys modules by name, registers the manifest's projects first and `ZEPHYR_EXTRA_MODULES` after them, and keeps the last module registered under a name without a message. Registering the bundled copy in `ZEPHYR_EXTRA_MODULES` therefore replaces the manifest's `cmsis-dsp` for that build; the manifest's copy is not built, so the image contains one CMSIS-DSP library.

## Build an application

`<app>/prj.conf`:

```text
CONFIG_CMSIS_DSP=y
CONFIG_HELIA_DSP=y
```

`<app>/CMakeLists.txt`:

```cmake
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(rfft_demo)
target_sources(app PRIVATE src/main.c)
```

`<app>/src/main.c`:

```c
#include <stdio.h>
#include "arm_math.h"

#define N 256

static float32_t x[N], X[N];

int main(void)
{
    arm_rfft_fast_instance_f32 S;

    for (int n = 0; n < N; n++) {
        x[n] = arm_cos_f32(2.0f * PI * 8.0f * (float32_t)n / N);   /* bin 8 */
    }
    arm_rfft_fast_init_256_f32(&S);
    arm_rfft_fast_f32(&S, x, X, 0);   /* forward; x is overwritten */

    /* X[0] = DC, X[1] = Nyquist, then re/im pairs for bins 1..N/2-1 */
    printf("bin 8: re=%.3f im=%.3f\n", (double)X[16], (double)X[17]);
    return 0;
}
```

Build for the `native_sim` board, which runs the image as a Linux process:

```sh
west build -b native_sim/native/64 <app> -- \
  -DZEPHYR_EXTRA_MODULES="<helia-dsp>/third_party/CMSIS-DSP;<helia-dsp>"
```

Configuration completes with no line beginning `helia-dsp:`. The order of the two entries does not matter, because `helia-dsp` declares its dependency on `cmsis-dsp`. To confirm which `cmsis-dsp` Zephyr selected:

```sh
grep cmsis-dsp build/zephyr_modules.txt
```

Expected:

```text
"cmsis-dsp":"<helia-dsp>/third_party/CMSIS-DSP":"<helia-dsp>/third_party/CMSIS-DSP/zephyr"
```

Run the image; `-stop_at=1` ends the simulation after one simulated second, since a `native_sim` image keeps running after `main` returns:

```sh
./build/zephyr/zephyr.exe -stop_at=1
```

Expected output, exit status 0:

```text
*** Booting Zephyr OS build v4.4.2 ***
bin 8: re=128.000 im=-0.000

Stopped at 1.010s
```

The forward transform is unnormalized, so a unit cosine at bin 8 of a 256-point transform gives 128 at that bin. The run above used the host's C compiler (`ZEPHYR_TOOLCHAIN_VARIANT=host`); a workspace with the Zephyr SDK installed needs no such setting.

## When the wrong CMSIS-DSP is selected

If `<helia-dsp>` is registered alone, or the manifest's copy is registered after the bundled copy, Zephyr selects the manifest's `cmsis-dsp` and configuration stops:

```text
CMake Error at <helia-dsp>/cmake/HeliaDspDependencyIdentity.cmake:103 (message):
  helia-dsp: the Zephyr-selected CMSIS-DSP provider at

    <workspace>/modules/lib/cmsis-dsp

  is not the pinned dependency.
```

followed by the expected and found digests. Register the bundled copy as above.

To remove the manifest's copy from the workspace instead, run:

```sh
west config manifest.project-filter -- -cmsis-dsp
```

The `west build` command above is unchanged. With the filter set and the bundled copy not registered, Zephyr itself stops with `Unmet or cyclic dependencies in modules: ... depends on: ['cmsis-dsp']`. `west config -d manifest.project-filter` removes the filter.

## Kconfig

| Symbol | Owner | Effect |
| --- | --- | --- |
| `CONFIG_CMSIS_DSP` | CMSIS-DSP's Zephyr integration | Builds CMSIS-DSP. Required by `CONFIG_HELIA_DSP`. |
| `CONFIG_HELIA_DSP` | heliaDSP | Enables the identity check and heliaDSP's own operations (from 2.1.0). |
| `CONFIG_CMSIS_DSP_AUTOVECTORIZE` | CMSIS-DSP's Zephyr integration | Portable C kernels instead of the Helium kernels. The pinned CMSIS-DSP (AmbiqAI/CMSIS-DSP#13) defines `ARM_MATH_AUTOVECTORIZE` for the CMSIS-DSP library and for every source file that includes its headers, and not for other libraries in the image; a source file sees the definition after its first CMSIS-DSP include, and one that has a directory holding its own `arm_dsp_config.h` on its target's own include path does not see it (see below). A source file that includes `arm_math.h` before heliaCORE headers compiles those headers' inline helpers as scalar code. An application that defines `ARM_MATH_AUTOVECTORIZE` for every target, for example with `zephyr_compile_definitions()`, also compiles heliaCORE's Helium paths as scalar C: on the Apollo510 EVB heliaCORE's `arm_nn_mat_mult_nt_t_s8` at 125 x 64 x 64 took 12.9 times more cycles under GCC 14.2.1 and 1.44 times more under ATfE 22.1.0 (image built from `helia/tests/mixed_provider`). |
| `CONFIG_CMSIS_DSP_FLOAT16` | CMSIS-DSP's Zephyr integration | f16 functions. Enabled by default when FP16 arithmetic is configured (`FP16_ARITHMETIC`). |
| `CONFIG_FPU` | Zephyr | Hardware floating point; on Cortex-M55 this is also what makes the compiler target Helium. |

The Zephyr integration in the qualified CMSIS-DSP commit compiles every CMSIS-DSP component. An application written for Zephyr's own CMSIS-DSP integration that sets per-component options such as `CONFIG_CMSIS_DSP_TRANSFORM=y` gets a Kconfig warning (`CMSIS_DSP_TRANSFORM ... was assigned the value 'y' but got the value 'n'`) and the build proceeds.

## Helium builds under Zephyr

Helium is the vector extension of the Cortex-M55. On Cortex-M55 boards, Zephyr builds without `CONFIG_FPU` use soft-float and CMSIS-DSP's scalar kernels; these build and run (Zephyr SDK 1.0.1, GCC 14.3.0, on the Corstone-300 simulator as `mps3/corstone300/fvp`).

With `CONFIG_FPU=y`, the Helium kernels at the qualified CMSIS-DSP commit of 2.0.0 compile under Zephyr v4.4.2 and pass on the Corstone-300 simulator with f16 on and off. Zephyr compiles every file with a header that redefines `int32_t` as `int`, which GCC's Helium intrinsics reject (`'q31_t' {aka 'int'} is not a valid MVE element type`); Zephyr's own CMSIS-DSP integration suppresses that header by defining its include guard, and the integration in Arm's CMSIS-DSP defines a misspelled guard name. Pull request #12 of the upstream-tracking fork corrects the name, and the qualified CMSIS-DSP commit carries it (the `carried_patches` list of [`PROVENANCE.toml`](https://github.com/AmbiqAI/helia-dsp/blob/4a7b7f054eafd96ae50c12050ad3ef3ec1ebaea1/PROVENANCE.toml)). The executed Zephyr configurations are in the Zephyr row of [`COMPATIBILITY.md`](https://github.com/AmbiqAI/helia-dsp/blob/4a7b7f054eafd96ae50c12050ad3ef3ec1ebaea1/COMPATIBILITY.md).

## Compiler policy under Zephyr

The Helium complex FFT of the qualified CMSIS-DSP commit compiles correctly with GCC 15.2 (see [Toolchains](https://ambiqai.github.io/helia-dsp/guide/toolchains/)), so Zephyr Helium builds are not refused with any GCC. One part of the policy that applies in CMake and NSX builds does not apply under Zephyr: the `DISABLEFLOAT16` default for GCC below 15, because Zephyr fixes `CONFIG_CMSIS_DSP_FLOAT16` before module CMake runs. The GCC 15.0 to 15.2 `__QSUB`/`__QADD` workaround is in the CMSIS-DSP headers and does not depend on the build system.

The Zephyr integration in the qualified CMSIS-DSP commit gives every source file that includes the CMSIS-DSP headers the library's `DISABLEFLOAT16` and `ARM_MATH_AUTOVECTORIZE` (pull request #13 of the upstream-tracking fork). The definitions come from a generated `arm_dsp_config.h` that the CMSIS-DSP headers include, not from the compiler command line, so code before the first CMSIS-DSP include does not see them. Code that selects a biquad initializer with `#if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)` (see [Contracts](https://ambiqai.github.io/helia-dsp/guide/contracts/)) makes that test after including a CMSIS-DSP header; otherwise, with `CONFIG_CMSIS_DSP_AUTOVECTORIZE=y`, it calls `arm_biquad_cascade_df1_mve_init_f32`, which that library does not contain, and the link fails. With `CONFIG_CMSIS_DSP_CUSTOM_CONFIG=y` the generated header includes the application's own `arm_dsp_config.h` with `#include_next`; add that header's directory with `zephyr_include_directories()` and to no target's own include path: a target that has it on its own path finds that header first, skips the generated one, and compiles without the CMSIS-DSP feature definitions of the library.

## Limits

- Zephyr evidence for 2.0.0 is `native_sim/native/64` and the Corstone-300 simulator, run by hand, not in CI. No Ambiq board or Zephyr Ambiq SoC target has been built.
- `zephyr/CMakeLists.txt` sees only the module Zephyr selected. A copy replaced by the selection is not reported. CMSIS-DSP sources added a second time by the application's own CMake are not detected.
- heliaDSP's own operations, when added, will join the build as additional sources. Replacing a CMSIS-DSP source file is not available under Zephyr, whose CMSIS-DSP integration compiles aggregate files (one file per function group that `#include`s every source of the group).

## Next

- [Contracts](https://ambiqai.github.io/helia-dsp/guide/contracts/)
- [Toolchains](https://ambiqai.github.io/helia-dsp/guide/toolchains/)
- [Verification](https://ambiqai.github.io/helia-dsp/guide/verification/)
