esp_hal

Attribute Macro ram

#[ram]
Available on crate feature unstable only.
Expand description

§Stability

This API is marked as unstable and is only available when the unstable crate feature is enabled. This comes with no stability guarantees, and could be changed or removed at any time. Sets which segment of RAM to use for a function or static and how it should be initialized.

Requires the ram feature.

§Options

  • rtc_fast: Use RTC fast RAM.
  • rtc_slow: Use RTC slow RAM. Note: not available on all targets.
  • persistent: Persist the contents of the static across resets. See the section below for details.
  • zeroed: Initialize the memory of the static to zero. The initializer expression will be discarded. Types used must implement bytemuck::Zeroable.

Using both rtc_fast and rtc_slow or persistent and zeroed together is an error.

§persistent

Initialize the memory to zero after the initial boot. Thereafter, initialization is skipped to allow communication across software_reset(), deep sleep, watchdog timeouts, etc.

Types used must implement bytemuck::AnyBitPattern.

§Warnings

  • A system-level or lesser reset occurring before the ram has been zeroed could skip initialization and start the application with the static filled with random bytes.
  • There is no way to keep some kinds of resets from happening while updating a persistent static—not even a critical section.

If these are issues for your application, consider adding a checksum alongside the data.

§Examples

#[ram(rtc_fast)]
static mut SOME_INITED_DATA: [u8; 2] = [0xaa, 0xbb];

#[ram(rtc_fast, persistent)]
static mut SOME_PERSISTENT_DATA: [u8; 2] = [0; 2];

#[ram(rtc_fast, zeroed)]
static mut SOME_ZEROED_DATA: [u8; 8] = [0; 8];

See the ram example in the esp-hal repository for a full usage example.