

In normal programs, only one of these Stack Pointers will be visible.īoth MSP and PSP are 32-bit, but the lowest two bits of the Stack Pointers (either MSP or PSP) are always zero, and writes to these two bits are ignored. The selection of Stack Pointer is determined by a special register called CONTROL, which will be explained in section 4.2.3. The other Stack Pointer is called the Process Stack Pointer (PSP, or SP_process in some ARM documentation). It is selected after reset, or when the processor is in Handler Mode. Physically there are two different Stack Pointers: the Main Stack Pointer (MSP, or SP_main in some ARM documentation) is the default Stack Pointer. It is used for accessing the stack memory via PUSH and POP operations. Joseph Yiu, in The Definitive Guide to ARM® CORTEX®-M3 and CORTEX®-M4 Processors (Third Edition), 2014 R13, stack pointer (SP) However, if you edit the file manually, you need to make sure that the stack sizes are set up as multiple of 8 bytes. By default, the Configuration Wizard in Keil μVision IDE ( Figure 20.5) ensures that the stack sizes are set up as multiple of 8 bytes. You might also need to check the linker report or memory map report to make sure the stack areas are aligned to double-word boundaries. In addition, stack size should be multiple of 8. For IAR, the stack usage can be obtained from a linker report file (see Section 15.5). You can also see the stack usage in debugger at certain time as shown in Figure 20.14. If Keil MDK is used, the stack size required for a thread can be obtained from an HTML file generated after the compilation.
#Keep it a stack code
This includes the stack size setting in start-up code (e.g., Keil MDK) or linker configuration (e.g., IAR), default stack size for main and thread, and stack size options in osThreadDef defines (Note: if set to 0, then the default stack size is used). It is important to set up sufficient stack memory for your project. Joseph Yiu, in The Definitive Guide to Arm® Cortex®-M0 and Cortex-M0+ Processors (Second Edition), 2015 20.5.1 Stack Size Requirements Also, since exception handlers only use main stack, each of the stack spaces allocated to application tasks do not need to reserve space needed for exception handler, thus allow better memory usage efficiency. This allows the OS to carry out context switching quickly (switching from execution of one application process to another). This allows the stack for the kernel to be separate from stack memory for the application processes.

In a typical embedded application with an OS, the OS kernel uses the MSP and the application processes use the PSP. The PSP is usually only required when an OS is used in the embedded application. In many simple applications, only one stack pointer is needed and by default the MSP is used. Depending on the processor state and the CONTROL register value, the stack pointer accessed can either be the MSP or the PSP. In programming, the stack pointer can be accessed as either R13 or SP in the program codes. For this reason, bit of both stack pointers in the Cortex-M processors are hardwired to zeros and read as zeros. The stack memory accesses in the Cortex-M processors are designed to be always word aligned (address values must be a multiple of 4, for example, 0x0, 0x4, 0x8,…) as this gives the best efficiency for minimum design complexity. The minimum data size to be transferred for each push and pop operations is one word (32-bit) and multiple registers can be pushed or popped in one instruction. This can result in unpredictable behaviors, for example, function return to incorrect addresses. Typically, each register PUSH operation should have a corresponding register POP operation otherwise the stack pointer will not be able to restore registers to their original values. At the beginning of a function, the current contents of the registers used by the calling program are stored onto the stack memory using PUSH operations, and at the end of the function, the data on the stack memory is restored to the registers using POP operations. PUSH and POP are commonly used at the beginning and at the end of a function or subroutine. Stack PUSH and POP in the Cortex ®-M processors.
