Browse Source

I2C: Fix issue that pointer would be NULL if calls i2c_param first,
Closes https://github.com/espressif/esp-idf/issues/10163

Cao Sen Miao 3 years ago
parent
commit
467356cfcd
2 changed files with 5 additions and 2 deletions
  1. 1 0
      components/driver/i2c.c
  2. 4 2
      components/hal/i2c_hal.c

+ 1 - 0
components/driver/i2c.c

@@ -756,6 +756,7 @@ esp_err_t i2c_param_config(i2c_port_t i2c_num, const i2c_config_t *i2c_conf)
         return ret;
     }
     i2c_hw_enable(i2c_num);
+    i2c_hal_init(&i2c_context[i2c_num].hal, i2c_num);
     I2C_ENTER_CRITICAL(&(i2c_context[i2c_num].spinlock));
     i2c_ll_disable_intr_mask(i2c_context[i2c_num].hal.dev, I2C_LL_INTR_MASK);
     i2c_ll_clear_intr_mask(i2c_context[i2c_num].hal.dev, I2C_LL_INTR_MASK);

+ 4 - 2
components/hal/i2c_hal.c

@@ -51,8 +51,10 @@ void i2c_hal_master_init(i2c_hal_context_t *hal)
 
 void i2c_hal_init(i2c_hal_context_t *hal, int i2c_port)
 {
-    hal->dev = I2C_LL_GET_HW(i2c_port);
-    i2c_ll_enable_controller_clock(hal->dev, true);
+    if (hal->dev == NULL) {
+        hal->dev = I2C_LL_GET_HW(i2c_port);
+        i2c_ll_enable_controller_clock(hal->dev, true);
+    }
 }
 
 void i2c_hal_deinit(i2c_hal_context_t *hal)