SoC
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
  • Arm Cortex M3
  • Contact
Reading: Tips on Cortex-M3 Memory Mapping
SUBSCRIBE
SoCSoC
Font ResizerAa
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
Search
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
Have an existing account? Sign In
Follow US
  • Looking for Something?
  • Privacy Policy
  • About Us
  • Sitemap
  • Contact Us
© S-O-C.ORG, All Rights Reserved.
Arm

Tips on Cortex-M3 Memory Mapping

Neil Salmon
Last updated: October 5, 2023 9:55 am
Neil Salmon 8 Min Read
Share
SHARE

The Cortex-M3 processor has a flexible memory mapping scheme that allows you to customize the memory layout to suit your application’s needs. Here are some tips on how to effectively map memory regions in a Cortex-M3 based system:

Contents
Understand the Default Memory MapMap Code and SRAM Regions EfficientlyMap Peripherals at Fixed AddressesReserve Memory for Stack and HeapDisable Unused Memory RegionsUse Memory Protection UnitsInitialize Memory Regions at StartupKeep Performance in MindUse Linker Scripts for MappingBeware of CachesTake Advantage of RemappingAlign Regions to MPU RequirementsTest Memory Access BehaviorAllow Address Space for GrowthDocument Your Memory MapIn Summary

Understand the Default Memory Map

By default, the Cortex-M3 memory map consists of the following regions:

  • Code region from 0x00000000 to 0x1FFFFFFF (512MB)
  • SRAM region from 0x20000000 to 0x3FFFFFFF (512MB)
  • Peripherals region from 0x40000000 to 0x5FFFFFFF (512MB)
  • External device region from 0x60000000 to 0x9FFFFFFF (1GB)
  • Private peripheral bus region from 0xA0000000 to 0xDFFFFFFF (1GB)
  • System region from 0xE0000000 to 0xE00FFFFF (1MB)

This default memory map provides a good starting point. You can customize it by remapping certain regions, disabling unused regions, or changing region sizes.

Map Code and SRAM Regions Efficiently

The code and SRAM regions occupy the first 1GB of address space in the default memory map. Make sure to size these regions appropriately for your application code and data needs. Allocate enough space for your program code in the code region. Size the SRAM region based on your estimated RAM usage.

Try to place time-critical code and data in the SRAM region for fastest access. The code region resides in slower flash memory. You can remap the SRAM region to address 0x00000000 for ultra-fast access if your code size permits.

Map Peripherals at Fixed Addresses

Map peripherals at fixed addresses within the peripherals region. This gives you a clean, constant address to access each peripheral register. Avoid remapping peripherals dynamically.

Group related peripherals together in contiguous address ranges for better organization. For example, map all UART peripherals from 0x40000000 to 0x40000FFF.

Reserve Memory for Stack and Heap

You need to allocate a region of SRAM for the stack and heap used by your C/C++ program. Place this memory region at the end of the SRAM space. The stack grows downwards from the top of this space while the heap grows upwards from the bottom.

Size the stack and heap region generously. Undersizing it can lead to stack overflows or heap allocation failures at runtime. Oversize the region if you are unsure of the exact requirements.

Disable Unused Memory Regions

Any memory regions that are not used by your application can be disabled to save power. Unused sections of the code or SRAM regions can be disabled. Also, disable the external device and private peripheral bus regions if you don’t need them.

Use Memory Protection Units

Use the MPU to set access permissions on memory regions for security. Mark code and peripherals regions as execute-only. Mark SRAM regions containing critical data as no-execute. Disable external access to private peripherals.

The MPU allows creating safe memory partitions preventing accidental/errant writes across memory regions. Use it to protect critical system data structures and firmware code from corruption.

Initialize Memory Regions at Startup

Add initialization code that configures your customized memory map at system startup. The code needs to set up the MPU, remap regions, and disable unused sections. This ensures the memory system is correctly configured before main application code runs.

Perform memory and bus fault tests to verify that restrictions are enforced properly on memory access. Fault handlers can be written to catch any violations.

Keep Performance in Mind

Certain memory mapping practices can impact performance:

  • Frequently accessed code/data should be placed in SRAM instead of flash memory.
  • Minimize number of sections in the MPU to reduce hit time.
  • Memory alignment and interleaving options can help exploit bus architecture.

So optimize the mapping with performance considerations in mind. The goal is mapping memory efficiently while fully utilizing the Cortex-M3 memory architecture.

Use Linker Scripts for Mapping

ARM’s linker scripts provide a convenient way to map memory regions. The linker script contains the memory layout and mappings. The compiler refers to this script when generating the final executable binary.

Linker scripts let you easily define physical memory regions, assign names, set sizes, map code/data sections into regions, and configure placement of stack/heap. Configuring the linker script helps avoid manual mapping work.

Beware of Caches

Some Cortex-M3 variants include instruction and data caches. This provides performance benefits but alters the memory mapping view. The processor may fetch instructions and data from cache instead of external memory.

So your memory mappings need to account for cache behavior. Critical variables should be marked as “uncached” to avoid issues. Cache handling requires flushing/invalidating at times. Cortex-M3 manages caches automatically but an awareness of their impact is useful.

Take Advantage of Remapping

The Cortex-M3 memory remap feature allows flexible reconfiguration of memory regions. Any region can be remapped to a different address range. Some examples:

  • Remap SRAM region to 0x00000000 for fastest access.
  • Swap flash and SRAM regions depending on boot mode.
  • Remap peripherals region to allow contiguous numbering.
  • Move internal flash region after external memory.

Remapping provides optimization opportunities. But it also adds complexity. Use remaps judiciously based on your system requirements.

Align Regions to MPU Requirements

The Cortex-M3 MPU requires memory regions to follow certain alignment rules for region starting address and region size.

Ensure all your MPU regions are aligned correctly. If not, adjust boundaries or sizes slightly so that they conform to the alignment constraints. This avoids MPU configuration errors.

Test Memory Access Behavior

Rigorously test memory access behavior once you have mapped your regions. Verify that:

  • Code executes properly from flash and SRAM
  • Data read/write works as expected in SRAM
  • Peripheral register accesses function correctly
  • Stack/heap available space is sufficient
  • MPU settings enforce permissions on memory regions

Catching any errors or strange behavior during memory access testing can help identify and fix problems in your memory mappings.

Allow Address Space for Growth

When sizing memory regions, allocate slightly more space than needed for your current usage. This provides room to grow as your application evolves.

Having some address space free in code, SRAM, and peripherals regions accommodates future expansion needs. You also avoid having to reshuffle memory on each increment.

Document Your Memory Map

Fully document the memory map in your specifications. Include details like:

  • Start and end address of each region
  • Resource allocated to each region (flash, RAM, peripherals)
  • Access permissions
  • Stack and heap locations

Clear documentation ensures your memory mapping is understood across the design team and for future users as well.

In Summary

Careful planning is essential for utilizing Cortex-M3 memory regions efficiently. Consider application requirements, performance, flexibility, and testing when mapping memory. Leverage remaps and MPUs to optimize usage. Document thoroughly once a final memory map is reached. Following these tips will lead to effective memory utilization on your Cortex-M3 system.

Newsletter Form (#3)

More ARM insights right in your inbox

 


Share This Article
Facebook Twitter Email Copy Link Print
Previous Article Memory Addressing on Cortex-M3 Microcontrollers
Next Article How to Find Valid vs Invalid Addresses for Your Cortex-M3 Microcontroller?
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

2k Followers Like
3k Followers Follow
10.1k Followers Pin
- Sponsored-
Ad image

You Might Also Like

STR instructions in Arm Cortex-M

The STR (store register) instructions in Arm Cortex-M processors are…

8 Min Read

How much RAM does STM32F407 have?

The STM32F407 is an ARM Cortex-M4 based microcontroller from STMicroelectronics.…

10 Min Read

What is the difference between ARM Cortex-M1 and M3?

The main differences between the ARM Cortex-M1 and Cortex-M3 processors…

5 Min Read

ARM Cortex-M SP Registers

The ARM Cortex-M processors contain a number of special purpose…

8 Min Read
SoCSoC
  • Looking for Something?
  • Privacy Policy
  • About Us
  • Sitemap
  • Contact Us
Welcome Back!

Sign in to your account