简体   繁体   English

无法初始化我的 i2c_config_t 结构

[英]Could not initialize my i2c_config_t structure

According to the i2C.h as follows:根据i2C.h如下:


/**
 * @brief I2C initialization parameters
 */
typedef struct{
    i2c_mode_t mode;       /*!< I2C mode */
    gpio_num_t sda_io_num;        /*!< GPIO number for I2C sda signal */
    gpio_pullup_t sda_pullup_en;  /*!< Internal GPIO pull mode for I2C sda signal*/
    gpio_num_t scl_io_num;        /*!< GPIO number for I2C scl signal */
    gpio_pullup_t 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 wrote the following assignment:我写了以下作业:

    const i2c_port_t i2c_master_port = (i2c_port_t)I2C_MASTER_NUM;
    i2c_config_t conf = {
        .mode = I2C_MODE_MASTER,
        .sda_io_num = I2C_MASTER_SDA_IO,
        .scl_io_num = I2C_MASTER_SCL_IO,
        .sda_pullup_en = (gpio_pullup_t)GPIO_PULLUP_ENABLE,
        .scl_pullup_en = (gpio_pullup_t)GPIO_PULLUP_ENABLE,
        .master = {.clk_speed = I2C_MASTER_FREQ_HZ},
    };

Which reports 2 errors (sorry its in French so I added my own translation into English):其中报告了 2 个错误(抱歉是法语,所以我添加了自己的英语翻译):

At line.sda_pullup_en (translated as: "Out-of-order initializers are not standard in C++"):在 line.sda_pullup_en(翻译为:“乱序初始化器在 C++ 中不是标准的”):


[{

    "code": "2904",
    "severity": 8,
    "message": "les initialiseurs hors service ne sont pas standard en C++",
    "source": "C/C++",
    "startLineNumber": 103,
    "startColumn": 9,
    "endLineNumber": 103,
    "endColumn": 9
}

At line.master (translated as: "a designator for an anonymous union member can only appear between braces that match that anonymous union"):在 line.master(翻译为:“匿名联合成员的指示符只能出现在与该匿名联合匹配的大括号之间”):

    "owner": "C/C++",
    "code": "2358",
    "severity": 8,
    "message": "un désignateur pour un membre d'union anonyme peut uniquement apparaître entre des accolades qui correspondent à cette union anonyme",
    "source": "C/C++",
    "startLineNumber": 105,
    "startColumn": 9,
    "endLineNumber": 105,
    "endColumn": 9
}]

[UPDATE] My initial code writings was as follows: [更新] 我最初的代码写作如下:

 i2c_config_t conf = {
        .mode = I2C_MODE_MASTER,
        .sda_io_num = I2C_MASTER_SDA_IO,
        .scl_io_num = I2C_MASTER_SCL_IO,
        .sda_pullup_en = (gpio_pullup_t)GPIO_PULLUP_ENABLE,
        .scl_pullup_en = (gpio_pullup_t)GPIO_PULLUP_ENABLE,
        .master.clk_speed = I2C_MASTER_FREQ_HZ
    };

And were producing those two errors:并产生了这两个错误:

At .sda_pullup_en = line (109) (translated as: "out of order initializers are not standard in C++"):.sda_pullup_en =行 (109)(翻译为:“乱序初始化器在 C++ 中不是标准的”):

[{
    "owner": "C/C++",
    "code": "2904",
    "severity": 8,
    "message": "les initialiseurs hors service ne sont pas standard en C++",
    "source": "C/C++",
    "startLineNumber": 109,
    "startColumn": 3,
    "endLineNumber": 109,
    "endColumn": 3
}]

At .master.clk_speed = line (111), translated as:.master.clk_speed = (111)行,翻译为:

  • a designator for an anonymous union member can only appear between braces that match that anonymous union匿名联合成员的指示符只能出现在与该匿名联合匹配的大括号之间
  • expected primary-expression before '.' “.”之前的预期主表达式token令牌
  • class "i2c_config_t" has no "clk_speed" field class “i2c_config_t”没有“clk_speed”字段
[{

    "owner": "C/C++",
    "code": "2358",
    "severity": 8,
    "message": "un désignateur pour un membre d'union anonyme peut uniquement apparaître entre des accolades qui correspondent à cette union anonyme",
    "source": "C/C++",
    "startLineNumber": 111,
    "startColumn": 3,
    "endLineNumber": 111,
    "endColumn": 3
},{

    "owner": "cpp",
    "severity": 8,
    "message": "expected primary-expression before '.' token",
    "startLineNumber": 111,
    "startColumn": 3,
    "endLineNumber": 111,
    "endColumn": 3
},{

    "owner": "C/C++",
    "code": "136",
    "severity": 8,
    "message": "classe \"i2c_config_t\" n'a pas de champ \"clk_speed\"",
    "source": "C/C++",
    "startLineNumber": 111,
    "startColumn": 10,
    "endLineNumber": 111,
    "endColumn": 10
}]

How to fix my code?如何修复我的代码?

For the first error, it means the "designator order for field .sda_pullup_en in your config does not match declaration order in i2c_config_t .对于第一个错误,这意味着“ config中字段.sda_pullup_en的指示符顺序与i2c_config_t中的声明顺序不匹配。

A struct is a series of data with various data type in sequence, in your i2c_config_t , the sequence(the order of different data) is结构是一系列具有各种数据类型的数据,在您的i2c_config_t中,序列(不同数据的顺序)是

typedef struct{
    i2c_mode_t mode;
    gpio_num_t sda_io_num;
    gpio_pullup_t sda_pullup_en;
    ...
}i2c_config_t;

While when you using it in your config , you change the order of the members within the struct which caused the first error because the data would not aligned with its definition:当您在config中使用它时,您会更改导致第一个错误的结构中成员的顺序,因为数据不会与其定义对齐:

i2c_config_t conf = {
    .mode = I2C_MODE_MASTER,
    .sda_io_num = I2C_MASTER_SDA_IO,
    .scl_io_num = I2C_MASTER_SCL_IO, // this is out of order from i2c_config_t
    ...
};

For your second error, the error message "a designator for an anonymous union member can only appear between braces that match that anonymous union" is clear, but your usage is wrong, the master is a struct within a struct, so with the braces notation, it should be:对于您的第二个错误,错误消息“匿名联合成员的指示符只能出现在与该匿名联合匹配的大括号之间”很清楚,但是您的用法是错误的, master是结构中的结构,因此使用大括号表示法, 它应该是:

i2c_config_t conf = {
      .master {.clk_speed = I2C_MASTER_FREQ_HZ}
};

In summary, this is the snippet with both errors corrected and should works.总之,这是纠正了两个错误并且应该可以工作的片段。

i2c_config_t conf = {
    .mode = I2C_MODE_MASTER,
    .sda_io_num = I2C_MASTER_SDA_IO,
    .sda_pullup_en = GPIO_PULLUP_ENABLE,
    .scl_io_num = I2C_MASTER_SCL_IO,
    .scl_pullup_en = GPIO_PULLUP_ENABLE,
    .master {.clk_speed = I2C_MASTER_FREQ_HZ}
};

@hcheung gave the right answer about the order of the members. @hcheung 对成员的顺序给出了正确的答案。

But regarding the anonymous union, his solution does not work.但是对于匿名工会,他的方案是行不通的。

I finally came up with the solution:我终于想出了解决方案:

i2c_config_t conf = {
        .mode = I2C_MODE_MASTER,
        .sda_io_num = I2C_MASTER_SDA_IO,
        .sda_pullup_en = GPIO_PULLUP_ENABLE,
        .scl_io_num = I2C_MASTER_SCL_IO,
        .scl_pullup_en = GPIO_PULLUP_ENABLE,
        { {I2C_MASTER_FREQ_HZ}}
    };

Anonymous union are not standard but compiler extension and should actually be named.匿名联合不是标准的,而是编译器扩展,实际上应该命名。 But here, the struct belongs to a third-party lib I cannot update.但是在这里,该结构属于我无法更新的第三方库。

So the solution is that I need to put an open curry braces to catch the first member of the union and then provide the value I am interested in. Very important to note that it sounds we can only reach the first union member as we can't name it.所以解决方案是我需要放一个开放的咖喱大括号来捕捉联合的第一个成员,然后提供我感兴趣的值。非常重要的是要注意,这听起来我们只能到达第一个联合成员,因为我们可以'不要说出来。

Additional note: You can also be explicit about the clock speed as follows:附加说明:您还可以按如下方式明确时钟速度:

i2c_config_t conf = {
        .mode = I2C_MODE_MASTER,
        .sda_io_num = I2C_MASTER_SDA_IO,
        .sda_pullup_en = GPIO_PULLUP_ENABLE,
        .scl_io_num = I2C_MASTER_SCL_IO,
        .scl_pullup_en = GPIO_PULLUP_ENABLE,
        {{.clk_speed = I2C_MASTER_FREQ_HZ}}
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM