Memory Map, Bit-Banding, and Memory Protection Unit
What You'll Learn
- 1Navigate the standard Cortex-M address regions
- 2Compute bit-band alias addresses
- 3Configure MPU regions with proper attributes
- 4Explain how memory attributes affect ordering and caching
The Cortex-M memory map is a single flat 32 bit address space that the core and the vendor share by convention. ARM defines the low regions for code, RAM, and peripherals, then hands the rest to the silicon vendor. Bit-banding lets you turn a single bit into an aligned word address so a plain store becomes an atomic bit write. The memory protection unit then fences regions with attributes and permissions, which turns wild pointer bugs into precise faults.
The System Memory Map
Code starts at the base of the map and holds the vector table and program text, SRAM follows as a general read write region, and peripherals occupy the peripheral window where every register is device memory. External RAM and external device regions come next, and the top of the map holds the private peripheral bus with the NVIC, SysTick, and debug blocks.
The vector table is usually placed at address zero, and the boot option decides whether that alias points to flash, system memory, or SRAM. Reading a region that does not exist returns a bus fault, which is a useful trick for probing a part without a schematic.
Bit-Banding and Atomic Bit Writes
Bit-banding provides an alias region where each bit of a source word maps to one aligned word address. The bit-band alias for SRAM sits just above the SRAM region, and the peripheral alias sits just above the peripheral window. Writing a one or zero to the alias word atomically sets or clears the corresponding bit in the original word.
This matters because a read modify write sequence on a shared peripheral register can lose an update when an interrupt lands in the middle. Bit-banding removes the race for single bits, though newer code often prefers atomic set and clear hardware registers that the peripheral provides instead.
Memory Protection Unit Attributes
An MPU region defines a base address, a size, a permission set, and memory attributes. Normal memory can be cached and reordered, while device memory is strongly ordered and posted writes are limited so side effects happen in order. Execution permissions are separate, so a region can be readable but not executable, which is the basics of code protection.
The MPU can provide a background region for privileged access and can be configured so a region covers the stack with no execute permission. When an access violates a rule, the core raises a precise memory management fault that reports the faulting address, which turns a random corruption report into an actionable crash.
Cortex-M System Memory Map
| Region | Address range | Typical use |
|---|---|---|
| Code | 0x00000000 to 0x1FFFFFFF | Vector table and program flash |
| SRAM | 0x20000000 to 0x3FFFFFFF | Stack, variables, bit-band alias |
| Peripheral | 0x40000000 to 0x5FFFFFFF | Memory mapped registers |
| External RAM | 0x60000000 to 0x9FFFFFFF | Off chip SRAM or SDRAM |
| External device | 0xA0000000 to 0xDFFFFFFF | Bus mapped peripherals |
| Private peripheral | 0xE0000000 to 0xFFFFFFFF | NVIC, SysTick, debug, trace |
Real-World Examples
- Compute a bit-band address â Take the word address of the peripheral flag, subtract the region base, multiply by the alias stride, then add the alias base.
- Guard the stack â Create an MPU region over the stack with read and write access but no execute permission to catch code written to the stack.
- Mark device memory â Configure the peripheral region as device memory so register writes are not merged or reordered by the bus.
- Debug a bus fault â Read the fault status and address registers to identify which region the bad pointer targeted.
â ī¸Attributes Change Behavior, Not Just Speed
Common Mistakes
Treating all memory as normal cacheable memory
Fix: Peripheral registers must be device memory or writes may be buffered, merged, or skipped entirely.
Assuming bit-band works on all addresses
Fix: Only the defined alias windows support it, and many newer parts rely on hardware atomic registers instead.
Forgetting to align MPU region sizes
Fix: MPU regions require power of two sizes and aligned bases, or the configuration write is ignored.
Key Takeaways
- The map splits code, SRAM, peripherals, external space, and the private bus.
- Bit-band aliases turn one bit into an atomically writable word.
- MPU regions attach permissions and memory attributes to address ranges.
- Fault status registers convert silent corruption into debuggable faults.
Frequently Asked Questions
What happens when code reads an unmapped address?
The bus returns a bus fault, and the fault status registers report whether it was a precise or imprecise fault.
Is bit-banding still useful today?
Yes for shared flags, but many modern peripherals provide atomic set and clear registers that are cheaper to use.
Does the MPU change the address layout?
No, it only attaches permissions and attributes to existing addresses without moving anything.
Interview Questions
Q1: Explain memory attributes for a peripheral.
Peripheral space is device memory with strong ordering and limited buffering so side effect registers update exactly once and in program order.
Q2: How does an MPU help an RTOS?
Each task gets its own regions, the stack is guarded against execution, and a stack overflow raises a memory management fault instead of corrupting another task.
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type â Always start by categorizing the problem
- Incorrect formula application â Verify the formula applies to your specific case
- Unit errors â Always check units are consistent throughout
- Sign errors â Pay careful attention to positive and negative values
- Rounding too early â Keep full precision until the final answer
- Forgetting constraints â Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic â Build on the foundation laid here
- Next Topic â See how this concept extends to more advanced material
- Practice Problems â Additional exercises with solutions
- Video Explanation â Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions