ESP Dynamic Memory Issue in MBEDTLS with External PSRAM

nirav.agrawal
Posts: 5
Joined: Fri Mar 01, 2019 4:41 am

ESP Dynamic Memory Issue in MBEDTLS with External PSRAM

Postby nirav.agrawal » Wed Sep 25, 2019 1:39 pm

Hello,
I am using ESP32 8MB Flash + 4MB PSRAM variant for our project.

ESP-IDF SDK : 3.3 LTS
Toolchain:
Toolchain version: crosstool-ng-1.22.0-80-g6c4433a
Compiler version: 5.2.0

Please find PSRAM & MBEDTLS configurations which we used for our project at the end.

With internal 512KB RAM , there was not enough memory available hence we decided to use ESP32 module which provides 4MB of PSRAM.
But I got memory allocation issues with PSRAM configurations.

I do have few questions and issues which seems blocker for my project.
1) There is dynamic memory allocation problem from mbedtls module even if mbedtls memory configurations set to "default" or "external ram".
log print : E (13832) aws_iot: failed! mbedtls_ssl_handshake returned -0x4310
>> Is there any issue such that mbedTLS failed to acquire memory from PSRAM (it works fine with internal memory setting but we cant use this option!!).

2) I am not much confident on PSRAM full usage for dynamic memory configurations. I do not get exact flow with which I can configure PSRAM to be used by ESP32 module. How do i ensure that 4MB memory is fully utilized and there will not be any trouble while allocation of memory by any component?

3) Can I configure PSRAM to be used with static memory (.bss & data) as well as dynamic ?

4) is there any missing or incorrect configurations which I need to update in sdkconfig?

#
# SPI RAM config
#
CONFIG_SPIRAM_BOOT_INIT=y
CONFIG_SPIRAM_IGNORE_NOTFOUND=
CONFIG_SPIRAM_USE_MEMMAP=
CONFIG_SPIRAM_USE_CAPS_ALLOC=
CONFIG_SPIRAM_USE_MALLOC=y
CONFIG_SPIRAM_TYPE_AUTO=y
CONFIG_SPIRAM_TYPE_ESPPSRAM32=
CONFIG_SPIRAM_TYPE_ESPPSRAM64=
CONFIG_SPIRAM_SIZE=-1
CONFIG_SPIRAM_SPEED_40M=y
CONFIG_SPIRAM_MEMTEST=y
CONFIG_SPIRAM_CACHE_WORKAROUND=y
CONFIG_SPIRAM_BANKSWITCH_ENABLE=y
CONFIG_SPIRAM_BANKSWITCH_RESERVE=8
CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=16384
CONFIG_WIFI_LWIP_ALLOCATION_FROM_SPIRAM_FIRST=y
CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL=65536
CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY=
CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY=
CONFIG_PICO_PSRAM_CS_IO=10

#
# mbedTLS
#
CONFIG_MBEDTLS_INTERNAL_MEM_ALLOC=
CONFIG_MBEDTLS_EXTERNAL_MEM_ALLOC=
CONFIG_MBEDTLS_DEFAULT_MEM_ALLOC=y (i have tried with external memory configuration as well!)
CONFIG_MBEDTLS_CUSTOM_MEM_ALLOC=
CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=8192
CONFIG_MBEDTLS_ASYMMETRIC_CONTENT_LEN=
CONFIG_MBEDTLS_DEBUG=
CONFIG_MBEDTLS_HARDWARE_AES=y
CONFIG_MBEDTLS_HARDWARE_MPI=
CONFIG_MBEDTLS_HARDWARE_SHA=
CONFIG_MBEDTLS_HAVE_TIME=y
CONFIG_MBEDTLS_HAVE_TIME_DATE=
CONFIG_MBEDTLS_TLS_SERVER_AND_CLIENT=y
CONFIG_MBEDTLS_TLS_SERVER_ONLY=
CONFIG_MBEDTLS_TLS_CLIENT_ONLY=
CONFIG_MBEDTLS_TLS_DISABLED=
CONFIG_MBEDTLS_TLS_SERVER=y
CONFIG_MBEDTLS_TLS_CLIENT=y
CONFIG_MBEDTLS_TLS_ENABLED=y

Please suggest me the best resolution to this issue.

Thanks,
Nirav Agrawal

cashchew
Posts: 5
Joined: Wed Sep 18, 2019 4:14 pm

Re: ESP Dynamic Memory Issue in MBEDTLS with External PSRAM

Postby cashchew » Sun Sep 06, 2020 10:41 pm

I have the same problem with esp-idf v4.0.1

ESP_Mahavir
Posts: 190
Joined: Wed Jan 24, 2018 6:51 am

Re: ESP Dynamic Memory Issue in MBEDTLS with External PSRAM

Postby ESP_Mahavir » Mon Sep 07, 2020 6:39 am

Hi Nirav,

For using external SPIRAM in mbedTLS allocation, please keep `CONFIG_MBEDTLS_EXTERNAL_MEM_ALLOC` selected in sdkconfig. This config option allows to redirect all mbedTLS dynamic allocation to SPIRAM, can be easily verified with some debug prints at https://github.com/espressif/esp-idf/bl ... _mem.c#L27

Additionally, it will be helpful to collect heap statistics here. For example, free heap size, largest free block etc. That will allow to provide some more information to understand the problem. Relevant APIs can be found in https://docs.espressif.com/projects/esp ... alloc.html

If you still observe the same issue, please provide detailed console log.

Mahavir

hspuneet
Posts: 2
Joined: Tue Jul 16, 2019 3:44 am

Re: ESP Dynamic Memory Issue in MBEDTLS with External PSRAM

Postby hspuneet » Thu May 27, 2021 11:31 am

IDF 4.2.1

:cry: Was getting this error randomly in ESP32-S2 wrover-4Mb/2Mb :esp-tls-mbedtls: mbedtls_ssl_setup returned -0x7f00

If i disable SPI RAM then no error. After some digging, changed mbed_TLS Memory Allocation strategy in sdkconfig from "internal memory" to "default alloc mode" . Running without error for now. :?

Who is online

Users browsing this forum: Bing [Bot], ShinyGlossy and 324 guests