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
Solved! Go to Solution.
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
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
#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
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
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
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
Hello
I would recommend doing that as follows:
//--------------------------------------------------------------- |
CrasyCat
Hello CrasyCat:
That you recommend give error compiler:
Error: C2450: Expected: ; = ,
Error: C2450: Expected: ~ ( IDENT
Thanks
Jorge
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
Hello bigmac:
I have correct this error but is the same the compiler get error if I initilice the variabels
Thanks
Jorge
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
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
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