How do I use psram in esp32-s2-wrover?

The documentation states that yo ucan mark variables you want to place in PSRAM with EXT_RAM_BSS_ATTR per documentation, if the option CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY. is turned on. However, as you can see in

that is actually turned off.

This means that you have to convert the buffers and variables you want to be placed in PSRAM with a simple pointer and use the PSRAM malloc allocator heap_caps_malloc(size, MALLOC_CAP_SPIRAM) if you want to be 100% sure they are allocated in PSRAM. Otherwise, you can also use the regular malloc() option, since it will use PSRAM if needed, since the option CONFIG_SPIRAM_USE_MALLOC is turned on.

Example. Previous:

uint8_t my_buffer[512 * 1024]; // fills internal RAM by 0.5MByte

After:

uint8_t* my_buffer; // pointer to 'some' memory

void init_buffer() {
   // definitely allocate buffer in PSRAM
   // capabilities: 8/16/…-bit data accesses possible and in SPIRAM.
   my_buffer = heap_caps_malloc(512*1024, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);

   // try allocate buffer anywhere where's space, internal or PSRAM
   my_buffer = malloc(512*1024);
}

void deinit_buffer() {
  // if allocated with heaps_caps_malloc
  heap_caps_free(my_buffer);
  
  // if allocated with malloc
  free(my_buffer);

  // good practice
  my_buffer = nullptr;
}
3 Likes