diff --git a/ports/esp32/boards/sdkconfig.spiram b/ports/esp32/boards/sdkconfig.spiram index 4154b5e42..f2ed44c5d 100644 --- a/ports/esp32/boards/sdkconfig.spiram +++ b/ports/esp32/boards/sdkconfig.spiram @@ -13,6 +13,8 @@ CONFIG_LOG_BOOTLOADER_LEVEL_WARN=y # ESP32-specific CONFIG_ESP32_DEFAULT_CPU_FREQ_240=y CONFIG_SPIRAM_SUPPORT=y +CONFIG_SPIRAM_IGNORE_NOTFOUND=y +CONFIG_SPIRAM_USE_MEMMAP=y CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU0=n CONFIG_ESP32_XTAL_FREQ_AUTO=y diff --git a/ports/esp32/main.c b/ports/esp32/main.c index 01d22d3e7..a304dcbcc 100644 --- a/ports/esp32/main.c +++ b/ports/esp32/main.c @@ -37,6 +37,7 @@ #include "esp_task.h" #include "soc/cpu.h" #include "esp_log.h" +#include "esp_spiram.h" #include "py/stackctrl.h" #include "py/nlr.h" @@ -69,9 +70,29 @@ void mp_task(void *pvParameter) { #endif uart_init(); + #if CONFIG_SPIRAM_SUPPORT + // Try to use the entire external SPIRAM directly for the heap + size_t mp_task_heap_size; + void *mp_task_heap = (void*)0x3f800000; + switch (esp_spiram_get_chip_size()) { + case ESP_SPIRAM_SIZE_16MBITS: + mp_task_heap_size = 2 * 1024 * 1024; + break; + case ESP_SPIRAM_SIZE_32MBITS: + case ESP_SPIRAM_SIZE_64MBITS: + mp_task_heap_size = 4 * 1024 * 1024; + break; + default: + // No SPIRAM, fallback to normal allocation + mp_task_heap_size = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT); + mp_task_heap = malloc(mp_task_heap_size); + break; + } + #else // Allocate the uPy heap using malloc and get the largest available region size_t mp_task_heap_size = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT); void *mp_task_heap = malloc(mp_task_heap_size); + #endif soft_reset: // initialise the stack pointer for the main thread