diff --git a/components/driver/i2c.c b/components/driver/i2c.c index de12e675d8..cdcf713058 100644 --- a/components/driver/i2c.c +++ b/components/driver/i2c.c @@ -189,10 +189,10 @@ static i2c_clk_alloc_t i2c_clk_alloc[I2C_SCLK_MAX] = { {0, I2C_CLK_LIMIT_XTAL}, /*!< I2C XTAL characteristic*/ #endif #if SOC_I2C_SUPPORT_RTC - {I2C_SCLK_SRC_FLAG_LIGHT_SLEEP, I2C_CLK_LIMIT_RTC}, /*!< I2C 20M RTC characteristic*/ + {I2C_SCLK_SRC_FLAG_LIGHT_SLEEP | I2C_SCLK_SRC_FLAG_AWARE_DFS, I2C_CLK_LIMIT_RTC}, /*!< I2C 20M RTC characteristic*/ #endif #if SOC_I2C_SUPPORT_REF_TICK - {I2C_SCLK_SRC_FLAG_AWARE_DFS | I2C_SCLK_SRC_FLAG_LIGHT_SLEEP, I2C_CLK_LIMIT_REF_TICK}, /*!< I2C REF_TICK characteristic*/ + {I2C_SCLK_SRC_FLAG_AWARE_DFS, I2C_CLK_LIMIT_REF_TICK}, /*!< I2C REF_TICK characteristic*/ #endif }; @@ -628,7 +628,7 @@ static i2c_sclk_t i2c_get_clk_src(const i2c_config_t *i2c_conf) continue; } #endif - if (((i2c_conf->clk_flags | i2c_clk_alloc[clk].character) == i2c_clk_alloc[clk].character) && (i2c_conf->master.clk_speed <= i2c_clk_alloc[clk].clk_freq)) { + if (((i2c_conf->clk_flags & i2c_clk_alloc[clk].character) == i2c_conf->clk_flags) && (i2c_conf->master.clk_speed <= i2c_clk_alloc[clk].clk_freq)) { return clk; } } @@ -640,6 +640,9 @@ esp_err_t i2c_param_config(i2c_port_t i2c_num, const i2c_config_t *i2c_conf) I2C_CHECK(i2c_num < I2C_NUM_MAX, I2C_NUM_ERROR_STR, ESP_ERR_INVALID_ARG); I2C_CHECK(i2c_conf != NULL, I2C_ADDR_ERROR_STR, ESP_ERR_INVALID_ARG); I2C_CHECK(i2c_conf->mode < I2C_MODE_MAX, I2C_MODE_ERR_STR, ESP_ERR_INVALID_ARG); + if (i2c_conf->mode == I2C_MODE_MASTER) { + I2C_CHECK(i2c_get_clk_src(i2c_conf) != I2C_SCLK_MAX, I2C_CLK_FLAG_ERR_STR, ESP_ERR_INVALID_ARG); + } esp_err_t ret = i2c_set_pin(i2c_num, i2c_conf->sda_io_num, i2c_conf->scl_io_num, i2c_conf->sda_pullup_en, i2c_conf->scl_pullup_en, i2c_conf->mode); @@ -664,7 +667,6 @@ esp_err_t i2c_param_config(i2c_port_t i2c_num, const i2c_config_t *i2c_conf) i2c_hal_master_init(&(i2c_context[i2c_num].hal), i2c_num); //Default, we enable hardware filter i2c_hal_set_filter(&(i2c_context[i2c_num].hal), I2C_FILTER_CYC_NUM_DEF); - I2C_CHECK(i2c_get_clk_src(i2c_conf) != I2C_SCLK_MAX, I2C_CLK_FLAG_ERR_STR, ESP_ERR_INVALID_ARG); i2c_hal_set_bus_timing(&(i2c_context[i2c_num].hal), i2c_conf->master.clk_speed, i2c_get_clk_src(i2c_conf)); } I2C_EXIT_CRITICAL(&(i2c_context[i2c_num].spinlock)); diff --git a/examples/peripherals/i2c/i2c_self_test/main/i2c_example_main.c b/examples/peripherals/i2c/i2c_self_test/main/i2c_example_main.c index ad70cbcba0..7b7928c1ad 100644 --- a/examples/peripherals/i2c/i2c_self_test/main/i2c_example_main.c +++ b/examples/peripherals/i2c/i2c_self_test/main/i2c_example_main.c @@ -144,14 +144,19 @@ static esp_err_t i2c_master_sensor_test(i2c_port_t i2c_num, uint8_t *data_h, uin static esp_err_t i2c_master_init(void) { int i2c_master_port = I2C_MASTER_NUM; - i2c_config_t conf; - conf.mode = I2C_MODE_MASTER; - conf.sda_io_num = I2C_MASTER_SDA_IO; - conf.sda_pullup_en = GPIO_PULLUP_ENABLE; - conf.scl_io_num = I2C_MASTER_SCL_IO; - conf.scl_pullup_en = GPIO_PULLUP_ENABLE; - conf.master.clk_speed = I2C_MASTER_FREQ_HZ; - i2c_param_config(i2c_master_port, &conf); + 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, + // .clk_flags = 0, /*!< Optional, you can use I2C_SCLK_SRC_FLAG_* flags to choose i2c source clock here. */ + }; + esp_err_t err = i2c_param_config(i2c_master_port, &conf); + if (err != ESP_OK) { + return err; + } return i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0); } @@ -161,15 +166,19 @@ static esp_err_t i2c_master_init(void) static esp_err_t i2c_slave_init(void) { int i2c_slave_port = I2C_SLAVE_NUM; - i2c_config_t conf_slave; - conf_slave.sda_io_num = I2C_SLAVE_SDA_IO; - conf_slave.sda_pullup_en = GPIO_PULLUP_ENABLE; - conf_slave.scl_io_num = I2C_SLAVE_SCL_IO; - conf_slave.scl_pullup_en = GPIO_PULLUP_ENABLE; - conf_slave.mode = I2C_MODE_SLAVE; - conf_slave.slave.addr_10bit_en = 0; - conf_slave.slave.slave_addr = ESP_SLAVE_ADDR; - i2c_param_config(i2c_slave_port, &conf_slave); + i2c_config_t conf_slave = { + .sda_io_num = I2C_SLAVE_SDA_IO, + .sda_pullup_en = GPIO_PULLUP_ENABLE, + .scl_io_num = I2C_SLAVE_SCL_IO, + .scl_pullup_en = GPIO_PULLUP_ENABLE, + .mode = I2C_MODE_SLAVE, + .slave.addr_10bit_en = 0, + .slave.slave_addr = ESP_SLAVE_ADDR, + }; + esp_err_t err = i2c_param_config(i2c_slave_port, &conf_slave); + if (err != ESP_OK) { + return err; + } return i2c_driver_install(i2c_slave_port, conf_slave.mode, I2C_SLAVE_RX_BUF_LEN, I2C_SLAVE_TX_BUF_LEN, 0); } diff --git a/examples/peripherals/i2c/i2c_tools/main/cmd_i2ctools.c b/examples/peripherals/i2c/i2c_tools/main/cmd_i2ctools.c index 4adb0b9ea2..5a2138a692 100644 --- a/examples/peripherals/i2c/i2c_tools/main/cmd_i2ctools.c +++ b/examples/peripherals/i2c/i2c_tools/main/cmd_i2ctools.c @@ -56,7 +56,8 @@ static esp_err_t i2c_master_driver_initialize(void) .sda_pullup_en = GPIO_PULLUP_ENABLE, .scl_io_num = i2c_gpio_scl, .scl_pullup_en = GPIO_PULLUP_ENABLE, - .master.clk_speed = i2c_frequency + .master.clk_speed = i2c_frequency, + // .clk_flags = 0, /*!< Optional, you can use I2C_SCLK_SRC_FLAG_* flags to choose i2c source clock here. */ }; return i2c_param_config(i2c_port, &conf); }