When defining global variables in such way:
byte var1;
byte var2;
byte var3;
byte varArray[10];
After compiling, var1 is placed at the lower 8 bits of address 0x00, var2 is placed at the upper 8 bits of address 0x00, var3 is placed at the lower 8 bits of address 0x01, and varArray is located from the upper 8 bits of address 0x01.
If there's a structure pointer such as:
typedef struct
{
byte w8Data1;
byte w8Data2;
byte w8Data3;
byte w8Data4;
byte w8Data5;
byte w8Data6;
byte w8Data7;
byte w8Data8;
word w16Data10;
}STRUCT_TEST;
STRUCT_TEST *sPtr = (STRUCT_TEST *)varArray;
now sPtr->w8Data1 is actually the lower 8 bits of address 0x01, which is var3, but not varArray[0]. The reason is that varArray is not aligned and a structure pointer is always a word pointer in DSC compiler. So I tried to find a flexible way to do the alignment without modifying linker file. I found there's a similar question in this community, and the answer is to define variables in such way:
byte varArray[10] __attribute__((aligned(2)));
But it is NOT working at all in CW10.6 on 56F84xxx.
Is there a solution for this? I need the answer asap.
Hi Zhou
sorry for the confusion.
I just tested "byte varArray[10] __attribute__((aligned(2)));" under 56F84789. it works normal. Which chip can not work? I will investigate.
Thanks
Best Regards
Jennie Zhang
Hi Jennie,
I attached a simple project based on 56F84789 in CW10.6.
The variables definition is as follow:
byte varArray[10] __attribute__((aligned(2)));
byte w8Var1;
byte w8Var2;
byte w8Var3;
byte varArray_1[10] __attribute__((aligned(2)));
They are initialized in main function:
w8Var1 = 0x11;
w8Var2 = 0x22;
w8Var3 = 0x33;
varArray[0] = 0x44;
varArray[1] = 0x55;
varArray[2] = 0x66;
varArray[3] = 0x77;
varArray[4] = 0x88;
varArray[5] = 0x99;
varArray[6] = 0xaa;
varArray[7] = 0xbb;
varArray[8] = 0xcc;
varArray[9] = 0xdd;
varArray_1[0] = 0x01;
varArray_1[1] = 0x02;
varArray_1[2] = 0x03;
varArray_1[3] = 0x04;
varArray_1[4] = 0x05;
varArray_1[5] = 0x06;
varArray_1[6] = 0x07;
varArray_1[7] = 0x08;
varArray_1[8] = 0x09;
varArray_1[9] = 0x0a;
From the xMAP file, we can see how these variables are located:
It is obvious that w8Var1 occupies the lower 8 bits of address 0x06, and varArray[] starts from the higher 8 bits of address 0x06, which can be an issue if 'varArray' is converted into a structure pointer.
You can also check it out from the memory show as below. (Default little endian is applied for display)
Please have a check, thanks.
Hi Sutter,
I reproduced your problem. I will contact DevTech today.
Will let you know as soon as I get any feedback there.
Have a great day,
Jennie Zhang
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Hi zhou,
56800E compiler does not have the possibility to align structures to 1.
We have a feature to align to 2 bytes for 4-byte long data types. Here it is (from the release notes):
- Support for pack & align on 2 bytes for 4-byte long data types;
the only __attribute__ combination that is supported is
__attribute__((packed, aligned(2)))
and its purpose is to allow data types that are 4-byte long to be
aligned on 2-byte boundary.
It will be the case for long data type and pointers to any data type
but in large-memory-model.
Declaration examples:
char* __attribute__((packed, aligned(2))) in; // packed pointer to char
char* __attribute__((packed, aligned(2))) * pin; // pointer to packed pointer to char
char* __attribute__((packed, aligned(2))) * __attribute__((packed, aligned(2))) ppin; // packed pointer to packed pointer to char
long __attribute__((packed, aligned(2))) lg; //packed long
struct attribute__((packed, aligned(2))) STag { // __attribute__ propagated to member level
char c; // offset 0
long l; // offset 2
char c1; // offset 6
char* pc; /* assume LMM*/ // offset 8
};
Have a great day,
Jennie Zhang
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
I don't think you understand my question. I need to align a byte array, not a structure variable. Anyway, I've tried __attribute__((packed, aligned(2))) in byte array definition, it's not working.