Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 37 ยท Microcontroller Architecture

Memory Types in a Microcontroller

Learn the memory types inside a microcontroller (flash, SRAM, EEPROM, ROM, registers), what each is used for, how the address map is laid out, and how to budget them.

A microcontroller has very little memory, and it comes in several kinds, each chosen for a purpose. Flash stores your program and survives power loss. SRAM holds your variables and the stack and is lost at power-off. EEPROM (or emulated EEPROM) keeps settings you change at run time. Even the peripheral control registers are "memory" as far as the CPU is concerned. Knowing which kind you are touching tells you how fast it is, whether you can write it, and how long it lasts.

This lesson surveys the memory types, shows a typical memory map, and then lets you experiment with a simulated memory bus that routes addresses to flash, RAM and peripherals. It sets the stage for memory-mapped registers in chapter 4.

Volatile and non-volatile memory

Memory Volatile? Writable by firmware? Speed Typical size Holds
Mask ROM no never (factory) fast 1-128 KB boot loader on some chips
Flash (NOR) no yes, but slowly, in pages read fast, write ms 8 KB to 2 MB program code, constants
SRAM yes yes, any time fastest 1 KB to 1 MB variables, stack, heap, buffers
EEPROM no yes, byte by byte read fast, write ~3 to 5 ms 128 B to 16 KB calibration, settings, counters
FRAM / MRAM no yes, like RAM fast 8 KB to MBs (external) frequently written non-volatile data
Registers -- yes, with side effects fastest n/a CPU and peripheral control
External RAM / flash / SD varies yes slower MB to GB frames, logs, assets

Flash

Flash is where your compiled program lives, plus constants (const tables, strings). It is read at nearly full CPU speed, but on faster chips flash is slower than the CPU, so there are wait states and often an instruction cache or "accelerator" (ART on STM32). Flash is written in pages, erased in sectors, and wears out after tens of thousands of erase cycles (see the development workflow). Code can run directly from flash, called execute in place (XIP); on an RP2040 or ESP32 the flash is external and read through a cache.

SRAM

Static RAM is fast, needs no refresh and no erase, and can be written any number of times. It is also the scarcest resource. It holds:

  • .data: global and static variables that have a non-zero initial value (copied from flash at startup),
  • .bss: global and static variables initialized to zero,
  • the stack: function call frames and local variables,
  • the heap: memory from malloc, if you use it at all.

When people say an MCU "has 20 KB of RAM" this is what they mean, and all four compete for it. Startup code, linker sections and the stack shows exactly how.

EEPROM and emulated EEPROM

EEPROM lets you rewrite single bytes without erasing a whole page. AVR chips have it built in; many ARM chips do not, so libraries emulate it by rotating small records around two flash pages. Rules of thumb: write only when a value actually changed, never inside a fast loop, and keep a version number or checksum so a power failure in mid-write does not leave nonsense.

Special cases you will meet

  • Boot ROM: read-only code from the manufacturer that runs first and can load an application over USB or UART.
  • Option bytes / fuses: a few bytes that configure the chip itself (read-out protection, brown-out level, clock source). Writing wrong fuse values can lock you out of a chip, so read the manual first.
  • Backup SRAM / RTC registers: a few bytes powered from a battery that survive deep sleep or main power loss.
  • CCM / TCM: tightly coupled memory with zero wait states for critical code and data (Cortex-M7).

The memory map

A CPU sees memory through one flat address space; each region of addresses is wired to a different device. Here is the layout of a Cortex-M3/M4 (simplified, and similar across vendors):

Address range Region Contents
0x0000_0000 Code flash alias, vector table (boot)
0x0800_0000 Flash (STM32) your program
0x2000_0000 SRAM variables, stack
0x4000_0000 Peripherals GPIO, UART, timers, ADC registers
0x6000_0000 External memory optional
0xE000_0000 Core private NVIC, SysTick, debug

Exact addresses differ per chip, and you always read them from the reference manual. The key idea is that reading or writing certain addresses does not touch memory at all: it controls hardware. Writing 1 to bit 5 of a particular address in the 0x4000_0000 region turns on an LED.

flowchart LR
    CPU["CPU"] --> BUS["Bus matrix / address decoder"]
    BUS -->|"0x0800_0000..."| FL["Flash"]
    BUS -->|"0x2000_0000..."| RAM["SRAM"]
    BUS -->|"0x4000_0000..."| PER["Peripheral registers"]
    BUS -->|"0xE000_0000..."| SYS["Core peripherals"]

Simulate the memory bus

The simulation below builds a memory map in software: an address decoder sends each access to flash (read-only), RAM, or a peripheral register. Try changing an address to see the "bus fault" that real chips raise when you touch a hole in the map.

C An address decoder routes reads and writes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Budgeting memory

Embedded engineers keep a running budget. A sample for a 20 KB SRAM chip:

Item Bytes
.data + .bss (globals) 3,400
UART RX/TX buffers 512
Sensor sample buffer 1,024
Main stack 2,048
Interrupt stack, RTOS task stacks 4,096
Heap (if any) 0 (avoid)
Headroom (safety margin) the rest, never below ~20 %

A good habit is to fill the stack with a pattern at startup and later check how much of it was overwritten (the "high-water mark" technique, covered in chapter 4). The code below uses the same idea on a small static buffer.

C Stack high-water mark with a canary pattern

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where do global variables with a non-zero initial value live while the program runs?

    Variables must be writable, so they live in SRAM. The initial values are stored in flash and copied to RAM at startup.
  2. 2. Which memory is best for a calibration constant that changes rarely but must survive power loss?

    It must be non-volatile and rewritable. SRAM and registers lose their content when power is off.
  3. 3. What happens when you read an address that no device occupies on a Cortex-M?

    Accessing an unmapped region typically raises a fault, which is a useful way to catch wild pointers.
  4. 4. Why does flash typically have wait states at high CPU speeds?

    Flash access times of tens of nanoseconds cannot keep up with a 100+ MHz core, so the chip inserts wait states or uses a cache.

Summary

  • Flash stores code and constants, SRAM stores variables, stack and heap, EEPROM keeps small non-volatile settings, and registers control hardware.
  • Flash is erased by page and wears out; SRAM is fast, volatile and scarce.
  • A single address space is divided among flash, SRAM, peripherals and core registers; touching a hole raises a bus fault.
  • Keep a memory budget with headroom; painting the stack reveals its true maximum depth.

Next: Clocks, Reset and Boot