# STM32 Memory Regions

The device currently supports three memory regions:

- Flash
- SRAM
- Backup SRAM

## Flash

Code and constants are stored in Flash. Code is executed out of flash rather than copied to SRAM.

## SRAM

By default, all variables you declare and all allocations from the heap will occur from primary SRAM, which is 1 MB in size.

These variables are held as long as the CPU is powered, i.e. within the processor's Run, Sleep, and Stop modes.

In the processor's Suspend mode (CPF Deep Sleep), primary SRAM is turned off and the contents will be lost. If you need data to survive Suspend mode, you should place it into Backup SRAM.

## Backup SRAM

The processor maintains 4KB of backup SRAM. This SRAM section maintains its power in the processor's Suspend mode (CPF Deep Sleep). Variables which should be maintained across sleep cycles, such as `StateVariables::profileNum`, should be placed into this section.

These variables are only initialized upon a cold boot or reset (software, hardware, or watchdog). If the system has restarted from Suspend mode due to a wakeup event, the contents of SRAM will be kept as-is and available to the program unchanged.

The linker script places all variables in the section `.backup` (or, more generally, `.backup*`) into the backup SRAM region.

To mark variables for this section, you need to declare them with the following attribute:

```cpp
__attribute__((section(".backup")))
```

The header `BackupMemory.hpp` provides a macro that you can use as well:

```cpp
uint16_t PLACE_IN_BACKUP_SRAM() a_variable;
```
