i2c_config_t union issue on definition of variables

kyrpav
Posts: 37
Joined: Wed Jan 29, 2020 8:27 am

i2c_config_t union issue on definition of variables

Postby kyrpav » Thu Jul 29, 2021 6:53 am

Hello,

I am trying to create a i2c_config_t in order to user i2c for BME680.

My setup is

Code: Select all

 i2c_config_t i2c_config = {
                        .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};
compiler disaggrees with .master.clk_speed and specifically with the first . before master
and returns error

{
"resource": "/Users/kyrpav/workspace/esp32/tutorials/LearnEsp32/main/Lesson5/BME680.cpp",
"owner": "cpp",
"severity": 8,
"message": "expected primary-expression before '.' token",
"startLineNumber": 38,
"startColumn": 25,
"endLineNumber": 38,
"endColumn": 25
}

My compiler extra path on configuration is :

.espressif/tools/xtensa-esp32-elf/esp-2020r3-8.4.0/xtensa-esp32-elf/bin
.espressif/tools/xtensa-esp32s2-elf/esp-2020r3-8.4.0/xtensa-esp32s2-elf/bin
.espressif/tools/esp32ulp-elf/2.28.51-esp-20191205/esp32ulp-elf-binutils/bin
.espressif/tools/esp32s2ulp-elf/2.28.51-esp-20191205/esp32s2ulp-elf-binutils/bin
.espressif/tools/openocd-esp32/v0.10.0-esp32-20200709/openocd-esp32/bin

Esp-idf version is 4.1.1

from i2c_types.h the definition of the struct is

Code: Select all

/**
 * @brief I2C initialization parameters
 */
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;
i tried to place a name to the union like "testit" and the same setup on my code like .testit.master.clk_speed and it works but i do not want to touch the library of idf

boarchuz
Posts: 559
Joined: Tue Aug 21, 2018 5:28 am

Re: i2c_config_t union issue on definition of variables

Postby boarchuz » Thu Jul 29, 2021 7:00 am

Code: Select all

.master = {
  .clk_speed = 100000,
}

kyrpav
Posts: 37
Joined: Wed Jan 29, 2020 8:27 am

Re: i2c_config_t union issue on definition of variables

Postby kyrpav » Thu Jul 29, 2021 7:10 am

boarchuz wrote:
Thu Jul 29, 2021 7:00 am

Code: Select all

.master = {
  .clk_speed = 100000,
}
Thank you this is solving the issue. One more comment i think also in examples of idf in i2c the setup of config_t follows the logic of .master.clk_speed

and it could bring to others also issue.

I would like to know why it works for others like this and not for me what other setup should i check

ESP_Sprite
Posts: 8926
Joined: Thu Nov 26, 2015 4:08 am

Re: i2c_config_t union issue on definition of variables

Postby ESP_Sprite » Thu Jul 29, 2021 10:08 am

It works if you set it in code as a struct member:

Code: Select all

config.master.spi_clock=123; //works
It does not work in a named initializer:

Code: Select all

i2c_config_t config={
	master.spi_clock=123 //does NOT work
}

Who is online

Users browsing this forum: Majestic-12 [Bot] and 123 guests