← Back to the chapter · Next detail →

Technical detail · MCU vs MPU: Workload Fit

Account for flash, EEPROM, and SRAM

An ATmega328-class device makes the memory contracts concrete. Its three stores differ by lifetime and purpose:

MemoryCapacityPersists without power?Firmware role
Flash32 KiBYesProgram image and constants; erase/program rules apply
EEPROM1 KiBYesSmall calibration or configuration records; writes are slow and endurance-limited
SRAM2 KiBNoStack, globals, heap, peripheral buffers, and live state share this pool

The flash figure is not “all application code.” Bootloader allocation, interrupt vectors, constants, update strategy, and linker layout consume part of it. The 2 KiB SRAM limit is often more dangerous: a 512-byte radio buffer uses one quarter of the entire working memory before stacks or sensor libraries are counted. Record linker-section sizes and measure worst-case stack headroom with the deepest interrupt and call path active.

A build can compile yet fail later when calls and interrupts use the remaining stack. On AVR devices, the PROGMEM pattern can keep constant strings and tables in flash rather than copying them into SRAM. Measure the remaining stack space on the real workload after that change.

Code-size tactics have trade-offs. -Os asks the compiler to optimize for size, but the actual image and timing must be inspected because inlining, library selection, and arithmetic helpers can dominate. A compressed instruction encoding such as Arm Thumb can reduce instruction footprint and fetch traffic, but it is an instruction-set property, not a promise that every source expression becomes smaller or faster. Compare the linked binary, disassembly, worst-case path, and energy trace on the chosen target.

EEPROM has a separate persistence job. Use it for bounded configuration or calibration that must survive reset, with versioning, CRC or another integrity check, and wear-aware updates. Do not treat it as an extra kilobyte of ordinary RAM, and do not rewrite an unchanged counter every loop.

← Back to the chapter · Next detail →