Skip to main content

psram_split

Function psram_split 

Source
pub fn psram_split(
    psram: PSRAM<'static>,
    reserve: usize,
) -> Result<PsramSplit, PsramSplitError>
Expand description

Map the board’s external PSRAM, carve a private region off the base, and register the remainder with the global heap.

The deliberate-global-exposure counterpart of psram_map (which makes the whole region private and touches the global heap not at all). Reach for psram_split only once you’ve decided that some of PSRAM should be reachable by the checked psram_box / psram_vec helpers — that decision then applies to the whole crate graph’s plain allocations, not just yours, once internal DRAM runs out (see the module docs).

  • reserve carves that many bytes private from the base and registers the remainder globally. reserve == 0 is the maximal-exposure case: an empty PsramSplit::private slice and all PSRAM registered globally. Sound (a zero-length slice grants access to nothing) but rarely what you want — prefer psram_map if you don’t need any global exposure at all.
  • The private region is carved from the base, so PsramSplit::private starts at the (large-aligned) PSRAM mapping base — aligned for LVGL’s TLSF with no math; esp-alloc aligns the remainder’s base internally.
  • This primitive controls placement (the private/global split). The PSRAM hardware mapping uses the default [esp_hal::psram] config, which auto-detects size — the board-correct choice for both boards. A psram_split_with(config) variant is a non-breaking addition if a consumer ever needs a custom PsramConfig (a fixed PsramSize, say); no current one does.

Call once, after [esp_hal::init], instead of psram_map: taking PSRAM<'static> by value makes the once-only mapping a type-level guarantee, so the two cannot both run.

§Caveats handed back to the caller

  • No atomics in the private region. The checked psram_box / psram_vec cannot guard a foreign allocator, so keeping Atomic* out of whatever is placed here is the caller’s responsibility (holds for LVGL while LV_USE_OS is LV_OS_NONE). See PsramSafe.
  • DMA. The ESP32 (Fire27) cannot DMA to/from PSRAM at all; the ESP32-S3 can but slowly. A foreign allocator must not place DMA’d buffers here. See assert_dma_capable / dma_buffer.

§Errors

PsramSplitError::NotMapped if PSRAM does not map; PsramSplitError::TooSmall if it maps smaller than reserve.