Allocating variables to SRAM or PSRAM.
Posted: Sun Dec 15, 2024 5:27 am
Hi All,
I hope someone can help me. We are looking to use the ESP32 in a product and this all started becuase we need to send TFT sprites to SRAM however there seems to be a size limit on variables sent to SRAM when you have PSRAM enabled on a wrover. it seems to be around the 4KB mark (45 x 45 pixel of 2 bytes per pixel esprite). I have tried everything to force sprites and even malloc and ps_malloc of variables without any luck. does anyone know of a workaround for forcing more than 4kb into SRAM on a wrover while still accessing PSRAM?
If you have a look at the code below you can see I try and attempt to send a 6 and a 4 KB variable to SRAM as well as PSRAM. When attempting to send 6kb to the SRAM buffer its returning a PSRAM address on the serial monitor. Also only 4KB is seen in the SRAM buffer and 16KB is seen in the PSRAM buffer.
Can someone please help with this.
I have tried using different boards in the board manager :
ESP32 dev module
ESP32 wrover module
ESP32 wrover kit (all versions)
Im using:
arduino IDE version 2.3.3
I hope someone can help me. We are looking to use the ESP32 in a product and this all started becuase we need to send TFT sprites to SRAM however there seems to be a size limit on variables sent to SRAM when you have PSRAM enabled on a wrover. it seems to be around the 4KB mark (45 x 45 pixel of 2 bytes per pixel esprite). I have tried everything to force sprites and even malloc and ps_malloc of variables without any luck. does anyone know of a workaround for forcing more than 4kb into SRAM on a wrover while still accessing PSRAM?
If you have a look at the code below you can see I try and attempt to send a 6 and a 4 KB variable to SRAM as well as PSRAM. When attempting to send 6kb to the SRAM buffer its returning a PSRAM address on the serial monitor. Also only 4KB is seen in the SRAM buffer and 16KB is seen in the PSRAM buffer.
Can someone please help with this.
I have tried using different boards in the board manager :
ESP32 dev module
ESP32 wrover module
ESP32 wrover kit (all versions)
Im using:
arduino IDE version 2.3.3
Code: Untitled.cpp Select all
#include "esp_system.h"
#include "esp_spiram.h" // Include this header for PSRAM functions
long initialFreeSRAM;
long initialFreePSRAM;
long usedSRAM = 0; // Track used SRAM
long usedPSRAM = 0; // Track used PSRAM
int *heap1Var; // Pointer to dynamically allocated memory in heap
int *heap2Var; // Pointer to dynamically allocated memory in heap
int *PSRAM1Var; // Pointer to dynamically allocated memory in PSRAM
int *PSRAM2Var; // Pointer to dynamically allocated memory in PSRAM
void checkPSRAM() {
initialFreePSRAM = ESP.getFreePsram(); // Free PSRAM available
Serial.print("Total PSRAM size: ");
Serial.println(esp_spiram_get_size());
Serial.print("Free PSRAM size: ");
Serial.println(initialFreePSRAM);
}
void checkSRAM() {
initialFreeSRAM = ESP.getFreeHeap();
Serial.print("Free SRAM heap memory: ");
Serial.println(initialFreeSRAM);
Serial.println("");
}
void calculateRAMused() {
// Recalculate based on direct tracking
usedSRAM = initialFreeSRAM - ESP.getFreeHeap();
usedPSRAM = initialFreePSRAM - ESP.getFreePsram();
Serial.println("");
Serial.print("**SRAM allocated = ");
Serial.println(usedSRAM);
Serial.print("**PSRAM allocated = ");
Serial.println(usedPSRAM);
}
void setup() {
Serial.begin(115200);
psramInit();
Serial.println("Before allocation of RAM");
checkPSRAM();
checkSRAM();
// Dynamically allocate 4KB of memory on the heap
heap1Var = (int *)malloc(4096); // Allocate 4KB (4096 bytes) on the heap
if (heap1Var != NULL) {
*heap1Var = 14; // Assign a value to the allocated memory
// Print the memory address of the allocated heap memory
Serial.print("Memory address of heap1Var (Heap): ");
Serial.println((intptr_t)heap1Var, HEX);
// Print the value stored in the heap
Serial.print("heap1Var value: ");
Serial.println(*heap1Var);
} else {
Serial.println("Failed to allocate 4KB of memory on the heap");
}
heap2Var = (int *)malloc(6144); // Allocate 6KB (6144 bytes) on the heap
if (heap2Var != NULL) {
*heap2Var = 16; // Assign a value to the allocated memory
// Print the memory address of the allocated heap memory
Serial.print("Memory address of heap2Var (Heap): ");
Serial.println((intptr_t)heap2Var, HEX);
// Print the value stored in the heap
Serial.print("heap2Var value: ");
Serial.println(*heap2Var);
} else {
Serial.println("Failed to allocate 6KB of memory on the heap");
}
// Dynamically allocate 4KB of memory on PSRAM
PSRAM1Var = (int *)ps_malloc(4096); // Allocate 4KB (4096 bytes) in PSRAM
if (PSRAM1Var != NULL) {
*PSRAM1Var = 24; // Assign a value to the allocated PSRAM memory
// Print the memory address of the allocated PSRAM memory
Serial.print("Memory address of PSRAM1Var (PSRAM): ");
Serial.println((intptr_t)PSRAM1Var, HEX);
// Print the value stored in PSRAM
Serial.print("PSRAM1Var value: ");
Serial.println(*PSRAM1Var);
} else {
Serial.println("Failed to allocate 4KB of memory in PSRAM");
}
PSRAM2Var = (int *)ps_malloc(6144); // Allocate 6KB (6144 bytes) in PSRAM
if (PSRAM2Var != NULL) {
*PSRAM2Var = 26; // Assign a value to the allocated PSRAM memory
// Print the memory address of the allocated PSRAM memory
Serial.print("Memory address of PSRAM2Var (PSRAM): ");
Serial.println((intptr_t)PSRAM2Var, HEX);
// Print the value stored in PSRAM
Serial.print("PSRAM2Var value: ");
Serial.println(*PSRAM2Var);
} else {
Serial.println("Failed to allocate 6KB of memory in PSRAM");
}
// Print free heap memory after allocation
Serial.println("");
Serial.println("After Allocating memory to PSRAM");
calculateRAMused();
checkPSRAM();
checkSRAM();
}
void loop() {
// Nothing to do here
}