How to place a function in RAM

From SEGGER Wiki
Revision as of 16:35, 8 February 2018 by Nino (talk | contribs) (Created page with "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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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.

Example Setup

The setup guide will be based on the following hardware and software components:

Setup Guide

The general procedure is as follows:

  1. Generate new project in Embedded Studio using the project wizard or use your already set up project.
  2. Edit the section placement file by right clicking your project in the project explorer and selecting "Edit Section Placement".
  3. Make sure that your section placement file has RAM and ROM memory segments. If not recreate the project or add them manually.
  4. For reference section .fast can be used.
  5. Create a new ROM (Flash) section where to place your future ROM to RAM code in.
  6. Create a new RAM (SRAM) section that has the name you selected in the newly created ROM section for the value runin=".RAMSectionName".
  7. Now the sections have been created.
  8. 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) {}
  9. 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__);
  10. 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.