Yes, it is possible to copy the vector table from flash to SRAM, remap the SRAM to address 0x0, and have interrupts fetch vector addresses from SRAM instead of flash. This can provide performance benefits by reducing flash access latency during interrupt handling. The key steps are:
Overview of Vector Table and Interrupts
On ARM Cortex-M processors, the vector table is a structure located at the start of memory that contains the reset and interrupt vector addresses. By default, it resides in flash memory starting at address 0x0. When an interrupt occurs, the processor fetches the corresponding vector address and jumps to the handler function.
Flash memory has higher read latency compared to SRAM. So fetching vector addresses from flash can increase interrupt latency. Copying the vector table to SRAM and remapping it to 0x0 allows fetchs to occur from low latency SRAM instead.
Copy Vector Table Contents to SRAM
The vector table can be copied from flash to SRAM manually or using the System Memory Management Unit (SMMU). To copy manually:
- Allocate a region of SRAM to hold the vector table.
- Copy vector table contents from flash to SRAM via memcpy or manually.
- Update vector addresses to point to SRAM copies of handler functions.
The SMMU can perform the copy automatically on bootup using its region attributes. A background region attribute will make the SMMU copy a flash region to SRAM transparently.
Remap SRAM to Address 0x0
To remap the SRAM region containing the vector table to address 0x0, memory remap registers need to be configured correctly:
- Setup the Memory Protection Unit (MPU) regions – one for flash and another for SRAM.
- Configure SRAM region to be privileged executable with read/write access.
- Map SRAM region to start at address 0x0.
- Enable MPU and memory remap controller.
This will remap SRAM to 0x0 while the flash remains mapped at its existing base address. The vector table copied to SRAM will now appear at address 0x0.
Configure Interrupt Handling
With the vector table now remapped to SRAM, some additional steps may be needed for interrupt handling:
- Update linker scripts to handle function addresses in SRAM.
- If SMMU used, configure interrupt handling to be consistent with SMMU behavior.
- Invalidate flash cache after copying vector table to ensure coherency.
- Configure interrupt priorities, if needed.
The processor should now fetch interrupt vectors from the vector table located in SRAM. This eliminates flash access latency during interrupt handling.
Advantages of Relocating Vector Table
Relocating the vector table to SRAM provides several benefits:
- Faster interrupt handling due to reduced flash access latency.
- Deterministic interrupt latency since SRAM has fixed read timing.
- Makes flash storage available for other uses.
- Allows flash to be powered down when not in use.
- Facilitates optimizations like background flash writes.
Disadvantages and Considerations
There are also some downsides to be aware of:
- Increased complexity in managing two memory maps (SRAM and flash).
- SRAM usage overhead since memory allocated for vector table.
- Slightly increased interrupt latency if vector fetch still misses cache.
- Extra steps needed to maintain coherency between SRAM and flash copies.
So while performance benefits can be significant, the increased complexity may not be justified in some low power applications. Factors like SRAM size limits, interrupt latency requirements, ease of software development, etc should be evaluated.
Summary
Relocating ARM Cortex-M vector tables from flash to SRAM can optimize interrupt performance by reducing vector fetch latency. Key steps are copying the vector table to SRAM manually or via the SMMU, remapping SRAM to address 0x0, and handling coherence between memory maps. Benefits include faster interrupts, deterministic latency, and flexibility in managing flash. Drawbacks are increased complexity and SRAM overhead. Overall, it is a useful optimization when latency and flash access are concerns.