Search code examples
c++arduinoesp32

How to extend ESP32 heap size?


I'm writing a code about play gif from SDCard on TFT Screen, so I create a array to put the gif file. (Using Nodemcu-32s 4MB)

#include<TFT_eSPI.h>
#include<SPI.h>
#include<AnimatedGIF.h>

TFT_eSPI tft;
AnimatedGIF gif;
uint8_t *gifArray;
int gifArrayLen;

void setup(){
  tft.init();
  tft.setRotation(2);
  tft.fillScreen(TFT_BLACK);

  File fgif=SD.open("/test.gif",FILE_READ);
  gifArrayLen=fgif.size();
  gifArray=(uint8_t *)malloc(gifArrayLen);
  for(int i=0;i<gifArrayLen;i++) gifArray[i]=fgif.read();
  fgif.close();
}

void loop(){
  tft.startWrite();
  gif.open(gifArray,gifArrayLen*sizeof(uint8_t),GIFDraw);
  while(gif.playFrame(true,NULL)) yield();
  gif.close();
  tft.endWrite();
}

But if gif size > 131KB, it will trigger fatal error like this.

Guru Meditation Error: Core 1 panic'ed (StoreProhibited). Exception was unhandled.

After malloc the array, when I set a value on it, it triggered.

I found some forum says it's because it over the FreeRTOS heap size.

Can I extend the heap size or use another storage method to replace it?


Solution

  • The "4MB" in NodeMCU refers to the size of flash, the size of RAM on ESP32 is fixed at 512KB, roughly 200KB of which is used by IRAM cache/code sections, leaving around 320KB for program memory, half of which is available for dynamic allocation.

    From documentation Heap Memory - Available Heap:

    Due to a technical limitation, the maximum statically allocated DRAM usage is 160KB. The remaining 160KB (for a total of 320KB of DRAM) can only be allocated at runtime as heap.

    There is a way to connect more RAM via SPI, but it's going to be very slow. You might want to look into a larger SoC with more RAM instead.