Configuring the stack and heap for use with microlib
To use microlib, you must specify an initial pointer for the stack. You can specify the initial pointer in a scatter file or using the __initial_sp
symbol.
To use the heap functions, for example, malloc()
, calloc()
, realloc()
and free()
,
you must specify the location and size of the heap region.
To configure the stack and heap for use with microlib, use either of the following methods:
Define the symbol
__initial_sp
to point to the top of the stack. If using the heap, also define symbols__heap_base
and__heap_limit
.__initial_sp
must be aligned to a multiple of eight bytes.__heap_limit
must point to the byte beyond the last byte in the heap region.In a scatter file, either:
-
Define
ARM_LIB_STACK
andARM_LIB_HEAP
regions.If you do not intend to use the heap, only define an
ARM_LIB_STACK
region. Define an
ARM_LIB_STACKHEAP
region.If you define an
ARM_LIB_STACKHEAP
region, the stack starts at the top of that region. The heap starts at the bottom.
-
Examples
To set up the initial stack and heap pointers using armasm assembly language:
.global __initial_sp .equ __initial_sp, 0x10000 ; top of the stack .global __heap_base .equ __heap_base, 0x400000 ; start of the heap .global __heap_limit .equ __heap_limit, 0x800000 ; end of the heap
To set up the initial stack pointer using inline assembler in C.
__asm(".global __initial_sp\n\t" ".equ __initial_sp, 0x10000\n\t" /* equal to the top of the stack */ );
To set up the heap pointer using inline assembler in C.
__asm(".global __heap_base\n\t" ".equ __heap_base, 0x400000\n\t" /* equal to the start of the heap */ ".global __heap_limit\n\t" ".equ __heap_limit, 0x800000\n\t" /* equal to the end of the heap */ );