How to place a function in RAM
In some instances it is beneficial to run certain code parts in RAM instead of ROM. For this e.g. a function from ROM needs to be copied at some point into RAM for later usage. The following article will explain how this can be accomplished using Embedded Studio.
__attribute__ ((section(".fast"))) void _Delay(void) { } void main(void) { do { Delay(); } while (1); }
Contents
Example Setup
The setup guide will be based on the following hardware and software components:
- J-Link Software Package V6.30b or later
- Embedded Studio V3.34 or later
- Cortex-M Trace Reference Board, for other target devices the reference project needs to be adjusted respectively
- Any J-Link/J-Trace hardware
- Reference Project: Generic_RAM_to_ROM_Example.zip
Setup Guide
The general procedure is as follows:
- Generate new project in Embedded Studio using the project wizard or use your already set up project.
- Edit the section placement file by right clicking your project in the project explorer and selecting "Edit Section Placement".
- Make sure that your section placement file has RAM and ROM memory segments. If not recreate the project or add them manually.
- For reference section .fast can be used.
- Create a new ROM (Flash) section where to place your future ROM to RAM code in.
- Create a new RAM (SRAM) section that has the name you selected in the newly created ROM section for the value runin=".RAMSectionName".
- Now the sections have been created.
- Go to your application and create e.g. a function that should run in RAM. Place it in your ROM section with the attribute section caller e.g.: void __attribute__((section(".ROMSectionName"))) RunInRAM(int var1, int var2) {}
- Before calling the function the first time in your application you first need to move it manually into RAM with the function memcpy: memcpy((void*)&__RAMSectionName_start__, (const void*)&__ROMSectionName_load_start__, (size_t)&__ROMSectionName_size__);
- If you now call RunInRAM somewhere in your application the function will run in RAM instead of ROM.
For reference the project from section "Example Setup" can be used.