From 482c0a3c26d9f69a6e4b3cc7da92a5545d16f27a Mon Sep 17 00:00:00 2001 From: me-no-dev Date: Tue, 1 Oct 2024 15:43:05 +0300 Subject: [PATCH] fix(psram): Do not abort if PSRAM is not found Also add to heap in app_main --- cores/esp32/esp32-hal-misc.c | 8 +++++++- cores/esp32/esp32-hal-psram.c | 18 +++++++++++++----- cores/esp32/esp32-hal-psram.h | 1 + 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/cores/esp32/esp32-hal-misc.c b/cores/esp32/esp32-hal-misc.c index d3782c39a..1fb1d2af9 100644 --- a/cores/esp32/esp32-hal-misc.c +++ b/cores/esp32/esp32-hal-misc.c @@ -255,7 +255,8 @@ extern bool btInUse(); #if CONFIG_SPIRAM_SUPPORT || CONFIG_SPIRAM #ifndef CONFIG_SPIRAM_BOOT_INIT ESP_SYSTEM_INIT_FN(init_psram_new, CORE, BIT(0), 99) { - return psramInit() ? ESP_OK : ESP_FAIL; + psramInit(); + return ESP_OK; } #endif #endif @@ -263,6 +264,11 @@ ESP_SYSTEM_INIT_FN(init_psram_new, CORE, BIT(0), 99) { void initArduino() { //init proper ref tick value for PLL (uncomment if REF_TICK is different than 1MHz) //ESP_REG(APB_CTRL_PLL_TICK_CONF_REG) = APB_CLK_FREQ / REF_CLK_FREQ - 1; +#if CONFIG_SPIRAM_SUPPORT || CONFIG_SPIRAM +#ifndef CONFIG_SPIRAM_BOOT_INIT + psramAddToHeap(); +#endif +#endif #ifdef CONFIG_APP_ROLLBACK_ENABLE if (!verifyRollbackLater()) { const esp_partition_t *running = esp_ota_get_running_partition(); diff --git a/cores/esp32/esp32-hal-psram.c b/cores/esp32/esp32-hal-psram.c index d2c5ab96f..6b0b63179 100644 --- a/cores/esp32/esp32-hal-psram.c +++ b/cores/esp32/esp32-hal-psram.c @@ -81,17 +81,25 @@ bool psramInit() { ESP_EARLY_LOGE(TAG, "PSRAM test failed!"); return false; } + ESP_EARLY_LOGI(TAG, "PSRAM enabled"); +#endif /* CONFIG_SPIRAM_BOOT_INIT */ + spiramDetected = true; + return true; +} + +bool psramAddToHeap() { + if (!spiramDetected) { + log_e("PSRAM not initialized!"); + return false; + } if (esp_psram_extram_add_to_heap_allocator() != ESP_OK) { - spiramFailed = true; - ESP_EARLY_LOGE(TAG, "PSRAM could not be added to the heap!"); + log_e("PSRAM could not be added to the heap!"); return false; } #if CONFIG_SPIRAM_USE_MALLOC && !CONFIG_ARDUINO_ISR_IRAM heap_caps_malloc_extmem_enable(CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL); #endif - ESP_EARLY_LOGI(TAG, "PSRAM enabled"); -#endif /* CONFIG_SPIRAM_BOOT_INIT */ - spiramDetected = true; + log_i("PSRAM added to the heap."); return true; } diff --git a/cores/esp32/esp32-hal-psram.h b/cores/esp32/esp32-hal-psram.h index 0ba6763c6..e82af1342 100644 --- a/cores/esp32/esp32-hal-psram.h +++ b/cores/esp32/esp32-hal-psram.h @@ -31,6 +31,7 @@ extern "C" { #endif bool psramInit(); +bool psramAddToHeap(); bool psramFound(); void *ps_malloc(size_t size);