From 082c10002912774fd9de3b0b6e693c75d668bc77 Mon Sep 17 00:00:00 2001
From: me-no-dev <hristo@espressif.com>
Date: Mon, 30 Sep 2024 13:13:39 +0300
Subject: [PATCH 1/2] fix(psram): Init PSRAM before app_main to fix mmu_map

Makes sure that PSRAM is part of the map before app_main is called.
---
 cores/esp32/esp32-hal-log.h   |  1 +
 cores/esp32/esp32-hal-misc.c  | 12 +++++++++---
 cores/esp32/esp32-hal-psram.c | 13 +++++++------
 3 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/cores/esp32/esp32-hal-log.h b/cores/esp32/esp32-hal-log.h
index b8810c8bba9..da63c6dea94 100644
--- a/cores/esp32/esp32-hal-log.h
+++ b/cores/esp32/esp32-hal-log.h
@@ -20,6 +20,7 @@ extern "C" {
 
 #include "sdkconfig.h"
 #include "esp_timer.h"
+#include "rom/ets_sys.h"
 
 #define ARDUHAL_LOG_LEVEL_NONE    (0)
 #define ARDUHAL_LOG_LEVEL_ERROR   (1)
diff --git a/cores/esp32/esp32-hal-misc.c b/cores/esp32/esp32-hal-misc.c
index 82363b97bd0..eaa79a33e93 100644
--- a/cores/esp32/esp32-hal-misc.c
+++ b/cores/esp32/esp32-hal-misc.c
@@ -24,6 +24,7 @@
 #ifdef CONFIG_APP_ROLLBACK_ENABLE
 #include "esp_ota_ops.h"
 #endif  //CONFIG_APP_ROLLBACK_ENABLE
+#include "esp_private/startup_internal.h"
 #ifdef CONFIG_BT_ENABLED
 #include "esp_bt.h"
 #endif  //CONFIG_BT_ENABLED
@@ -249,12 +250,17 @@ extern bool btInUse();
 #endif
 #endif
 
+#if CONFIG_SPIRAM_SUPPORT || CONFIG_SPIRAM
+#ifndef CONFIG_SPIRAM_BOOT_INIT
+ESP_SYSTEM_INIT_FN(init_psram_new, BIT(0), 99) {
+  return psramInit()?ESP_OK:ESP_FAIL;
+}
+#endif
+#endif
+
 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
-  psramInit();
-#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 5a741908f07..6b9a1fb7e8f 100644
--- a/cores/esp32/esp32-hal-psram.c
+++ b/cores/esp32/esp32-hal-psram.c
@@ -31,6 +31,8 @@
 #error Target CONFIG_IDF_TARGET is not supported
 #endif
 
+#define TAG "arduino-psram"
+
 static volatile bool spiramDetected = false;
 static volatile bool spiramFailed = false;
 
@@ -52,7 +54,7 @@ bool psramInit() {
   uint32_t pkg_ver = chip_ver & 0x7;
   if (pkg_ver == EFUSE_RD_CHIP_VER_PKG_ESP32D2WDQ5 || pkg_ver == EFUSE_RD_CHIP_VER_PKG_ESP32PICOD2) {
     spiramFailed = true;
-    log_w("PSRAM not supported!");
+    ESP_EARLY_LOGW(TAG, "PSRAM not supported!");
     return false;
   }
 #elif CONFIG_IDF_TARGET_ESP32S2
@@ -62,7 +64,7 @@ bool psramInit() {
 #endif
   if (esp_psram_init() != ESP_OK) {
     spiramFailed = true;
-    log_w("PSRAM init failed!");
+    ESP_EARLY_LOGW(TAG, "PSRAM init failed!");
 #if CONFIG_IDF_TARGET_ESP32
     if (pkg_ver != EFUSE_RD_CHIP_VER_PKG_ESP32PICOD4) {
       pinMatrixOutDetach(16, false, false);
@@ -71,23 +73,22 @@ bool psramInit() {
 #endif
     return false;
   }
-
   //testSPIRAM() allows user to bypass SPI RAM test routine
   if (!testSPIRAM()) {
     spiramFailed = true;
-    log_e("PSRAM test failed!");
+    ESP_EARLY_LOGE(TAG, "PSRAM test failed!");
     return false;
   }
   if (esp_psram_extram_add_to_heap_allocator() != ESP_OK) {
     spiramFailed = true;
-    log_e("PSRAM could not be added to the heap!");
+    ESP_EARLY_LOGE(TAG, "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 */
-  log_i("PSRAM enabled");
   spiramDetected = true;
   return true;
 }

From 8d42132f96793e819156e1a23041e6cefc3764e3 Mon Sep 17 00:00:00 2001
From: "pre-commit-ci-lite[bot]"
 <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
Date: Tue, 1 Oct 2024 08:14:57 +0000
Subject: [PATCH 2/2] ci(pre-commit): Apply automatic fixes

---
 cores/esp32/esp32-hal-misc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/cores/esp32/esp32-hal-misc.c b/cores/esp32/esp32-hal-misc.c
index eaa79a33e93..722e999f63f 100644
--- a/cores/esp32/esp32-hal-misc.c
+++ b/cores/esp32/esp32-hal-misc.c
@@ -253,7 +253,7 @@ extern bool btInUse();
 #if CONFIG_SPIRAM_SUPPORT || CONFIG_SPIRAM
 #ifndef CONFIG_SPIRAM_BOOT_INIT
 ESP_SYSTEM_INIT_FN(init_psram_new, BIT(0), 99) {
-  return psramInit()?ESP_OK:ESP_FAIL;
+  return psramInit() ? ESP_OK : ESP_FAIL;
 }
 #endif
 #endif