How to do on MC9S08AW60 constant union in Flash

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

How to do on MC9S08AW60 constant union in Flash

Jump to solution
4,398 Views
BairesRobotics
Contributor I

Hello all, I am currently working with MC9SO8AW60...My question is, how to do a union constant in flash. I try this

 

#define PAGE_MCU 512

union params{
   unsigned char buffer_flash[PAGE_MCU];
   char SetC1;
   char SetC2;
};


union params RomData @0x0900 = {

  SetC1 = 20;   // This value get error

};
 

How to inicializa the params in the fix union

 

Thanks

Jorge Miguel Dhios

Labels (1)
0 Kudos
Reply
1 Solution
1,811 Views
CrasyCat
Specialist III

Hello

 

I entered following code in a sample project:

#define PAGE_MCU    512
union param{
   struct{
      int  SetC1;
      int  SetC2;
      float ValTemp1;
      float ValTemp2;
      long  Id;
   } datos;
   unsigned char buffer_flash[512];
};

union param RomData @0x0900 = {20, 55, 2.34, 5.41, 1000};
 


int temp;
void main(void) {


temp = RomData.datos.SetC1;

 

This can be built (compiled + Linked) without any problem with CodeWarrior for MCU V6.2.

 

CrasyCat

View solution in original post

0 Kudos
Reply
15 Replies
1,812 Views
CrasyCat
Specialist III

Hello

 

If you define a union variable with an initialization value, the initialization value should apply to the first member in the union.

 

So in your case I would try

 

union params RomData @0x0900 = {
{ 20 } 
};

 

This will initialize address 0x900 with 20.

 

 

CrasyCat

0 Kudos
Reply
1,812 Views
BairesRobotics
Contributor I
Dear John: thank you very much for your answer. What you say is correct for variables of the same type, but in the case that these were of another type, this wouldnt work. Is there any other solution to apply? for example how initialize this union ??

 

#define PAGE_MCU    512
union params{
   unsigned char buffer_flash[PAGE_MCU];
   int    SetC1;
   float MaxC1;
};

 

union params RomData @0x0900 = {
      SetC1  = 2000;                           // if this is type int

      MaxC1  = 23.56;                          // if this is type float

};

 

Thanks

Jorge Miguel Dhios

0 Kudos
Reply
1,812 Views
CrasyCat
Specialist III

Hello

 

I am not sure what you want to do here. Are you really sure you want to use a union here?

 

Defining a union allocates all member within the union at the same address.

 

So if you write :

union params{
   unsigned char buffer_flash[PAGE_MCU];
   int    SetC1;
   float MaxC1;
};

 

 

union params RomData @0x0900 = {
      SetC1  = 2000;                           // if this is type int

      MaxC1  = 23.56;                          // if this is type float

};

 

will put buffer_flash, SetC1 and MaxC1 at address 0x9000.

 

Now do you want to get value 2000 or 23.56 stored at address 0x9000?

 

CrasyCat

0 Kudos
Reply
1,812 Views
BairesRobotics
Contributor I

Forgive me CrasyCat, this is what I want to do. To initialize variable you fix in flash wrapped(involved) for an union.

 

//---------------------------------------------------------------

#define PAGE_MCU    512
union param{
   unsigned char buffer_flash[512];
   struct{
      int  SetC1;
      int  SetC2;
      float ValTemp1;
      float ValTemp2;
      long  Id;
   } datos;
};

union params RomData @0x0900;

const params RomData.datos.SetC1      = 20;           // Compiler error
const params RomData.datos.SetC2      = 55;           // Compiler error
const params RomData.datos.ValTemp1 = 2.34;        // Compiler error
const params RomData.datos.ValTemp2 = 5.41;        // Compiler error
const params RomData.datos.Id           = 1000;        // Compiler error

//---------------------------------------------------------------
 

Thanks

Jorge

0 Kudos
Reply
1,812 Views
bigmac
Specialist III

Hello Jorge,

 

I don't see your need for the union since it seems your requirement is to store a constant structure in flash.  You can directly read each element of the structure.  The reason for your compiler errors would appear to be because you are attempting to assign values to the structure elements at run time, rather than initialising the structure at compile time, to be programmed into flash.

 

Perhaps the following code might be suitable.

 

struct param {
   int  SetC1;
   int  SetC2;
   float ValTemp1;
   float ValTemp2;
   long  Id;
};

const struct param RomData @0x0900 = {

   20, 55, 2.34, 5.41, 1000L

};

 

 

Regards,

Mac

0 Kudos
Reply
1,812 Views
CrasyCat
Specialist III

Hello

 

I would recommend doing that as follows:

 

 

//---------------------------------------------------------------

#define PAGE_MCU    512
union param{
   struct{
      int  SetC1;
      int  SetC2;
      float ValTemp1;
      float ValTemp2;
      long  Id;
   } datos;
   unsigned char buffer_flash[512];
};

union params RomData @0x0900 = {20, 55, 2.34, 5.41, 1000};

 

CrasyCat

 

0 Kudos
Reply
1,812 Views
BairesRobotics
Contributor I

Hello CrasyCat:

That you recommend give error compiler:

 

Error: C2450: Expected: ; = , 

 

Error: C2450: Expected: ~ ( IDENT

 

Thanks

Jorge

0 Kudos
Reply
1,812 Views
bigmac
Specialist III

Hello Jorge,

 

I wonder if the error is because in the union definition the name is param, but when you define the variable the name params is used.  In both cases the name should be identical.

 

Regards,

Mac

 

0 Kudos
Reply
1,812 Views
BairesRobotics
Contributor I

Hello bigmac:

I have correct this error but is the same the compiler get error if I initilice the variabels

 

Thanks

Jorge

0 Kudos
Reply
1,812 Views
CrasyCat
Specialist III

Hello

 

I entered following code in a sample project:

#define PAGE_MCU    512
union param{
   struct{
      int  SetC1;
      int  SetC2;
      float ValTemp1;
      float ValTemp2;
      long  Id;
   } datos;
   unsigned char buffer_flash[512];
};

union param RomData @0x0900 = {20, 55, 2.34, 5.41, 1000};
 


int temp;
void main(void) {


temp = RomData.datos.SetC1;

 

This can be built (compiled + Linked) without any problem with CodeWarrior for MCU V6.2.

 

CrasyCat

0 Kudos
Reply
1,812 Views
BairesRobotics
Contributor I
Thanks CrasyCat, the example works very good.
0 Kudos
Reply
1,812 Views
BairesRobotics
Contributor I
Hello CrasyCat I have proved(tried) the code and it works perfectly. I have looked at the file ".S19" and the address does not appear 0x0900. Logically that on having loaded ".S19" in the microcontroller the address 0x0900 is erased and it does not have the constants that I places in the structure. It is necessary to modify some option of the compiler in order to includes in ".S19" the constants??

Gracias
Jorge M. Dhios
0 Kudos
Reply
1,812 Views
CompilerGuru
NXP Employee
NXP Employee

Check the map file what is placed where.

Also note that unless RomData is declared const it will be initialized during startup. If RomData should be in rom/flash, make the variable const.

 

Daniel

0 Kudos
Reply
1,812 Views
CrasyCat
Specialist III

Hello

 

Something else.

Is RomData  referenced somewhere in the application?

 

If not you may need to tell the linker you want to add that object to the executable file.

In that purpose add RomData to your .prm file ENTRIES block.

 

 

CrasyCat

0 Kudos
Reply
1,812 Views
BairesRobotics
Contributor I
Thanks Daniel and CrasyCat:

Placing in front of the word union the word const generates the constants in flash. It is necessary to write it of the form:

const union params RomData @0x0900 = {
         5,     // SetC1
         6,     // SetC2
      2.34,     // ValTemp1
      5.41,     // ValTemp2
      1000      // long  Id
   };
  
Thanks, thanks all.
Jorge
0 Kudos
Reply