Skip to content

Free internal RAM: PSRAM task stacks, BT memory release, smaller LVGL buffer - #5

Open
adampog wants to merge 3 commits into
fix-mcp-xiaozhi-2.5from
claude/project-thread-k610gz
Open

adampog wants to merge 3 commits into
fix-mcp-xiaozhi-2.5from
claude/project-thread-k610gz

Conversation

@adampog

@adampog adampog commented Nov 16, 2026

Copy link
Copy Markdown
Owner

Requested by Alan · project thread

Stacked on #4 (fix-mcp-xiaozhi-2.5, what runs on the robot). Goal: more internal SRAM headroom in AI Agent mode with the LAN MCP server and mDNS on (about 16 KB free now, dips to 1.7 KB at boot).

Not compiled and not run on hardware. The cloud session couldn't build because the Espressif component registry is blocked there. Each change uses IDF 6.1 APIs I checked against the IDF source (xTaskCreateWithCaps/vTaskDeleteWithCaps self-delete, esp_bt_mem_release, BT_CTRL_RUN_IN_FLASH_ONLY on S3).

Changes (estimated internal RAM saved)

Change Est. saved Risk
opus_codec task stack (24 KB) moved to PSRAM. xiaozhi already runs AFE and wake-word tasks on PSRAM stacks. The task never touches flash. 24 KB, all the time Low to medium: Opus encode/decode may be a bit slower. Kconfig STACKCHAN_OPUS_TASK_STACK_IN_PSRAM
CONFIG_BT_CTRL_RUN_IN_FLASH_ONLY=y. Measured from the IDF libs: libbtdm_app.a has 26.5 KB of IRAM, the flash variant has 1.6 KB. On the S3, IRAM comes out of the same SRAM as the heap. ~20-25 KB, all the time Medium: BLE timing is a bit worse, and a BLE link can drop during a flash erase. BLE is only used by Setup and Dance
esp_bt_mem_release(BLE) when AI Agent starts and BLE was never started this boot. Frees BT/NimBLE .bss/.data and the BLE ROM's reserved RAM. Leaving AI Agent already reboots (home indicator, then requestWarmReboot). ble_init refuses to start after a release. Logs before/after free. ~5-20 KB (exact number in the boot log line released BT memory) Low. Kconfig STACKCHAN_RELEASE_BT_MEMORY_FOR_AI_AGENT
LVGL draw buffer cut from 20 lines to 10 (internal DMA) 6.25 KB Low: more SPI flushes per frame. Kconfig STACKCHAN_LVGL_BUFFER_LINES
lan_server start task (4 KB) moved to a PSRAM stack. It only lives until Wi-Fi is up, which is when the boot dip happens. 4 KB at boot Low

Estimated total: about 55-75 KB more free internal RAM in AI Agent mode.

Options not in this PR

  • Network receive tasks (tcp_receive/ssl_receive, 4 KB internal each, one per open connection, in 78/esp-ml307). This is a likely cause of the camera http->Open failure: opening the explain URL needs a new 4 KB internal stack. Moving them to PSRAM means vendoring and patching esp-ml307 (it's a managed component). Medium effort, about 4 KB per connection.
  • CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL 512 → 0/64. This moves small mallocs (cJSON nodes for MCP, std::string) to PSRAM, which could save tens of KB at peaks. lwIP uses plain malloc here, so small pbufs would move to PSRAM too. That's close to the TRY_ALLOCATE_WIFI_LWIP change that broke Wi-Fi. Medium-high risk; try it separately.
  • LVGL task stack to PSRAM (port_cfg.task_stack_caps, ~7 KB). Unsafe if any LVGL callback writes NVS (IDF asserts on flash ops from a PSRAM stack). Needs an audit.
  • Smaller ICache/DCache (32→16 KB) frees SRAM but slows PSRAM-heavy AFE. Not recommended.
  • Rewrite (e.g. drop xiaozhi's AFE/wake word, or move the voice pipeline to a lighter stack). The changes above get most of the gain at a fraction of the risk. Revisit if the remaining options are exhausted.

Testing (on the robot)

  1. Existing build dirs keep their old sdkconfig. Run idf.py fullclean or delete sdkconfig, or set BT_CTRL_RUN_IN_FLASH_ONLY in menuconfig. The new Kconfig options get their defaults automatically.
  2. Boot into AI Agent. Check for HAL-BLE: BLE unused this boot, released BT memory: internal free X -> Y and the periodic heap stats; compare with ~16 KB before.
  3. Talk to it for a few turns (Opus on the PSRAM stack), call MCP tools from ESP-Claw, try the camera tool, and turn the reefwatch view back on.
  4. Run Setup's phone-app BLE Wi-Fi config once to confirm BLE still works from flash.

Generated by Claude Code

opus_codec has a 24 KB stack, the largest on the robot; xiaozhi already
runs its AFE and wake-word tasks on PSRAM stacks. lan_server only lives
until Wi-Fi is up, when internal RAM is at its lowest. Neither touches
flash. Adds a StackChan Memory Kconfig menu.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Xju5qGrWGLo7EfBXNe5Gy2
When AI Agent starts and BLE was never started this boot, release the
BT controller, NimBLE and BLE ROM static RAM to the heap. Leaving AI
Agent reboots, so BLE comes back on the next boot. Also run the BLE
controller from flash, which frees about 25 KB of IRAM.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Xju5qGrWGLo7EfBXNe5Gy2
The buffer lives in internal DMA RAM; 10 lines is 6.25 KB instead of 12.5 KB.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Xju5qGrWGLo7EfBXNe5Gy2
@adampog adampog self-assigned this Nov 16, 2026
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