ARM嵌入式编程中需要设置堆栈的空间大小,设置堆栈空间大小是有一个函数来实现的,这个函数就是:__user_initial_stackheap。下面我将这个函数的内容拷贝出来:
/*
* This can be defined to override the standard memory models' way
* of determining where to put the initial stack and heap.
*
* The input parameters R0 and R2 contain nothing useful. The input
* parameters SP and SL are the values that were in SP and SL when
* the program began execution (so you can return them if you want
* to keep that stack).
*
* The two `limit' fields in the return structure are ignored if
* you are using the one-region memory model: the memory region is
* taken to be all the space between heap_base and stack_base.
*/
struct __initial_stackheap {
unsigned heap_base; /* low-address end of initial heap */
unsigned stack_base; /* high-address end of initial stack */
unsigned heap_limit; /* high-address end of initial heap */
unsigned stack_limit; /* low-address end of initial stack */
};
//*----------------------------------------------------------------------------
//* Function Name : __user_initial_stackheap
//* Object : Returns the locations of the initial stack and heap.
//* Input Parameters :
//* Output Parameters :The values returned in r0 to r3 depend on whether you
//* are using the one or two region model:
//* One region (r0,r1) is the single stack and heap region. r1 is
//* greater than r0. r2 and r3 are ignored.
//* Two regions (r0, r2) is the initial heap and (r3, r1) is the initial
//* stack. r2 is greater than or equal to r0. r3 is less than r1.
//* Functions called : none
//*----------------------------------------------------------------------------
__value_in_regs struct __initial_stackheap __user_initial_stackheap(
unsigned R0, unsigned SP, unsigned R2, unsigned SL)
{
struct __initial_stackheap config;
config.stack_base = SP;
config.stack_limit = SP-STACK_SIZE;
config.heap_base = (unsigned)(USER_HEAP_ADDRESS);
config.heap_limit = ((unsigned)(USER _HEAP_ADDRESS))+USER_SRAM_SIZE;
return config;
}
从上面的注释,可以清楚的看出来这个函数的用法。
在配置堆栈区域的时候分为两个模式:单区和双区。
单区的时候,堆和栈共用一块空间区域。堆从高地址往低地址增长。栈从低地址向高地址增长。两个撞上了,程序就崩溃。这时后两个参数stack_limit和heap_limit不用配置。
双区的时候,堆和栈是有各自独立的空间区域。SP是RAM的最高位置的指针。增长方向也是一样的。
在配置这四个参数时要注意堆栈的增长方向。