ESP32: how to use PSRAM (ps_malloc)

Hi,

I have ESP32-WROVER module which has 4MB PSRAM, but can't figure out how to use it.

Total heap: 363876
Free heap: 338836
Total PSRAM: 4192139
Free PSRAM: 4192139

Option 1: If I declare array (in psram) globally, I get core dump when calling array in loop(). Guru Meditation Error: Core 1 panic'ed (StoreProhibited).

Option 2: Somebody seemed to have this same issue and says it was solved by moving array declaration to setup(). Collect websocket data packets to a single char array that is allocated in external ram · Issue #1074 · me-no-dev/ESPAsyncWebServer · GitHub. But when I do that, the compiler doesn't recognize the array in loop().

So how should I declare the array and call it in loop()?

Below are codes and errors for both "options".

Option 1:

int n_elements = 1000;
unsigned char * acc_data_all = (unsigned char *) ps_malloc (n_elements * sizeof (unsigned char));  

void setup(){
        delay(3000);
        Serial.begin(115200);
        if(psramInit()){
        Serial.println("\nPSRAM is correctly initialized");
        }else{
        Serial.println("PSRAM not available");
        }
}

void loop() {
  Serial.println(acc_data_all[1]);
  delay(5000);
}
PSRAM is correctly initialized
Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.

Core  1 register dump:
PC      : 0x400d1141  PS      : 0x00060630  A0      : 0x800d1ba8  A1      : 0x3ffb2800  
A2      : 0x3ffc1170  A3      : 0x00000000  A4      : 0x00000014  A5      : 0x00000004  
A6      : 0x3ffb8874  A7      : 0x80000001  A8      : 0x00000000  A9      : 0x3ffb27d0  
A10     : 0x3ffc1170  A11     : 0x0000001f  A12     : 0x0000000a  A13     : 0x00000003  
A14     : 0x3ffc10d5  A15     : 0x00000000  SAR     : 0x00000003  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000001  LBEG    : 0x400875b9  LEND    : 0x400875c9  LCOUNT  : 0xfffffff7  


Backtrace:0x400d113e:0x3ffb28000x400d1ba5:0x3ffb2820

Option 2:

int n_elements = 1000;

void setup(){
        delay(3000);
        Serial.begin(115200);
        if(psramInit()){
        Serial.println("\nPSRAM is correctly initialized");
        }else{
        Serial.println("PSRAM not available");
        unsigned char * acc_data_all = (unsigned char *) ps_malloc (n_elements * sizeof (unsigned char));  
        }
}

void loop() {
  Serial.println(acc_data_all[1]);
  delay(5000);
}
exit status 1
'acc_data_all' was not declared in this scope

Thanks,
Tipo

I tried one more and got different error message: (LoadProhibited)

Option 3:

int n_elements = 1000;
unsigned char * acc_data_all;

void setup(){
        delay(3000);
        Serial.begin(115200);
        if(psramInit()){
        Serial.println("\nPSRAM is correctly initialized");
        }else{
        Serial.println("PSRAM not available");
        acc_data_all = (unsigned char *) ps_malloc (n_elements * sizeof (unsigned char));  
        }
}

void loop() {
  Serial.println(acc_data_all[1]);
  delay(5000);
}
PSRAM is correctly initialized
Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.

Core  1 register dump:
PC      : 0x400d115d  PS      : 0x00060630  A0      : 0x800d1bac  A1      : 0x3ffb2800  
A2      : 0x3ffc1170  A3      : 0x00000000  A4      : 0x00000014  A5      : 0x00000004  
A6      : 0x3ffb8874  A7      : 0x80000001  A8      : 0x00000000  A9      : 0x3ffb27d0  
A10     : 0x3ffc1170  A11     : 0x0000001f  A12     : 0x0000000a  A13     : 0x00000003  
A14     : 0x3ffc10d5  A15     : 0x00000000  SAR     : 0x00000003  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000001  LBEG    : 0x400875b9  LEND    : 0x400875c9  LCOUNT  : 0xfffffff7  


Backtrace:0x400d115a:0x3ffb28000x400d1ba9:0x3ffb2820

This works. I moved "psramInit" after allocation of the array.
The main thing is that allocation can't be done globally. As is said also here: ESP32 and PSRAM - Phil Schatzmann

int n_elements = 1000;
unsigned char * acc_data_all;

void setup(){
        delay(3000);
        Serial.begin(115200);
        acc_data_all = (unsigned char *) ps_malloc (n_elements * sizeof (unsigned char)); 
        if(psramInit()){
        Serial.println("\nPSRAM is correctly initialized");
        }else{
        Serial.println("PSRAM not available");
        }
}

void loop() {
  Serial.println(acc_data_all[1]);
  acc_data_all[1] = 'a';
  delay(5000);
}
PSRAM is correctly initialized
92
92

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.