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的最高位置的指標。增長方向也是一樣的。
在配置這四個參數時要注意堆棧的增長方向。