#include "esp_check.h" #include "esp_log.h" #include "i2c_bus.h" static const char *TAG = "i2c_bus"; /* * CrowPanel 7" Advance pins. The Basic model uses 19/20 instead -- and on the * ESP32-S3 those are the native USB D-/D+ pins, which is a separate trap. */ #define I2C_BUS_SDA_GPIO 15 #define I2C_BUS_SCL_GPIO 16 #define I2C_BUS_PORT I2C_NUM_0 static i2c_master_bus_handle_t bus = NULL; esp_err_t i2c_bus_init(void) { if (bus != NULL) { return ESP_OK; } const i2c_master_bus_config_t cfg = { .i2c_port = I2C_BUS_PORT, .sda_io_num = I2C_BUS_SDA_GPIO, .scl_io_num = I2C_BUS_SCL_GPIO, .clk_source = I2C_CLK_SRC_DEFAULT, .glitch_ignore_cnt = 7, .flags.enable_internal_pullup = true, }; ESP_RETURN_ON_ERROR(i2c_new_master_bus(&cfg, &bus), TAG, "i2c bus init failed"); ESP_LOGI(TAG, "I2C bus up on SDA=%d SCL=%d", I2C_BUS_SDA_GPIO, I2C_BUS_SCL_GPIO); return ESP_OK; } i2c_master_bus_handle_t i2c_bus_get(void) { return bus; } void i2c_bus_scan(const char *tag) { if (bus == NULL) { ESP_LOGW(tag, "I2C bus not initialized"); return; } int n = 0; for (uint8_t addr = 0x03; addr < 0x78; addr++) { if (i2c_master_probe(bus, addr, 20) == ESP_OK) { const char *what = "?"; if (addr == 0x30) what = "STC8H1K28 board controller"; else if (addr == 0x5D || addr == 0x14) what = "GT911 touch"; ESP_LOGI(tag, " I2C 0x%02X %s", addr, what); n++; } } ESP_LOGI(tag, " %d device(s) on the bus", n); }