Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declare register structures as unions with nested structures #2

Open
TMarkivULCRobotics opened this issue Feb 2, 2022 · 0 comments

Comments

@TMarkivULCRobotics
Copy link

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, */
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant