ESP Dynamic Memory Issue in MBEDTLS with External PSRAM
Posted: 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
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