Home | Projects | Notes > ARM Cortex-M3/M4 Processor > Stack Memory

Stack Memory

 

Stack Memory

 

Stack Memory Usage

  1. The temporary storage of processor register values

  2. The temporary storage of local variables of the function

  3. During system exception or interrupt, stack memory will be used to save the context (status of general-purpose registers, processor status register, return address, etc.) of the currently executing code

 

usage-of-ram

 

 

Stack Operation Models

Stack operation model is determined at the processor design time. This is not configurable by the programmer.

 

Stack Placement

 

stack-placement

 

 

Banked Stack Pointers

 

msp-vs-psp

 

 

stack-placement

 

 

msp-and-psp-space

 

 

Stack Initialization

Stack Initialization Tips

  1. Evaluate your targeted application. Decide the amount of stack that would be needed for the worst-case scenario of your application run-time.

  2. Know your processor's stack consumption model (FD, FA, ED, EA)

  3. Decide stack placement in the RAM (middle, end, external memory). This can be adjusted using the linker script.

  4. In many applications, there may be second stage stack init. For example, if you want to allocate stack in an external SDRAM then

    • First, start with the the internal RAM.

    • Second, initialize (configure) the SDRAM in the main or startup code and then change the stack pointer to point to that SDRAM.

  5. If you are using the ARM cortex Mx processor, make sure that the first location of the vector table contains the initial stack address (MSP). The startup code of the project usually does this.

  6. You may also use the linker script to decide the stack, heap and other RAM area boundaries. Startup code usually fetches boundary information from linker scripts.

  7. In an RTOS scenario, the kernel code may use MSP to trace its own stack and configure PSP for user task's stack.

 

Exercise

 

References

Nayak, K. (2022). Embedded Systems Programming on ARM Cortex-M3/M4 Processor [Video file]. Retrieved from https://www.udemy.com/course/embedded-system-programming-on-arm-cortex-m3m4/