diff --git a/ports/espressif/supervisor/port.c b/ports/espressif/supervisor/port.c index 78c3508458..8d10c1f7dc 100644 --- a/ports/espressif/supervisor/port.c +++ b/ports/espressif/supervisor/port.c @@ -102,22 +102,6 @@ static size_t spiram_size_usable(void) { } #endif -// Heap sizes for when there is no external RAM for CircuitPython to use -// exclusively. -#ifdef CONFIG_IDF_TARGET_ESP32 -// TODO: Determine better: 520kB of internal RAM; similar to 512kB for ESP32-S3. -#define HEAP_SIZE (88 * 1024) -#endif -#ifdef CONFIG_IDF_TARGET_ESP32S2 -#define HEAP_SIZE (48 * 1024) -#endif -#ifdef CONFIG_IDF_TARGET_ESP32S3 -#define HEAP_SIZE (176 * 1024) -#endif -#ifdef CONFIG_IDF_TARGET_ESP32C3 -#define HEAP_SIZE (88 * 1024) -#endif - uint32_t *heap; uint32_t heap_size; @@ -231,8 +215,10 @@ safe_mode_t port_init(void) { #endif if (heap == NULL) { - heap = malloc(HEAP_SIZE); - heap_size = HEAP_SIZE / sizeof(uint32_t); + size_t heap_total = heap_caps_get_total_size(MALLOC_CAP_8BIT); + heap_size = MIN(heap_caps_get_largest_free_block(MALLOC_CAP_8BIT), heap_total / 2); + heap = malloc(heap_size); + heap_size = heap_size / sizeof(uint32_t); } if (heap == NULL) { heap_size = 0;