This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Accessing initial_sp and heap_base at run time?

Hello,

We are currently working on stack and heap management on STM32F10x. Is there a way to access then __initial_sp and __heap_base values in the start up file at runtime.

We have found that when tracing the stack, that some code changes effect the position of both these values and rather then having to manual check after each build if the address has change we would like to do this at runtime.

Any suggestion on this can be achieved?

Cheers.

Parents
  • Hi Robert,

    I am trying to monitor the heap and stack to see if they are ever being corrupted.

    I want capture the starting SP and the Heap Base addresses on bootup so that I can monitor the stack usage while the program is running I am expecting the the hex address of each

        __heap_base                              0x2000ab98
        __heap_limit                             0x2000b398
        __initial_sp                             0x2000d398
    

    but I am getting value AAA7655 for the SP and 0 for the heap base.

    The reason for doing it this way is that if the code changes I don't have to manually look at the map file every time and realign the addresses.

    #define HEAP_BASE_ADDR          0x2000ab98 -- replace with __heap_base
    #define HEAP_LIMIT_ADDR         0x2000b398 -- replace with __heap_limit
    #define STACK_P_ADDR            0x2000d398 -- replace with __initial_sp
    

    Regards

Reply
  • Hi Robert,

    I am trying to monitor the heap and stack to see if they are ever being corrupted.

    I want capture the starting SP and the Heap Base addresses on bootup so that I can monitor the stack usage while the program is running I am expecting the the hex address of each

        __heap_base                              0x2000ab98
        __heap_limit                             0x2000b398
        __initial_sp                             0x2000d398
    

    but I am getting value AAA7655 for the SP and 0 for the heap base.

    The reason for doing it this way is that if the code changes I don't have to manually look at the map file every time and realign the addresses.

    #define HEAP_BASE_ADDR          0x2000ab98 -- replace with __heap_base
    #define HEAP_LIMIT_ADDR         0x2000b398 -- replace with __heap_limit
    #define STACK_P_ADDR            0x2000d398 -- replace with __initial_sp
    

    Regards

Children
  • in startup.s
    
           EXPORT __inital_sp
           EXPORT __heap_base
    
    In c file
    
    
    int main()
    {
        extern uint32_t __inital_sp;
        extern uint32_t __heap_base;
    
        printf("%08x : %08x\r\n",__initial_sp,__heap_base);
    }
    

    output is:

    0x20000208 : 0x20000008
    

    The size of my heap is actually 0 (I use no heap) and the stack size is 0x0200. You can see in the map file the __heap_base is 0x20000008 and __heap_limit is 0x20000008.

    You should be able to see these in your map file and the values they contain.

    Still not sure how you determined that your __initial_sp variable contained 0x0AAA7655.

  • Just in case anybody else stumbles over this problem, the correct way to get the real values should be:

    int main()
    {
        extern uint32_t __heap_base;
        extern uint32_t __heap_limit;
    
        uint32_t heap_base = (uint32_t)&__heap_base;
        uint32_t heap_limit = (uint32_t)&__heap_limit;
        ...
    }

    Without these extra &s in assignment compiler will _always_ try to get the value at the label and not its address.