Skip to content

Commit

Permalink
Merge branch 'suppport/ot_message_pool_using_PSRAM' into 'master'
Browse files Browse the repository at this point in the history
feat(openthread): Add support to allocate message pool from PSRAM

See merge request espressif/esp-idf!25760
  • Loading branch information
zwx1995esp committed Sep 15, 2023
2 parents addfc0d + 60bb5b0 commit a9349f4
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 92 deletions.
5 changes: 5 additions & 0 deletions components/openthread/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ if(CONFIG_OPENTHREAD_ENABLED)
"src/port/esp_openthread_sleep.c")
endif()

if(NOT CONFIG_OPENTHREAD_PLATFORM_MSGPOOL_MANAGEMENT)
list(APPEND exclude_srcs
"src/port/esp_openthread_messagepool.c")
endif()

if(CONFIG_OPENTHREAD_FTD)
set(device_type "OPENTHREAD_FTD=1")
elseif(CONFIG_OPENTHREAD_MTD)
Expand Down
10 changes: 9 additions & 1 deletion components/openthread/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,19 @@ menu "OpenThread"
help
Select this option to enable border router features in OpenThread.

config OPENTHREAD_PLATFORM_MSGPOOL_MANAGEMENT
bool 'Allocate message pool buffer from PSRAM'
depends on OPENTHREAD_ENABLED && (SPIRAM_USE_CAPS_ALLOC || SPIRAM_USE_MALLOC)
default n
help
If enabled, the message pool is managed by platform defined logic.

config OPENTHREAD_NUM_MESSAGE_BUFFERS
int "The number of openthread message buffers"
depends on OPENTHREAD_ENABLED
default 65
range 50 100
range 10 100 if !OPENTHREAD_PLATFORM_MSGPOOL_MANAGEMENT
range 10 8191 if OPENTHREAD_PLATFORM_MSGPOOL_MANAGEMENT

config OPENTHREAD_DNS64_CLIENT
bool "Use dns64 client"
Expand Down
25 changes: 0 additions & 25 deletions components/openthread/private_include/esp_openthread_flash.h

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -517,4 +517,23 @@
#define OPENTHREAD_CONFIG_OPERATIONAL_DATASET_AUTO_INIT 1
#endif


/**
*
* Define as 1 to enable support for allocating message pool buffer in PSRAM
*
*/
#if CONFIG_OPENTHREAD_PLATFORM_MSGPOOL_MANAGEMENT

/**
* @def OPENTHREAD_CONFIG_PLATFORM_MESSAGE_MANAGEMENT
*
* The message pool is managed by platform defined logic when this flag is set.
* This feature would typically be used when operating in a multi-threaded system
* and multiple threads need to access the message pool.
*
*/
#define OPENTHREAD_CONFIG_PLATFORM_MESSAGE_MANAGEMENT 1
#endif

#define OPENTHREAD_FTD 1
1 change: 0 additions & 1 deletion components/openthread/src/esp_openthread_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "esp_log.h"
#include "esp_openthread_alarm.h"
#include "esp_openthread_common_macro.h"
#include "esp_openthread_flash.h"
#include "esp_openthread_lock.h"
#include "esp_openthread_radio.h"
#include "esp_openthread_spi_slave.h"
Expand Down
65 changes: 0 additions & 65 deletions components/openthread/src/port/esp_openthread_flash.c

This file was deleted.

52 changes: 52 additions & 0 deletions components/openthread/src/port/esp_openthread_messagepool.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "openthread-core-config.h"
#include "esp_openthread_common_macro.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_heap_caps.h"
#include "openthread/instance.h"
#include "openthread/platform/messagepool.h"

int s_buffer_pool_head = -1;
otMessageBuffer **s_buffer_pool_pointer = NULL;

void otPlatMessagePoolInit(otInstance *aInstance, uint16_t aMinNumFreeBuffers, size_t aBufferSize)
{
otMessageBuffer *buffer_pool = (otMessageBuffer *)heap_caps_calloc(aMinNumFreeBuffers, aBufferSize, MALLOC_CAP_SPIRAM);
s_buffer_pool_pointer = (otMessageBuffer **)heap_caps_calloc(aMinNumFreeBuffers, sizeof(otMessageBuffer **), MALLOC_CAP_SPIRAM);
if (buffer_pool == NULL || s_buffer_pool_pointer == NULL) {
ESP_LOGE(OT_PLAT_LOG_TAG, "Failed to create message buffer pool");
assert(false);
}
for (uint16_t i = 0; i < aMinNumFreeBuffers; i++) {
s_buffer_pool_pointer[i] = buffer_pool + i * aBufferSize / sizeof(otMessageBuffer);
}
s_buffer_pool_head = aMinNumFreeBuffers - 1;
ESP_LOGI(OT_PLAT_LOG_TAG, "Create message buffer pool successfully, size %d", aMinNumFreeBuffers*aBufferSize);
}

otMessageBuffer *otPlatMessagePoolNew(otInstance *aInstance)
{
otMessageBuffer *ret = NULL;
if (s_buffer_pool_head >= 0) {
ret = s_buffer_pool_pointer[s_buffer_pool_head];
s_buffer_pool_head--;
}
return ret;
}

void otPlatMessagePoolFree(otInstance *aInstance, otMessageBuffer *aBuffer)
{
s_buffer_pool_head++;
s_buffer_pool_pointer[s_buffer_pool_head] = aBuffer;
}

uint16_t otPlatMessagePoolNumFreeBuffers(otInstance *aInstance)
{
return s_buffer_pool_head + 1;
}
1 change: 1 addition & 0 deletions docs/en/api-guides/performance/ram-usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ To minimize static memory use:

- Constant data can be stored in flash memory instead of RAM, thus it is recommended to declare structures, buffers, or other variables as ``const``. This approach may require modifying firmware functions to accept ``const *`` arguments instead of mutable pointer arguments. These changes can also help reduce the stack usage of certain functions.
:SOC_BT_SUPPORTED: - If using Bluedroid, setting the option :ref:`CONFIG_BT_BLE_DYNAMIC_ENV_MEMORY` will cause Bluedroid to allocate memory on initialization and free it on deinitialization. This does not necessarily reduce the peak memory usage, but changes it from static memory usage to runtime memory usage.
- If using OpenThread, enabling the option :ref:`CONFIG_OPENTHREAD_PLATFORM_MSGPOOL_MANAGEMENT` will cause OpenThread to allocate message pool buffers from PSRAM, which will reduce static memory use.

.. _optimize-stack-sizes:

Expand Down
1 change: 1 addition & 0 deletions docs/zh_CN/api-guides/performance/ram-usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ ESP-IDF 包含一系列堆 API,可以在运行时测量空闲堆内存,请

- 由于常量数据可以存储在 flash 中,不占用 RAM,建议尽量将结构体、缓冲区或其他变量声明为 ``const``。为此,可能需要修改固件参数,使其接收 ``const *`` 参数而非可变指针参数。以上更改还可以减少某些函数的栈内存使用。
:SOC_BT_SUPPORTED: - 若使用 Bluedroid,请设置 :ref:`CONFIG_BT_BLE_DYNAMIC_ENV_MEMORY` 选项,Bluedroid 将在初始化时分配内存,并在去初始化时释放内存。这并不一定会降低内存使用峰值,但可以将使用静态内存改为运行时使用动态内存。
- 若使用 OpenThread,请设置 :ref:`CONFIG_OPENTHREAD_PLATFORM_MSGPOOL_MANAGEMENT` 选项,OpenThread 将从外部 PSRAM 中分配消息池缓冲区,从而减少对内部静态内存的使用。

.. _optimize-stack-sizes:

Expand Down

0 comments on commit a9349f4

Please sign in to comment.