On my custom board, I have set an external RAM on 32MB. I use it as heap with the following:
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:
My code end with a HardFault trying to execute a NULL pointer.
Why this is happening? For sure I'm messing something about memory segmentation, or about its initialization.
I am thankful for your help
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...
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
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
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.