0
0
Embedded Cprogramming~20 mins

Memory layout on a microcontroller (Flash, SRAM, EEPROM) in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Embedded Memory Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of variable storage in different memory sections

Consider this embedded C code snippet for a microcontroller:

const int flash_var = 10;
int sram_var = 20;
void main() {
    sram_var += flash_var;
    // What is the value of sram_var after this?
}

What is the value of sram_var after the addition?

Embedded C
const int flash_var = 10;
int sram_var = 20;
void main() {
    sram_var += flash_var;
}
A10
B30
C20
DCompiler error due to const variable
Attempts:
2 left
💡 Hint

Remember that const variables are stored in Flash memory and can be read but not changed.

Predict Output
intermediate
2:00remaining
EEPROM variable persistence after reset

In embedded C, variables stored in EEPROM retain their values after a reset. Consider this code snippet:

int eeprom_var __attribute__((section(".eeprom"))) = 5;
void main() {
    eeprom_var += 3;
    // After a reset, what is the value of eeprom_var?
}

What will be the value of eeprom_var after the microcontroller resets?

Embedded C
int eeprom_var __attribute__((section(".eeprom"))) = 5;
void main() {
    eeprom_var += 3;
}
AUndefined behavior
B5
C0
D8
Attempts:
2 left
💡 Hint

EEPROM memory keeps its data even when power is lost or the system resets.

🔧 Debug
advanced
2:00remaining
Identify the memory section error

Look at this embedded C code snippet:

volatile int flag __attribute__((section(".eeprom"))) = 0;
void main() {
    flag = 1;
    // Some code
}

What is the main problem with placing a volatile int variable in EEPROM?

Embedded C
volatile int flag __attribute__((section(".eeprom"))) = 0;
void main() {
    flag = 1;
}
AWriting to EEPROM is slow and limited; frequent writes can wear it out
BVolatile variables cannot be stored in EEPROM
CEEPROM variables cannot be initialized
DThe compiler will optimize out the variable
Attempts:
2 left
💡 Hint

Think about the physical characteristics of EEPROM memory.

📝 Syntax
advanced
2:00remaining
Correct syntax to place variable in Flash memory

Which of the following code snippets correctly places a variable in Flash memory on a microcontroller using GCC?

Aint val __attribute__((flash)) = 100;
Bint val __flash = 100;
Cconst int val = 100; // automatically in Flash
Dconst int val __attribute__((section(".flash"))) = 100;
Attempts:
2 left
💡 Hint

Constants are usually stored in Flash by default.

🚀 Application
expert
3:00remaining
Memory usage calculation for embedded system

An embedded system has 32 KB Flash, 8 KB SRAM, and 1 KB EEPROM. You have the following variables:

  • const int lookup_table[1000]; // stored in Flash
  • int runtime_buffer[500]; // stored in SRAM
  • int config_data[100]; // stored in EEPROM

Assuming int is 2 bytes, how much memory (in KB) is used in each memory section?

AFlash: 2 KB, SRAM: 1 KB, EEPROM: 0.2 KB
BFlash: 2 KB, SRAM: 1 KB, EEPROM: 0.8 KB
CFlash: 2 KB, SRAM: 1 KB, EEPROM: 0.4 KB
DFlash: 2 KB, SRAM: 1 KB, EEPROM: 0.1 KB
Attempts:
2 left
💡 Hint

Calculate size by multiplying number of elements by size of int (2 bytes), then convert to KB (1024 bytes = 1 KB).