diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index cf711421..a707acf9 100755 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -11,7 +11,6 @@ SRCS "nvs_config.c" "oled.c" "system.c" - "TMP1075.c" "TPS546.c" "vcore.c" "work_queue.c" diff --git a/main/DS4432U.c b/main/DS4432U.c index d931072a..c1da0511 100644 --- a/main/DS4432U.c +++ b/main/DS4432U.c @@ -3,7 +3,6 @@ #include "esp_log.h" #include "i2c_bitaxe.h" - #include "DS4432U.h" // DS4432U+ -- Adjustable current DAC @@ -13,6 +12,17 @@ static const char *TAG = "DS4432U"; +static i2c_master_dev_handle_t ds4432u_dev_handle; + +/** + * @brief Initialize the DS4432U+ sensor. + * + * @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); +} + /** * @brief Set the current DAC code for a specific DS4432U output. * @@ -22,7 +32,7 @@ static const char *TAG = "DS4432U"; */ esp_err_t DS4432U_set_current_code(uint8_t output, uint8_t code) { uint8_t reg = (output == 0) ? DS4432U_OUT0_REG : DS4432U_OUT1_REG; - return i2c_master_register_write_byte(DS4432U_SENSOR_ADDR, reg, code); + return i2c_bitaxe_register_write_byte(ds4432u_dev_handle, reg, code); } /** @@ -34,7 +44,7 @@ esp_err_t DS4432U_set_current_code(uint8_t output, uint8_t code) { */ esp_err_t DS4432U_get_current_code(uint8_t output, uint8_t *code) { uint8_t reg = (output == 0) ? DS4432U_OUT0_REG : DS4432U_OUT1_REG; - return i2c_master_register_read(DS4432U_SENSOR_ADDR, reg, code, 1); + return i2c_bitaxe_register_read(ds4432u_dev_handle, reg, code, 1); } bool DS4432U_test(void) @@ -42,7 +52,7 @@ bool DS4432U_test(void) uint8_t data; /* Read the DS4432U+ WHO_AM_I register, on power up the register should have the value 0x00 */ - esp_err_t register_result = i2c_master_register_read(DS4432U_SENSOR_ADDR, DS4432U_OUT0_REG, &data, 1); + esp_err_t register_result = i2c_bitaxe_register_read(ds4432u_dev_handle, DS4432U_OUT0_REG, &data, 1); ESP_LOGI(TAG, "DS4432U+ OUT0 = 0x%02X", data); return register_result == ESP_OK; } diff --git a/main/DS4432U.h b/main/DS4432U.h index 2075d877..651348d4 100644 --- a/main/DS4432U.h +++ b/main/DS4432U.h @@ -7,6 +7,7 @@ #define DS4432_VRFS 0.997 bool DS4432U_test(void); +esp_err_t DS4432U_init(void); esp_err_t DS4432U_set_current_code(uint8_t output, uint8_t code); esp_err_t DS4432U_get_current_code(uint8_t output, uint8_t *code); diff --git a/main/EMC2101.c b/main/EMC2101.c index fa4a45bc..daf1237e 100644 --- a/main/EMC2101.c +++ b/main/EMC2101.c @@ -1,22 +1,36 @@ -#include "esp_log.h" #include +#include "esp_log.h" +#include "i2c_bitaxe.h" #include "EMC2101.h" static const char * TAG = "EMC2101"; // static const char *TAG = "EMC2101.c"; -// run this first. sets up the config register -void EMC2101_init(bool invertPolarity) -{ +static i2c_master_dev_handle_t emc2101_dev_handle; + +/** + * @brief Initialize the EMC2101 sensor. + * + * @return esp_err_t ESP_OK on success, or an error code on failure. + */ +esp_err_t EMC2101_init(bool invertPolarity) { + + if (i2c_bitaxe_add_device(EMC2101_I2CADDR_DEFAULT, &emc2101_dev_handle) != ESP_OK) { + ESP_LOGE(TAG, "Failed to add device"); + return ESP_FAIL; + } // set the TACH input - ESP_ERROR_CHECK(i2c_master_register_write_byte(EMC2101_I2CADDR_DEFAULT, EMC2101_REG_CONFIG, 0x04)); + ESP_ERROR_CHECK(i2c_bitaxe_register_write_byte(emc2101_dev_handle, EMC2101_REG_CONFIG, 0x04)); if (invertPolarity) { - ESP_ERROR_CHECK(i2c_master_register_write_byte(EMC2101_I2CADDR_DEFAULT, EMC2101_FAN_CONFIG, 0b00100011)); + ESP_ERROR_CHECK(i2c_bitaxe_register_write_byte(emc2101_dev_handle, EMC2101_FAN_CONFIG, 0b00100011)); } + + return ESP_OK; + } // takes a fan speed percent @@ -25,7 +39,7 @@ void EMC2101_set_fan_speed(float percent) uint8_t speed; speed = (uint8_t) (63.0 * percent); - ESP_ERROR_CHECK(i2c_master_register_write_byte(EMC2101_I2CADDR_DEFAULT, EMC2101_REG_FAN_SETTING, speed)); + ESP_ERROR_CHECK(i2c_bitaxe_register_write_byte(emc2101_dev_handle, EMC2101_REG_FAN_SETTING, speed)); } // RPM = 5400000/reading @@ -35,8 +49,8 @@ uint16_t EMC2101_get_fan_speed(void) uint16_t reading; uint16_t RPM; - ESP_ERROR_CHECK(i2c_master_register_read(EMC2101_I2CADDR_DEFAULT, EMC2101_TACH_LSB, &tach_lsb, 1)); - ESP_ERROR_CHECK(i2c_master_register_read(EMC2101_I2CADDR_DEFAULT, EMC2101_TACH_MSB, &tach_msb, 1)); + ESP_ERROR_CHECK(i2c_bitaxe_register_read(emc2101_dev_handle, EMC2101_TACH_LSB, &tach_lsb, 1)); + ESP_ERROR_CHECK(i2c_bitaxe_register_read(emc2101_dev_handle, EMC2101_TACH_MSB, &tach_msb, 1)); // ESP_LOGI(TAG, "Raw Fan Speed = %02X %02X", tach_msb, tach_lsb); @@ -55,8 +69,8 @@ float EMC2101_get_external_temp(void) uint8_t temp_msb, temp_lsb; uint16_t reading; - ESP_ERROR_CHECK(i2c_master_register_read(EMC2101_I2CADDR_DEFAULT, EMC2101_EXTERNAL_TEMP_MSB, &temp_msb, 1)); - ESP_ERROR_CHECK(i2c_master_register_read(EMC2101_I2CADDR_DEFAULT, EMC2101_EXTERNAL_TEMP_LSB, &temp_lsb, 1)); + ESP_ERROR_CHECK(i2c_bitaxe_register_read(emc2101_dev_handle, EMC2101_EXTERNAL_TEMP_MSB, &temp_msb, 1)); + ESP_ERROR_CHECK(i2c_bitaxe_register_read(emc2101_dev_handle, EMC2101_EXTERNAL_TEMP_LSB, &temp_lsb, 1)); reading = temp_lsb | (temp_msb << 8); reading >>= 5; @@ -80,6 +94,6 @@ float EMC2101_get_external_temp(void) uint8_t EMC2101_get_internal_temp(void) { uint8_t temp; - ESP_ERROR_CHECK(i2c_master_register_read(EMC2101_I2CADDR_DEFAULT, EMC2101_INTERNAL_TEMP, &temp, 1)); + ESP_ERROR_CHECK(i2c_bitaxe_register_read(emc2101_dev_handle, EMC2101_INTERNAL_TEMP, &temp, 1)); return temp; } diff --git a/main/EMC2101.h b/main/EMC2101.h index 622aa4f7..00c82a47 100644 --- a/main/EMC2101.h +++ b/main/EMC2101.h @@ -88,7 +88,7 @@ typedef enum void EMC2101_set_fan_speed(float); // void EMC2101_read(void); uint16_t EMC2101_get_fan_speed(void); -void EMC2101_init(bool); +esp_err_t EMC2101_init(bool); float EMC2101_get_external_temp(void); uint8_t EMC2101_get_internal_temp(void); #endif /* EMC2101_H_ */ diff --git a/main/INA260.c b/main/INA260.c index d65727e9..6abd77d1 100644 --- a/main/INA260.c +++ b/main/INA260.c @@ -1,21 +1,33 @@ #include #include "esp_log.h" +#include "i2c_bitaxe.h" #include "INA260.h" // static const char *TAG = "INA260.c"; +static i2c_master_dev_handle_t ina260_dev_handle; + +/** + * @brief Initialize the INA260 sensor. + * + * @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); +} + bool INA260_installed(void) { uint8_t data[2]; - return i2c_master_register_read(INA260_I2CADDR_DEFAULT, INA260_REG_BUSVOLTAGE, data, 2) == ESP_OK; + return i2c_bitaxe_register_read(ina260_dev_handle, INA260_REG_BUSVOLTAGE, data, 2) == ESP_OK; } float INA260_read_current(void) { uint8_t data[2]; - ESP_ERROR_CHECK(i2c_master_register_read(INA260_I2CADDR_DEFAULT, INA260_REG_CURRENT, data, 2)); + ESP_ERROR_CHECK(i2c_bitaxe_register_read(ina260_dev_handle, INA260_REG_CURRENT, data, 2)); // ESP_LOGI(TAG, "Raw Current = %02X %02X", data[1], data[0]); return (uint16_t)(data[1] | (data[0] << 8)) * 1.25; @@ -25,7 +37,7 @@ float INA260_read_voltage(void) { uint8_t data[2]; - ESP_ERROR_CHECK(i2c_master_register_read(INA260_I2CADDR_DEFAULT, INA260_REG_BUSVOLTAGE, data, 2)); + ESP_ERROR_CHECK(i2c_bitaxe_register_read(ina260_dev_handle, INA260_REG_BUSVOLTAGE, data, 2)); // ESP_LOGI(TAG, "Raw Voltage = %02X %02X", data[1], data[0]); return (uint16_t)(data[1] | (data[0] << 8)) * 1.25; @@ -35,7 +47,7 @@ float INA260_read_power(void) { uint8_t data[2]; - ESP_ERROR_CHECK(i2c_master_register_read(INA260_I2CADDR_DEFAULT, INA260_REG_POWER, data, 2)); + ESP_ERROR_CHECK(i2c_bitaxe_register_read(ina260_dev_handle, INA260_REG_POWER, data, 2)); // ESP_LOGI(TAG, "Raw Power = %02X %02X", data[1], data[0]); return (data[1] | (data[0] << 8)) * 10; diff --git a/main/INA260.h b/main/INA260.h index ee14fb2c..ea524d74 100644 --- a/main/INA260.h +++ b/main/INA260.h @@ -104,6 +104,8 @@ typedef enum _alert_latch INA260_ALERT_LATCH_TRANSPARENT = 0x0, /**< Alert will reset when fault is cleared **/ } INA260_AlertLatch; + +esp_err_t INA260_init(void); bool INA260_installed(void); float INA260_read_current(void); float INA260_read_voltage(void); diff --git a/main/TMP1075.c b/main/TMP1075.c index 891e29f1..0efba1b6 100644 --- a/main/TMP1075.c +++ b/main/TMP1075.c @@ -6,8 +6,16 @@ // static const char *TAG = "TMP1075.c"; -bool TMP1075_installed(int); -uint8_t TMP1075_read_temperature(int); +static i2c_master_dev_handle_t tmp1075_dev_handle; + +/** + * @brief Initialize the TMP1075 sensor. + * + * @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); +} bool TMP1075_installed(int device_index) { @@ -16,7 +24,7 @@ bool TMP1075_installed(int device_index) // read the configuration register //ESP_LOGI(TAG, "Reading configuration register"); - ESP_ERROR_CHECK(i2c_master_register_read(TMP1075_I2CADDR_DEFAULT + device_index, TMP1075_CONFIG_REG, data, 2)); + result = i2c_bitaxe_register_read(tmp1075_dev_handle, TMP1075_CONFIG_REG, data, 2); //ESP_LOGI(TAG, "Configuration[%d] = %02X %02X", device_index, data[0], data[1]); return (result == ESP_OK?true:false); @@ -26,7 +34,7 @@ uint8_t TMP1075_read_temperature(int device_index) { uint8_t data[2]; - ESP_ERROR_CHECK(i2c_master_register_read(TMP1075_I2CADDR_DEFAULT + device_index, TMP1075_TEMP_REG, data, 2)); + ESP_ERROR_CHECK(i2c_bitaxe_register_read(tmp1075_dev_handle, TMP1075_TEMP_REG, data, 2)); //ESP_LOGI(TAG, "Raw Temperature = %02X %02X", data[0], data[1]); //ESP_LOGI(TAG, "Temperature[%d] = %d", device_index, data[0]); return data[0]; diff --git a/main/TMP1075.h b/main/TMP1075.h index ff11da74..e64c6174 100644 --- a/main/TMP1075.h +++ b/main/TMP1075.h @@ -10,5 +10,6 @@ bool TMP1075_installed(int); uint8_t TMP1075_read_temperature(int); +esp_err_t TMP1075_init(void); #endif /* TMP1075_H_ */ diff --git a/main/TPS546.c b/main/TPS546.c index 65de3fcf..21356807 100644 --- a/main/TPS546.c +++ b/main/TPS546.c @@ -1,13 +1,15 @@ -#include "driver/i2c.h" -#include "esp_log.h" #include #include #include #include - +#include "esp_log.h" #include "pmbus_commands.h" + +#include "i2c_bitaxe.h" #include "TPS546.h" +#define _DEBUG_LOG_ 1 + #define I2C_MASTER_NUM 0 /*!< I2C master i2c port number, the number of i2c peripheral interfaces available will depend on the chip */ #define WRITE_BIT I2C_MASTER_WRITE @@ -31,27 +33,31 @@ static uint8_t MFR_REVISION[] = {0x00, 0x00, 0x01}; //static uint8_t COMPENSATION_CONFIG[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; +static i2c_master_dev_handle_t tps546_dev_handle; + /** * @brief SMBus read byte */ static esp_err_t smb_read_byte(uint8_t command, uint8_t *data) { - esp_err_t err = ESP_FAIL; - - i2c_cmd_handle_t cmd = i2c_cmd_link_create(); - i2c_master_start(cmd); - i2c_master_write_byte(cmd, TPS546_I2CADDR << 1 | WRITE_BIT, ACK_CHECK); - i2c_master_write_byte(cmd, command, ACK_CHECK); - i2c_master_start(cmd); - i2c_master_write_byte(cmd, TPS546_I2CADDR << 1 | READ_BIT, ACK_CHECK); - i2c_master_read_byte(cmd, data, NACK_VALUE); - i2c_master_stop(cmd); - i2c_set_timeout(I2C_MASTER_NUM, 20); - ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, SMBUS_DEFAULT_TIMEOUT)); - i2c_cmd_link_delete(cmd); - - // return get an actual error status - return err; + // esp_err_t err = ESP_FAIL; + + // i2c_cmd_handle_t cmd = i2c_cmd_link_create(); + // i2c_master_start(cmd); + // i2c_master_write_byte(cmd, TPS546_I2CADDR << 1 | WRITE_BIT, ACK_CHECK); + // i2c_master_write_byte(cmd, command, ACK_CHECK); + // i2c_master_start(cmd); + // i2c_master_write_byte(cmd, TPS546_I2CADDR << 1 | READ_BIT, ACK_CHECK); + // i2c_master_read_byte(cmd, data, NACK_VALUE); + // i2c_master_stop(cmd); + // i2c_set_timeout(I2C_MASTER_NUM, 20); + // ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, SMBUS_DEFAULT_TIMEOUT)); + // i2c_cmd_link_delete(cmd); + + // // return get an actual error status + // return err; + + return i2c_bitaxe_register_read(tps546_dev_handle, command, data, 1); } /** @@ -59,19 +65,21 @@ static esp_err_t smb_read_byte(uint8_t command, uint8_t *data) */ static esp_err_t smb_write_byte(uint8_t command, uint8_t data) { - esp_err_t err = ESP_FAIL; - - i2c_cmd_handle_t cmd = i2c_cmd_link_create(); - i2c_master_start(cmd); - i2c_master_write_byte(cmd, TPS546_I2CADDR << 1 | WRITE_BIT, ACK_CHECK); - i2c_master_write_byte(cmd, command, ACK_CHECK); - i2c_master_write_byte(cmd, data, ACK_CHECK); - i2c_master_stop(cmd); - ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, SMBUS_DEFAULT_TIMEOUT)); - i2c_cmd_link_delete(cmd); - - // TODO return an actual error status - return err; + // esp_err_t err = ESP_FAIL; + + // i2c_cmd_handle_t cmd = i2c_cmd_link_create(); + // i2c_master_start(cmd); + // i2c_master_write_byte(cmd, TPS546_I2CADDR << 1 | WRITE_BIT, ACK_CHECK); + // i2c_master_write_byte(cmd, command, ACK_CHECK); + // i2c_master_write_byte(cmd, data, ACK_CHECK); + // i2c_master_stop(cmd); + // ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, SMBUS_DEFAULT_TIMEOUT)); + // i2c_cmd_link_delete(cmd); + + // // TODO return an actual error status + // return err; + + return i2c_bitaxe_register_write_byte(tps546_dev_handle, command, data); } /** @@ -80,24 +88,31 @@ static esp_err_t smb_write_byte(uint8_t command, uint8_t data) static esp_err_t smb_read_word(uint8_t command, uint16_t *result) { uint8_t data[2]; - esp_err_t err = ESP_FAIL; - - i2c_cmd_handle_t cmd = i2c_cmd_link_create(); - i2c_master_start(cmd); - i2c_master_write_byte(cmd, TPS546_I2CADDR << 1 | WRITE_BIT, ACK_CHECK); - i2c_master_write_byte(cmd, command, ACK_CHECK); - i2c_master_start(cmd); - i2c_master_write_byte(cmd, TPS546_I2CADDR << 1 | READ_BIT, ACK_CHECK); - i2c_master_read(cmd, &data[0], 1, ACK_VALUE); - i2c_master_read_byte(cmd, &data[1], NACK_VALUE); - i2c_master_stop(cmd); - i2c_set_timeout(I2C_MASTER_NUM, 20); - ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, SMBUS_DEFAULT_TIMEOUT)); - i2c_cmd_link_delete(cmd); - - *result = (data[1] << 8) + data[0]; - // TODO return an actual error status - return err; + // esp_err_t err = ESP_FAIL; + + // i2c_cmd_handle_t cmd = i2c_cmd_link_create(); + // i2c_master_start(cmd); + // i2c_master_write_byte(cmd, TPS546_I2CADDR << 1 | WRITE_BIT, ACK_CHECK); + // i2c_master_write_byte(cmd, command, ACK_CHECK); + // i2c_master_start(cmd); + // i2c_master_write_byte(cmd, TPS546_I2CADDR << 1 | READ_BIT, ACK_CHECK); + // i2c_master_read(cmd, &data[0], 1, ACK_VALUE); + // i2c_master_read_byte(cmd, &data[1], NACK_VALUE); + // i2c_master_stop(cmd); + // i2c_set_timeout(I2C_MASTER_NUM, 20); + // ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, SMBUS_DEFAULT_TIMEOUT)); + // i2c_cmd_link_delete(cmd); + + // *result = (data[1] << 8) + data[0]; + // // TODO return an actual error status + // return err; + + if (i2c_bitaxe_register_read(tps546_dev_handle, command, data, 2) != ESP_OK) { + return ESP_FAIL; + } else { + *result = (data[1] << 8) + data[0]; + return ESP_OK; + } } /** @@ -105,73 +120,103 @@ static esp_err_t smb_read_word(uint8_t command, uint16_t *result) */ static esp_err_t smb_write_word(uint8_t command, uint16_t data) { - esp_err_t err = ESP_FAIL; - - i2c_cmd_handle_t cmd = i2c_cmd_link_create(); - i2c_master_start(cmd); - i2c_master_write_byte(cmd, TPS546_I2CADDR << 1 | WRITE_BIT, ACK_CHECK); - i2c_master_write_byte(cmd, command, ACK_CHECK); - i2c_master_write_byte(cmd, (uint8_t)(data & 0x00FF), ACK_CHECK); - i2c_master_write_byte(cmd, (uint8_t)((data & 0xFF00) >> 8), NACK_VALUE); - i2c_master_stop(cmd); - ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, SMBUS_DEFAULT_TIMEOUT)); - i2c_cmd_link_delete(cmd); - - // TODO return an actual error status - return err; + // esp_err_t err = ESP_FAIL; + + // i2c_cmd_handle_t cmd = i2c_cmd_link_create(); + // i2c_master_start(cmd); + // i2c_master_write_byte(cmd, TPS546_I2CADDR << 1 | WRITE_BIT, ACK_CHECK); + // i2c_master_write_byte(cmd, command, ACK_CHECK); + // i2c_master_write_byte(cmd, (uint8_t)(data & 0x00FF), ACK_CHECK); + // i2c_master_write_byte(cmd, (uint8_t)((data & 0xFF00) >> 8), NACK_VALUE); + // i2c_master_stop(cmd); + // ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, SMBUS_DEFAULT_TIMEOUT)); + // i2c_cmd_link_delete(cmd); + + // // TODO return an actual error status + // return err; + + return i2c_bitaxe_register_write_word(tps546_dev_handle, command, data); } /** - * @brief SMBus read block + * @brief SMBus read block -- SMBus is funny in that the first byte returned is the length of data?? */ static esp_err_t smb_read_block(uint8_t command, uint8_t *data, uint8_t len) { - i2c_cmd_handle_t cmd = i2c_cmd_link_create(); - i2c_master_start(cmd); - i2c_master_write_byte(cmd, TPS546_I2CADDR << 1 | WRITE_BIT, ACK_CHECK); - i2c_master_write_byte(cmd, command, ACK_CHECK); - i2c_master_start(cmd); - i2c_master_write_byte(cmd, TPS546_I2CADDR << 1 | READ_BIT, ACK_CHECK); - uint8_t slave_len = 0; - i2c_master_read_byte(cmd, &slave_len, ACK_VALUE); - ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, SMBUS_DEFAULT_TIMEOUT)); - i2c_cmd_link_delete(cmd); - - cmd = i2c_cmd_link_create(); - for (size_t i = 0; i < slave_len - 1; ++i) - { - i2c_master_read_byte(cmd, &data[i], ACK_VALUE); + // i2c_cmd_handle_t cmd = i2c_cmd_link_create(); + // i2c_master_start(cmd); + // i2c_master_write_byte(cmd, TPS546_I2CADDR << 1 | WRITE_BIT, ACK_CHECK); + // i2c_master_write_byte(cmd, command, ACK_CHECK); + // i2c_master_start(cmd); + // i2c_master_write_byte(cmd, TPS546_I2CADDR << 1 | READ_BIT, ACK_CHECK); + // uint8_t slave_len = 0; + // i2c_master_read_byte(cmd, &slave_len, ACK_VALUE); + // ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, SMBUS_DEFAULT_TIMEOUT)); + // i2c_cmd_link_delete(cmd); + + // cmd = i2c_cmd_link_create(); + // for (size_t i = 0; i < slave_len - 1; ++i) + // { + // i2c_master_read_byte(cmd, &data[i], ACK_VALUE); + // } + // i2c_master_read_byte(cmd, &data[slave_len - 1], NACK_VALUE); + // i2c_master_stop(cmd); + // ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, SMBUS_DEFAULT_TIMEOUT)); + // i2c_cmd_link_delete(cmd); + + // // TODO return an actual error status + // return 0; + + //malloc a buffer len+1 to store the length byte + uint8_t *buf = (uint8_t *)malloc(len+1); + if (i2c_bitaxe_register_read(tps546_dev_handle, command, buf, len+1) != ESP_OK) { + free(buf); + return ESP_FAIL; } - i2c_master_read_byte(cmd, &data[slave_len - 1], NACK_VALUE); - i2c_master_stop(cmd); - ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, SMBUS_DEFAULT_TIMEOUT)); - i2c_cmd_link_delete(cmd); + //copy the data into the buffer + memcpy(data, buf+1, len); + free(buf); - // TODO return an actual error status - return 0; + return ESP_OK; } /** - * @brief SMBus write block + * @brief SMBus write block - don;t forget the length byte first :P */ static esp_err_t smb_write_block(uint8_t command, uint8_t *data, uint8_t len) { - i2c_cmd_handle_t cmd = i2c_cmd_link_create(); - i2c_master_start(cmd); - i2c_master_write_byte(cmd, TPS546_I2CADDR << 1 | WRITE_BIT, ACK_CHECK); - i2c_master_write_byte(cmd, command, ACK_CHECK); - i2c_master_write_byte(cmd, len, ACK_CHECK); - for (size_t i = 0; i < len; ++i) - { - i2c_master_write_byte(cmd, data[i], ACK_CHECK); + // i2c_cmd_handle_t cmd = i2c_cmd_link_create(); + // i2c_master_start(cmd); + // i2c_master_write_byte(cmd, TPS546_I2CADDR << 1 | WRITE_BIT, ACK_CHECK); + // i2c_master_write_byte(cmd, command, ACK_CHECK); + // i2c_master_write_byte(cmd, len, ACK_CHECK); + // for (size_t i = 0; i < len; ++i) + // { + // i2c_master_write_byte(cmd, data[i], ACK_CHECK); + // } + // i2c_master_stop(cmd); + // i2c_set_timeout(I2C_MASTER_NUM, 20); + // ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, SMBUS_DEFAULT_TIMEOUT)); + // i2c_cmd_link_delete(cmd); + + // // TODO return an actual error status + // return 0; + + //malloc a buffer len+2 to store the command byte and then the length byte + uint8_t *buf = (uint8_t *)malloc(len+2); + buf[0] = command; + buf[1] = len; + //copy the data into the buffer + memcpy(buf+2, data, len); + + //write it all + if (i2c_bitaxe_register_write_bytes(tps546_dev_handle, buf, len+2) != ESP_OK) { + free(buf); + return ESP_FAIL; + } else { + free(buf); + return ESP_OK; } - i2c_master_stop(cmd); - i2c_set_timeout(I2C_MASTER_NUM, 20); - ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, SMBUS_DEFAULT_TIMEOUT)); - i2c_cmd_link_delete(cmd); - - // TODO return an actual error status - return 0; } /** @@ -359,7 +404,7 @@ static uint16_t float_2_ulinear16(float value) // Set up the TPS546 regulator and turn it on int TPS546_init(void) { - uint8_t data[6]; + uint8_t data[7]; uint8_t u8_value; uint16_t u16_value; uint8_t read_mfr_revision[4]; @@ -368,14 +413,18 @@ int TPS546_init(void) ESP_LOGI(TAG, "Initializing the core voltage regulator"); + if (i2c_bitaxe_add_device(TPS546_I2CADDR, &tps546_dev_handle) != ESP_OK) { + ESP_LOGE(TAG, "Failed to add I2C device"); + return -1; + } + /* Establish communication with regulator */ - smb_read_block(PMBUS_IC_DEVICE_ID, data, 6); - ESP_LOGI(TAG, "Device ID: %02x %02x %02x %02x %02x %02x", data[0], data[1], - data[2], data[3], data[4], data[5]); + smb_read_block(PMBUS_IC_DEVICE_ID, data, 6); //the DEVICE_ID block first byte is the length. + ESP_LOGI(TAG, "Device ID: %02x %02x %02x %02x %02x %02x", data[0], data[1], data[2], data[3], data[4], data[5]); /* There's 3 different known device IDs observed so far */ if ( (memcmp(data, DEVICE_ID1, 6) != 0) && (memcmp(data, DEVICE_ID2, 6) != 0) && (memcmp(data, DEVICE_ID3, 6) != 0)) { - ESP_LOGI(TAG, "ERROR- cannot find TPS546 regulator"); + ESP_LOGE(TAG, "Cannot find TPS546 regulator - Device ID mismatch"); return -1; } @@ -438,7 +487,10 @@ int TPS546_init(void) ESP_LOGI(TAG, "--------------------------------------"); // Read the compensation config registers - smb_read_block(PMBUS_COMPENSATION_CONFIG, comp_config, 5); + if (smb_read_block(PMBUS_COMPENSATION_CONFIG, comp_config, 5) != ESP_OK) { + ESP_LOGE(TAG, "Failed to read COMPENSATION CONFIG"); + return -1; + } ESP_LOGI(TAG, "COMPENSATION CONFIG"); ESP_LOGI(TAG, "%02x %02x %02x %02x %02x", comp_config[0], comp_config[1], comp_config[2], comp_config[3], comp_config[4]); @@ -453,11 +505,20 @@ void TPS546_read_mfr_info(uint8_t *read_mfr_revision) uint8_t read_mfr_model[4]; ESP_LOGI(TAG, "Reading MFR info"); - smb_read_block(PMBUS_MFR_ID, read_mfr_id, 3); + if (smb_read_block(PMBUS_MFR_ID, read_mfr_id, 3) != ESP_OK) { + ESP_LOGE(TAG, "Failed to read MFR ID"); + return; + } read_mfr_id[3] = 0x00; - smb_read_block(PMBUS_MFR_MODEL, read_mfr_model, 3); + if (smb_read_block(PMBUS_MFR_MODEL, read_mfr_model, 3) != ESP_OK) { + ESP_LOGE(TAG, "Failed to read MFR MODEL"); + return; + } read_mfr_model[3] = 0x00; - smb_read_block(PMBUS_MFR_REVISION, read_mfr_revision, 3); + if (smb_read_block(PMBUS_MFR_REVISION, read_mfr_revision, 3) != ESP_OK) { + ESP_LOGE(TAG, "Failed to read MFR REVISION"); + return; + } ESP_LOGI(TAG, "MFR_ID: %s", read_mfr_id); ESP_LOGI(TAG, "MFR_MODEL: %s", read_mfr_model); @@ -480,18 +541,25 @@ void TPS546_write_entire_config(void) ESP_LOGI(TAG, "---Writing new config values to TPS546---"); /* set up the ON_OFF_CONFIG */ ESP_LOGI(TAG, "Setting ON_OFF_CONFIG"); - smb_write_byte(PMBUS_ON_OFF_CONFIG, TPS546_INIT_ON_OFF_CONFIG); + if (smb_write_byte(PMBUS_ON_OFF_CONFIG, TPS546_INIT_ON_OFF_CONFIG) != ESP_OK) { + ESP_LOGE(TAG, "Failed to write ON_OFF_CONFIG"); + return; + } /* Switch frequency */ ESP_LOGI(TAG, "Setting FREQUENCY"); smb_write_word(PMBUS_FREQUENCY_SWITCH, int_2_slinear11(TPS546_INIT_FREQUENCY)); /* vin voltage */ - ESP_LOGI(TAG, "Setting VIN"); + ESP_LOGI(TAG, "Setting VIN_ON"); smb_write_word(PMBUS_VIN_ON, float_2_slinear11(TPS546_INIT_VIN_ON)); + ESP_LOGI(TAG, "Setting VIN_OFF"); smb_write_word(PMBUS_VIN_OFF, float_2_slinear11(TPS546_INIT_VIN_OFF)); + ESP_LOGI(TAG, "Setting VIN_UV_WARN_LIMIT"); smb_write_word(PMBUS_VIN_UV_WARN_LIMIT, float_2_slinear11(TPS546_INIT_VIN_UV_WARN_LIMIT)); + ESP_LOGI(TAG, "Setting VIN_UV_FAULT_LIMIT"); smb_write_word(PMBUS_VIN_OV_FAULT_LIMIT, float_2_slinear11(TPS546_INIT_VIN_OV_FAULT_LIMIT)); + ESP_LOGI(TAG, "Setting VIN_OV_WARN_LIMIT"); smb_write_byte(PMBUS_VIN_OV_FAULT_RESPONSE, TPS546_INIT_VIN_OV_FAULT_RESPONSE); /* vout voltage */ @@ -612,12 +680,16 @@ float TPS546_get_vin(void) float vin; /* Get voltage input (ULINEAR16) */ - smb_read_word(PMBUS_READ_VIN, &u16_value); - vin = slinear11_2_float(u16_value); -#ifdef _DEBUG_LOG_ - ESP_LOGI(TAG, "Got Vin: %2.3f V", vin); -#endif - return vin; + if (smb_read_word(PMBUS_READ_VIN, &u16_value) != ESP_OK) { + ESP_LOGE(TAG, "Could not read VIN"); + return 0; + } else { + vin = ulinear16_2_float(u16_value); + #ifdef _DEBUG_LOG_ + ESP_LOGI(TAG, "Got Vin: %2.3f V", vin); + #endif + return vin; + } } float TPS546_get_iout(void) @@ -626,14 +698,18 @@ float TPS546_get_iout(void) float iout; /* Get current output (SLINEAR11) */ - smb_read_word(PMBUS_READ_IOUT, &u16_value); - iout = slinear11_2_float(u16_value); + if (smb_read_word(PMBUS_READ_IOUT, &u16_value) != ESP_OK) { + ESP_LOGE(TAG, "Could not read Iout"); + return 0; + } else { + iout = slinear11_2_float(u16_value); -#ifdef _DEBUG_LOG_ - ESP_LOGI(TAG, "Got Iout: %2.3f A", iout); -#endif + #ifdef _DEBUG_LOG_ + ESP_LOGI(TAG, "Got Iout: %2.3f A", iout); + #endif - return iout; + return iout; +} } float TPS546_get_vout(void) @@ -642,12 +718,16 @@ float TPS546_get_vout(void) float vout; /* Get voltage output (ULINEAR16) */ - smb_read_word(PMBUS_READ_VOUT, &u16_value); - vout = ulinear16_2_float(u16_value); -#ifdef _DEBUG_LOG_ - ESP_LOGI(TAG, "Got Vout: %2.3f V", vout); -#endif - return vout; + if (smb_read_word(PMBUS_READ_VOUT, &u16_value) != ESP_OK) { + ESP_LOGE(TAG, "Could not read Vout"); + return 0; + } else { + vout = ulinear16_2_float(u16_value); + #ifdef _DEBUG_LOG_ + ESP_LOGI(TAG, "Got Vout: %2.3f V", vout); + #endif + return vout; + } } /** @@ -663,19 +743,26 @@ void TPS546_set_vout(float volts) if (volts == 0) { /* turn off output */ - smb_write_byte(PMBUS_OPERATION, OPERATION_OFF); + if (smb_write_byte(PMBUS_OPERATION, OPERATION_OFF) != ESP_OK) { + ESP_LOGE(TAG, "Could not turn off Vout"); + } } else { /* make sure we're in range */ if ((volts < TPS546_INIT_VOUT_MIN) || (volts > TPS546_INIT_VOUT_MAX)) { - ESP_LOGI(TAG, "ERR- Voltage requested (%f V) is out of range", volts); + ESP_LOGE(TAG, "Voltage requested (%f V) is out of range", volts); } else { /* set the output voltage */ value = float_2_ulinear16(volts); - smb_write_word(PMBUS_VOUT_COMMAND, value); + if (smb_write_word(PMBUS_VOUT_COMMAND, value) != ESP_OK) { + ESP_LOGE(TAG, "Could not set Vout to %1.2f V", volts); + } + ESP_LOGI(TAG, "Vout changed to %1.2f V", volts); /* turn on output */ - smb_write_byte(PMBUS_OPERATION, OPERATION_ON); + if (smb_write_byte(PMBUS_OPERATION, OPERATION_ON) != ESP_OK) { + ESP_LOGE(TAG, "Could not turn on Vout"); + } } } } diff --git a/main/i2c_bitaxe.c b/main/i2c_bitaxe.c index 5b3a94d2..d5ba00b9 100644 --- a/main/i2c_bitaxe.c +++ b/main/i2c_bitaxe.c @@ -1,3 +1,5 @@ +#include "esp_event.h" +#include "esp_log.h" #include "i2c_bitaxe.h" #define I2C_MASTER_SCL_IO 48 /*!< GPIO number used for I2C master clock */ @@ -6,68 +8,118 @@ #define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */ #define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */ +static i2c_master_bus_handle_t i2c_bus_handle; + +// /** +// * @brief i2c master initialization +// */ +// esp_err_t i2c_master_init(void) +// { +// int i2c_master_port = I2C_MASTER_NUM; + +// i2c_config_t conf = { +// .mode = I2C_MODE_MASTER, +// .sda_io_num = I2C_MASTER_SDA_IO, +// .scl_io_num = I2C_MASTER_SCL_IO, +// .sda_pullup_en = GPIO_PULLUP_ENABLE, +// .scl_pullup_en = GPIO_PULLUP_ENABLE, +// .master.clk_speed = I2C_MASTER_FREQ_HZ, +// }; + +// i2c_param_config(i2c_master_port, &conf); + +// return i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0); +// } + /** * @brief i2c master initialization */ -esp_err_t i2c_master_init(void) +esp_err_t i2c_bitaxe_init(void) { - int i2c_master_port = I2C_MASTER_NUM; - - i2c_config_t conf = { - .mode = I2C_MODE_MASTER, - .sda_io_num = I2C_MASTER_SDA_IO, + i2c_master_bus_config_t i2c_bus_config = { + .clk_source = I2C_CLK_SRC_DEFAULT, + .i2c_port = I2C_MASTER_NUM, .scl_io_num = I2C_MASTER_SCL_IO, - .sda_pullup_en = GPIO_PULLUP_ENABLE, - .scl_pullup_en = GPIO_PULLUP_ENABLE, - .master.clk_speed = I2C_MASTER_FREQ_HZ, + .sda_io_num = I2C_MASTER_SDA_IO, + .glitch_ignore_cnt = 7, + .flags.enable_internal_pullup = true, }; - i2c_param_config(i2c_master_port, &conf); - - return i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0); + return i2c_new_master_bus(&i2c_bus_config, &i2c_bus_handle); } /** - * @brief i2c master delete + * @brief Add a new I2C Device */ -esp_err_t i2c_master_delete(void) +esp_err_t i2c_bitaxe_add_device(uint8_t device_address, i2c_master_dev_handle_t * dev_handle) { - return i2c_driver_delete(I2C_MASTER_NUM); + 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); } + +// /** +// * @brief i2c master delete +// */ +// esp_err_t i2c_master_delete(void) +// { +// return i2c_driver_delete(I2C_MASTER_NUM); +// } + /** * @brief Read a sequence of I2C bytes + * @param dev_handle The I2C device handle + * @param reg_addr The register address to read from + * @param read_buf The buffer to store the read data + * @param len The number of bytes to read */ -esp_err_t i2c_master_register_read(uint8_t device_address, uint8_t reg_addr, uint8_t * data, size_t len) +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) { - return i2c_master_write_read_device(I2C_MASTER_NUM, device_address, ®_addr, 1, data, len, - I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS); + // 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_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS); } /** * @brief Write a byte to a I2C register + * @param dev_handle The I2C device handle + * @param reg_addr The register address to write to + * @param data The data to write */ -esp_err_t i2c_master_register_write_byte(uint8_t device_address, uint8_t reg_addr, uint8_t data) +esp_err_t i2c_bitaxe_register_write_byte(i2c_master_dev_handle_t dev_handle, uint8_t reg_addr, uint8_t data) { - int ret; uint8_t write_buf[2] = {reg_addr, data}; - ret = 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_write_to_device(I2C_MASTER_NUM, device_address, write_buf, sizeof(write_buf), I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS); - return ret; + return i2c_master_transmit(dev_handle, write_buf, 2, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS); +} + +/** + * @brief Write a bytes to a I2C register + * @param dev_handle The I2C device handle + * @param data The data to write + * @param len The number of bytes to write + */ +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_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS); } /** * @brief Write a word to a I2C register */ -esp_err_t i2c_master_register_write_word(uint8_t device_address, uint8_t reg_addr, uint16_t data) +esp_err_t i2c_bitaxe_register_write_word(i2c_master_dev_handle_t dev_handle, uint8_t reg_addr, uint16_t data) { - int ret; - uint8_t write_buf[3] = {reg_addr, (data >> 8) & 0xFF, data & 0xFF}; + uint8_t write_buf[3] = {reg_addr, (uint8_t)(data & 0x00FF), (uint8_t)((data & 0xFF00) >> 8)}; - ret = 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_write_to_device(I2C_MASTER_NUM, device_address, write_buf, sizeof(write_buf), I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS); - return ret; + return i2c_master_transmit(dev_handle, write_buf, 3, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS); } diff --git a/main/i2c_bitaxe.h b/main/i2c_bitaxe.h index bcc1eb73..399e0108 100644 --- a/main/i2c_bitaxe.h +++ b/main/i2c_bitaxe.h @@ -1,16 +1,19 @@ #ifndef I2C_MASTER_H_ #define I2C_MASTER_H_ -//#include "driver/i2c_master.h" -#include "driver/i2c.h" +#include "driver/i2c_master.h" +//#include "driver/i2c.h" #define I2C_MASTER_NUM 0 /*!< I2C master i2c port number, the number of i2c peripheral interfaces available will depend on the chip */ #define I2C_MASTER_TIMEOUT_MS 1000 -esp_err_t i2c_master_init(void); -esp_err_t i2c_master_delete(void); -esp_err_t i2c_master_register_read(uint8_t device_address, uint8_t reg_addr, uint8_t * data, size_t len); -esp_err_t i2c_master_register_write_byte(uint8_t device_address, uint8_t reg_addr, uint8_t data); -esp_err_t i2c_master_register_write_word(uint8_t device_address, uint8_t reg_addr, uint16_t data); +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_register_read(i2c_master_dev_handle_t dev_handle, uint8_t reg_addr, uint8_t * read_buf, size_t len); +esp_err_t i2c_bitaxe_register_write_byte(i2c_master_dev_handle_t dev_handle, uint8_t reg_addr, uint8_t data); +esp_err_t i2c_bitaxe_register_write_bytes(i2c_master_dev_handle_t dev_handle, uint8_t * data, uint8_t len); +esp_err_t i2c_bitaxe_register_write_word(i2c_master_dev_handle_t dev_handle, uint8_t reg_addr, uint16_t data); #endif /* I2C_MASTER_H_ */ diff --git a/main/oled.c b/main/oled.c index 2c283f1a..2770d6fd 100644 --- a/main/oled.c +++ b/main/oled.c @@ -15,16 +15,18 @@ // A copy of the display memory is maintained by this code so that single pixel // writes can occur without having to read from the display controller. -#include "i2c_bitaxe.h" -#include "esp_err.h" -#include "esp_log.h" -#include "nvs_config.h" #include #include #include +#include "esp_err.h" +#include "esp_log.h" +#include "nvs_config.h" +#include "i2c_bitaxe.h" #include "oled.h" +#define OLED_I2C_ADDR 0x3C + extern unsigned char ucSmallFont[]; static int iScreenOffset; // current write offset of screen data static unsigned char ucScreen[1024]; // local copy of the image buffer @@ -35,9 +37,17 @@ static esp_err_t write(uint8_t *, uint8_t); static bool oled_active; +static i2c_master_dev_handle_t ssd1306_dev_handle; + // Initialialize the OLED Screen -bool OLED_init(void) +esp_err_t OLED_init(void) { + + //init the I2C device + if (i2c_bitaxe_add_device(OLED_I2C_ADDR, &ssd1306_dev_handle) != ESP_OK) { + return ESP_FAIL; + } + uint8_t oled32_initbuf[] = {0x00, 0xae, // cmd: display off 0xd5, // cmd: set display clock @@ -281,9 +291,7 @@ bool OLED_status(void) */ static esp_err_t write(uint8_t * data, uint8_t len) { - int ret; - - ret = i2c_master_write_to_device(I2C_MASTER_NUM, 0x3C, data, len, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS); + //return i2c_master_write_to_device(I2C_MASTER_NUM, 0x3C, data, len, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS); - return ret; + return i2c_bitaxe_register_write_bytes(ssd1306_dev_handle, data, len); } diff --git a/main/oled.h b/main/oled.h index e3593fd2..538a1473 100644 --- a/main/oled.h +++ b/main/oled.h @@ -27,7 +27,7 @@ typedef enum // Optionally enable inverted or flipped mode // returns 0 for success, 1 for failure // -bool OLED_init(void); +esp_err_t OLED_init(void); // Turns off the display and closes the I2C handle void OLED_shutdown(void); diff --git a/main/self_test/self_test.c b/main/self_test/self_test.c index 02298bfc..868b23cd 100644 --- a/main/self_test/self_test.c +++ b/main/self_test/self_test.c @@ -137,7 +137,7 @@ void self_test(void * pvParameters) } // Init I2C - ESP_ERROR_CHECK(i2c_master_init()); + ESP_ERROR_CHECK(i2c_bitaxe_init()); ESP_LOGI(TAG, "I2C initialized successfully"); diff --git a/main/system.c b/main/system.c index f90a9b91..04ba56ab 100644 --- a/main/system.c +++ b/main/system.c @@ -4,8 +4,7 @@ #include "i2c_bitaxe.h" #include "EMC2101.h" -#include "INA260.h" -#include "TMP1075.h" +//#include "INA260.h" #include "adc.h" #include "connect.h" #include "led_controller.h" @@ -105,9 +104,12 @@ static void _init_system(GlobalState * GLOBAL_STATE) // led_set(); // Init I2C - ESP_ERROR_CHECK(i2c_master_init()); + ESP_ERROR_CHECK(i2c_bitaxe_init()); ESP_LOGI(TAG, "I2C initialized successfully"); + //wait for I2C to init + vTaskDelay(100 / portTICK_PERIOD_MS); + // Initialize the core voltage regulator VCORE_init(GLOBAL_STATE); VCORE_set_voltage(nvs_config_get_u16(NVS_CONFIG_ASIC_VOLTAGE, CONFIG_ASIC_VOLTAGE) / 1000.0, GLOBAL_STATE); diff --git a/main/tasks/power_management_task.c b/main/tasks/power_management_task.c index c0116f11..be019d1c 100644 --- a/main/tasks/power_management_task.c +++ b/main/tasks/power_management_task.c @@ -9,7 +9,6 @@ #include "mining.h" #include "nvs_config.h" #include "serial.h" -#include "TMP1075.h" #include "TPS546.h" #include "vcore.h" #include @@ -134,10 +133,14 @@ void POWER_MANAGEMENT_task(void * pvParameters) power_management->power = (TPS546_get_vout() * power_management->current) / 1000; // The power reading from the TPS546 is only it's output power. So the rest of the Bitaxe power is not accounted for. power_management->power += SUPRA_POWER_OFFSET; // Add offset for the rest of the Bitaxe power. TODO: this better. - } else if (INA260_installed() == true) { - power_management->voltage = INA260_read_voltage(); - power_management->current = INA260_read_current(); - power_management->power = INA260_read_power() / 1000; + } else { + INA260_init(); + + if (INA260_installed() == true) { + power_management->voltage = INA260_read_voltage(); + power_management->current = INA260_read_current(); + power_management->power = INA260_read_power() / 1000; + } } break; diff --git a/main/vcore.c b/main/vcore.c index 2efc73da..f91dec19 100644 --- a/main/vcore.c +++ b/main/vcore.c @@ -79,6 +79,8 @@ bool VCORE_set_voltage(float core_voltage, GlobalState * global_state) ESP_LOGI(TAG, "Set ASIC voltage = %.3fV", core_voltage); TPS546_set_vout(core_voltage * (float)global_state->voltage_domain); } else { + DS4432U_init(); + uint8_t reg_setting = ds4432_tps40305_bitaxe_voltage_to_reg(core_voltage * (float)global_state->voltage_domain); ESP_LOGI(TAG, "Set ASIC voltage = %.3fV [0x%02X]", core_voltage, reg_setting); DS4432U_set_current_code(0, reg_setting); /// eek!