2.5.6. Stack pointer initialization

As a minimum, your reset handler must assign initial values to the stack pointers of any execution modes that are used by your application.

In Example 2.10, the stacks are located at stack_base. This symbol can be a hard-coded address, or it can be defined in a separate assembler source file and located by a scatter-loading description file. Details of how this is done are given in Placing the stack and heap in the scatter-loading description file.

Example 2.10. Initializing stack pointers

; --- Amount of memory (in bytes) allocated for stacks
Len_FIQ_Stack    EQU     256
Len_IRQ_Stack    EQU     256
...
Offset_FIQ_Stack         EQU     0
Offset_IRQ_Stack         EQU     Offset_FIQ_Stack + Len_FIQ_Stack
...
Reset_Handler

; stack_base could be defined above, or located in a description file
        LDR     r0, stack_base ; 

; Enter each mode in turn and set up the stack pointer
        MSR     CPSR_c, #Mode_FIQ:OR:I_Bit:OR:F_Bit 
        SUB     sp, r0, #Offset_FIQ_Stack

        MSR     CPSR_c, #Mode_IRQ:OR:I_Bit:OR:F_Bit 
        SUB     sp, r0, #Offset_IRQ_Stack
        ...

Example 2.10 allocates 256 bytes of stack for FIQ and IRQ mode, but you can do the same for any other execution mode. To set up the stack pointers, enter each mode (interrupts disabled) and assign the appropriate value to the stack pointer.

The stack pointer value set up in the reset handler is automatically passed as a parameter to __user_initial_stackheap() by C library initialization code. Therefore, this value must not be modified by __user_initial_stackheap().

Example 2.11 shows an implementation of __user_initial_stackheap() that you can use with the stack pointer setup shown in Example 2.10.

Example 2.11. 

    IMPORT heap_base
    EXPORT __user_initial_stackheap

__user_initial_stackheap

; heap base could be hard-coded, or placed by description file
    LDR   r0,=heap_base  
    ; r1 contains SB value
    BX   lr
Copyright © 2002-2006 ARM Limited. All rights reserved.ARM DUI 0203G
Non-Confidential