how to initialize a const array at specific address in memory?
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.
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)));
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; }