Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to initialize a const array at specific address in memory?

This question is about embedded controllers. I want to initialize a const array in memory.but while storing this array in memory i want to store it in specific location say 0x8000.This way I want to occupy some amount of code momory so that latter on during run time I can erase that portion and use it for my own other purpose. Basically I want do this:

const unsigned char dummy_string[] = "This is dummy string";

but the address of dummy_string should be in my hand.like i can assign whatever address i want to it.

like image 831
prt Avatar asked Jan 05 '15 05:01

prt


2 Answers

Use a pragma statement to place the variable into a named memory section. Then use the linker command script to locate the named memory section at the desired address.

I scanned through some MSP430 documentation and I think it might work something like this...

In the source code use #pragma DATA_SECTION.

#pragma DATA_SECTION(dummy_string, ".my_section")
const unsigned char dummy_string[] = "This is dummy string";

Then in the linker .cmd file do something like this.

MEMORY
{
    ...
    FLASH    : origin = 0x8000, length = 0x3FE0
    ...
}

SECTIONS
{
    ...
    .my_section    : {} > FLASH
    ...
}

If there are multiple sections located in FLASH then perhaps listing .my_section first will guarantee that it is located at the beginning of FLASH. Or maybe you should define a specially named MEMORY region, such as MYFLASH, which will contain only .my_section. Read the linker command manual for more ideas on how to locate sections at specific addresses.

like image 134
kkrambo Avatar answered Oct 15 '22 15:10

kkrambo


Portable way is to use pointer to set address

  const unsigned char dummy_string[] = "This is dummy string";
  unsigned char* p = (unsigned char*)0x1234;

  strcpy(p, dummy_string);

Non-portable way is to use compiler/platform-specific instructions to set address. For example, for GCC on AVR one can use something like

  int data __attribute__((address (0x1234)));
like image 27
Severin Pappadeux Avatar answered Oct 15 '22 16:10

Severin Pappadeux