Skip to content

Commit

Permalink
Merge branch 'configs_for_controller_ram_optimizatin' into 'main'
Browse files Browse the repository at this point in the history
examples/controller: add configs for controller RAM optimization

See merge request app-frameworks/esp-matter!1039
  • Loading branch information
chshu committed Feb 18, 2025
2 parents 66dbd75 + 6f68333 commit 5477dd4
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 3 deletions.
11 changes: 11 additions & 0 deletions examples/controller/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,14 @@ I cannot send commands to the light from the controller:
- If you are still facing issues, reproduce the issue on the default example for the device and then raise an [issue](https://github.com/espressif/esp-matter/issues). Make sure to share these:
- The complete device logs for both the devices taken over UART.
- The esp-matter and esp-idf branch you are using.

### A1.3 RAM optimization
- The `sdkconfig.defaults.ram_optimization` file is provided for RAM optimization. These configurations enable SPIRAM (CONFIG_SPIRAM=y) and allow the BSS segment to be placed in SPIRAM (CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY). With these configurations, [linker file](./main/linker.lf) can move move BSS segments of certain main controller libraries to SPIRAM. Build the example with the sdkconfig:
```
idf.py -D SDKCONFIG_DEFAULTS="sdkconfig.defaults;sdkconfig.defaults.ram_optimization" set-target esp32s3 build
```
- The OTBR's sdkconfig file `sdkconfig.defaults.otbr` has RAM optimization configurations enabled by default.
- If you encounter a crash with error message: "PSRAM chip not found or not supported, or wrong PSRAM line mode", please check whether the module has SPIRAM and if the SPIRAM mode is configured correctly:
- For 2MB SPIRAM, set `CONFIG_SPIRAM_MODE_QUAD=y`
- For SPIRAM larger than 2MB, set `CONFIG_SPIRAM_MODE_OCT=y`
- Refer to [linker](https://docs.espressif.com/projects/esp-matter/en/latest/esp32/optimizations.html#configuration-options-to-optimize-ram-and-flash) for other options to optimize RAM and Flash.
3 changes: 2 additions & 1 deletion examples/controller/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
idf_component_register(SRC_DIRS "."
INCLUDE_DIRS "."
PRIV_INCLUDE_DIRS "." "${ESP_MATTER_PATH}/examples/common/utils")
PRIV_INCLUDE_DIRS "." "${ESP_MATTER_PATH}/examples/common/utils"
LDFRAGMENTS "linker.lf")

if(CONFIG_SPIFFS_ATTESTATION_TRUST_STORE)
spiffs_create_partition_image(paa_cert ${CMAKE_SOURCE_DIR}/paa_cert FLASH_IN_PROJECT)
Expand Down
6 changes: 6 additions & 0 deletions examples/controller/main/app_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ static void app_event_cb(const ChipDeviceEvent *event, intptr_t arg)
#if CONFIG_OPENTHREAD_BORDER_ROUTER
static bool sThreadBRInitialized = false;
if (!sThreadBRInitialized) {
// TODO: Remove InitThreadStack() and StartThreadTask() if submodule contains the fix
// (https://github.com/project-chip/connectedhomeip/pull/37033)
if (chip::DeviceLayer::ThreadStackMgr().InitThreadStack() != CHIP_NO_ERROR ||
chip::DeviceLayer::ThreadStackMgr().StartThreadTask() != CHIP_NO_ERROR) {
break;
}
esp_openthread_set_backbone_netif(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"));
esp_openthread_lock_acquire(portMAX_DELAY);
esp_openthread_border_router_init();
Expand Down
31 changes: 31 additions & 0 deletions examples/controller/main/linker.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[mapping:CHIP]
archive: libCHIP.a
entries:
if SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY = y:
* (extram_bss)
else:
* (default)

[mapping:esp_matter]
archive: libesp_matter.a
entries:
if SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY = y:
* (extram_bss)
else:
* (default)

[mapping:main]
archive: libmain.a
entries:
if SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY = y:
* (extram_bss)
else:
* (default)

[mapping:openthread_bss]
archive: libopenthread.a
entries:
if SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY = y && OPENTHREAD_ENABLED = y:
* (extram_bss)
else:
* (default)
8 changes: 6 additions & 2 deletions examples/controller/sdkconfig.defaults.otbr
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,17 @@ CONFIG_CHIP_PROJECT_CONFIG="main/matter_project_config.h"
# Enable ble controller
CONFIG_ENABLE_ESP32_BLE_CONTROLLER=y

# SPIRAM
# Use SPIRAM and external alloc
CONFIG_SPIRAM=y
CONFIG_SPIRAM_MODE_QUAD=y
CONFIG_SPIRAM_SPEED_80M=y
CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=512
CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL=8192
CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL=16384
CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY=y
CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP=y
CONFIG_BT_NIMBLE_MEM_ALLOC_MODE_EXTERNAL=y
CONFIG_ESP_MATTER_MEM_ALLOC_MODE_EXTERNAL=y
CONFIG_MBEDTLS_EXTERNAL_MEM_ALLOC=y

# Enable HKDF for mbedtls
CONFIG_MBEDTLS_HKDF_C=y
Expand Down
11 changes: 11 additions & 0 deletions examples/controller/sdkconfig.defaults.ram_optimization
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Use SPIRAM and external alloc
CONFIG_SPIRAM=y
CONFIG_SPIRAM_SPEED_80M=y
CONFIG_SPIRAM_MODE_OCT=y
CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=512
CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL=16384
CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY=y
CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP=y
CONFIG_BT_NIMBLE_MEM_ALLOC_MODE_EXTERNAL=y
CONFIG_ESP_MATTER_MEM_ALLOC_MODE_EXTERNAL=y
CONFIG_MBEDTLS_EXTERNAL_MEM_ALLOC=y

0 comments on commit 5477dd4

Please sign in to comment.