cortex-mkeil

Cortex-M3 heap-stack organization using keil


Trying to run blinky sample for Atmel sam3s and inspecting the stack pointer... SP has the value 0x20000238 at the start of main function which is equal too Ram base + RW + ZI for this sample.

The base RAM address for this chip is : 0x20000000 Total ram size is: 0x10000

I expected the sp to be initialized on 0x20010000 and coming down. Can anyone explain if I am wrong or not?


Solution

  • As Pait said (he/she should have answered the question so I could accept it, I think), I was wrong to think the stack will be placed at the end of RAM by default. But I think it is wise to make it be placed there.

    This is how I do it for my SAM3S micro, by changing the scatter file

    LR_IROM1 0x00400000 0x00080000  {    ; load region size_region
      ER_IROM1 0x00400000 0x00080000  {  ; load address = execution address
       *.o (RESET, +First)
       *(InRoot$$Sections)
       .ANY (+RO)
      }
      RW_IRAM1 0x20000000 0x00010000  {  ; RW data
       .ANY (+RW +ZI)
      }
      RW_STACK 0x2000C000 UNINIT 0x4000 { ; STACK data
       *.o (STACK)
      }
    }
    

    RAM base = 0x20000000

    Total RAM = 64 kb

    Intended stack size = 16 kb