Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP32: Add menuconfig to use PSRAM for Matter memory allocation #37482

Merged
merged 6 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/esp32/args.gni
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ lwip_platform = "external"
chip_inet_config_enable_tcp_endpoint = false
chip_inet_config_enable_udp_endpoint = true

chip_config_memory_management = "platform"

custom_toolchain = "//third_party/connectedhomeip/config/esp32/toolchain:esp32"

# Avoid constraint forcing for ESP32:
Expand Down
8 changes: 8 additions & 0 deletions config/esp32/components/chip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ if(CONFIG_ENABLE_ICD_SERVER)
endif()
endif()

if(CONFIG_CHIP_MEM_ALLOC_MODE_INTERNAL)
chip_gn_arg_append("chip_memory_alloc_mode" "\"internal\"")
elseif(CONFIG_CHIP_MEM_ALLOC_MODE_EXTERNAL)
chip_gn_arg_append("chip_memory_alloc_mode" "\"external\"")
elseif(CONFIG_CHIP_MEM_ALLOC_MODE_DEFAULT)
chip_gn_arg_append("chip_memory_alloc_mode" "\"default\"")
endif()

if(CONFIG_ENABLE_PW_RPC)
string(APPEND chip_gn_args "import(\"//build_overrides/pigweed.gni\")\n")
chip_gn_arg_append("remove_default_configs" "[\"//third_party/connectedhomeip/third_party/pigweed/repo/pw_build:toolchain_cpp_standard\"]")
Expand Down
21 changes: 21 additions & 0 deletions config/esp32/components/chip/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,27 @@ menu "CHIP Core"
Enable dynamic server to handle a different interaction model dispatch.
Can be implied when users do not want to use the same server clusters.

choice CHIP_MEM_ALLOC_MODE
prompt "Memory allocation strategy"
default CHIP_MEM_ALLOC_MODE_DEFAULT
help
Strategy for Matter memory management
- Internal DRAM memory only
- External SPIRAM memory only
- Either internal or external memory based on default malloc() behavior in ESP-IDF

config CHIP_MEM_ALLOC_MODE_INTERNAL
bool "Internal memory"

config CHIP_MEM_ALLOC_MODE_EXTERNAL
bool "External SPIRAM"
depends on SPIRAM_USE_CAPS_ALLOC || SPIRAM_USE_MALLOC

config CHIP_MEM_ALLOC_MODE_DEFAULT
bool "Default alloc mode"

endchoice # CHIP_MEM_ALLOC_MODE

endmenu # "General Options"

menu "Networking Options"
Expand Down
6 changes: 5 additions & 1 deletion examples/platform/esp32/common/CommonDeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ void CommonDeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, i
break;
}

ESP_LOGI(TAG, "Current free heap: %u\n", static_cast<unsigned int>(heap_caps_get_free_size(MALLOC_CAP_8BIT)));
ESP_LOGI(TAG, "Current free heap: Internal: %u/%u External: %u/%u",
static_cast<unsigned int>(heap_caps_get_free_size(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL)),
static_cast<unsigned int>(heap_caps_get_total_size(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL)),
static_cast<unsigned int>(heap_caps_get_free_size(MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM)),
static_cast<unsigned int>(heap_caps_get_total_size(MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM)));
}

void CommonDeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event)
Expand Down
12 changes: 12 additions & 0 deletions src/platform/ESP32/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import("//build_overrides/chip.gni")

import("${chip_root}/src/inet/inet.gni")
import("${chip_root}/src/lib/core/core.gni")
import("${chip_root}/src/platform/device.gni")

assert(chip_device_platform == "esp32")
Expand All @@ -33,6 +34,7 @@ declare_args() {
chip_use_esp32_ecdsa_peripheral = false
chip_enable_ethernet = false
chip_enable_route_hook = false
chip_memory_alloc_mode = "default"
}

defines = [
Expand Down Expand Up @@ -81,6 +83,16 @@ static_library("ESP32") {
"${chip_root}/src/platform:platform_base",
]

if (chip_config_memory_management == "platform") {
if (chip_memory_alloc_mode == "internal") {
sources += [ "CHIPMem-PlatformInternal.cpp" ]
} else if (chip_memory_alloc_mode == "external") {
sources += [ "CHIPMem-PlatformExternal.cpp" ]
} else {
sources += [ "CHIPMem-PlatformDefault.cpp" ]
}
}

if (chip_enable_ota_requestor) {
sources += [
"OTAImageProcessorImpl.cpp",
Expand Down
67 changes: 67 additions & 0 deletions src/platform/ESP32/CHIPMem-PlatformDefault.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
*
* Copyright (c) 2025 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <lib/support/CHIPMem.h>
#include <platform/CHIPDeviceLayer.h>

#include <esp_heap_caps.h>
#include <stdlib.h>

namespace chip {
namespace Platform {

CHIP_ERROR MemoryAllocatorInit(void * buf, size_t bufSize)
{
return CHIP_NO_ERROR;
}

void MemoryAllocatorShutdown() {}

void * MemoryAlloc(size_t size)
{
return malloc(size);
}

void * MemoryAlloc(size_t size, bool isLongTermAlloc)
{
return malloc(size);
}

void * MemoryCalloc(size_t num, size_t size)
{
return calloc(num, size);
}

void * MemoryRealloc(void * p, size_t size)
{
return realloc(p, size);
}

void MemoryFree(void * p)
{
free(p);
}

bool MemoryInternalCheckPointer(const void * p, size_t min_size)
{
// We don't have a way to know if p is allocated from the heap, just do a null check here.
return (p != nullptr);
}

} // namespace Platform
} // namespace chip
67 changes: 67 additions & 0 deletions src/platform/ESP32/CHIPMem-PlatformExternal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
*
* Copyright (c) 2025 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <lib/support/CHIPMem.h>
#include <platform/CHIPDeviceLayer.h>

#include <esp_heap_caps.h>
#include <stdlib.h>

namespace chip {
namespace Platform {

CHIP_ERROR MemoryAllocatorInit(void * buf, size_t bufSize)
{
return CHIP_NO_ERROR;
}

void MemoryAllocatorShutdown() {}

void * MemoryAlloc(size_t size)
{
return heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
}

void * MemoryAlloc(size_t size, bool isLongTermAlloc)
{
return heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
}

void * MemoryCalloc(size_t num, size_t size)
{
return heap_caps_calloc(num, size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
}

void * MemoryRealloc(void * p, size_t size)
{
return heap_caps_realloc(p, size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
}

void MemoryFree(void * p)
{
heap_caps_free(p);
}

bool MemoryInternalCheckPointer(const void * p, size_t min_size)
{
// We don't have a way to know if p is allocated from the heap, just do a null check here.
return (p != nullptr);
}

} // namespace Platform
} // namespace chip
67 changes: 67 additions & 0 deletions src/platform/ESP32/CHIPMem-PlatformInternal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
*
* Copyright (c) 2025 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <lib/support/CHIPMem.h>
#include <platform/CHIPDeviceLayer.h>

#include <esp_heap_caps.h>
#include <stdlib.h>

namespace chip {
namespace Platform {

CHIP_ERROR MemoryAllocatorInit(void * buf, size_t bufSize)
{
return CHIP_NO_ERROR;
}

void MemoryAllocatorShutdown() {}

void * MemoryAlloc(size_t size)
{
return heap_caps_malloc(size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
}

void * MemoryAlloc(size_t size, bool isLongTermAlloc)
{
return heap_caps_malloc(size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
}

void * MemoryCalloc(size_t num, size_t size)
{
return heap_caps_calloc(num, size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
}

void * MemoryRealloc(void * p, size_t size)
{
return heap_caps_realloc(p, size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
}

void MemoryFree(void * p)
{
heap_caps_free(p);
}

bool MemoryInternalCheckPointer(const void * p, size_t min_size)
{
// We don't have a way to know if p is allocated from the heap, just do a null check here.
return (p != nullptr);
}

} // namespace Platform
} // namespace chip
Loading