This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Good Morning.
I have a problem with Initializing variable in Flash Memory.
I wish the following behaviour :
- One variable initialized at 1, when loading firmware in the MSP
-User can change this variable for example to 2
-When Powering Off- Powering On the device, Variable value should be 2
The code :
In the Linker File
Memory Map: INFOB : origin = 0x1000, length = 0x0080
Section allocation into memory : SEG_PSM_SETTINGS : {} > INFOB
#pragma DATA_SECTION (cFlashPsmId,"SEG_PSM_SETTINGS")
char cFlashPsmId=1;
cFlashPsmId is not initialized whatever the ram/rom model initialization I choose.
When Writing
#pragma DATA_SECTION (cFlashPsmId,"SEG_PSM_SETTINGS")
const char cFlashPsmId=1;
cFlashPsmId is correctly initialized.
*BUT*cFlashPsmId is treated as a constant by the compiler
switch(cFlashPsmId)
{
case 0: ...
case 1: ...
}
is not compiled as *I* expected . Casting into Char cFlashPsmId doest not work too.
What would be the correct syntax / linker option ?
Thanks for your help
Hi Pierre,
fo this case I would not edit the linker command file; I recommend changeing your code as follows:
// assign the variable into data section
#pragma DATA_SECTION(cFlashPsmId, ".infoB"); // you did this in your example
// allocate appropriate size in bytes (one byte) for the variable
#pragma DATA_ALIGN(cFlashPsmId, 1); // this one needs to be added
// declare the variable
const char cFlashPsmId;
Refer to slau131c.pdf (Chapter 7) for details on the above!!
aBUGSworstnightmare
Thanks for your reply.
Unfortunaly it does not seem to work as I wish.
http://img242.imageshack.us/img242/354/croppercapture8.png
Did I do someting wrong ?
Hi Pierre_,
be shure to ckeck your FET Debugger settings; by default code and info memory will be erased when you download your application!
Have a look at PROJECT-->FET DEBUGGER-->DOWNLOAD and select the option ERASE MAIN MEMORY to prevent erasing of your info memory. Now it should work.
Rgds
aBUGSworstnightmare
Thanks for your help, but...
It does not.
Anyway , according to SLA132C , the only thing that DATA_ALIGN Pragma directive do is to align symbol in memory. (Not to allocate space).
And Info Memory should (/must) be erased first before writing any data.
I'm not sure we are on the way. (..to solve the problem)
In 5.11.2 of SLAU132C
"You can use the DATA_SECTION pragma to put the variable in a section other than .const. For example,
the following C code:
#pragma DATA_SECTION (var, ".mysect");
const int zero=0;
is compiled into this assembly code:
.sect .mysect
_zero
.word 0"
OK That's what I need..
The C Code:
#pragma DATA_SECTION (cFlashPsmId,".infoB")
const char cFlashPsmId=6;
produces assembly output :
.global cFlashPsmId
.sect ".infoB"
.align 1
cFlashPsmId:
.field 6,8 ; cFlashPsmId @ 0
But *HOW* to prevent the compiler to treat cFlashPsmId as a constant
For example
C code
idx=cFlashPsmId-1;
produces
MOV.B #0x0005,R12
Hi,
if you declare a variable as a constant you will ge a constant (placed in your own memory segment (INFOB in your case))! Pls review my post above.
INFOB is a Flash Memory and NOT EEPROM --> if you want to write new data to it you will have to erase it first (and than write your data). Have a look at the forum; there are various posts with C-Sample for writing to Flash/INFOx memory.
Rgds
aBUGSworstnightmare
thanks but I have no problem with writing in Flash Memory.
(It seems that we do not understand each other.)
The only thing I wish is to Initialize data in specific section that lies in flash memory (out of .const sections)
5.4.1 of SLAU132C
If you define an object as const, the .const section allocates storage for the object. The const data storage
allocation rule has two exceptions:
• If the keyword volatile is also specified in the definition of an object (for example, volatile const int x).
Volatile keywords are assumed to be allocated to RAM. (The program does not modify a const volatile
object, but something external to the program might.)
The correct syntax seems to be
#pragma DATA_SECTION (cFlashPsmId,".infoB")
volatile const char cFlashPsmId=1;
Hi,
if you think that this is the best option for you it's o.k. (but it wouldn't be the best one for me ..)
aBUGSworstnightmare
Pierre,
I have an aternate suggestion that I usuallly use. I store al my settings in flash.
The way that I d it is I create a structure with al my settings and initialization constants.. when the program boots it sequentialy copies the data byte by byte into the structure.. I then check the structure and see if it has valid data (I us a magic number for each version) if not, I set the defauts in the structure (for a specific version) and then copy this data to flash, I can also use this for upgrades... now I can freely modify the variables in the strutre as needed, if I make a change that I want to say I just copy the structure back to flash.. I dont have to worry about any special pragmas or how the compiler wil handle things since I am operating on a r/w structure that I can load or save whenever I want..
**Attention** This is a public forum