This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/TMS570LS3137: Copy code from flash to ram

Part Number: TMS570LS3137

Tool/software: Code Composer Studio

Hi,

I'm using GCC compiler to generate code,I want to run code in ram instead of flash,how to copy the code from flash to ram?here is the ld linker file:

/*----------------------------------------------------------------------------*/
/* sys_link.ld                                                              */
/*                                                                            */
/* (c) Texas Instruments 2009-2013, All rights reserved.                      */
/*                                                                            */
/*----------------------------------------------------------------------------*/
/* Entry Point */
ENTRY(_c_int00)

/* Highest address of the stack */
_estack = 0x8040000;    /* end of 256K RAM */

/* Generate a link error if heap and stack don't fit into RAM */
_Min_Heap_Size = 0x400;      /* required amount of heap  */

/* Specify the memory areas */
MEMORY
{
  VECTORS(rx)     : ORIGIN = 0x00000000, LENGTH = 0x00000020
  FLASH  (rx)     : ORIGIN = 0x00000020, LENGTH = (0x0017FFE0 + 0x00180000)
  CPU_STACK (rw)  : ORIGIN = 0x08000000, LENGTH = 0x00001500 /* Stack is configured in sys_core.asm */
  RAM (xrw)       : ORIGIN = 0x08001500, LENGTH = 0x0003EB00
  MEMORY_B1 (rx)  : ORIGIN = 0x60000000, LENGTH = 0K
}

/* Define output sections */
SECTIONS
{
  /* The ISR vector goes first into RAM */
  .intvecs :
  {
    . = ALIGN(4);
    KEEP(*(.intvecs)) /* ISR vector */
    . = ALIGN(4);
  } >VECTORS

  /* The program code and other data goes into RAM */
  .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 RAM */
  .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.*) } >RAM
    .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

  /* 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

  PROVIDE ( end = _ebss );
  PROVIDE ( _end = _ebss );

  /* 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) }
}

  • Hello,

    I didn't read your linker script. Here is a helpful link to understand the GCC linker script: 

    The MEMORY command in linker ld file describes the target system's memory map. These memory spaces are then used as targets for statements in the SECTIONS command.

    The a SECTIONS command describe the placement of each named output section, and specify which input sections go into them. 

    You can add commands inside the output sections (for example .text section, .data section) to create symbols: _text_start, _text_end, _data_start, or _data_end. Those symbol will be useful when copying data from flash to RAM.

    In your c or asm file, you need to copy the code initially stored in flash to RAM.

    For example:

    //copy the contents in .text output section from FLASH to RAM:

    extern const char _text_start, _text_end;   //created in linker ld file

    void __main (void)

    {

        memcpy(&_text_start, &_text_end, &_text_end - &_text_start);

        return;

    }

  • Hi QJ Wang,

    Thank you for your reply,if I copy main function from flash to ram,I can do this:

    .text :
      {
         _copy_start = .;
       
    main.o(.text) _copy_end = .; } >FLASH


    extern const char _copy_start, _copy_end; //created in linker ld file void __main (void) { memcpy(&_copy_start, &_copy_end, &_copy_end - &_copy_start); return; }

    Is my understanding correct?But where is the ram copied?

  • Sorry,

     memcpy(&_copy_start, &_copy_end, &_copy_end - &_copy_start); 

    should be 

     memcpy(&_copy_start, &_run_start, &_copy_end - &_copy_start);

    You can define _run_start as 0x020

    To be honest, I don't know much about the ld file used by IAR.

  • Hi QJ Wang,

    Thank you for your reply,but 0x020 is flash region,I want to run the code in ram.

    /* Specify the memory areas */
    MEMORY
    {
      VECTORS(rx)     : ORIGIN = 0x00000000, LENGTH = 0x00000020
      FLASH  (rx)     : ORIGIN = 0x00000020, LENGTH = (0x0017FFE0 + 0x00180000)
      CPU_STACK (rw)  : ORIGIN = 0x08000000, LENGTH = 0x00001500 /* Stack is configured in sys_core.asm */
      RAM (xrw)       : ORIGIN = 0x08001500, LENGTH = 0x0003EB00
      MEMORY_B1 (rx)  : ORIGIN = 0x60000000, LENGTH = 0K
    }
  • sorry for typo. It should be a RAM address between 0x08001500 and 0x0803FFFF.

  • Hi QJ Wang,

    Thank you for your reply,except for the .text section,does the .data section and other sections need to be copied to ram?

  • Hello,

    It is up to your application. Normally the .data and .bss are in RAM.

  • Hi QJ Wang,

    Do I need to copy all the code, including the initialization code, or just copy the main function to ram?

    Thanks.

  • Hello,

    Can you let me know why you copy and code to RAM and execute the code from RAM? 

  • Hi QJ Wang,

    I want to run the flash API in ram,so I can operate the flash,but I don't know how to copy it to ram with GCC tools.

    Thanks.

  • So you don't need to copy all the code to SRAM. But you need to copy API related code and const to SRAM.

    1. F021 flash API library

    2. your own functions which call the flash APIs directly

    3. const used for definition of flash sectors