Skip to content

Commit

Permalink
switched to non-depreciated esp-idf I2C driver. it's working on TPS40…
Browse files Browse the repository at this point in the history
…305-based on Bitaxe. Almost there on TPS546-based.. still some SMBus strangeness
  • Loading branch information
skot committed Oct 7, 2024
1 parent c469581 commit 59b98e8
Show file tree
Hide file tree
Showing 18 changed files with 420 additions and 216 deletions.
1 change: 0 additions & 1 deletion main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ SRCS
"nvs_config.c"
"oled.c"
"system.c"
"TMP1075.c"
"TPS546.c"
"vcore.c"
"work_queue.c"
Expand Down
18 changes: 14 additions & 4 deletions main/DS4432U.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "esp_log.h"

#include "i2c_bitaxe.h"

#include "DS4432U.h"

// DS4432U+ -- Adjustable current DAC
Expand All @@ -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.
*
Expand All @@ -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);
}

/**
Expand All @@ -34,15 +44,15 @@ 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)
{
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;
}
1 change: 1 addition & 0 deletions main/DS4432U.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
38 changes: 26 additions & 12 deletions main/EMC2101.c
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
#include "esp_log.h"
#include <stdio.h>
#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
Expand All @@ -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
Expand All @@ -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);

Expand All @@ -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;
Expand All @@ -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;
}
2 changes: 1 addition & 1 deletion main/EMC2101.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */
20 changes: 16 additions & 4 deletions main/INA260.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
#include <stdio.h>
#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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions main/INA260.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 12 additions & 4 deletions main/TMP1075.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);
Expand All @@ -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];
Expand Down
1 change: 1 addition & 0 deletions main/TMP1075.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@

bool TMP1075_installed(int);
uint8_t TMP1075_read_temperature(int);
esp_err_t TMP1075_init(void);

#endif /* TMP1075_H_ */
Loading

0 comments on commit 59b98e8

Please sign in to comment.