Configuring of I2C fails

Fckx35056
Posts: 3
Joined: Sat Aug 28, 2021 1:37 pm

Configuring of I2C fails

Postby Fckx35056 » Fri Nov 15, 2024 9:37 pm

Hi,

I use ESP-IDF and the Espressif-IDE (Eclipse plugin). I succesfully compiled and ran the i2c_tools example.

Now I am trying to develop my own application. I have a component MPU9250 with a source file MPU9250_fckx.cpp.
Parts of my code:

Code: Select all

#include "driver/i2c_master.h"
#include "esp_err.h"
#include "esp_log.h"
#include "MPU9250_fckx.h"
#include <math.h>

.....

void MPU9250_fckx::i2c_init() {
	ESP_LOGI(TAG,"Hello i2c_init!");
	printf("Hello i2c_init!\n");
    //i2c_config_t conf;

    i2c_master_bus_config_t i2c_bus_config = {  
        .clk_source = I2C_CLK_SRC_DEFAULT,
        .i2c_port = I2C_NUM_0,
        .scl_io_num = SCL_PIN, //i2c_gpio_scl,
        .sda_io_num = SDA_PIN, //i2c_gpio_sda,
        .glitch_ignore_cnt = 7,
        .flags.enable_internal_pullup = true,
    };
The definition of i2c_master_bus_config_t i2c_bus_config is (apart from the names of the right hand sides) identical as the code in the i2c_tools example (cmd_i2ctools.c)

Code: Select all

    i2c_master_bus_config_t i2c_bus_config = {
        .clk_source = I2C_CLK_SRC_DEFAULT,
        .i2c_port = i2c_port,
        .scl_io_num = i2c_gpio_scl,
        .sda_io_num = i2c_gpio_sda,
        .glitch_ignore_cnt = 7,
        .flags.enable_internal_pullup = true,
    };
Upon compilation of my own code I get the error:

error: either all initializer clauses should be designated or none of them should be
.flags.enable_internal_pullup = true,

This is very puzzling.

Can anyone help?
Thanks

MicroController
Posts: 1724
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Configuring of I2C fails

Postby MicroController » Sat Nov 16, 2024 1:27 am

C++ is a bit more restrictive w.r.t structure initialization than C. That's why the exact same lines may compile in a .c file but not in a .cpp file.

Quick work-around:

Code: Select all

i2c_master_bus_config_t i2c_bus_config = {};

i2c_bus_config.clk_source = I2C_CLK_SRC_DEFAULT;
...
i2c_bus_config.flags.enable_internal_pullup = true;
or

Code: Select all

i2c_master_bus_config_t i2c_bus_config = {
...
  .flags = { .enable_internal_pullup = true }
};

Who is online

Users browsing this forum: Google [Bot] and 82 guests