I am somewhat unfamiliar with unions, have done some reading on them and am having trouble figuring this out.
Someone has defined a union for me:
union CANDATA // Unionize for easier cooperation amongst types
{
unsigned long long ull;
signed long long sll;
u32 ui[2];
u16 us[4];
u8 uc[8];
u8 u8[8];
s32 si[2];
s16 ss[4];
s8 sc[8];
};
There is also a struct that has this union as one of its members:
struct CANRCVBUF // Combine CAN msg ID and data fields
{ // offset name: verbose desciption
u32 id; // 0x00 CAN_TIxR: mailbox receive register ID p 662
u32 dlc; // 0x04 CAN_TDTxR: time & length p 660
union CANDATA cd; // 0x08,0x0C CAN_TDLxR,CAN_TDLxR: Data payload (low, high)
};
I am creating an instance of CANRCVBUF:
static struct CANRCVBUF increasingMessage = {
0x44400000, /* 11 bit id */
0x00000002, /* 2 data bytes */
{
0x0000
}
};
What I want to do next, is create a loop that increments the data portion of increasingMessage. This is where I am having trouble. My attempt was:
if(increasingMessage.cd + 1 > 65535) {
increasingMessage.cd = 0x0000;
} else {
increasingMessage++;
}
I realize that by using increasingMessage.cd
I am accessing the union, not the data in the union. My trouble is, how do I know which member of the union is used when creating increasingMessage?
Any tips are greatly appreciated