Skip to content

Commit 953f44b

Browse files
committed
Fix spiram limit calculation on esp32
esp32 places the psram start at SOC_EXTRAM_DATA_LOW and it can extend up to SOC_EXTRAM_DATA_SIZE. This is different than esp32-s2 and later, which place the end at EXTRAM_DATA_HIGH and the limitation of SOC_EXTRAM_DATA_SIZE was not previously identified as important. Additionally, the esp32 has a reserved area within himem which was not being accounted for. With this change, the Feather ESP32 V2 feather can be used via thonny, and the other "quick memory corruption tests" I was performing also all succeed instead of failing. Before this change, the incorrect address being used for spiram was 0x3fa00000..0x3fc00000 (2MiB). Now, it's 0x3f800000..0x3f9c0000 (1.75MiB) due to the reserved area and the changed start address. This is intended to be a no-effect change for other espressif chips besides original esp32.
1 parent afbaa2e commit 953f44b

File tree

1 file changed

+16
-1
lines changed
  • ports/espressif/supervisor

1 file changed

+16
-1
lines changed

ports/espressif/supervisor/port.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,17 @@
8989

9090
#ifdef CONFIG_SPIRAM
9191
#include "esp32/spiram.h"
92+
#ifdef CONFIG_IDF_TARGET_ESP32
93+
#include "esp32/himem.h"
94+
#else
95+
#define esp_himem_reserved_area_size() (0)
96+
#endif
97+
98+
static size_t spiram_size_usable_for_malloc(void) {
99+
/* SPIRAM chip may be larger than the size we can map into address space */
100+
size_t s = MIN(esp_spiram_get_size(), SOC_EXTRAM_DATA_SIZE);
101+
return s - esp_himem_reserved_area_size();
102+
}
92103
#endif
93104

94105
// Heap sizes for when there is no external RAM for CircuitPython to use
@@ -209,8 +220,12 @@ safe_mode_t port_init(void) {
209220

210221
#ifdef CONFIG_SPIRAM
211222
if (esp_spiram_is_initialized()) {
212-
size_t spiram_size = esp_spiram_get_size();
223+
size_t spiram_size = spiram_size_usable_for_malloc();
224+
#ifdef CONFIG_IDF_TARGET_ESP32
225+
heap = (uint32_t *)SOC_EXTRAM_DATA_LOW;
226+
#else
213227
heap = (uint32_t *)(SOC_EXTRAM_DATA_HIGH - spiram_size);
228+
#endif
214229
heap_size = spiram_size / sizeof(uint32_t);
215230
}
216231
#endif

0 commit comments

Comments
 (0)