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.
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)));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With