From bb2e436e7451c86c623ccd3f7974b718a9c15c9e Mon Sep 17 00:00:00 2001 From: Kenta IDA Date: Thu, 26 Dec 2024 05:34:47 +0900 Subject: [PATCH 1/3] Reduce maximum RAM usage and disable SPIRAM by default. --- sdkconfig.defaults | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/sdkconfig.defaults b/sdkconfig.defaults index 16712df..3f2f0ea 100644 --- a/sdkconfig.defaults +++ b/sdkconfig.defaults @@ -1,3 +1,7 @@ +# ESP32-S3 target +CONFIG_IDF_TARGET="esp32s3" +CONFIG_IDF_TARGET_ESP32S3=y + # ESP Event Loop on Linux CONFIG_ESP_EVENT_POST_FROM_ISR=n CONFIG_ESP_EVENT_POST_FROM_IRAM_ISR=n @@ -11,7 +15,7 @@ CONFIG_ESP_TLS_SKIP_SERVER_CERT_VERIFY=y CONFIG_MBEDTLS_SSL_PROTO_DTLS=y # libpeer requires large stack allocations -CONFIG_ESP_MAIN_TASK_STACK_SIZE=16384 +CONFIG_ESP_MAIN_TASK_STACK_SIZE=10240 # Defaults to partitions.csv CONFIG_PARTITION_TABLE_CUSTOM=y @@ -19,8 +23,9 @@ CONFIG_PARTITION_TABLE_CUSTOM=y # Set highest CPU Freq CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240=y -CONFIG_SPIRAM=y -CONFIG_SPIRAM_MODE_OCT=y +# No PSRAM by default. +# CONFIG_SPIRAM=y +# CONFIG_SPIRAM_MODE_OCT=y # Disable Watchdog # CONFIG_ESP_INT_WDT is not set @@ -29,3 +34,7 @@ CONFIG_SPIRAM_MODE_OCT=y # Enable Compiler Optimization CONFIG_COMPILER_OPTIMIZATION_PERF=y CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE=y + +# Optimize internal SRAM utilization. +# Allocate WiFi TX buffer dynamically to reduce max RAM usage. +CONFIG_ESP_WIFI_DYNAMIC_TX_BUFFER=y \ No newline at end of file From 3c19aa4971f49c4e399e3521931b703acd9ad375 Mon Sep 17 00:00:00 2001 From: Kenta IDA Date: Thu, 26 Dec 2024 05:35:32 +0900 Subject: [PATCH 2/3] Fix that stack memory allocation fails when SPIRAM is disabled. --- src/webrtc.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/webrtc.cpp b/src/webrtc.cpp index bc1782d..e71f17e 100644 --- a/src/webrtc.cpp +++ b/src/webrtc.cpp @@ -59,9 +59,19 @@ static void oai_onconnectionstatechange_task(PeerConnectionState state, #endif } else if (state == PEER_CONNECTION_CONNECTED) { #ifndef LINUX_BUILD +#if CONFIG_SPIRAM + constexpr size_t stack_size = 20000; StackType_t *stack_memory = (StackType_t *)heap_caps_malloc( 20000 * sizeof(StackType_t), MALLOC_CAP_SPIRAM); - xTaskCreateStaticPinnedToCore(oai_send_audio_task, "audio_publisher", 20000, +#else // CONFIG_SPIRAM + constexpr size_t stack_size = 20000; + StackType_t *stack_memory = (StackType_t *)malloc(stack_size * sizeof(StackType_t)); +#endif // CONFIG_SPIRAM + if (stack_memory == nullptr) { + ESP_LOGE(LOG_TAG, "Failed to allocate stack memory for audio publisher."); + esp_restart(); + } + xTaskCreateStaticPinnedToCore(oai_send_audio_task, "audio_publisher", stack_size, NULL, 7, stack_memory, &task_buffer, 0); #endif } From 7cc4ef34d1ae6b3f694deae21d1c11104e1314db Mon Sep 17 00:00:00 2001 From: Kenta IDA Date: Fri, 10 Jan 2025 06:59:30 +0900 Subject: [PATCH 3/3] Use heap_caps_malloc_prefer instead of switching allocator by CONFIG_SPIRAM. --- src/webrtc.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/webrtc.cpp b/src/webrtc.cpp index e71f17e..cde4c55 100644 --- a/src/webrtc.cpp +++ b/src/webrtc.cpp @@ -59,20 +59,20 @@ static void oai_onconnectionstatechange_task(PeerConnectionState state, #endif } else if (state == PEER_CONNECTION_CONNECTED) { #ifndef LINUX_BUILD -#if CONFIG_SPIRAM constexpr size_t stack_size = 20000; - StackType_t *stack_memory = (StackType_t *)heap_caps_malloc( - 20000 * sizeof(StackType_t), MALLOC_CAP_SPIRAM); -#else // CONFIG_SPIRAM - constexpr size_t stack_size = 20000; - StackType_t *stack_memory = (StackType_t *)malloc(stack_size * sizeof(StackType_t)); -#endif // CONFIG_SPIRAM + // Allocate the stack memory from the PSRAM if available. Otherwise, + // allocate from the internal memory. + StackType_t *stack_memory = (StackType_t *)heap_caps_malloc_prefer( + stack_size * sizeof(StackType_t), 2, + MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT, + MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); if (stack_memory == nullptr) { ESP_LOGE(LOG_TAG, "Failed to allocate stack memory for audio publisher."); esp_restart(); } - xTaskCreateStaticPinnedToCore(oai_send_audio_task, "audio_publisher", stack_size, - NULL, 7, stack_memory, &task_buffer, 0); + xTaskCreateStaticPinnedToCore(oai_send_audio_task, "audio_publisher", + stack_size, NULL, 7, stack_memory, + &task_buffer, 0); #endif } }