Sooo easy! Just 1 magical word !
To execute a function from RAM using IAR EWARM.

So easy to execute function from RAM

Haven’t you ever experienced that your system was not enough performance? I have often had such situation. If you select MCU with even higher performance, the cost of the system will increase and the sky is the limit.


The material procurement team has strict price targets and there is a limit for selection of components. Therefore, as one method to improve the performance of program execution, the code is executed from RAM. It is a showcase of the skills of software engineers.

Execute out of RAM

When trying to execute all the program code from RAM, a large RAM size is required. Generally, it is impossible to do it on MCU with a small RAM size, if the program code is larger than RAM. So it is often the case that you want to execute out of RAM only the functions whose processing speed is important. (Executing out of RAM whole specific object module like foo.c will be covered in a separate article.) Normally the program code is placed in the code segment of the flash region. You need to place a section for the desired function in the RAM and copy the program code from Flash into RAM. To execute a function from RAM, when you use IAR Embedded Workbench for ARM (EWARM) , with just one magical word it is so easy to execute a function out of RAM. And, EWARM does automatically place the function and copy it from Flash into RAM. You don’t need to do anything but use one magical keyword. Well, let’s see how to do it. First, start off from the points to do.

Points

  • IAR EWARM is used
  • IAR EWARM only requires __ramfunc keyword when you define a function to be executed from RAM.
  • EWARM automatically arranges and copies the function specified with __ramfunc into RAM.

Sample code

We will use the Hello World sample code in the SDK for FRDM-K64F. For the generation of SDK, please refer to the previous article ( MCUXpresso SDK Tutorial: How to generate SDK.). For the test purpose, create an appropriate function test () to be placed in RAM.


void test()
{
    volatile uint32_t cnt;
    cnt++;
}

int main(void)
{
    char ch;

    /* Init board hardware. */
    BOARD_InitPins();
    BOARD_BootClockRUN();
    BOARD_InitDebugConsole();

    PRINTF("hello world.\r\n");

    while (1)
    {
        test();
        ch = GETCHAR();
        PUTCHAR(ch);
    }
}

First, confirm without doing anything

I am going to build the above code without doing anything. The above code should be placed in the flash ROM region.

Well let’s check out the map file after build.
mapfile-pic1

The test () function is located at 0x13e1 as expected.
The memory configuration of Kinetis K64F I used this time as follows. The RAM region is divided into two areas: 0x1fff – 0000 – 0x1fff_ffff and 0x2000 – 0000 – 0x2002_ffff.
memory-map-k64

When __ramfunc extended keyword is used

Next, I tried using the __ramfunc extended keyword in the define of the test () function.
This time, the __ramfunc extended keyword is used only for the function you want to execute from RAM (this time is test ()). When using EWARM, it is possible to automatically execute the target function from RAM without doing anything special, but adding __ramfunc keyword.

That’s it.

You do not need to touch the link file at all.


__ramfunc void test()
{
    volatile uint32_t cnt;
    cnt++;
}

Let’s build and check out the map file.

Here is the Map file when __ramfunc is used.
mapfile-pic2

Check the placement of the test () function.
It is 0x1fff00d. Surely it can be confirmed that it is a RAM area.

The test () function automatically places __ramfunc specified functions from Flash to RAM and copies them by EWARM’s initializer automatically when the system boots up.

Confirmation by debugging

Ok, now let’s check it out by debugging whether the test() function is executed from RAM. If you break at the test() function and check the execution address by disassembly, you can see that it is certainly the address of the RAM area.
debug-breakpoint

Surely, it jumps to RAM and executed out of there.

Summary

With using IAR EWARM, it is possible to execute functions from RAM very easily, simply by using the __ramfunc extended keyword.

In addition, IAR EWARM automatically places and copies the code from Flash into RAM.
You don’t really have to do anything but magical keyword.

In some cases, it is necessary to execute the code execution from the RAM as well depending on the flash ROM when the write operation to the flash is done. Also for the purpose of the execution speed of the function.

This time, I shared a tips that you can automatically placed your functions in RAM and executed from RAM, but in some cases you may want to specify the address in RAM. In the future, I am going to show you the tips as well.

Thank you so much for your reading to the end.

If you think it was good! It would be greatly appreciated if you could share it.