0
0
Embedded Cprogramming~3 mins

Why Memory layout on a microcontroller (Flash, SRAM, EEPROM) in Embedded C? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your robot forgets its own name every time you turn it off? Understanding memory layout can stop that from happening!

The Scenario

Imagine you are building a small robot with a microcontroller. You try to store all your program code, temporary data, and saved settings in one big box without organizing it. You mix everything together, and soon you run out of space or lose important data when the power goes off.

The Problem

Without understanding the memory layout, you might overwrite your program code or lose saved settings because the microcontroller treats Flash, SRAM, and EEPROM differently. This causes bugs that are hard to find and fix. Manually managing memory like this is slow and error-prone.

The Solution

Knowing the memory layout helps you put your program code in Flash (where it stays safe), use SRAM for temporary data while the microcontroller runs, and store important settings in EEPROM that keep data even when power is off. This clear separation makes your program reliable and efficient.

Before vs After
Before
char settings[100]; // no clear memory use
int data[50]; // mixed storage
// risk of overwriting code or losing data
After
const char settings[100] __attribute__((section(".eeprom"))) = {0};
int data[50]; // stored in SRAM
// program code in Flash automatically
What It Enables

Understanding memory layout lets you write programs that run smoothly, save important data safely, and use your microcontroller's resources wisely.

Real Life Example

When you program a thermostat, the temperature settings must be saved in EEPROM so they stay after power off, the program code runs from Flash, and the current temperature reading is stored temporarily in SRAM.

Key Takeaways

Microcontrollers have different memory types for different purposes.

Flash stores program code permanently.

SRAM holds temporary data while running.

EEPROM saves data that must survive power loss.