Keil Logo

ARM: LOCATING VARIABLES AT ABSOLUTE MEMORY ADDRESSES


Information in this article applies to:

  • MDK-ARM All Versions
  • CARM All Versions
  • GNU C Compiler for ARM All Versions

QUESTION

I need to locate a variable at a fixed memory address. How can I do this using C source code?

BACKGROUND

There are many reasons to store an array variable, such as a table or function to specific address. A checksum might need to be at a certain address. A function might need to be placed at a particular memory location. Depending on what compiler is used, there are options for setting a specific memory area.

ANSWER

Using the Keil µVision IDE :

In some cases it might be simpler to use the GUI to place a section - see ARMCC: Placing Code at an Absolute Address Using the Keil µVision IDE.

ARM Compiler 6 (aka ARMCLANG):

There are several methods to place functions and data at specific addresses, including using the __at attribute to place a section at a specific address. Sections using the __at attribute can be manually placed if need be. There are some restrictions on placing __at sections.

The two most common techniques used for section placement are:

  • Use pointer definitions (as shown below in GCC Compiler example below).

  • With a scatter file, create a region at the exact address needed, and give it a name. Then place the code (or data) with attribute((section("name"))). This attribute works for functions and for variables. Alternatively for assembly code, use AREA.

    See more in the Language extension compatibility: attributes in the Migration and Compatibility Guide, in the subsection Migrating __attribute__((at(address))) and zero-initialized __attribute__((section("name"))). It shows how to to define a variable at a fixed memory address. The following definition also makes a correct memory reservation, so that the area cannot be used twice.

int var __attribute__((section(".ARM.__at_0x40001000")));
.
.
.
var = 4;   // changes the memory location at 0x40001000

ARMCC Compiler version 5.x

There are several methods to place functions and data at specific addresses, including using the __at attribute to place a section at a specific address. Sections using the __at attribute can be manually placed if need be. There are some restrictions on placing __at sections. The three most common techniques used for section placement are:

int var __attribute__((at(0x40001000)));
.
.
.
var = 4;   // changes the memory location at 0x40001000

GNU GCC Compiler
Use only pointer definitions to access absolute memory locations. For example:

/* General Purpose Input/Output (GPIO) */
#define IOPIN0         (*((volatile unsigned long *) 0xE0028000))
.
.
.
IOPIN0 = 0x4;

Keil CARM C Compiler (a deprecated product)
Use either pointer definitions (as shown above) or _at_ keyword to define a variable at a fixed memory address. As with the RealView C Compiler, the following definition also makes a correct memory reservation, so that the area cannot be used twice.

int var _at_ 0x40001000;
.
.
.
var = 4;   // changes the memory location at 0x40001000

MORE INFORMATION

SEE ALSO

FORUM THREADS

The following Discussion Forum threads may provide information related to this topic.

Last Reviewed: Monday, November 5, 2018


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.