Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve i2c error logging #552

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion main/DS4432U.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
4 changes: 1 addition & 3 deletions main/EMC2101.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

static const char * TAG = "EMC2101";

// static const char *TAG = "EMC2101.c";

static i2c_master_dev_handle_t emc2101_dev_handle;

/**
Expand All @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions main/INA260.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
}


Expand Down
4 changes: 2 additions & 2 deletions main/TMP1075.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions main/TPS546.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
57 changes: 51 additions & 6 deletions main/i2c_bitaxe.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include <string.h>
#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 */
Expand All @@ -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
*/
Expand All @@ -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;
}

/**
Expand All @@ -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, &reg_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, &reg_addr, 1, read_buf, len, I2C_DEFAULT_TIMEOUT);
return log_on_error(i2c_master_transmit_receive(dev_handle, &reg_addr, 1, read_buf, len, I2C_DEFAULT_TIMEOUT), dev_handle);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}
2 changes: 1 addition & 1 deletion main/i2c_bitaxe.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions main/oled.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading