Set Stack onto external memory

cancel
Showing results for 
Search instead for 
Did you mean: 

Set Stack onto external memory

501 Views
abderrezak
Contributor III

Hi,

On my custom board, I have set an external RAM on 32MB. I use it as heap with the following:HardFault.png

--defsym=__user_heap_base=0xA0000000

--defsym=_pvHeapLimit=0xA2000000

since Redlib offers this symbols. It works fine.

Now I want to try using the external RAM for both heap and stack using this symbols:

--defsym=__user_heap_base=0xA0000000

--defsym=__user_stack_top=0xA2000000

My code end with a HardFault trying to execute a NULL pointer.

MCU.png

Why this is happening? For sure I'm messing something about memory segmentation, or about its initialization.

I am thankful for your help

Labels (1)
4 Replies

218 Views
converse
Senior Contributor V

Don't forget that as soon as you come out of reset, the stack will be used. If you are using external memory, it won't have been initialised (through the EMC) and so it is anybody's guess what will happen - hard fault seems very likely. You will only be able to set up the stack after you have set up the EMC for your memory, so you will need to use on chip ram for the initial stack, and then you will need to change the SP to point to the new memory.

p.s. Don't forget the stack grows DOWN so you will need to set it to the top of memory, otherwise, as you are trying to do, it will occupy the same space as your heap - not a good idea, and really hard to debug if you don't realise...

0 Kudos

218 Views
abderrezak
Contributor III

Thank you, it is more clear for me now. For setting SP can I directly call __set_MSP from 'main' or my 'SystemInit', or is there some precaution to take before.

I know that Stack grows down and Heap grows up. For this reason I set Heap at start (0xA0000000) and Stack at top (0xA2000000). Why it is wrong to set it this way?

I thought malloc take care of not overflow Heap into the Stack. In addition, I will set _pvHeapLimit

I referred to this picture

code-data-segments.png

Thank you

0 Kudos

218 Views
converse
Senior Contributor V

Don't forget that once you change the stack, you lose whatever information is currently on the stack, so you need to do it as soon as possible after startup (and after setting the EMC). I would set the EMC and change stack in your ResetISR, before doing anything else.

In your 1st post you set

--defsym=_pvHeapLimit=0xA2000000

So this is the maximum extent of the heap. If you set the stack to that same address, you are going to overwrite your heap with stack. So, you need to make sure that your heap and stack are far enough apart that they will not overwrite each other. That was the point I was trying to make. And no, malloc WILL NOT check for stack overflowing into the heap.

218 Views
abderrezak
Contributor III

Hi,

Thank you for help.

Sure, in case of Heap and Stack, I will set _pvHeapLimit to an other value.

0 Kudos