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.

Run code in RAM on flash based device

Other Parts Discussed in Thread: HALCOGEN, TMS570LC4357

Hi. I am using RM57x based board for my project. For a Flash diganostic test, the requirement is that I have to run two functions from RAM. I have tried various ways and various suggestions from TI site, but to no avail.

In our project we use a simple .ld file and we put all text on Flash memory and all data and bss on DRAM memory. I have tried various ways like trying to load from flash and run from ram. But I am getting syntax error in ld file. I tried below line to do this.

.SL_Ramtest : {} load=FLASH, run=RAM

Also I tried using the dram_rsvd2 memory section to load functions in ram. I sort of succeeded in that the map file was showing the functions' starting address in ram. But during execution, when I tried to execute this function, I got an exception.

Also I tried the ramfunc suggestion, but this doesnt seem to work. When I tried to put ramfunc definition in ld file, I am getting error.

Can anyone please help me out, and give detailed steps on how to achieve this?

  • Section 8.8 of the Assembly Language Tools Manual has a lot of information about using different load and run addresses. (Maybe too much information.) The simple steps are (1) create a section with a load address in flash and a run address in RAM. (2) In your code, you must copy the code to be run in RAM into RAM, for example using memcpy(). And (3) then you call the function normally. Can you provide me more information on which steps are not working for you?

  • On a TMS570LC4347, I just completed an analog work.
    As Bob says, you have just to create a section with a LOAD= and a RUN= that receives the code part from the file containing your functions that have to run in RAM.
    I used the TI ARM 5.1.6 compiler suite, and on the TMS570LC4347, I had to reconfigure the MPU so that the code coming from SDRam didn't left stuck in the data cache. Of course, this MPU region should also have the rights to contain executable code. This could be the very origin of your exception.
    The start-up code generated by HalCoGen contains the function that copies the code from Flash to SDRam.
    So to summarize: divide your SDRam into 2 parts, one that receives the normal data (SDRAM_DATA) and the other (SDRAM_CODe) that will receive some code
    populate the SDRAM_CODE region with a section receiving the .text content of the file containing the functions to execute from SDRam ; this section has the attributes LOAD= and RUN= mentioned above
  • Thanks for you valuable tips. I followed your copy table suggestion and I can see that the functions are indeed copied to RAM area. But I am getting a new problem in that when I am trying to execute the function in RAM area, I am getting an exception. The execution is entering the following exception area whenever I try to execute the code. Can you please suggest how to solve this?

    0xc  resetEntry+0xc:   .byte  fe,ff,ff,ea

  • As Marc mentioned, you also need to configure the MPU. HALCoGen by default has the RAM as non-executable:

    Change at least a portion of the RAM to write-through and executable:

  • But I am getting a new problem in that when I am trying to execute the function in RAM area, I am getting an exception.

    As well as the need to change the MPU settings to allow code execution from RAM, tests with a TMS570LC4357 have shown when copying a function from flash to RAM after the Cortex-R5F caches have been enabled, you have to flush the data cache to RAM before attempting to call the RAM function. Failure to flush the data cache can cause a crash / exception when attempting to call a function in RAM since the Cortex-R5F instruction cache is filled with the "stale" RAM contents.

    The attached project for a TMS570LC4357 with the cache enabled has a cache_clean function in HLsys_main.c which is called before attempting to execute a function which has been copied to RAM, which then is executed successfully. The cache_clean function is written in assembler, a TI assembler compatible version of the GNU version which was posted by a different user in Incorrect Cache Enable/Disable procedure in RM57 Technical Reference Manual?

    The project also shows how to use the TI ARM v15.12.12 compiler ramfunc attribute to get the compiler run-time start up code to copy a function from flash to RAM.  User code has been added to the HL_sys_link.cmd to add the linker settings to support RAM functions.

    1832.TMS570LC4357_test.zip