From d01074bf3c125449d87c508ea1a5bfa27c03c9d9 Mon Sep 17 00:00:00 2001 From: "Deomid \"rojer\" Ryabkov" Date: Mon, 28 Dec 2020 21:24:10 +0000 Subject: [PATCH] ESP32: Fix I2C module init https://github.com/cesanta/mongoose-os/issues/565 --- .clang-format | 1 + src/esp32/esp32_i2c_master.c | 15 +++++---------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/.clang-format b/.clang-format index efc3abb..24fab55 100644 --- a/.clang-format +++ b/.clang-format @@ -3,3 +3,4 @@ AllowShortFunctionsOnASingleLine: false SpaceAfterCStyleCast: true PointerBindsToType: false DerivePointerBinding: false +IncludeBlocks: Preserve diff --git a/src/esp32/esp32_i2c_master.c b/src/esp32/esp32_i2c_master.c index 66d84ed..c1b13de 100644 --- a/src/esp32/esp32_i2c_master.c +++ b/src/esp32/esp32_i2c_master.c @@ -19,20 +19,15 @@ #include #include +#include "mgos.h" #include "mgos_i2c.h" #include "driver/gpio.h" #include "driver/periph_ctrl.h" -#include "esp_attr.h" #include "soc/gpio_sig_map.h" #include "soc/i2c_reg.h" #include "soc/i2c_struct.h" -#include "mgos_sys_config.h" -#include "mgos_system.h" - -#include "common/cs_dbg.h" - #define I2C_COMMAND_OP_RSTART (0 << 11) #define I2C_COMMAND_OP_WRITE (1 << 11) #define I2C_COMMAND_OP_READ (2 << 11) @@ -128,10 +123,7 @@ static bool esp32_i2c_reset(struct mgos_i2c *c, int new_freq) { gpio_matrix_out(c->cfg.sda_gpio, SIG_GPIO_OUT_IDX, false /* out_inv */, false /* oen_inv */); - periph_module_disable(c->pm); - mgos_msleep(1); - periph_module_enable(c->pm); - mgos_msleep(1); + periph_module_reset(c->pm); dev->ctr.val = 0; /* MSB first for RX and TX */ @@ -374,6 +366,8 @@ struct mgos_i2c *mgos_i2c_create(const struct mgos_config_i2c *cfg) { } c->debug = cfg->debug; + periph_module_enable(c->pm); + if (!esp32_i2c_reset(c, cfg->freq)) goto out_err; esp_err_t r = 0; @@ -410,6 +404,7 @@ struct mgos_i2c *mgos_i2c_create(const struct mgos_config_i2c *cfg) { } void mgos_i2c_close(struct mgos_i2c *c) { + periph_module_disable(c->pm); mgos_i2c_stop(c); free(c); }