Skip to content

Commit

Permalink
system: initialize I2C in system
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stintel authored and kristiankielhofner committed Oct 28, 2023
1 parent 20780c4 commit 37f99d6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
19 changes: 19 additions & 0 deletions main/system.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "board.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_lvgl_port.h"
Expand All @@ -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)
Expand Down Expand Up @@ -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());
}

Expand Down
2 changes: 2 additions & 0 deletions main/system.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "esp_peripherals.h"
#include "i2c_bus.h"

enum willow_hw_t {
WILLOW_HW_UNSUPPORTED = 0,
Expand All @@ -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;

Expand Down

0 comments on commit 37f99d6

Please sign in to comment.