【问题标题】:access uino variable when union name is not defined未定义联合名称时访问 uino 变量
【发布时间】:2021-10-01 18:54:30
【问题描述】:

我正在使用 esp32 并尝试使用 i2c 库。

有一个配置结构。

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;

当我尝试在我的代码中创建和分配 i2c_config_t 时,如下所示:

                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};

我在 .在主变量之前。

错误提示

'.' 之前的预期主表达式令牌

从这里我看到工会没有定义名称,这可能是问题吗?

【问题讨论】:

  • 你用什么来编译这段代码?使用 gcc 11.1.1 编译时似乎没有任何错误。
  • "cStandard": "c11","cppStandard": "c++17",但它是来自 esp-idf 框架的 xtensa-esp32-elf 编译器。它是 esp32 项目
  • 尝试在 i2c_config_t 中为 union 命名,例如 options 并使用 .options.master.clk_speed = 100000 进行初始化,但你真的需要联合吗?您可以在i2c_config_t 内将master 和slave 设置为structs,而不会占用大量空间。
  • @infixed,该结构是库 API 的一部分
  • 那个错误在哪里?哪个点需要主表达式?

标签: c struct esp32 i2c arduino-esp32


【解决方案1】:
.master = {
  .clk_speed = 100000,
}

【讨论】:

    猜你喜欢
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多