2021-03-02 11:45:47 +00:00
|
|
|
#include "bq27220.h"
|
|
|
|
#include "bq27220_reg.h"
|
2021-02-18 12:49:32 +00:00
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
#include <furi.h>
|
2020-11-11 06:31:35 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
#define TAG "Gauge"
|
|
|
|
|
2021-11-28 18:28:19 +00:00
|
|
|
uint16_t bq27220_read_word(FuriHalI2cBusHandle* handle, uint8_t address) {
|
2022-01-21 16:55:44 +00:00
|
|
|
uint16_t buf = 0;
|
2021-11-28 18:28:19 +00:00
|
|
|
|
2022-01-21 16:55:44 +00:00
|
|
|
furi_hal_i2c_read_mem(
|
|
|
|
handle, BQ27220_ADDRESS, address, (uint8_t*)&buf, 2, BQ27220_I2C_TIMEOUT);
|
2021-11-28 18:28:19 +00:00
|
|
|
|
2022-01-21 16:55:44 +00:00
|
|
|
return buf;
|
2020-11-11 06:31:35 +00:00
|
|
|
}
|
|
|
|
|
2021-11-28 18:28:19 +00:00
|
|
|
bool bq27220_control(FuriHalI2cBusHandle* handle, uint16_t control) {
|
2022-01-21 16:55:44 +00:00
|
|
|
bool ret = furi_hal_i2c_write_mem(
|
|
|
|
handle, BQ27220_ADDRESS, CommandControl, (uint8_t*)&control, 2, BQ27220_I2C_TIMEOUT);
|
2021-11-28 18:28:19 +00:00
|
|
|
|
2021-02-18 12:49:32 +00:00
|
|
|
return ret;
|
2020-11-11 06:31:35 +00:00
|
|
|
}
|
|
|
|
|
2021-03-26 11:23:31 +00:00
|
|
|
uint8_t bq27220_get_checksum(uint8_t* data, uint16_t len) {
|
|
|
|
uint8_t ret = 0;
|
|
|
|
for(uint16_t i = 0; i < len; i++) {
|
|
|
|
ret += data[i];
|
|
|
|
}
|
|
|
|
return 0xFF - ret;
|
|
|
|
}
|
|
|
|
|
2021-11-28 18:28:19 +00:00
|
|
|
bool bq27220_set_parameter_u16(FuriHalI2cBusHandle* handle, uint16_t address, uint16_t value) {
|
2021-03-26 11:23:31 +00:00
|
|
|
bool ret;
|
2022-01-21 16:55:44 +00:00
|
|
|
uint8_t buffer[4];
|
2021-11-28 18:28:19 +00:00
|
|
|
|
2022-01-21 16:55:44 +00:00
|
|
|
buffer[0] = address & 0xFF;
|
|
|
|
buffer[1] = (address >> 8) & 0xFF;
|
|
|
|
buffer[2] = (value >> 8) & 0xFF;
|
|
|
|
buffer[3] = value & 0xFF;
|
|
|
|
ret = furi_hal_i2c_write_mem(
|
|
|
|
handle, BQ27220_ADDRESS, CommandSelectSubclass, buffer, 4, BQ27220_I2C_TIMEOUT);
|
2021-11-28 18:28:19 +00:00
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_delay_us(10000);
|
2021-11-28 18:28:19 +00:00
|
|
|
|
2022-02-09 12:03:27 +00:00
|
|
|
uint8_t checksum = bq27220_get_checksum(buffer, 4);
|
2022-01-21 16:55:44 +00:00
|
|
|
buffer[0] = checksum;
|
|
|
|
buffer[1] = 6;
|
2022-05-06 13:37:10 +00:00
|
|
|
ret &= furi_hal_i2c_write_mem(
|
2022-01-21 16:55:44 +00:00
|
|
|
handle, BQ27220_ADDRESS, CommandMACDataSum, buffer, 2, BQ27220_I2C_TIMEOUT);
|
2021-11-28 18:28:19 +00:00
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_delay_us(10000);
|
2021-03-26 11:23:31 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-11-28 18:28:19 +00:00
|
|
|
bool bq27220_init(FuriHalI2cBusHandle* handle, const ParamCEDV* cedv) {
|
2021-03-26 11:23:31 +00:00
|
|
|
uint32_t timeout = 100;
|
2021-11-28 18:28:19 +00:00
|
|
|
uint16_t design_cap = bq27220_get_design_capacity(handle);
|
2021-05-18 14:59:16 +00:00
|
|
|
if(cedv->design_cap == design_cap) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_I(TAG, "Skip battery profile update");
|
2021-05-18 14:59:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_I(TAG, "Start updating battery profile");
|
2022-05-06 13:37:10 +00:00
|
|
|
OperationStatus status = {0};
|
2021-11-28 18:28:19 +00:00
|
|
|
if(!bq27220_control(handle, Control_ENTER_CFG_UPDATE)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Can't configure update");
|
2021-04-30 04:47:02 +00:00
|
|
|
return false;
|
|
|
|
};
|
2021-05-18 14:59:16 +00:00
|
|
|
|
2022-05-06 13:37:10 +00:00
|
|
|
while((status.CFGUPDATE != true) && (timeout-- > 0)) {
|
2021-11-28 18:28:19 +00:00
|
|
|
bq27220_get_operation_status(handle, &status);
|
2021-03-26 11:23:31 +00:00
|
|
|
}
|
2021-11-28 18:28:19 +00:00
|
|
|
bq27220_set_parameter_u16(handle, AddressGaugingConfig, cedv->cedv_conf.gauge_conf_raw);
|
|
|
|
bq27220_set_parameter_u16(handle, AddressFullChargeCapacity, cedv->full_charge_cap);
|
|
|
|
bq27220_set_parameter_u16(handle, AddressDesignCapacity, cedv->design_cap);
|
|
|
|
bq27220_set_parameter_u16(handle, AddressEMF, cedv->EMF);
|
|
|
|
bq27220_set_parameter_u16(handle, AddressC0, cedv->C0);
|
|
|
|
bq27220_set_parameter_u16(handle, AddressR0, cedv->R0);
|
|
|
|
bq27220_set_parameter_u16(handle, AddressT0, cedv->T0);
|
|
|
|
bq27220_set_parameter_u16(handle, AddressR1, cedv->R1);
|
|
|
|
bq27220_set_parameter_u16(handle, AddressTC, (cedv->TC) << 8 | cedv->C1);
|
|
|
|
bq27220_set_parameter_u16(handle, AddressStartDOD0, cedv->DOD0);
|
|
|
|
bq27220_set_parameter_u16(handle, AddressStartDOD10, cedv->DOD10);
|
|
|
|
bq27220_set_parameter_u16(handle, AddressStartDOD20, cedv->DOD20);
|
|
|
|
bq27220_set_parameter_u16(handle, AddressStartDOD30, cedv->DOD30);
|
|
|
|
bq27220_set_parameter_u16(handle, AddressStartDOD40, cedv->DOD40);
|
|
|
|
bq27220_set_parameter_u16(handle, AddressStartDOD50, cedv->DOD40);
|
|
|
|
bq27220_set_parameter_u16(handle, AddressStartDOD60, cedv->DOD60);
|
|
|
|
bq27220_set_parameter_u16(handle, AddressStartDOD70, cedv->DOD70);
|
|
|
|
bq27220_set_parameter_u16(handle, AddressStartDOD80, cedv->DOD80);
|
|
|
|
bq27220_set_parameter_u16(handle, AddressStartDOD90, cedv->DOD90);
|
|
|
|
bq27220_set_parameter_u16(handle, AddressStartDOD100, cedv->DOD100);
|
|
|
|
bq27220_set_parameter_u16(handle, AddressEDV0, cedv->EDV0);
|
|
|
|
bq27220_set_parameter_u16(handle, AddressEDV1, cedv->EDV1);
|
|
|
|
bq27220_set_parameter_u16(handle, AddressEDV2, cedv->EDV2);
|
|
|
|
|
2022-01-14 18:14:25 +00:00
|
|
|
bq27220_control(handle, Control_EXIT_CFG_UPDATE_REINIT);
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_delay_us(10000);
|
2021-11-28 18:28:19 +00:00
|
|
|
design_cap = bq27220_get_design_capacity(handle);
|
2021-05-18 14:59:16 +00:00
|
|
|
if(cedv->design_cap == design_cap) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_I(TAG, "Battery profile update success");
|
2021-05-18 14:59:16 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Battery profile update failed");
|
2021-05-18 14:59:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-11-11 06:31:35 +00:00
|
|
|
}
|
|
|
|
|
2021-11-28 18:28:19 +00:00
|
|
|
uint16_t bq27220_get_voltage(FuriHalI2cBusHandle* handle) {
|
|
|
|
return bq27220_read_word(handle, CommandVoltage);
|
2020-11-11 06:31:35 +00:00
|
|
|
}
|
|
|
|
|
2021-11-28 18:28:19 +00:00
|
|
|
int16_t bq27220_get_current(FuriHalI2cBusHandle* handle) {
|
|
|
|
return bq27220_read_word(handle, CommandCurrent);
|
2020-12-02 10:47:13 +00:00
|
|
|
}
|
|
|
|
|
2021-11-28 18:28:19 +00:00
|
|
|
uint8_t bq27220_get_battery_status(FuriHalI2cBusHandle* handle, BatteryStatus* battery_status) {
|
|
|
|
uint16_t data = bq27220_read_word(handle, CommandBatteryStatus);
|
2021-03-10 13:23:54 +00:00
|
|
|
if(data == BQ27220_ERROR) {
|
2020-12-02 10:47:13 +00:00
|
|
|
return BQ27220_ERROR;
|
|
|
|
} else {
|
|
|
|
*(uint16_t*)battery_status = data;
|
|
|
|
return BQ27220_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 16:10:18 +00:00
|
|
|
uint8_t
|
|
|
|
bq27220_get_operation_status(FuriHalI2cBusHandle* handle, OperationStatus* operation_status) {
|
2021-11-28 18:28:19 +00:00
|
|
|
uint16_t data = bq27220_read_word(handle, CommandOperationStatus);
|
2021-03-10 13:23:54 +00:00
|
|
|
if(data == BQ27220_ERROR) {
|
2020-12-02 10:47:13 +00:00
|
|
|
return BQ27220_ERROR;
|
|
|
|
} else {
|
|
|
|
*(uint16_t*)operation_status = data;
|
|
|
|
return BQ27220_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-28 18:28:19 +00:00
|
|
|
uint16_t bq27220_get_temperature(FuriHalI2cBusHandle* handle) {
|
|
|
|
return bq27220_read_word(handle, CommandTemperature);
|
2020-11-11 06:31:35 +00:00
|
|
|
}
|
|
|
|
|
2021-11-28 18:28:19 +00:00
|
|
|
uint16_t bq27220_get_full_charge_capacity(FuriHalI2cBusHandle* handle) {
|
|
|
|
return bq27220_read_word(handle, CommandFullChargeCapacity);
|
2020-11-11 06:31:35 +00:00
|
|
|
}
|
|
|
|
|
2021-11-28 18:28:19 +00:00
|
|
|
uint16_t bq27220_get_design_capacity(FuriHalI2cBusHandle* handle) {
|
|
|
|
return bq27220_read_word(handle, CommandDesignCapacity);
|
2021-03-26 11:23:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-28 18:28:19 +00:00
|
|
|
uint16_t bq27220_get_remaining_capacity(FuriHalI2cBusHandle* handle) {
|
|
|
|
return bq27220_read_word(handle, CommandRemainingCapacity);
|
2020-11-11 06:31:35 +00:00
|
|
|
}
|
|
|
|
|
2021-11-28 18:28:19 +00:00
|
|
|
uint16_t bq27220_get_state_of_charge(FuriHalI2cBusHandle* handle) {
|
|
|
|
return bq27220_read_word(handle, CommandStateOfCharge);
|
2020-11-11 06:31:35 +00:00
|
|
|
}
|
2021-03-02 16:07:26 +00:00
|
|
|
|
2021-11-28 18:28:19 +00:00
|
|
|
uint16_t bq27220_get_state_of_health(FuriHalI2cBusHandle* handle) {
|
|
|
|
return bq27220_read_word(handle, CommandStateOfHealth);
|
2021-03-02 16:07:26 +00:00
|
|
|
}
|