2

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?

kyrpav
  • 756
  • 1
  • 13
  • 43
  • What are you using to compile this code? With gcc 11.1.1 it seems to compile without any errors. – larsks Jul 25 '21 at 14:54
  • "cStandard": "c11","cppStandard": "c++17", but it is xtensa-esp32-elf compiler from esp-idf framework. it is esp32 project – kyrpav Jul 25 '21 at 14:59
  • try giving the `union` a name within `i2c_config_t` , say for example `options` and initialize with `.options.master.clk_speed = 100000` But do you really need the union? you could just make `master` and `slave` as `struct`s within `i2c_config_t` without a huge space hit. – infixed Jul 25 '21 at 15:21
  • @infixed, the struct is part of library API – Juraj Jul 25 '21 at 15:41
  • Where is that error? Which dot does expect the primary-expression ? – CiaPan Jul 25 '21 at 17:12
  • The code seems ok, but please do post a [mcve]. – n. m. could be an AI Jul 25 '21 at 17:44

1 Answers1

1
.master = {
  .clk_speed = 100000,
}
kyrpav
  • 756
  • 1
  • 13
  • 43