I am trying to get a union together to map out some bit fields in a register map. The code I have is the following:
typedef union __attribute__((packed)) {
struct {
uint8_t MODE:3;
uint8_t VSHCT:3;
uint8_t VBUSCT:3;
uint8_t AVG:3;
uint8_t RSVD:3;
uint8_t RST:1;
};
struct {
uint8_t lsbyte:8;
uint8_t msbyte:8;
};
uint16_t w;
} CON_MAP_t;
I am initializing the fields with:
CON_MAP_t map = {
.RST = 0,
.RSVD = 4,
.AVG = 0,
.VBUSCT = 4,
.VSHCT = 4,
.MODE = 7
}
So far this is all fine, no compiler issues or warnings.
I expect the binary/hex representation to be 01000001_00100111 / 0x4127.
However, in the debugger I end up with a value for 'w' of: 00000100_00100111 The least significant byte is correct, but the msb(yte) is not.
I am not sure if I'm missing something fundamental here and I've just been staring at it too long, but any insight would be highly appreciated!
I am using: MPLABX v6.05 Latest XC32 Compiler
Device is a PIC32MX130F064D debugging with a PICKIT4.
