From c686b3d76ecc6a6fa88167ee13f8d6f092e4d163 Mon Sep 17 00:00:00 2001 From: mutatrum Date: Wed, 4 Dec 2024 22:57:08 +0100 Subject: [PATCH] Improve i2c error logging --- main/DS4432U.c | 2 +- main/EMC2101.c | 4 +--- main/INA260.c | 5 +++-- main/TMP1075.c | 4 ++-- main/TPS546.c | 4 ++-- main/i2c_bitaxe.c | 57 ++++++++++++++++++++++++++++++++++++++++++----- main/i2c_bitaxe.h | 2 +- main/oled.c | 4 ++-- 8 files changed, 63 insertions(+), 19 deletions(-) diff --git a/main/DS4432U.c b/main/DS4432U.c index e79099258..a22b679c5 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 633962a30..34753491d 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 6abd77d1d..c1296196c 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 0efba1b6e..af82e1093 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 5967cd225..b8fc541a3 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 494c48bf4..562209ea8 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 I2C_MASTER_SCL_IO 48 /*!< GPIO number used for I2C master clock */ #define I2C_MASTER_SDA_IO 47 /*!< GPIO number used for I2C master data */ @@ -14,6 +18,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 */ @@ -36,15 +68,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_MASTER_FREQ_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; } /** @@ -59,7 +104,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); } /** @@ -74,7 +119,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); } /** @@ -85,7 +130,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); } /** @@ -100,5 +145,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 67521bca6..7f3211e13 100644 --- a/main/i2c_bitaxe.h +++ b/main/i2c_bitaxe.h @@ -5,7 +5,7 @@ //#include "driver/i2c.h" 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_register_read(i2c_master_dev_handle_t dev_handle, uint8_t reg_addr, uint8_t * read_buf, size_t len); diff --git a/main/oled.c b/main/oled.c index 41277ff31..17f4240d7 100644 --- a/main/oled.c +++ b/main/oled.c @@ -28,7 +28,7 @@ #define OLED_I2C_ADDR 0x3C -static const char * TAG = "oled"; +static const char * TAG = "SSD1306"; extern unsigned char ucSmallFont[]; static int iScreenOffset; // current write offset of screen data @@ -47,7 +47,7 @@ esp_err_t OLED_init(void) { //init the I2C device - ESP_RETURN_ON_ERROR(i2c_bitaxe_add_device(OLED_I2C_ADDR, &ssd1306_dev_handle), TAG, "Failed to add display i2c device"); + ESP_RETURN_ON_ERROR(i2c_bitaxe_add_device(OLED_I2C_ADDR, &ssd1306_dev_handle, TAG), TAG, "Failed to add display i2c device"); uint8_t oled32_initbuf[] = {0x00, 0xae, // cmd: display off