Skip to content

Declare register structures as unions with nested structures #2

Description

@mrkvtrs

Here is updated register data structure that is a union that has a byte variable and anonymous structure:

typedef union
{
    uint8_t buf;

    struct
    {
#if DRV_BYTE_ORDER == DRV_LITTLE_ENDIAN
  uint8_t not_used_01              : 6;
  uint8_t sdo_pu_en                : 1;
  uint8_t not_used_02              : 1;
#elif DRV_BYTE_ORDER == DRV_BIG_ENDIAN
  uint8_t not_used_02              : 1;
  uint8_t sdo_pu_en                : 1;
  uint8_t not_used_01              : 6;
#endif /* DRV_BYTE_ORDER */
    };
} asm330lhh_pin_ctrl_t;

Since the data structure is anonymous you can still access to the structure elements like before:
pin_ctrl.sdo_pu_en = 1;

It is convenient to have an access to the register structure as a byte, when you want to set default value in the register:

asm330lhh_pin_ctrl_t pin_ctrl = {.buf = 0x3f};

also you can use buf as pointer to a register structure, and then you don't need uint8_t type cast:

stc_imu_data_t imu_data[] =
{
    {ASM330LHH_REG_PIN_CTRL,          &pin_ctrl.buf},
    /* Here I would have all of the rest of the IMU registers, */
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions