diff --git a/main/DS4432U.c b/main/DS4432U.c index e790992..a22b679 100644 --- a/main/DS4432U.c +++ b/main/DS4432U.c @@ -20,7 +20,7 @@ static i2c_master_dev_handle_t ds4432u_dev_handle; * @return esp_err_t ESP_OK on success, or an error code on failure. */ esp_err_t DS4432U_init(void) { - return i2c_bitaxe_add_device(DS4432U_SENSOR_ADDR, &ds4432u_dev_handle); + return i2c_bitaxe_add_device(DS4432U_SENSOR_ADDR, &ds4432u_dev_handle, TAG); } /** diff --git a/main/EMC2101.c b/main/EMC2101.c index 633962a..3475349 100644 --- a/main/EMC2101.c +++ b/main/EMC2101.c @@ -6,8 +6,6 @@ static const char * TAG = "EMC2101"; -// static const char *TAG = "EMC2101.c"; - static i2c_master_dev_handle_t emc2101_dev_handle; /** @@ -17,7 +15,7 @@ static i2c_master_dev_handle_t emc2101_dev_handle; */ esp_err_t EMC2101_init(bool invertPolarity) { - if (i2c_bitaxe_add_device(EMC2101_I2CADDR_DEFAULT, &emc2101_dev_handle) != ESP_OK) { + if (i2c_bitaxe_add_device(EMC2101_I2CADDR_DEFAULT, &emc2101_dev_handle, TAG) != ESP_OK) { ESP_LOGE(TAG, "Failed to add device"); return ESP_FAIL; } diff --git a/main/INA260.c b/main/INA260.c index 6abd77d..c129619 100644 --- a/main/INA260.c +++ b/main/INA260.c @@ -4,7 +4,8 @@ #include "i2c_bitaxe.h" #include "INA260.h" -// static const char *TAG = "INA260.c"; +static const char *TAG = "INA260"; + static i2c_master_dev_handle_t ina260_dev_handle; /** @@ -13,7 +14,7 @@ static i2c_master_dev_handle_t ina260_dev_handle; * @return esp_err_t ESP_OK on success, or an error code on failure. */ esp_err_t INA260_init(void) { - return i2c_bitaxe_add_device(INA260_I2CADDR_DEFAULT, &ina260_dev_handle); + return i2c_bitaxe_add_device(INA260_I2CADDR_DEFAULT, &ina260_dev_handle, TAG); } diff --git a/main/TMP1075.c b/main/TMP1075.c index 0efba1b..af82e10 100644 --- a/main/TMP1075.c +++ b/main/TMP1075.c @@ -4,7 +4,7 @@ #include "TMP1075.h" -// static const char *TAG = "TMP1075.c"; +static const char *TAG = "TMP1075"; static i2c_master_dev_handle_t tmp1075_dev_handle; @@ -14,7 +14,7 @@ static i2c_master_dev_handle_t tmp1075_dev_handle; * @return esp_err_t ESP_OK on success, or an error code on failure. */ esp_err_t TMP1075_init(void) { - return i2c_bitaxe_add_device(TMP1075_I2CADDR_DEFAULT, &tmp1075_dev_handle); + return i2c_bitaxe_add_device(TMP1075_I2CADDR_DEFAULT, &tmp1075_dev_handle, TAG); } bool TMP1075_installed(int device_index) diff --git a/main/TPS546.c b/main/TPS546.c index 5967cd2..b8fc541 100644 --- a/main/TPS546.c +++ b/main/TPS546.c @@ -22,7 +22,7 @@ #define SMBUS_DEFAULT_TIMEOUT (1000 / portTICK_PERIOD_MS) -static const char *TAG = "TPS546.c"; +static const char *TAG = "TPS546"; static uint8_t DEVICE_ID1[] = {0x54, 0x49, 0x54, 0x6B, 0x24, 0x41}; // TPS546D24A static uint8_t DEVICE_ID2[] = {0x54, 0x49, 0x54, 0x6D, 0x24, 0x41}; // TPS546D24A @@ -330,7 +330,7 @@ int TPS546_init(void) ESP_LOGI(TAG, "Initializing the core voltage regulator"); - if (i2c_bitaxe_add_device(TPS546_I2CADDR, &tps546_dev_handle) != ESP_OK) { + if (i2c_bitaxe_add_device(TPS546_I2CADDR, &tps546_dev_handle, TAG) != ESP_OK) { ESP_LOGE(TAG, "Failed to add I2C device"); return -1; } diff --git a/main/i2c_bitaxe.c b/main/i2c_bitaxe.c index e72a186..59c871f 100644 --- a/main/i2c_bitaxe.c +++ b/main/i2c_bitaxe.c @@ -1,6 +1,10 @@ +#include #include "esp_event.h" #include "esp_log.h" +#include "esp_err.h" +#include "esp_check.h" #include "i2c_bitaxe.h" +#include "driver/i2c_master.h" #define GPIO_I2C_SDA CONFIG_GPIO_I2C_SDA #define GPIO_I2C_SCL CONFIG_GPIO_I2C_SCL @@ -15,6 +19,34 @@ static i2c_master_bus_handle_t i2c_bus_handle; +static const char * TAG = "i2c_bitaxe"; + +typedef struct { + i2c_master_dev_handle_t handle; + uint16_t device_address; + char device_tag[32]; +} i2c_dev_map_entry_t; + +#define MAX_DEVICES 10 // Adjust as needed +static i2c_dev_map_entry_t i2c_device_map[MAX_DEVICES]; +static int i2c_device_count = 0; + +static esp_err_t log_on_error(esp_err_t err, i2c_master_dev_handle_t handle) { + if (err == ESP_OK) { + return ESP_OK; + } + + for (int i = 0; i < i2c_device_count; i++) { + if (i2c_device_map[i].handle == handle) { + ESP_LOGE(TAG, "Device %s (0x%02x)", i2c_device_map[i].device_tag, i2c_device_map[i].device_address); + return err; + } + } + + ESP_LOGE(TAG, "Unknown device"); + return err; +} + /** * @brief i2c master initialization */ @@ -37,15 +69,28 @@ esp_err_t i2c_bitaxe_init(void) * @param device_address The I2C device address * @param dev_handle The I2C device handle */ -esp_err_t i2c_bitaxe_add_device(uint8_t device_address, i2c_master_dev_handle_t * dev_handle) +esp_err_t i2c_bitaxe_add_device(uint8_t device_address, i2c_master_dev_handle_t * dev_handle, const char *device_tag) { + if (i2c_device_count >= MAX_DEVICES) { + ESP_LOGE(TAG, "Device map full, cannot add more devices"); + return ESP_FAIL; + } + i2c_device_config_t dev_cfg = { .dev_addr_length = I2C_ADDR_BIT_LEN_7, .device_address = device_address, .scl_speed_hz = I2C_BUS_SPEED_HZ, }; - return i2c_master_bus_add_device(i2c_bus_handle, &dev_cfg, dev_handle); + ESP_RETURN_ON_ERROR(i2c_master_bus_add_device(i2c_bus_handle, &dev_cfg, dev_handle), TAG, "Device 0x%02x", device_address); + + i2c_device_map[i2c_device_count].handle = *dev_handle; + i2c_device_map[i2c_device_count].device_address = device_address; + strncpy(i2c_device_map[i2c_device_count].device_tag, device_tag, sizeof(i2c_device_map[i2c_device_count].device_tag) - 1); + i2c_device_map[i2c_device_count].device_tag[sizeof(i2c_device_map[i2c_device_count].device_tag) - 1] = '\0'; + i2c_device_count++; + + return ESP_OK; } esp_err_t i2c_bitaxe_get_master_bus_handle(i2c_master_bus_handle_t * dev_handle) @@ -66,7 +111,7 @@ esp_err_t i2c_bitaxe_register_read(i2c_master_dev_handle_t dev_handle, uint8_t r // return i2c_master_write_read_device(I2C_MASTER_NUM, device_address, ®_addr, 1, data, len, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS); //ESP_LOGI("I2C", "Reading %d bytes from register 0x%02X", len, reg_addr); - return i2c_master_transmit_receive(dev_handle, ®_addr, 1, read_buf, len, I2C_DEFAULT_TIMEOUT); + return log_on_error(i2c_master_transmit_receive(dev_handle, ®_addr, 1, read_buf, len, I2C_DEFAULT_TIMEOUT), dev_handle); } /** @@ -81,7 +126,7 @@ esp_err_t i2c_bitaxe_register_write_byte(i2c_master_dev_handle_t dev_handle, uin //return i2c_master_write_to_device(I2C_MASTER_NUM, device_address, write_buf, sizeof(write_buf), I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS); - return i2c_master_transmit(dev_handle, write_buf, 2, I2C_DEFAULT_TIMEOUT); + return log_on_error(i2c_master_transmit(dev_handle, write_buf, 2, I2C_DEFAULT_TIMEOUT), dev_handle); } /** @@ -92,7 +137,7 @@ esp_err_t i2c_bitaxe_register_write_byte(i2c_master_dev_handle_t dev_handle, uin */ esp_err_t i2c_bitaxe_register_write_bytes(i2c_master_dev_handle_t dev_handle, uint8_t * data, uint8_t len) { - return i2c_master_transmit(dev_handle, data, len, I2C_DEFAULT_TIMEOUT); + return log_on_error(i2c_master_transmit(dev_handle, data, len, I2C_DEFAULT_TIMEOUT), dev_handle); } /** @@ -107,5 +152,5 @@ esp_err_t i2c_bitaxe_register_write_word(i2c_master_dev_handle_t dev_handle, uin //return i2c_master_write_to_device(I2C_MASTER_NUM, device_address, write_buf, sizeof(write_buf), I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS); - return i2c_master_transmit(dev_handle, write_buf, 3, I2C_DEFAULT_TIMEOUT); + return log_on_error(i2c_master_transmit(dev_handle, write_buf, 3, I2C_DEFAULT_TIMEOUT), dev_handle); } diff --git a/main/i2c_bitaxe.h b/main/i2c_bitaxe.h index 9d495c8..1297753 100644 --- a/main/i2c_bitaxe.h +++ b/main/i2c_bitaxe.h @@ -6,7 +6,7 @@ #define I2C_BUS_SPEED_HZ 100000 /*!< I2C master clock frequency */ esp_err_t i2c_bitaxe_init(void); -esp_err_t i2c_bitaxe_add_device(uint8_t device_address, i2c_master_dev_handle_t * dev_handle); +esp_err_t i2c_bitaxe_add_device(uint8_t device_address, i2c_master_dev_handle_t * dev_handle, const char *device_tag); esp_err_t i2c_bitaxe_get_master_bus_handle(i2c_master_bus_handle_t * dev_handle); esp_err_t i2c_bitaxe_register_read(i2c_master_dev_handle_t dev_handle, uint8_t reg_addr, uint8_t * read_buf, size_t len);