MC9S08SH16 - Local static variable initialization

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

MC9S08SH16 - Local static variable initialization

2,567 Views
Octavian_D_Iosu
Contributor I

Hello,

I am relatively new to Freescale micros and CodeWarrior. I currently work at a project that uses a MC9S08SH16 micro and I have a question about variable initialization at start-up.

I used the following code

 

#define DEFINED_VAR  

 

void nameFunction1(void)

{

  static unsigned char var1 = DEFINED_VAR;

 

  if(var1 != 0)

  {

    --var1;

  }

  else

  {

    var1 = DEFINED_VAR;

    nameFunction2(); /* function call */  

  } 

 

....

} /* end of function */

 

At compile time  I got the following warning

 

L1981:  No copydown created for initialized object "var1.4". Initialization data lost. 

 

Basically at start-up  the var1 was initialized to 0 and the "else" branch was executed immediately even tough that was not the intention.

I realize that are work around solutions for this particular example but I would like to understand why this warning message in showed up the first place and what settings must be enabled in the project such that the static local variable gets initialized at start-up.

 

Thank you for your consideration 

Labels (1)
0 Kudos
Reply
5 Replies

1,489 Views
pgo
Senior Contributor V

Dear Octavian,

 

Is this the code you actually compiled?

 

The code as listed appears to have a syntax error since you have defined DEFINE_VAR as nothing!

 

The line

 static unsigned char var1 = DEFINED_VAR;

 

would translate as :

 

 static unsigned char var1 = ;

 

Which would be a syntax error other than the one you indicate.

 

 

Changing the #define line to:

 

#define DEFINED_VAR  1

 

results in no errors or warnings.

 

 

Also, are you using ANSI Startup Code option in the new Project Wizard? This would be necessary to initialise static variables.

 

bye

 

0 Kudos
Reply

1,489 Views
Octavian_D_Iosu
Contributor I

Hello,

My mistake...

the code snippet was just an example and I forgot to assign a value to DEFINED_VAR

 

In the actual code I did assign a value to my #define.

Also the compiler has been set up to generate start-up code using ANSI and not minimal requirements and yet I still got the warning message L1981.

After reading some Help documentation and looking at the map file I think it has to do with how the variables are allocated in the memory at startup (data is copied from ROM to RAM).

If I would have declared my variable global ( static implied) the warning would not have showed up but I tried to minimize the number of the global variable used.

 

Thank you for your consideration 

0 Kudos
Reply

1,489 Views
CompilerGuru
NXP Employee
NXP Employee

As far as the initialization is concerned, static locals and globals behave the same.

The question is why does the linker drop the initialization data, and for this only the minimal startup option or in general the lack of startup code comes to mind.

Can you try the same in a newly created project? I think in your setup something got changed which

broke the global initialization (maybe intentionally).

 

Daniel

0 Kudos
Reply

1,489 Views
Octavian_D_Iosu
Contributor I

That is what I think myself - that it is a compiler settings that distinguishes between the globals and the static variables initialization at startup. I kinda inherited the project and I have to continue with what I got but I will try your suggestion to generate another project to see if the warning persists.

Thank you 

0 Kudos
Reply

1,489 Views
CompilerGuru
NXP Employee
NXP Employee

When rereading the post I thought of another reason, apart from disabled startup code, to get the dropped initialization data.

The linker supports different segment types, only READ_WRITE segments get initialized by the copy down. If any variable gets allocated into another type (NO_INIT or PAGED) segment, that variables initialization data is dropped.

So into what kind of segment is the variable you experienced the issue placed?

Not sure out of memory, but it is possible that static locals and globals are placed by default into different ELF sections and therefore can be placed into different segments in the prm.

 

 

Daniel

 

BTW: To make the list of segment types complete, if a variable gets allocated in a READ_ONLY segment, then the variable is initialized during download, not during startup. This is typically used placing constants into flash or EEPROM.

 

0 Kudos
Reply