AnsweredAssumed Answered

STM32F407 Placing all code in RAM

Question asked by rodriguez.saul.001 on Oct 23, 2014
Latest reply on Oct 24, 2014 by sung.chen_chung
Hi
I have been trying to place all code in RAM as follows:
1. Create a normal project with code in FLASH (Atollic)
2. Modify linker command file as follows:

/* Entry Point */
ENTRY(Reset_Handler)
 
 
 
/* Highest address of the user mode stack */
_estack = 0x20020000;    /* end of 128K 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_SU (rx)   : ORIGIN = 0x08000000, LENGTH = 8K   
  FLASH (rx)      : ORIGIN = 0x08002000, LENGTH = 1020K 
  RAMCODE (xrw)   : ORIGIN = 0x20000000, LENGTH = 64K
  RAM(rxw)        : ORIGIN = 0x20010000, LENGTH = 64K
  MEMORY_B1 (rx)  : ORIGIN = 0x60000000, LENGTH = 0K
  CCMRAM (rw)     : ORIGIN = 0x10000000, LENGTH = 64K
}
 
 
/*--------------------------------------------------*/
/*            Define output sections                */
/*--------------------------------------------------*/
 
SECTIONS
{
 
  /*----------------------------------------*/
  /* The startup code goes first into FLASH */
  /*----------------------------------------*/
 
  _ssisr = LOADADDR(.isr_vector);
   
  .isr_vector :
  {
    . = ALIGN(4);
     _sisr_vector = .;
    KEEP(*(.isr_vector)) /* Startup code */
    . = ALIGN(4);
    _eisr_vector = .;
  } >RAM AT> FLASH_SU
 
 
 
  /* Start up code makes all memory copies remains in FLASH */
  .startup_text :
  {
    . = ALIGN(4);
    *(.startup_text)
    *(.startup_text*)
    . = ALIGN(4);
    _estartup_text = . ;
  } > FLASH_SU
 
   
 
  /* The program code and other data goes into FLASH */
  _sstext = LOADADDR(.text);
  .text :
  {
    . = ALIGN(4);
    _stext = . ;
    *(.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 */
  } >RAMCODE AT> FLASH
 
 
 
  /* Constant data goes into FLASH->RAM */
  _ssrodata = LOADADDR(.rodata);
  .rodata :
  {
    . = ALIGN(4);
    _srodata = . ;
    *(.rodata)         /* .rodata sections (constants, strings, etc.) */
    *(.rodata*)        /* .rodata* sections (constants, strings, etc.) */
    . = ALIGN(4);
    _erodata = . ;
  } >RAM AT> FLASH
 
 
 
  _ssarm_extab = LOADADDR(.ARM.extab);
  .ARM.extab :
  {
    . = ALIGN(4);
    _sarm_extab = . ;
    *(.ARM.extab* .gnu.linkonce.armextab.*)
    . = ALIGN(4);
    _earm_extab = . ;
  } >RAMCODE AT> FLASH
   
   
   
  _ssarm = LOADADDR(.ARM);
  .ARM :
  {
    . = ALIGN(4);
    __exidx_start = .;
    *(.ARM.exidx*)
    . = ALIGN(4);
    __exidx_end = .;
  } >RAMCODE AT> FLASH
 
 
 
  _sspreinit_array = LOADADDR(.preinit_array);
  .preinit_array : 
  {
    . = ALIGN(4);
    _spreinit_array = . ;
    PROVIDE_HIDDEN (__preinit_array_start = .);
    KEEP (*(.preinit_array*))
    PROVIDE_HIDDEN (__preinit_array_end = .);
    . = ALIGN(4);
    _epreinit_array = . ;
  } >RAMCODE AT> FLASH
   
   
   
  _ssinit_array = LOADADDR(.init_array);
  .init_array :
  {
    . = ALIGN(4);
    _sinit_array = . ;
    PROVIDE_HIDDEN (__init_array_start = .);
    KEEP (*(SORT(.init_array.*)))
    KEEP (*(.init_array*))
    PROVIDE_HIDDEN (__init_array_end = .);
    . = ALIGN(4);
    _einit_array = . ;
  } >RAMCODE AT> FLASH
   
   
   
  _ssfini_array = LOADADDR(.fini_array);
  .fini_array :
  {
    . = ALIGN(4);
    _sfini_array = . ;
    PROVIDE_HIDDEN (__fini_array_start = .);
    KEEP (*(SORT(.fini_array.*)))
    KEEP (*(.fini_array*))
    PROVIDE_HIDDEN (__fini_array_end = .);
    . = ALIGN(4);
    _efini_array = . ;
  } >RAMCODE AT> 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 */
  } >RAMCODE 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) }
}

So as you can see, the "new thing" was  >RAM_CODE AT> FLASH.
Then I modified the start-up assembly file to copy all Flash data into RAM (except for the start-up code itself). I also modified the normal branch instructions to a long branch:

  .syntax unified
  .cpu cortex-m3
  .fpu softvfp
  .thumb
 
.global  g_pfnVectors
.global  Default_Handler
 
/* start address for the initialization values of the .data section.
defined in linker script */
.word  _sidata
/* start address for the .data section. defined in linker script */ 
.word  _sdata
/* end address for the .data section. defined in linker script */
.word  _edata
/* start address for the .bss section. defined in linker script */
.word  _sbss
/* end address for the .bss section. defined in linker script */
.word  _ebss
/* stack used for SystemInit_ExtMemCtl; always internal RAM used */
 
 
.word  _ssisr
.word  _sisr_vector
.word  _eisr_vector
 
.word  _sstext
.word  _stext
.word  _etext
 
.word  _ssrodata
.word  _srodata
.word  _erodata
 
.word  _sspreinit_array
.word  _spreinit_array
.word  _epreinit_array
 
.word  _ssinit_array
.word  _sinit_array
.word  _einit_array
 
.word _ssfini_array
.word _sfini_array
.word _efini_array
 
.word  _ssarm
.word  __exidx_start
.word  __exidx_end
 
.word  _ssarm_extab
.word  _sarm_extab
.word  _earm_extab
 
 
/**
 * @brief  This is the code that gets called when the processor first
 *          starts execution following a reset event. Only the absolutely
 *          necessary set is performed, after which the application
 *          supplied main() routine is called.
 * @param  None
 * @retval : None
*/
 
    //.section  .text.Reset_Handler
    .section  .startup_text.Reset_Handler
  .weak  Reset_Handler
  .type  Reset_Handler, %function
Reset_Handler: 
  ldr   sp, =_estack    /* Atollic update: set stack pointer */
 
/* Copy the data segment initializers from flash to SRAM */ 
  movs  r1, #0
  //b  LoopCopyDataInit
  b     LoopCopyIsrVector
 
 
CopyIsrVector:
  ldr  r3, =_ssisr
  ldr  r3, [r3, r1]
  str  r3, [r0, r1]
  adds  r1, r1, #4
 
LoopCopyIsrVector:
  ldr  r0, =_sisr_vector
  ldr  r3, =_eisr_vector
  adds r2, r0, r1
  cmp  r2, r3
  bcc  CopyIsrVector
  movs  r1, #0
  b  LoopCopyText
 
 
CopyText:
  ldr  r3, =_sstext
  ldr  r3, [r3, r1]
  str  r3, [r0, r1]
  adds  r1, r1, #4
 
LoopCopyText:
  ldr  r0, =_stext
  ldr  r3, =_etext
  adds r2, r0, r1
  cmp  r2, r3
  bcc  CopyText
  movs  r1, #0
  b  LoopCopyRomData
 
 
 
CopyRomData:
  ldr  r3, =_ssrodata
  ldr  r3, [r3, r1]
  str  r3, [r0, r1]
  adds  r1, r1, #4
 
LoopCopyRomData:
  ldr  r0, =_srodata
  ldr  r3, =_erodata
  adds r2, r0, r1
  cmp  r2, r3
  bcc  CopyRomData
  movs  r1, #0
  b  LoopCopyPreInitArray
 
 
 
CopyPreInitArray:
  ldr  r3, =_sspreinit_array
  ldr  r3, [r3, r1]
  str  r3, [r0, r1]
  adds  r1, r1, #4
 
LoopCopyPreInitArray:
  ldr  r0, =_spreinit_array
  ldr  r3, =_epreinit_array
  adds r2, r0, r1
  cmp  r2, r3
  bcc  LoopCopyInitArray
  movs  r1, #0
  b  LoopCopyArm
 
 
 
CopyInitArray:
  ldr  r3, =_ssinit_array
  ldr  r3, [r3, r1]
  str  r3, [r0, r1]
  adds  r1, r1, #4
 
LoopCopyInitArray:
  ldr  r0, =_sinit_array
  ldr  r3, =_einit_array
  adds r2, r0, r1
  cmp  r2, r3
  bcc  CopyInitArray
  movs  r1, #0
  b  LoopCopyFiniArray
 
 
 
CopyFiniArray:
  ldr  r3, =_ssfini_array
  ldr  r3, [r3, r1]
  str  r3, [r0, r1]
  adds  r1, r1, #4
 
LoopCopyFiniArray:
  ldr  r0, =_sfini_array
  ldr  r3, =_efini_array
  adds r2, r0, r1
  cmp  r2, r3
  bcc  CopyFiniArray
  movs  r1, #0
  b  LoopCopyArm
 
 
 
CopyArm:
  ldr  r3, =_ssarm
  ldr  r3, [r3, r1]
  str  r3, [r0, r1]
  adds  r1, r1, #4
 
LoopCopyArm:
  ldr  r0, =__exidx_start
  ldr  r3, =__exidx_end
  adds r2, r0, r1
  cmp  r2, r3
  bcc  CopyArm
  movs  r1, #0
  b  LoopCopyArmExtab
 
 
 
CopyArmExtab:
  ldr  r3, =_ssarm_extab
  ldr  r3, [r3, r1]
  str  r3, [r0, r1]
  adds  r1, r1, #4
 
LoopCopyArmExtab:
  ldr  r0, =_sarm_extab
  ldr  r3, =_earm_extab
  adds r2, r0, r1
  cmp  r2, r3
  bcc  CopyArmExtab
  movs  r1, #0
  b  LoopCopyDataInit
 
 
 
CopyDataInit:
  ldr  r3, =_sidata
  ldr  r3, [r3, r1]
  str  r3, [r0, r1]
  adds  r1, r1, #4
     
LoopCopyDataInit:
  ldr  r0, =_sdata
  ldr  r3, =_edata
  adds  r2, r0, r1
  cmp  r2, r3
  bcc  CopyDataInit
  ldr  r2, =_sbss
  b  LoopFillZerobss
 
 
/* Zero fill the bss segment. */ 
FillZerobss:
  movs  r3, #0
  str  r3, [r2], #4
     
LoopFillZerobss:
  ldr  r3, = _ebss
  cmp  r2, r3
  bcc  FillZerobss
 
 
StupSystemInit:
// Call the clock system intitialization function.
  //bl  SystemInit
  ldr r4, =SystemInit
  mov lr, pc
  bx r4
 
 
StupInitConstructors:
// Call static constructors
  //bl __libc_init_array
  ldr r4, =__libc_init_array
  mov lr, pc
  bx r4
 
 
 
// Call the application's entry point.
  //bl  main
  //bx  lr
  ldr r4, =main
  mov lr, pc
  bx r4
 
.size  Reset_Handler, .-Reset_Handler

... and continues with the normal week interrupt vector definitions, etc.

PROBLEM:
The problem occurs right after (I think) returning from SystemInit(); when the CPU tries to load the address of __libc_init_array, at this point, for some reason the LR register changes its value from some address in RAM to '-1'.

In the code that is shown here I partitioned the RAM into two blocks, one for code and one for data. I have tried having only one RAM section but is the same problem.

QUESTION:
1. Any ideas why is crashing?
2. Does anybody have an example project for the Discovery stm32F407 board or similar that executes *all* code from RAM ?

Thank you.

Ps. I am attaching the project.

Attachments

Outcomes