开发者

How to break 32 bit value and assign it to three different data types.?

开发者 https://www.devze.com 2023-01-12 01:04 出处:网络
I have a #define PROT_EN_DAT0x140 //(320 in decimal) Its loaded into 64 bit value register(ex setup_data[39:8]=PROT_EN_DATA)

I have a

#define PROT_EN_DAT  0x140  
//(320 in decimal)

Its loaded into 64 bit value register(ex setup_data[39:8]=PROT_EN_DATA)

Now i want to put this value(0x140)into

 uint8_t    bRequest
 uint16_t   wValue
 uint16_t   wIndex

How can load the value so that i don't have to manually do it for other values again. I think开发者_JAVA百科 we can do with shift operators but don know how.

EDIT:Ya its related to USB. bRequest(8:15),wValue(16:31),wIndex(32:47) but setup_data is 64 bit value.I want to know how can i load proper values into the these fields.

For example say next time i am using #define PROT_EN2_REG 0x1D8. and say setup_data[39:8]=PROT_EN2_DATA


General read form:

aField = (aRegister >> kBitFieldLSBIndex) & ((1 << kBitFieldWidth) - 1)

General write form:

mask = ((1 << kBitFieldWidth) - 1) << kBitFieldLSBIndex;
aRegister = (aRegister & ~mask) | ((aField << kBitFieldLSBIndex) & mask);

where:

  • aRegister is the value you read from the bit-field-packed register,
  • kBitFieldLSBIndex is the index of the least significant bit of the bit field, and
  • kBitFieldWidth is the width of the bit field, and
  • aField is the value of the bit field

These are generalized, and some operations (such as bit-masking) may be unnecessary in your case. Replace the 1 with 1L if the register is larger than 32 bits.

EDIT: In your example case (setup_data[39:8]=PROT_EN_DATA):

Read:

aField = (setup_data >> 8) & ((1L << 32) - 1L)

Write:

#define PROT_EN_MASK = (((1L << 32) - 1L) << 8) // 0x0000000FFFFFFFF0
setup_data = (setup_data & ~PROT_EN_MASK) | ((PROT_EN_DATA << 8) & PROT_EN_MASK);
0

精彩评论

暂无评论...
验证码 换一张
取 消