Chapter 4. API Guides
4.11.3 Configuring External RAM
ESP-IDF fully supports the use of external memory in applications. Once the external RAM is initialized at startup,
ESP-IDF can be configured to handle it in several ways:
• Integrate RAM into the ESP32-S2 memory map
• Add external RAM to the capability allocator
• Provide external RAM via malloc() (default)
• Allow .bss segment placed in external memory
Integrate RAM into the ESP32-S2 memory map
Select this option by choosing “Integrate RAM into memory map”from CONFIG_SPIRAM_USE.
This is the most basic option for external SPI RAM integration. Most likely, you will need another, more advanced
option.
During the ESP-IDF startup, external RAM is mapped into the data address space, starting at address 0x3F500000
(byte-accessible). The length of this region is the same as the SPI RAM size (up to the limit of 10.5 MB).
Applications can manually place data in external memory by creating pointers to this region. So if an application uses
external memory, it is responsible for all management of the external SPI RAM: coordinating buffer usage, preventing
corruption, etc.
Add external RAM to the capability allocator
Select this option by choosing “Make RAM allocatable using heap_caps_malloc(…, MALLOC_CAP_SPIRAM)”
from CONFIG_SPIRAM_USE.
When enabled, memory is mapped to address 0x3F500000 and also added to the capabilities-based heap memory
allocator using MALLOC_CAP_SPIRAM.
To allocate memory from external RAM, a program should call heap_caps_malloc(size, MAL-
LOC_CAP_SPIRAM). After use, this memory can be freed by calling the normal free() function.
Provide external RAM via malloc()
Select this option by choosing“Make RAM allocatable using malloc() as well”from CONFIG_SPIRAM_USE. This
is the default option.
In this case, memory is added to the capability allocator as described for the previous option. However, it is also
added to the pool of RAM that can be returned by the standard malloc() function.
This allows any application to use the external RAM without having to rewrite the code to use
heap_caps_malloc(..., MALLOC_CAP_SPIRAM).
An additional configuration item, CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL, can be used to set the size
threshold when a single allocation should prefer external memory:
• When allocating a size less than the threshold, the allocator will try internal memory first.
• When allocating a size equal to or larger than the threshold, the allocator will try external memory first.
If a suitable block of preferred internal/external memory is not available, the allocator will try the other type of
memory.
Because some buffers can only be allocated in internal memory, a second configuration item CON-
FIG_SPIRAM_MALLOC_RESERVE_INTERNAL defines a pool of internal memory which is reserved for only ex-
plicitly internal allocations (such as memory for DMA use). Regular malloc() will not allocate from this pool.
The MALLOC_CAP_DMA and MALLOC_CAP_INTERNAL flags can be used to allocate memory from this pool.
Espressif Systems 1333
Submit Document Feedback
Release v4.4