Support for External RAM
Introduction
ESP32 has a few hundred kilobytes of internal RAM, residing on the same die as the rest of the chip components. It can be insufficient for some purposes, so ESP32 has the ability to use up to 4 MB of virtual addresses for external PSRAM (Psuedostatic RAM) memory. The external memory is incorporated in the memory map and, with certain restrictions, is usable in the same way as internal data RAM.
Hardware
ESP32 supports PSRAM connected in parallel with the SPI flash chip. While ESP32 is capable of supporting several types of RAM chips, ESP-IDF currently only supports Espressif branded PSRAM chips (e.g., ESP-PSRAM32, ESP-PSRAM64, etc).
Note
Some PSRAM chips are 1.8 V devices and some are 3.3 V. The working voltage of the PSRAM chip must match the working voltage of the flash component. Consult the datasheet for your PSRAM chip and ESP32 device to find out the working voltages. For a 1.8 V PSRAM chip, make sure to either set the MTDI pin to a high signal level on boot-up, or program ESP32 eFuses to always use the VDD_SIO level of 1.8 V. Not doing this can damage the PSRAM and/or flash chip.
Note
Espressif produces both modules and system-in-package chips that integrate compatible PSRAM and flash and are ready to mount on a product PCB. Consult the Espressif website for more information. If you are using a custom PSRAM chip, ESP-IDF SDK might not be compatible with it.
For specific details about connecting the SoC or module pins to an external PSRAM chip, consult the SoC or module datasheet.
Configuring External RAM
ESP-IDF fully supports the use of external RAM in applications. Once the external RAM is initialized at startup, ESP-IDF can be configured to integrate the external RAM in several ways:
Provide External RAM via malloc() (default)
Integrate RAM into the ESP32 Memory Map
Select this option by choosing Integrate RAM into memory map
from CONFIG_SPIRAM_USE.
This is the most basic option for external RAM integration. Most likely, you will need another, more advanced option.
During the ESP-IDF startup, external RAM is mapped into the data virtual address space. The address space is dynamically allocated. The length will be the minimum length between the PSRAM size and the available data virtual address space size.
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 RAM: coordinating buffer usage, preventing corruption, etc.
It is recommended to access the PSRAM by ESP-IDF heap memory allocator (see next chapter).
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 data virtual address space 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, MALLOC_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 or equal to the threshold, the allocator will try internal memory first.
When allocating a size 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 CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL defines a pool of internal memory which is reserved for only explicitly 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.
Allow .bss Segment to Be Placed in External Memory
Enable this option by checking CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY.
If enabled, the region of the data virtual address space where the PSRAM is mapped to will be used to store zero-initialized data (BSS segment) from the lwIP, net80211, libpp, wpa_supplicant and bluedroid ESP-IDF libraries.
Additional data can be moved from the internal BSS segment to external RAM by applying the macro EXT_RAM_BSS_ATTR
to any static declaration (which is not initialized to a non-zero value).
It is also possible to place the BSS section of a component or a library to external RAM using linker fragment scheme extram_bss
.
This option reduces the internal static memory used by the BSS segment.
Remaining external RAM can also be added to the capability heap allocator using the method shown above.
Allow .noinit Segment to Be Placed in External Memory
Enable this option by checking CONFIG_SPIRAM_ALLOW_NOINIT_SEG_EXTERNAL_MEMORY. If enabled, the region of the data virtual address space where the PSRAM is mapped to will be used to store non-initialized data. The values placed in this segment will not be initialized or modified even during startup or restart.
By applying the macro EXT_RAM_NOINIT_ATTR
, data could be moved from the internal NOINIT segment to external RAM. Remaining external RAM can still be added to the capability heap allocator using the method shown above, Add External RAM to the Capability Allocator.
Restrictions
External RAM use has the following restrictions:
When flash cache is disabled (for example, if the flash is being written to), the external RAM also becomes inaccessible. Any read operations from or write operations to it will lead to an illegal cache access exception. This is also the reason why ESP-IDF does not by default allocate any task stacks in external RAM (see below).
External RAM uses the same cache region as the external flash. This means that frequently accessed variables in external RAM can be read and modified almost as quickly as in internal RAM. However, when accessing large chunks of data (> 32 KB), the cache can be insufficient, and speeds will fall back to the access speed of the external RAM. Moreover, accessing large chunks of data can "push out" cached flash, possibly making the execution of code slower afterwards.
In general, external RAM will not be used as task stack memory.
xTaskCreate()
and similar functions will always allocate internal memory for stack and task TCBs.
The option CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY can be used to allow placing task stacks into external memory. In these cases xTaskCreateStatic()
must be used to specify a task stack buffer allocated from external memory, otherwise task stacks will still be allocated from internal memory.
Failure to Initialize
By default, failure to initialize external RAM will cause the ESP-IDF startup to abort. This can be disabled by enabling the config item CONFIG_SPIRAM_IGNORE_NOTFOUND.
If CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY is enabled, the option to ignore failure is not available as the linker will have assigned symbols to external memory addresses at link time.
Regarding stacks in PSRAM: For tasks that do not call ROM code in any way (directly or indirectly), the CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY option will eliminate the check in
xTaskCreateStatic()
, allowing a task's stack to be in external RAM. However, using this is not advised.When used at 80 MHz clock speed, external RAM must also occupy either the HSPI or VSPI bus. Select which SPI host will be used by CONFIG_SPIRAM_OCCUPY_SPI_HOST.
Chip Revisions
There are some issues with certain revisions of ESP32 that have repercussions for use with external RAM. The issues are documented in the ESP32 Series SoC Errata document. In particular, ESP-IDF handles the bugs mentioned in the following ways:
ESP32 Rev v0.0
ESP-IDF has no workaround for the bugs in this revision of silicon, and it cannot be used to map external PSRAM into ESP32's main memory map.
ESP32 Rev v1.0
The bugs in this revision of silicon cause issues if certain sequences of machine instructions operate on external memory. (ESP32 Series SoC Errata 3.2). As a workaround, the -mfix-esp32-psram-cache-issue
flag has been added to the ESP32 GCC compiler such that these sequences are filtered out. As a result, the compiler only outputs code that can safely be executed. The CONFIG_SPIRAM_CACHE_WORKAROUND option can be used to enable this workaround.
Aside from linking to a recompiled version of Newlib with the additional flag, ESP-IDF also does the following:
Avoids using some ROM functions
Allocates static memory for the Wi-Fi stack
ESP32 Rev v3.0
ESP32 rev v3.0 fixes the PSRAM cache issue found in rev v1.0. When CONFIG_ESP32_REV_MIN option is set to rev v3.0
, compiler workarounds related to PSRAM will be disabled. For more information about ESP32 v3.0, see ESP32 Chip Revision v3.0 User Guide.