Challenge - 5 Problems
Memory-mapped I/O Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Memory-mapped I/O Register Write
What will be the output of this embedded C code snippet that writes to a memory-mapped I/O register?
Embedded C
#include <stdio.h> #include <stdint.h> #define REG_ADDR ((volatile uint32_t*)0x40000000) int main() { *REG_ADDR = 0xABCD1234; printf("Register value: 0x%X\n", *REG_ADDR); return 0; }
Attempts:
2 left
💡 Hint
Memory-mapped I/O registers behave like normal memory locations for read/write operations.
✗ Incorrect
The code writes the value 0xABCD1234 to the memory address 0x40000000 and then reads it back. Since this is a memory-mapped register, the value read matches the value written.
🧠 Conceptual
intermediate1:30remaining
Purpose of volatile in Memory-mapped I/O
Why is the
volatile keyword important when accessing memory-mapped I/O registers in embedded C?Attempts:
2 left
💡 Hint
Think about how the compiler treats variables that can change outside program control.
✗ Incorrect
Memory-mapped I/O registers can change independently of the program flow, so volatile tells the compiler to always read/write them directly and not optimize those accesses away.
🔧 Debug
advanced2:00remaining
Identify the error in memory-mapped I/O access
What error will occur when running this code snippet that tries to read a memory-mapped I/O register?
Embedded C
#include <stdio.h> #include <stdint.h> #define REG_ADDR 0x40000000 int main() { uint32_t value = *REG_ADDR; printf("Value: 0x%X\n", value); return 0; }
Attempts:
2 left
💡 Hint
Check how REG_ADDR is defined and used as a pointer.
✗ Incorrect
REG_ADDR is defined as an integer constant, but the code tries to dereference it as a pointer without casting. This causes a compilation error.
📝 Syntax
advanced1:30remaining
Correct syntax for writing to a memory-mapped register
Which option correctly writes the value 0x55 to the memory-mapped register at address 0x50000000?
Attempts:
2 left
💡 Hint
Casting the address to a pointer and dereferencing it is required.
✗ Incorrect
Option D correctly casts the address to a volatile pointer and writes the value. Option D misses the cast, causing a type error. Option D writes to a local copy, not the register. Option D has invalid cast syntax.
🚀 Application
expert2:00remaining
Number of bytes accessed in memory-mapped I/O
Given this code accessing a 32-bit memory-mapped register, how many bytes does the program read from the hardware address?
Embedded C
#define REG32 ((volatile uint32_t*)0x60000000) uint32_t read_value() { return *REG32; }
Attempts:
2 left
💡 Hint
Consider the size of the data type pointed to by REG32.
✗ Incorrect
The pointer is to a 32-bit unsigned integer, which is 4 bytes. So the read accesses 4 bytes from the hardware address.