Topic : typedef struct with bit values greater than 8 bits

Forum : 8051

Original Post
Post Information Post
October 24, 2008 - 10:11am
Guest

Hi,
there are now two questions regarding structures.
Question 1: I want to build a structure that represents a 128bit register table where the values have different bit depths (4,8,11,12,22 bits). The struct looks like this...

typedef struct
{
unsigned CSD_STRuCTURE : 2; // Works
unsigned reserved1 : 6; // Works

unsigned TAAC : 8; // Works
unsigned NSAC : 8; // Works
unsigned TRAN_SPEED : 8; // Works
unsigned CCC : 12; // Error bit-field too long
unsigned READ_BL_LEN : 4; // Works
unsigned REAL_BL_PARTIAL : 1; // Works
...
unsigned C_SIZE : 12; // Error bit-field too long
...
} SDCSD10;

Is there a way to make or update RC51 to deal with bit-values up to 32 bits? I hate bit-moving and masking and that was the reason to move from ASM to C ;-)

Question 2: Regarding bit ordering within a struct.

The structure in placed from address 0x0100...0x010F. The bit order of the structure is: 0x0100 [Bit 127].... 0x010F[Bit 0]. That means, writing 0x03 to CSD_STRUCTURE[127:126] the address 0x0100 contains then 0x03 instead of 0x0C. So i need to change the structure as follows:

typedef struct
{
unsigned reserved1 : 6; // LSB first
unsigned CSD_STRUCTURE : 2; // MSB last

unsigned TAAC : 8;
unsigned NSAC : 8;
unsigned TRAN_SPEED : 8;
...
}

Is there a way to define the LSB and MSB direction of a byte within a struct?

Kind regards,
Sören

Replies
Post Information Post
+1
0
-1
November 10, 2008 - 2:30pm
Guest

1. No.. The maximum width is not specified by ANSI, and depends on the compiler implementation. In the case of RC51, we prefer to keep 8 bit as maimum value since the instruction set does not allow to handle larger 'bit fields'.
2. No, the LSB/MSB convention is unique for all the types defined in the compiler.

Kind Regards
Francis