heap in external SRAM
I am working with STM3220G-eval board; the processor is STM32f207.
I am trying to move heap into external RAM.
First I made sure that memory gets allocated with new. I set heap size to 4K
and when I executed
char* p = new char(100);
returned pointer is valid and withing expected address range ( 0x2xxxxxxx ) - internal RAM
Second I made sure that external RAM works.
I added
<MemorySegment start="0x64000000" name="EXTRAM" size="0x100000" access="$(NOR_PSRAM3_ACCESS:Read/Write)"/>
to the memory map file
and
<MemorySegment name="EXTRAM">
<ProgramSection alignment="4" size="0x50000" load="No" name=".extram"/>
</MemorySegment>
to flash placement file.
__IO char exta[0x50000] __attribute__((section(".extram")));
extra is placed in external RAM - address (0x64xxxx) and simple memory check is passed ( for loop writing and reading 0x55 and 0xAA to the entire array )
Now I moved to actually moving the heap.
I further modified flash placement file to:
<MemorySegment name="EXTRAM">
<ProgramSection alignment="4" size="__HEAPSIZE__" load="No" name=".heap"/>
<ProgramSection alignment="4" size="0x50000" load="No" name=".extram"/>
</MemorySegment>
re-compile.
Memory usage window shows that .heap is placed into EXTRAM.
However
char* p = new char(100); causes hard fault.
I clearly missing some setup/compile flag or something else?
Please help
Thank you
-
I know this was asked a LONG time ago, but I've just hit a similar problem and I'd like to share my solution.
The problem essentially is that the heap memory needs to have some key values written to it to indicate its size and that it is empty.
Your processor tries to write those words to the heap before main() is called - hence before anything like ctl_board_init() is called to set up the GPIO ports and identify the appropriate pins as being under the control of the memory-controller peripheral (FMC / FSMC). Because (at reset) all those GPIO pins are floating inputs, the writes never actually put these vital values into the external memory. So when you come to use the heap everything falls over.
The same problem occurs if you have any C++ objects with constructors in external memory - the constructor tries to build the object but can't actually put it into external RAM.
The solution I found was to define a routine (with C linkage) called void SystemInit(void); this gets called before the heap and any C++ constructors are called. SystemInit must enable the clocks to the GPIO lines and do whatever else is necessary for them to work with your external memory.
Hope this helps,
Danish
Please sign in to leave a comment.
Comments
1 comment