From the Experts: Executing code from RAM using TI compilers

 Many TI processors and microcontrollers (MCUs) store code in flash memory and then execute out of that same flash memory. When the CPU core is running at full frequency, wait states must be introduced on reads from flash memory. This can have a significant performance impact on the application. RAM typically operates with 0 wait states, even at full frequency. It can be very beneficial for performance-critical functions to execute from RAM instead of flash. TI’s MCU compilers for ARM-based MCUs, MSP430™ MCUs and C28x MCUs, starting with version 15.9.0.STS, provide a simple mechanism to accomplish this.

In version 15.9.0.STS we added support for the ramfunc attribute.

__attribute__((ramfunc))
void f(void) { ... }

This attribute provides a simple source-level mechanism for executing code from RAM. The attribute instructs the tools to place the function in flash memory and copy it to RAM at boot time. All calls to the function will call the copy in RAM.

Because the attribute is applied to the function, the compiler can optimize the code for execution from RAM. At the time of writing, the only example of this is on C28x. On C28x, there is a fast branch instruction which is optimal only when executing from RAM. With the attribute, the compiler will automatically select the correct branch instruction. In the future, we plan to expand the optimizations done for code executing from RAM.

This is a brand new feature and we are still rolling out the device file updates to make it a seamless experience. If you receive this error message:

error: ".TI.ramfunc" section generated by __attribute__((ramfunc)) or

                --ramfunc=on requires a SECTIONS specification to function.

Please add the following line in the SECTIONS directive of your linker command file:

.TI.ramfunc : {} load=FLASH,
run=RAM, table(BINIT)

The TI compiler tools provide more complex functionality for executing code from RAM. One example would be to have functions in RAM placed at the same address and copied into RAM before being called. More information can be found in Assembly Language Tools User Guide in the section titled “Linker-Generated Copy Tables”.

  • I'm trying to use the __attribute__((ramfunc))

    but the compiler rejects it.   #1173-D unknown attribute "ramfunc"

    i'm using ccs  6.1.2.00015 (MSP432).

    how can i place functions in ram?

  • Shai, Please make sure you are using the 15.12.x.LTS or 16.9.0.LTS compiler releases. These are available through the CCS App Store or "Install New Software" dialog. The compiler version is distinct from the CCS version.