2015-03-20 07:02 AM
Hi,
I am having a problem with RAM shortage on 32F429IDISCOVERY because of a complex processing algorithm. Generally speaking heap crushes into my stack and hard fault is called. The algorithm that I use cannot be changed so I need to extend my RAM. I already tried to allocate everything onto external SDRAM but its not enough.So I was thinking of expanding the RAM with the SDRAM to the linker but I cannot get it work. I am not really skilled in writing the linker scripts so I would like some help.My idea is to use first 3 banks of SDRAM (6MB) with the RAM like if RAM is full continue in SDRAM and leave the last bank of SDRAM (2MB) like an LCD buffer. So I modify the default EmBlocks (Alltoic) linker script like this:/* Entry Point */ENTRY(Reset_Handler)/* Highest address of the user mode stack */_estack = 0x20030000; /* end of 192K RAM *//* Generate a link error if heap and stack don't fit into RAM */_Min_Heap_Size = 0; /* required amount of heap */_Min_Stack_Size = 0x400; /* required amount of stack *//* Specify the memory areas */MEMORY{ FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 2M RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 192K MEMORY_B1 (rx) : ORIGIN = 0x60000000, LENGTH = 0K CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 64K SDRAM (rw) : ORIGIN = 0xD0000000, LENGTH = 6M}/* Define output sections */SECTIONS{ /* The startup code goes first into FLASH */ .isr_vector : { . = ALIGN(4); KEEP(*(.isr_vector)) /* Startup code */ . = ALIGN(4); } >FLASH /* The program code and other data goes into FLASH */ .text : { . = ALIGN(4); *(.text) /* .text sections (code) */ *(.text*) /* .text* sections (code) */ *(.glue_7) /* glue arm to thumb code */ *(.glue_7t) /* glue thumb to arm code */ *(.eh_frame) KEEP (*(.init)) KEEP (*(.fini)) . = ALIGN(4); _etext = .; /* define a global symbols at end of code */ } >FLASH /* Constant data goes into FLASH */ .rodata : { . = ALIGN(4); *(.rodata) /* .rodata sections (constants, strings, etc.) */ *(.rodata*) /* .rodata* sections (constants, strings, etc.) */ . = ALIGN(4); } >FLASH .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH .ARM : { __exidx_start = .; *(.ARM.exidx*) __exidx_end = .; } >FLASH .preinit_array : { PROVIDE_HIDDEN (__preinit_array_start = .); KEEP (*(.preinit_array*)) PROVIDE_HIDDEN (__preinit_array_end = .); } >FLASH .init_array : { PROVIDE_HIDDEN (__init_array_start = .); KEEP (*(SORT(.init_array.*))) KEEP (*(.init_array*)) PROVIDE_HIDDEN (__init_array_end = .); } >FLASH .fini_array : { PROVIDE_HIDDEN (__fini_array_start = .); KEEP (*(SORT(.fini_array.*))) KEEP (*(.fini_array*)) PROVIDE_HIDDEN (__fini_array_end = .); } >FLASH /* used by the startup to initialize data */ _sidata = LOADADDR(.data); /* Initialized data sections goes into RAM, load LMA copy after code */ .data : { . = ALIGN(4); _sdata = .; /* create a global symbol at data start */ *(.data) /* .data sections */ *(.data*) /* .data* sections */ . = ALIGN(4); _edata = .; /* define a global symbol at data end */ } >RAM AT> FLASH _siccmram = LOADADDR(.ccmram); /* CCM-RAM section * * IMPORTANT NOTE! * If initialized variables will be placed in this section, * the startup code needs to be modified to copy the init-values. */ .ccmram : { . = ALIGN(4); _sccmram = .; /* create a global symbol at ccmram start */ *(.ccmram) *(.ccmram*) . = ALIGN(4); _eccmram = .; /* create a global symbol at ccmram end */ } >CCMRAM AT> FLASH /* Uninitialized data section */ . = ALIGN(4); .bss : { /* This is used by the startup in order to initialize the .bss secion */ _sbss = .; /* define a global symbol at bss start */ __bss_start__ = _sbss; *(.bss) *(.bss*) *(COMMON) . = ALIGN(4); _ebss = .; /* define a global symbol at bss end */ __bss_end__ = _ebss; } >RAM /* User_heap_stack section, used to check that there is enough RAM left */ ._user_heap_stack : { . = ALIGN(4); PROVIDE ( end = . ); PROVIDE ( _end = . ); . = . + _Min_Heap_Size; . = . + _Min_Stack_Size; . = ALIGN(4); } >RAM /* MEMORY_bank1 section, code must be located here explicitly */ /* Example: extern int foo(void) __attribute__ ((section (''.mb1text''))); */ .memory_b1_text : { *(.mb1text) /* .mb1text sections (code) */ *(.mb1text*) /* .mb1text* sections (code) */ *(.mb1rodata) /* read-only data (constants) */ *(.mb1rodata*) } >MEMORY_B1 /* Remove information from the standard libraries */ /DISCARD/ : { libc.a ( * ) libm.a ( * ) libgcc.a ( * ) } .ARM.attributes 0 : { *(.ARM.attributes) } .HeapMemSection : {*(.HeapMemSection)} >SDRAM}I think that the problem is that the heap crushes the stack anyway so I was thinking allocating the whole CCM RAM to stack and merge the RAM and SDRAM together... #sdram #linker #stm32f429dicovery2015-03-20 07:51 AM
You'd need to look at the allocator routine and where it draws it's memory pool from, and what symbols it needs/uses from the linker/script.
2015-03-20 08:33 AM
Thank you very much for answering but I dont really know how to do that...I was trying to find something on the web but nobody really seem to be bother with that...
2015-03-20 09:44 AM
For GNU/GCC you'd rework this to suit your needs.
/*
sbrk
Increase program data space.
Malloc and related functions depend on this
*/
caddr_t _sbrk(int incr) {
extern char _ebss; // Defined by the linker
static char *heap_end;
char *prev_heap_end;
if (heap_end == 0) {
heap_end = &_ebss;
}
prev_heap_end = heap_end;
char * stack = (char*) __get_MSP();
if (heap_end + incr > stack)
{
_write (STDERR_FILENO, ''Heap and stack collision
'', 25);
errno = ENOMEM;
return (caddr_t) -1;
//abort ();
}
heap_end += incr;
return (caddr_t) prev_heap_end;
}
2015-03-20 09:54 AM
On the Keil front
[DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/STM32F4%20extending%20the%20heap%20to%20seperate%20RAM%20regions&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=54]https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2Fcortex_mx_stm32%2FSTM32F4%20extending%20the%20heap%20to%20seperate%20RAM%20regions&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=542015-03-26 04:10 PM
Hi,
Last few days I was really really desperate about this SDRAM extension and to be completely honest I was thinking that your solutions are misplaced...But I was terribly wrong...all the time I was trying to somehow hack the linker script but it was crushing exactly because this core dynamic allocation routine...basically SDRAM address is always bigger than address of stack that remains the same in classic RAM...Thank you very much clive1...amazing...The linker script stays the same and this code works for me...#include <sys/types.h>
caddr_t _sbrk(int incr) {
static char *heap_end;
static char *sdram_start = (char*) 0xD0000000;
static char *sdram_end = (char*) 0xD0600000;
char *prev_heap_end;
if (heap_end == 0) {
heap_end = sdram_start;
}
prev_heap_end = heap_end;
if (heap_end + incr > sdram_end)
{
return (caddr_t) -1;
//abort ();
}
heap_end += incr;
return (caddr_t) prev_heap_end;
}
2015-03-27 03:56 AM
Thanks for that, I'm glad you were able to get resolution.