Skip to content

Allocate NPL controller and host memory from internal RAM - #1

Merged
hhornbacher merged 1 commit into
peeriot:feature/ble-host-npl-upstreamfrom
hhornbacher:fix/ble-npl-internal-memory
Sep 28, 2026
Merged

hhornbacher merged 1 commit into
peeriot:feature/ble-host-npl-upstreamfrom
hhornbacher:fix/ble-npl-internal-memory

Conversation

@hhornbacher

@hhornbacher hhornbacher commented Sep 25, 2026 •

Copy link
Copy Markdown

Fixes peeriot/myrmic#6 (the issue closes once myrmic picks up the new revision of this branch).

On chips with PSRAM (ESP32-C5, C61) the NimBLE controller and the NPL host objects end up in PSRAM, and the controller faults on the first list walk after scanning starts:

[ProCpu] Exception 'Load access fault (5)' mepc=0x4080c65c, mtval=0x408b1238
0x4080c65c - r_ble_hw_list_details_dump

compat::malloc::malloc/calloc are the untagged esp-alloc C shims (EnumSet::empty()); only the *_internal variants request MemoryCapability::Internal. A firmware that registers PSRAM as the first heap region, so that general allocations prefer PSRAM and internal DRAM stays free for tagged callers, therefore puts the controller's memory in PSRAM. The ESP32/C3/S3 adapters already hand the controller malloc_internal; the NPL path for the C5/C6/C61/H2 controllers did not, and ble::malloc_internal was cfg-gated to the older chips.

Change

  • NPL funcs table: malloc → crate::ble::malloc_internal.
  • NPL events, callouts and the two msys mbuf pools: calloc → calloc_internal.
  • ble::malloc_internal gated on both controller kinds (bt_controller = "btdm" or "npl") instead of the three btdm chips; ble::malloc, now only used by the btdm adapters, gated to btdm.

free is unchanged: the esp-alloc shim reads the stored size, so it frees both kinds.

Verification (ESP32-C5 rev 1.0, 8 MB PSRAM)

Scaffolded firmware (myrmic new --firmware=esp32c5), deployed the adapter-ruuvitag-pro BLE cell, sent enable; one variable per round:

round change result
baseline this branch's head crash above, every time
stacks BLE host and HCI thread stacks doubled identical crash
heap order PSRAM registered last (diagnostic only) runs
this fix NPL allocations internal, PSRAM-first heap unchanged runs, scanning
this fix, end to end adapter-govee-h5075 deployed, enable receives and decodes advertisements (Received measurement from A4:C1:38:…: 23.74 °C, 35.0 %, battery 76 %, repeating)

Not touched, but the same pattern: the WiFi OS adapter hands the blob the untagged malloc as well (wifi/os_adapter/mod.rs). WiFi worked in every round above, so it is left for a separate look.

Changelog

esp-radio

  • Fixed: On chips with PSRAM, the NimBLE controller and NPL host memory (events, callouts, msys mbuf pools) is allocated from internal RAM instead of PSRAM

@github-actions

Copy link
Copy Markdown

⚠️ Do not edit CHANGELOG.md manually

This PR modifies the following changelog file(s) directly:

  • esp-radio/CHANGELOG.md

Changelog entries are no longer maintained by hand. Instead, add your entries to the PR description under the # Changelog section:

# Changelog

## esp-hal

- Added: Short description of the change
- Fixed: Short description of the fix

Migration guide entries go in the # Migration guide section (area is required; each breaking change needs a ### Title heading):

# Migration guide

## esp-hal/SPI

### `OldType` has been renamed to `NewType`

Replace all uses of `OldType` with `NewType`.

Please revert the changes to CHANGELOG.md and move your entries to the PR description instead.
Run cargo xtask check-pr-changelog locally to validate the format before pushing.

@hhornbacher
hhornbacher force-pushed the fix/ble-npl-internal-memory branch 2 times, most recently from 2536542 to 7a0b3f5 Compare September 25, 2026 16:03
- Hand the NimBLE controller malloc_internal instead of the untagged malloc
- Allocate NPL events, callouts and the msys mbuf pools with calloc_internal
- Make ble::malloc_internal available on every chip, not only ESP32/C3/S3
@hhornbacher
hhornbacher force-pushed the fix/ble-npl-internal-memory branch from 7a0b3f5 to 2e71bba Compare September 25, 2026 16:05

@Lakier15 Lakier15 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@hhornbacher
hhornbacher merged commit 17c3248 into peeriot:feature/ble-host-npl-upstream Sep 28, 2026
18 of 25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants