How to use a Global Struct Array in psram esp32-wrover

I’m not sure if this is possible to do, but someone may know a solution.

I have a structured array:

struct Plot
  {
    float lng, lat;
  };
struct Plot roundplot[50000];

But this is too big for the internal memory.
So I was hoping to put this array in the PSRAM.

But this needs to be global so I can call a function and others that can access it like this for example:

void my_function()
{
  roundplot[roundplotIndex] = {longitude, latitude};
  roundplotIndex++;
}

I have seen this;

But I can’t get it working.

I’m using v1.0.6 and my “platformio.ini” is…

[env:esp-wrover-kit]
platform = espressif32@3.4.0
board = esp-wrover-kit
framework = arduino
build_flags = 
	-D_BSD_SOURCE
	-DBOARD_HAS_PSRAM
	-mfix-esp32-psram-cache-issue
upload_speed = 1500000
monitor_speed = 921600
lib_deps = 
	vshymanskyy/TinyGSM@^0.11.5
	knolleary/PubSubClient@^2.8
	bblanchon/ArduinoJson@^6.19.4
	adafruit/Adafruit GFX Library@^1.11.3
	adafruit/Adafruit SSD1306@^2.5.6

I assume I have to change some settings in a config file somewhere?

Any help would be appreciated.

1 Like

Well first of all, is PSRAM generally detected initialized correctly? Run a simple

#include <Arduino.h>

void setup() {
  log_d("Total heap: %d", ESP.getHeapSize());
  log_d("Free heap: %d", ESP.getFreeHeap());
  log_d("Total PSRAM: %d", ESP.getPsramSize());
  log_d("Free PSRAM: %d", ESP.getFreePsram());
}

void loop() {}

what does it output?

1 Like

Yes, I have this (I’m using flash FFS too):

FFS available memory:        1378241 bytes
FFS memory used:                9789 bytes
PSRAM size:                  4194252 bytes
PSRAM available memory:      4194252 bytes

I get this if I do this in setup():

Plot *roundplot = (Plot *)ps_malloc(128000 * sizeof(Plot));
Serial.printf("PSRAM available memory:     %8d bytes\n", ESP.getFreePsram());

PSRAM available memory:      3170236 bytes

So it looks like it’s allocating the psram memory usage.

Great, just change your global variable to be

struct Plot* roundplot;

in which you allocated then and it should be fine.

1 Like

Many thanks @maxgerhardt

I’ve stitched in the code as below to my project and is looking good, it compiles no errors or warnings and runs, the psram memory allocation looks correct too.

#include <Arduino.h>

struct Plot
{
  float lng, lat;
};
struct Plot* roundplot;

void setup() 
{
Serial.printf("PSRAM size:                 %8d bytes\n", ESP.getPsramSize());
Serial.printf("PSRAM available memory:     %8d bytes\n", ESP.getFreePsram());

roundplot = (Plot *)ps_malloc(128000 * sizeof(Plot));

Serial.printf("PSRAM available memory:     %8d bytes\n", ESP.getFreePsram());
}

void loop(){};
1 Like