Hello Leong,
I think that the main issue here is that there are no 12-bit or 24 bit types in C. Therefore either 16-bit or 32-bit types need to be used to fit the data, and will obviously include padding bits. Since the C standard does not define how a compiler should pack the bits within a bitfield, the current packing arrangement by the CW compiler, within a 16 bit multiple, would seem to make a lot of sense because it would produce the most efficient access to each bitfield element, with no compromise on the total size of the bit field. Any other arrangement, such as your packing requirement, would involve additional implicit arithmetic to access each element, with a reduction of efficiency.
My understanding is that the MISRA standard does also not condone the use of bitfields. This would mean that you will need to provide either a function or a macro that uses explicit arithmetic to pack the data in the required manner. In your case, the 24 bits could be either left-aligned or right-aligned within the 32 bit type. The following functions do not use either a bitfield or a union.
dword packR24( word high12, word low12) // Right aligned result
{
return ((dword)high12 << 12) | (low12 & 0x0FFF);
}
dword packL24( word high12, word low12) // Left aligned result
{
return packR24( high12, low12) << 8;
}
result = packL24( foo, bar);
result = packR24( foo, bar);
#define get_fooR(x) (word)((x) >> 12)
#define get_barR(x) (word)((x) & 0x0FFF)
#define get_fooL(x) (word)((x) >> 20)
#define get_barL(x) (word)((x) >> 8) & 0x0FFF
foo = get_fooR( result);
bar = get_barR( result);
Regards,
Mac