From 37f99d6c12a824967af450126e9d999e5d69fd3b Mon Sep 17 00:00:00 2001 From: Stijn Tintel Date: Sat, 28 Oct 2023 20:51:15 +0300 Subject: [PATCH] system: initialize I2C in system To detect the touch type controller in the ESP32-S3-Box-3, we need a handle to the I2C bus. ESP-ADF initializes this handle in audio_board_init, but does not expose it. Therefore, initialize I2C in system, and make the handle a global variable. This will trigger a warning when ESP-ADF tries to initialize it again during board init, but this is harmless. --- main/system.c | 19 +++++++++++++++++++ main/system.h | 2 ++ 2 files changed, 21 insertions(+) diff --git a/main/system.c b/main/system.c index acffe0d9..3c34c455 100644 --- a/main/system.c +++ b/main/system.c @@ -1,3 +1,4 @@ +#include "board.h" #include "esp_event.h" #include "esp_log.h" #include "esp_lvgl_port.h" @@ -19,6 +20,7 @@ static const char *willow_hw_t[WILLOW_HW_MAX] = { [WILLOW_HW_ESP32_S3_BOX_3] = "ESP32-S3-BOX-3", }; +i2c_bus_handle_t hdl_i2c_bus; volatile bool restarting = false; const char *str_hw_type(int id) @@ -52,9 +54,26 @@ static esp_err_t init_ev_loop() return ret; } +static void init_i2c(void) +{ + int ret = ESP_OK; + i2c_config_t i2c_cfg = { + .mode = I2C_MODE_MASTER, + .sda_pullup_en = GPIO_PULLUP_ENABLE, + .scl_pullup_en = GPIO_PULLUP_ENABLE, + .master.clk_speed = 100000, + }; + ret = get_i2c_pins(I2C_NUM_0, &i2c_cfg); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "failed to get I2C pins"); + } + hdl_i2c_bus = i2c_bus_create(I2C_NUM_0, &i2c_cfg); +} + void init_system(void) { set_hw_type(); + init_i2c(); ESP_ERROR_CHECK(init_ev_loop()); } diff --git a/main/system.h b/main/system.h index 93cf6287..00934cfb 100644 --- a/main/system.h +++ b/main/system.h @@ -1,4 +1,5 @@ #include "esp_peripherals.h" +#include "i2c_bus.h" enum willow_hw_t { WILLOW_HW_UNSUPPORTED = 0, @@ -18,6 +19,7 @@ enum willow_state { extern enum willow_hw_t hw_type; extern enum willow_state state; extern esp_periph_set_handle_t hdl_pset; +extern i2c_bus_handle_t hdl_i2c_bus; extern volatile bool restarting;