Skip to content

Commit

Permalink
get INA219 driver to report voltage, current, and power correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
macphyter committed Nov 12, 2023
1 parent 9693ff8 commit 8672112
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
43 changes: 25 additions & 18 deletions main/INA219.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,46 +25,53 @@ static esp_err_t register_read(uint8_t reg_addr, uint8_t *data, size_t len)
/**
* @brief Write a byte to a I2C register
*/
// static esp_err_t register_write_byte(uint8_t reg_addr, uint8_t data) {
// int ret;
// uint8_t write_buf[2] = {reg_addr, data};
static esp_err_t register_write_word(uint8_t reg_addr, uint8_t *data) {
int ret;
uint8_t write_buf[3] = {reg_addr, data[0], data[1]};

// ret = i2c_master_write_to_device(I2C_MASTER_NUM, INA219_I2CADDR_DEFAULT, write_buf, sizeof(write_buf), I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
ret = i2c_master_write_to_device(I2C_MASTER_NUM, INA219_I2CADDR_DEFAULT, write_buf, sizeof(write_buf), I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);

return ret;
}

// return ret;
// }
bool INA219_installed(void)
{
uint8_t data[2];
return register_read(INA219_REG_BUSVOLTAGE, data, 2) == ESP_OK;
uint8_t data[2] = {0x50, 0x00};

// set the calibration register, otherwise the current and power regs will be zero
ESP_LOGI(TAG, "Setting calibration register");
return register_write_word(INA219_REG_CALIBRATION, data) == ESP_OK;
}

float INA219_read_current(void)
float INA219_read_voltage(void)
{
uint8_t data[2];

ESP_ERROR_CHECK(register_read(INA219_REG_CURRENT, data, 2));
ESP_LOGI(TAG, "Raw Current = %02X %02X", data[1], data[0]);
ESP_ERROR_CHECK(register_read(INA219_REG_BUSVOLTAGE, data, 2));
ESP_LOGI(TAG, "Raw Voltage = %02X %02X", data[0], data[1]);
ESP_LOGI(TAG, "mVolts: %d", (((data[0] << 8) | data[1]) >> 3) * 4);

return (uint16_t)(data[1] | (data[0] << 8)) * 1.25;
return (uint16_t)(((data[0] << 8) | data[1]) >> 3) * 4;
}

float INA219_read_voltage(void)
float INA219_read_current(void)
{
uint8_t data[2];

ESP_ERROR_CHECK(register_read(INA219_REG_BUSVOLTAGE, data, 2));
ESP_LOGI(TAG, "Raw Voltage = %02X %02X", data[1], data[0]);
ESP_ERROR_CHECK(register_read(INA219_REG_CURRENT, data, 2));
ESP_LOGI(TAG, "Raw Current = %02X %02X", data[0], data[1]);
ESP_LOGI(TAG, "mAmps: %d", (int16_t)(data[0] << 8) | data[1]);

return (uint16_t)(data[1] | (data[0] << 8)) * 1.25;
return (int16_t)((data[0] << 8) | data[0]);
}

float INA219_read_power(void)
{
uint8_t data[2];

ESP_ERROR_CHECK(register_read(INA219_REG_POWER, data, 2));
ESP_LOGI(TAG, "Raw Power = %02X %02X", data[1], data[0]);
ESP_LOGI(TAG, "Raw Power = %02X %02X", data[0], data[1]);
ESP_LOGI(TAG, "mWatts: %d", ((data[0] << 8) | data[1]) * 20);

return (data[1] | (data[0] << 8)) * 10;
return ((data[0] << 8) | data[1]) * 20;
}
2 changes: 1 addition & 1 deletion main/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static void _init_system(GlobalState * global_state, SystemModule * module)

// DS4432U tests
//DS4432U_set_vcore(nvs_config_get_u16(NVS_CONFIG_ASIC_VOLTAGE, CONFIG_ASIC_VOLTAGE) / 1000.0);
DS4432U_set_vcore(2000 / 1000.0);
DS4432U_set_vcore(1000 / 1000.0);

// Fan Tests
EMC2302_init(nvs_config_get_u16(NVS_CONFIG_INVERT_FAN_POLARITY, 1));
Expand Down

0 comments on commit 8672112

Please sign in to comment.