8000
Skip to content

[libretiny] Disable BLE stack on BK7231N to save ~21KB RAM - #13131

Merged
bdraco merged 5 commits into
devfrom
bk72xx_ble_disable
Jan 11, 2026
Merged

[libretiny] Disable BLE stack on BK7231N to save ~21KB RAM#13131
bdraco merged 5 commits into
devfrom
bk72xx_ble_disable

Conversation

@bdraco
@bdraco bdraco commented Jan 11, 2026
Copy link
Copy Markdown
Member

What does this implement/fix?

Disables the BLE stack on BK7231N to save ~21KB RAM and ~200KB Flash.

ESPHome currently has no BLE support on LibreTiny platforms, so the BLE stack is pure waste. The Beken SDK unconditionally initializes it when CFG_SUPPORT_BLE=1 (the default), consuming significant RAM on memory-constrained devices.

This can be flipped back on in a future PR when BLE support is added for BK7231N.

Before this change, users with typical configurations (WiFi, API, web_server, a few sensors) were seeing only ~10-13KB free heap at runtime, risking crashes under load. After this change, free heap increases to ~30KB+, providing a much safer margin.

Memory savings (BK7231N):

Metric Before After Savings
RAM 94,948 bytes (36.2%) 73,332 bytes (28.0%) ~21KB
Flash 802,024 bytes (74.0%) 577,030 bytes (53.3%) ~225KB

Why only BK7231N?
Other BK72XX chips (BK7231T, BK7251, BK7252) use BLE 4.2 which has a bug where the BLE library still links and references undefined symbols when CFG_SUPPORT_BLE=0. BK7231N uses BLE 5.x which doesn't have this issue.

Re-enabling BLE (for development/testing):
Users can override this default via platformio_options if needed:

esphome:
  platformio_options:
    "custom_options.sys_config#h":
      - "CFG_SUPPORT_BLE=1"

Other SDK options (CFG_TX_EVM_TEST, CFG_RX_SENSITIVITY_TEST, CFG_SUPPORT_BKREG, CFG_SUPPORT_OTA_HTTP, CFG_USE_SPI_SLAVE) were evaluated but provide no measurable benefit - the linker already strips unreferenced code via -gc-sections. Only BLE has an impact because its init code is called unconditionally.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Developer breaking change (an API change that could break external components)
  • Code quality improvements to existing code or addition of tests
  • Other

Related issue or feature (if applicable):

  • Improves memory situation for BK72XX users experiencing heap exhaustion

Pull request in esphome-docs with documentation (if applicable):

  • N/A (no user-facing configuration changes)

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx
  • nRF52840

Example entry for config.yaml:

# No configuration needed - BLE is now disabled automatically on BK7231N
bk72xx:
  board: cb3s

wifi:
  ssid: "MySSID"
  password: "MyPassword"

api:
ota:
  - platform: esphome
web_server:

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

@github-actions
Copy link
Copy Markdown
Contributor

To use the changes from this PR as an external component, add the following to your ESPHome configuration YAML file:

external_components:
  - source: github://pr#13131
    components: [libretiny]
    refresh: 1h

(Added by the PR bot)

@codecov-commenter
codecov-commenter commented Jan 11, 2026
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 73.73%. Comparing base (de82f96) to head (b3a2167).
⚠️ Report is 1 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev   #13131      +/-   ##
==========================================
- Coverage   73.76%   73.73%   -0.03%     
==========================================
  Files          53       53              
  Lines       11329    11329              
  Branches     1538     1538              
==========================================
- Hits         8357     8354       -3     
- Misses       2572     2574       +2     
- Partials      400      401       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bdraco bdraco changed the title [bk72xx] Disable BLE stack to save ~21KB RAM [libretiny] Disable BLE stack on BK7231N to save ~21KB RAM Jan 11, 2026
@github-actions
github-actions Bot commented Jan 11, 2026
Copy link
Copy Markdown
Contributor

Memory Impact Analysis

Components: libretiny
Platform: bk72xx-ard

Metric Target Branch This PR Change
RAM 79,564 bytes 79,564 bytes ➡️ +0 bytes (0.00%)
Flash 250,240 bytes 250,240 bytes ➡️ +0 bytes (0.00%)

Note: This analysis measures static RAM and Flash usage only (compile-time allocation).
Dynamic memory (heap) cannot be measured automatically.
⚠️ You must test this PR on a real device to measure free heap and ensure no runtime memory issues.

This analysis runs automatically when components change. Memory usage is measured from a representative test configuration.

@bdraco
bdraco commented Jan 11, 2026
Copy link
Copy Markdown
Member Author

memory impact is misleading because it doesn't build with bk7231n

@bdraco
bdraco marked this pull request as ready for review January 11, 2026 01:04
Copilot AI review requested due to automatic review settings January 11, 2026 01:04
@github-actions
github-actions Bot requested a review from kuba2k2 January 11, 2026 01:04
@github-actions
Copy link
Copy Markdown
Contributor

👋 Hi there! I've automatically requested reviews from codeowners based on the files changed in this PR.

@kuba2k2 - You've been requested to review this PR as codeowner(s) of 1 file(s) that were modified. Thanks for your time! 🙏

Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR disables the BLE stack on BK7231N chips to save approximately 21KB of RAM and 200KB of Flash memory. Since ESPHome currently has no BLE support on LibreTiny platforms, the BLE stack is consuming resources unnecessarily. The change only applies to BK7231N because other BK72XX chips have a linker bug when BLE is disabled.

Changes:

  • Adds SDK configuration option to disable BLE stack on BK7231N only
  • Applies the configuration via PlatformIO's custom_options mechanism
  • Includes detailed documentation explaining why this is BK7231N-specific

Comment thread esphome/components/libretiny/__init__.py Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@bdraco
bdraco commented Jan 11, 2026
Copy link
Copy Markdown
Member Author

On a device that has only 5-6 entities it does from 33904 heap free to 59328 heap free

Actual savings are better than just the static ram savings. > 25K nearly doubling the available heap

Screenshot 2026-01-10 at 4 29 32 PM

@bdraco
bdraco commented Jan 11, 2026
Copy link
Copy Markdown
Member Author

Thanks

@bdraco
bdraco merged commit 6222fae into dev Jan 11, 2026
23 checks passed
@bdraco
bdraco deleted the bk72xx_ble_disable branch January 11, 2026 02:43
@github-actions github-actions Bot locked and limited conversation to collaborators Jan 13, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

0