Skip to content

Commit dc864c4

Browse files
committed
Add option to continue running (with less ram) if psram is enabled but not detected
1 parent ca3faa6 commit dc864c4

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

components/esp32/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ config SPIRAM_BOOT_INIT
4545
have specific requirements, you'll want to leave this enabled so memory allocated
4646
during boot-up can also be placed in SPI RAM.
4747

48+
config SPIRAM_IGNORE_NOTFOUND
49+
bool "Ignore PSRAM when not found"
50+
default "n"
51+
depends on SPIRAM_BOOT_INIT
52+
help
53+
Normally, if psram initialization is enabled during compile time but not found at runtime, it
54+
is seen as an error making the ESP32 panic. If this is enabled, the ESP32 will keep on
55+
running but will not add the (non-existing) RAM to any allocator.
56+
4857
choice SPIRAM_USE
4958
prompt "SPI RAM access method"
5059
default SPIRAM_USE_MALLOC

components/esp32/cpu_start.c

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ struct object { long placeholder[ 10 ]; };
102102
void __register_frame_info (const void *begin, struct object *ob);
103103
extern char __eh_frame[];
104104

105+
//If CONFIG_SPIRAM_IGNORE_NOTFOUND is set and external RAM is not found or errors out on testing, this is set to false.
106+
static bool s_spiram_okay=true;
107+
105108
/*
106109
* We arrive here after the bootloader finished loading the program from flash. The hardware is mostly uninitialized,
107110
* and the app CPU is in reset. We do have a stack, so we can do the initialization in C.
@@ -147,8 +150,13 @@ void IRAM_ATTR call_start_cpu0()
147150
#if CONFIG_SPIRAM_BOOT_INIT
148151
esp_spiram_init_cache();
149152
if (esp_spiram_init() != ESP_OK) {
153+
#if CONFIG_SPIRAM_IGNORE_NOTFOUND
154+
ESP_EARLY_LOGI(TAG, "Failed to init external RAM; continuing without it.");
155+
s_spiram_okay = false;
156+
#else
150157
ESP_EARLY_LOGE(TAG, "Failed to init external RAM!");
151158
abort();
159+
#endif
152160
}
153161
#endif
154162

@@ -182,10 +190,12 @@ void IRAM_ATTR call_start_cpu0()
182190

183191

184192
#if CONFIG_SPIRAM_MEMTEST
185-
bool ext_ram_ok=esp_spiram_test();
186-
if (!ext_ram_ok) {
187-
ESP_EARLY_LOGE(TAG, "External RAM failed memory test!");
188-
abort();
193+
if (s_spiram_okay) {
194+
bool ext_ram_ok=esp_spiram_test();
195+
if (!ext_ram_ok) {
196+
ESP_EARLY_LOGE(TAG, "External RAM failed memory test!");
197+
abort();
198+
}
189199
}
190200
#endif
191201

@@ -252,23 +262,25 @@ void start_cpu0_default(void)
252262
esp_err_t err;
253263
esp_setup_syscall_table();
254264

265+
if (s_spiram_okay) {
255266
#if CONFIG_SPIRAM_BOOT_INIT && (CONFIG_SPIRAM_USE_CAPS_ALLOC || CONFIG_SPIRAM_USE_MALLOC)
256-
esp_err_t r=esp_spiram_add_to_heapalloc();
257-
if (r != ESP_OK) {
258-
ESP_EARLY_LOGE(TAG, "External RAM could not be added to heap!");
259-
abort();
260-
}
267+
esp_err_t r=esp_spiram_add_to_heapalloc();
268+
if (r != ESP_OK) {
269+
ESP_EARLY_LOGE(TAG, "External RAM could not be added to heap!");
270+
abort();
271+
}
261272
#if CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL
262-
r=esp_spiram_reserve_dma_pool(CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL);
263-
if (r != ESP_OK) {
264-
ESP_EARLY_LOGE(TAG, "Could not reserve internal/DMA pool!");
265-
abort();
266-
}
273+
r=esp_spiram_reserve_dma_pool(CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL);
274+
if (r != ESP_OK) {
275+
ESP_EARLY_LOGE(TAG, "Could not reserve internal/DMA pool!");
276+
abort();
277+
}
267278
#endif
268279
#if CONFIG_SPIRAM_USE_MALLOC
269-
heap_caps_malloc_extmem_enable(CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL);
280+
heap_caps_malloc_extmem_enable(CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL);
270281
#endif
271282
#endif
283+
}
272284

273285
//Enable trace memory and immediately start trace.
274286
#if CONFIG_ESP32_TRAX

components/esp32/spiram.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ esp_err_t esp_spiram_init()
108108
esp_err_t r;
109109
r = psram_enable(PSRAM_SPEED, PSRAM_MODE);
110110
if (r != ESP_OK) {
111+
#if CONFIG_SPIRAM_IGNORE_NOTFOUND
111112
ESP_EARLY_LOGE(TAG, "SPI RAM enabled but initialization failed. Bailing out.");
113+
#endif
112114
return r;
113115
}
114116

0 commit comments

Comments
 (0)