Search code examples
cstructesp32i2carduino-esp32

access uino variable when union name is not defined


I am working with esp32 and trying to use the i2c library.

There is a config struct.

typedef struct{
i2c_mode_t mode;     /*!< I2C mode */
int sda_io_num;      /*!< GPIO number for I2C sda signal */
int scl_io_num;      /*!< GPIO number for I2C scl signal */
bool sda_pullup_en;  /*!< Internal GPIO pull mode for I2C sda signal*/
bool scl_pullup_en;  /*!< Internal GPIO pull mode for I2C scl signal*/

union {
    struct {
        uint32_t clk_speed;     /*!< I2C clock frequency for master mode, (no higher than 1MHz for now) */
    } master;
    struct {
        uint8_t addr_10bit_en;  /*!< I2C 10bit address mode enable for slave mode */
        uint16_t slave_addr;    /*!< I2C address for slave mode */
    } slave;
};
} i2c_config_t;

From this when i try to create and assign in my code the i2c_config_t like:

                i2c_config_t i2cConfigT={
                    .mode = I2C_MODE_MASTER,
                    .sda_io_num = _sda,
                    .scl_io_num = _clk,
                    .sda_pullup_en = GPIO_PULLUP_ENABLE,
                    .scl_pullup_en = GPIO_PULLUP_ENABLE,
                    .master.clk_speed = 100000};

i get error on last row at the . before the master variable.

Error says

expected primary-expression before '.' token

From this i see that there is no defined name for the union is it possible this to be the issue?


Solution

  • .master = {
      .clk_speed = 100000,
    }