【发布时间】: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