Reputation: 215
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 at a specific location say 0x8000
. This way I want to occupy some amount of code memory so that later 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.
Upvotes: 8
Views: 9022
Reputation: 7057
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.
Upvotes: 6
Reputation: 283893
From C and/or C++, just the way you have written in the question. Possibly add an extern
to override C++'s const
-is-static
-by-default rule.
Then you will need to use a linker directive (.ld
file perhaps) to force that symbol to a particular address in code flash/ROM.
Or, you can assume something outside the build process programs the memory, and your code just accesses it. Then you can do something like:
inline const unsigned char* dummy_string() { return (const unsigned char*)0x8000; }
Upvotes: 0
Reputation: 20130
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)));
Upvotes: 1