How to run code from RAM on ARM architecture

I program ARM Cortex-R4 and I have some binaries that I would like to execute from TCRAM to make sure that the performance increase is good enough.

I know that I will have to write a function to copy binary files to RAM (which can be done using the script linker and knowing the size of the binary files). But how will they work?

Imagine: The first binary has func1 (), func2 (), func3 () and func4 (). I would copy the entire module into TCRAM and how would I call a function there? Will I need to use a pointer to this function? But what if func4 () calls func2 () and func3 ()? If I'm not mistaken, they will point to a piece of code located in the flash. Does this mean that I will need to write these functions? Use full function pointers? I was told that just a script linker is enough to do all this, and I don’t have to worry about anything, but I still don’t understand how it works.

+6
source share
2 answers

You have two options.

  • Copy them as you suggest, compile with pc relative .
  • Use the linker file with a different download / launch address.

A simple copy will only work if the routines do not use absolute addresses . This can be great if they use an absolute address , since I think you are going to leave a copy in standard RAM. However, this cannot fully utilize TCM .

With the script builder, you can specify different LOAD and RUN locations.

 sections { .text { *(.text); } >FLASH .tcm { *(.tcm); } >TCM_MEM AT>FLASH .data { *(.data); } > RAM .bss : NOLOAD { *(.bss); } > RAM } 

Pay attention, especially AT>FLASH .

See also: gnu linker map file ... and more at stackoverflow. The Gnu Ld manual contains information on LMA partitions ( LOAD ). Your LMA will flash, but the VMA ( RUN ) address will be TCM . The link above also shows how to copy. RAM , FLASH and TCM_MEM determined using ld MEMORY information, depending on the addresses for your board. All of this will be documented in the MAP file. Be sure to create a MAP file and check the addresses to double check your ld script.

The second case also requires a copy (at startup, or at least before using the first TCM function). However, the compiler can use absolute addresses , and they will be in the TCM memory. Also, any function in the main DRAM can invoke the TCM function directly. In the first case, you must use function pointers to call the TCM code. If you want to place global variables in this memory, you can use attributes to put them in different sections and use gnu ld to place them accordingly. I think there are ITCM and DTCM ? Therefore, perhaps this does not apply to you, or you need two sections.

The script component is more general and will work best if you put complex functionality in TCM . Just using -fpic etc., and copying can work quickly, especially if you have only one pure function.

+4
source

In GCC: just put the function in the .data section:

 __attribute__( ( section(".data") ) ) 

It will be copied with the rest of your initialized variables using the startup code (no need to mess with the linker script). You may also need the "long_call" option if the function is "far" from the rest of the code after it is placed in RAM.

 __attribute__( ( long_call, section(".data") ) ) 

Example:

 __attribute__( ( long_call, section(".data") ) ) void ram_foobar (void) { ... } 

You may get a compiler warning that you can safely ignore:

 Warning: ignoring changed section attributes for .data 
+9
source

All Articles