diff --git a/esphome/components/libretiny/__init__.py b/esphome/components/libretiny/__init__.py index 4c8a1999f9..4fbbcde6c3 100644 --- a/esphome/components/libretiny/__init__.py +++ b/esphome/components/libretiny/__init__.py @@ -32,6 +32,7 @@ from .const import ( CONF_SDK_SILENT, CONF_UART_PORT, FAMILIES, + FAMILY_BK7231N, FAMILY_COMPONENT, FAMILY_FRIENDLY, KEY_BOARD, @@ -50,6 +51,22 @@ CODEOWNERS = ["@kuba2k2"] AUTO_LOAD = ["preferences"] IS_TARGET_PLATFORM = True +# BK7231N SDK options to disable unused features. +# Disabling BLE saves ~21KB RAM and ~200KB Flash because BLE init code is +# called unconditionally by the SDK. ESPHome doesn't use BLE on LibreTiny. +# +# This only works on BK7231N (BLE 5.x). Other BK72XX chips using BLE 4.2 +# (BK7231T, BK7231Q, BK7251; BK7252 boards use the BK7251 family) have a bug +# where the BLE library still links and references undefined symbols when +# CFG_SUPPORT_BLE=0. +# +# Other options like CFG_TX_EVM_TEST, CFG_RX_SENSITIVITY_TEST, CFG_SUPPORT_BKREG, +# CFG_SUPPORT_OTA_HTTP, and CFG_USE_SPI_SLAVE were evaluated but provide no # NOLINT +# measurable benefit - the linker already strips unreferenced code via -gc-sections. +_BK7231N_SYS_CONFIG_OPTIONS = [ + "CFG_SUPPORT_BLE=0", +] + def _detect_variant(value): if KEY_LIBRETINY not in CORE.data: @@ -346,4 +363,10 @@ async def component_to_code(config): cg.add_platformio_option("custom_fw_name", "esphome") cg.add_platformio_option("custom_fw_version", __version__) + # Apply chip-specific SDK options to save RAM/Flash + if config[CONF_FAMILY] == FAMILY_BK7231N: + cg.add_platformio_option( + "custom_options.sys_config#h", _BK7231N_SYS_CONFIG_OPTIONS + ) + await cg.register_component(var, config)