mirror of
https://github.com/DarkFlippers/unleashed-firmware.git
synced 2024-11-22 17:33:13 +03:00
FuriHal, drivers: rework gauge initialization routine (#3912)
* FuriHal, drivers: rework gauge initialization, ensure that we can recover from any kind of internal/external issue * Make PVS happy * Format sources * bq27220: add gaps injection into write operations * Drivers: bq27220 cleanup and various fixes * Drivers: bq27220 verbose logging and full access routine fix * Drivers: better cfg mode exit handling in bq27220 driver * Drivers: rewrite bq27220 based on bqstudio+ev2400, experiments and guessing. Fixes all known issues. * PVS: hello license check * Drivers: minimize reset count in bq27220 init sequence * Drivers: bq27220 hide debug logging, reorganize routine to ensure predictable result and minimum amount of interaction with gauge, add documentation and notes. * Drivers: more reliable bq27220_full_access routine * Drivers: replace some warning with error in bq27220 * Drivers: move static asserts to headers in bq27220 * Fix PVS warnings * Drivers: simplify logic in bq27220 --------- Co-authored-by: hedger <hedger@users.noreply.github.com>
This commit is contained in:
parent
8c14361e6a
commit
0469ef0e55
@ -228,7 +228,7 @@ static bool plantain_parse(const NfcDevice* device, FuriString* parsed_data) {
|
|||||||
// Print card number with 4-digit groups
|
// Print card number with 4-digit groups
|
||||||
furi_string_cat_printf(parsed_data, "Number: ");
|
furi_string_cat_printf(parsed_data, "Number: ");
|
||||||
FuriString* card_number_s = furi_string_alloc();
|
FuriString* card_number_s = furi_string_alloc();
|
||||||
furi_string_cat_printf(card_number_s, "%lld", card_number);
|
furi_string_cat_printf(card_number_s, "%llu", card_number);
|
||||||
FuriString* tmp_s = furi_string_alloc_set_str("9643 3078 ");
|
FuriString* tmp_s = furi_string_alloc_set_str("9643 3078 ");
|
||||||
for(uint8_t i = 0; i < 24; i += 4) {
|
for(uint8_t i = 0; i < 24; i += 4) {
|
||||||
for(uint8_t j = 0; j < 4; j++) {
|
for(uint8_t j = 0; j < 4; j++) {
|
||||||
|
@ -1,29 +1,77 @@
|
|||||||
|
|
||||||
#include "bq27220.h"
|
#include "bq27220.h"
|
||||||
#include "bq27220_reg.h"
|
#include "bq27220_reg.h"
|
||||||
#include "bq27220_data_memory.h"
|
#include "bq27220_data_memory.h"
|
||||||
|
|
||||||
_Static_assert(sizeof(BQ27220DMGaugingConfig) == 2, "Incorrect structure size");
|
|
||||||
|
|
||||||
#include <furi.h>
|
#include <furi.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#define TAG "Gauge"
|
#define TAG "Gauge"
|
||||||
|
|
||||||
static uint16_t bq27220_read_word(FuriHalI2cBusHandle* handle, uint8_t address) {
|
#define BQ27220_ID (0x0220u)
|
||||||
uint16_t buf = 0;
|
|
||||||
|
|
||||||
furi_hal_i2c_read_mem(
|
/** Delay between 2 writes into Subclass/MAC area. Fails at ~120us. */
|
||||||
handle, BQ27220_ADDRESS, address, (uint8_t*)&buf, 2, BQ27220_I2C_TIMEOUT);
|
#define BQ27220_MAC_WRITE_DELAY_US (250u)
|
||||||
|
|
||||||
return buf;
|
/** Delay between we ask chip to load data to MAC and it become valid. Fails at ~500us. */
|
||||||
|
#define BQ27220_SELECT_DELAY_US (1000u)
|
||||||
|
|
||||||
|
/** Delay between 2 control operations(like unseal or full access). Fails at ~2500us.*/
|
||||||
|
#define BQ27220_MAGIC_DELAY_US (5000u)
|
||||||
|
|
||||||
|
/** Delay before freshly written configuration can be read. Fails at ? */
|
||||||
|
#define BQ27220_CONFIG_DELAY_US (10000u)
|
||||||
|
|
||||||
|
/** Config apply delay. Must wait, or DM read returns garbage. */
|
||||||
|
#define BQ27220_CONFIG_APPLY_US (2000000u)
|
||||||
|
|
||||||
|
/** Timeout for common operations. */
|
||||||
|
#define BQ27220_TIMEOUT_COMMON_US (2000000u)
|
||||||
|
|
||||||
|
/** Timeout for reset operation. Normally reset takes ~2s. */
|
||||||
|
#define BQ27220_TIMEOUT_RESET_US (4000000u)
|
||||||
|
|
||||||
|
/** Timeout cycle interval */
|
||||||
|
#define BQ27220_TIMEOUT_CYCLE_INTERVAL_US (1000u)
|
||||||
|
|
||||||
|
/** Timeout cycles count helper */
|
||||||
|
#define BQ27220_TIMEOUT(timeout_us) ((timeout_us) / (BQ27220_TIMEOUT_CYCLE_INTERVAL_US))
|
||||||
|
|
||||||
|
#ifdef BQ27220_DEBUG
|
||||||
|
#define BQ27220_DEBUG_LOG(...) FURI_LOG_D(TAG, ##__VA_ARGS__)
|
||||||
|
#else
|
||||||
|
#define BQ27220_DEBUG_LOG(...)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static inline bool bq27220_read_reg(
|
||||||
|
FuriHalI2cBusHandle* handle,
|
||||||
|
uint8_t address,
|
||||||
|
uint8_t* buffer,
|
||||||
|
size_t buffer_size) {
|
||||||
|
return furi_hal_i2c_trx(
|
||||||
|
handle, BQ27220_ADDRESS, &address, 1, buffer, buffer_size, BQ27220_I2C_TIMEOUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool bq27220_control(FuriHalI2cBusHandle* handle, uint16_t control) {
|
static inline bool bq27220_write(
|
||||||
bool ret = furi_hal_i2c_write_mem(
|
FuriHalI2cBusHandle* handle,
|
||||||
handle, BQ27220_ADDRESS, CommandControl, (uint8_t*)&control, 2, BQ27220_I2C_TIMEOUT);
|
uint8_t address,
|
||||||
|
const uint8_t* buffer,
|
||||||
|
size_t buffer_size) {
|
||||||
|
return furi_hal_i2c_write_mem(
|
||||||
|
handle, BQ27220_ADDRESS, address, buffer, buffer_size, BQ27220_I2C_TIMEOUT);
|
||||||
|
}
|
||||||
|
|
||||||
return ret;
|
static inline bool bq27220_control(FuriHalI2cBusHandle* handle, uint16_t control) {
|
||||||
|
return bq27220_write(handle, CommandControl, (uint8_t*)&control, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint16_t bq27220_read_word(FuriHalI2cBusHandle* handle, uint8_t address) {
|
||||||
|
uint16_t buf = BQ27220_ERROR;
|
||||||
|
|
||||||
|
if(!bq27220_read_reg(handle, address, (uint8_t*)&buf, 2)) {
|
||||||
|
FURI_LOG_E(TAG, "bq27220_read_word failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint8_t bq27220_get_checksum(uint8_t* data, uint16_t len) {
|
static uint8_t bq27220_get_checksum(uint8_t* data, uint16_t len) {
|
||||||
@ -56,49 +104,49 @@ static bool bq27220_parameter_check(
|
|||||||
if(update) {
|
if(update) {
|
||||||
// Datasheet contains incorrect procedure for memory update, more info:
|
// Datasheet contains incorrect procedure for memory update, more info:
|
||||||
// https://e2e.ti.com/support/power-management-group/power-management/f/power-management-forum/719878/bq27220-technical-reference-manual-sluubd4-is-missing-extended-data-commands-chapter
|
// https://e2e.ti.com/support/power-management-group/power-management/f/power-management-forum/719878/bq27220-technical-reference-manual-sluubd4-is-missing-extended-data-commands-chapter
|
||||||
|
// Also see note in the header
|
||||||
|
|
||||||
// 2. Write the address AND the parameter data to 0x3E+ (auto increment)
|
// Write the address AND the parameter data to 0x3E+ (auto increment)
|
||||||
if(!furi_hal_i2c_write_mem(
|
if(!bq27220_write(handle, CommandSelectSubclass, buffer, size + 2)) {
|
||||||
handle,
|
FURI_LOG_E(TAG, "DM write failed");
|
||||||
BQ27220_ADDRESS,
|
|
||||||
CommandSelectSubclass,
|
|
||||||
buffer,
|
|
||||||
size + 2,
|
|
||||||
BQ27220_I2C_TIMEOUT)) {
|
|
||||||
FURI_LOG_I(TAG, "DM write failed");
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
furi_delay_us(10000);
|
// We must wait, otherwise write will fail
|
||||||
|
furi_delay_us(BQ27220_MAC_WRITE_DELAY_US);
|
||||||
|
|
||||||
// 3. Calculate the check sum: 0xFF - (sum of address and data) OR 0xFF
|
// Calculate the check sum: 0xFF - (sum of address and data) OR 0xFF
|
||||||
uint8_t checksum = bq27220_get_checksum(buffer, size + 2);
|
uint8_t checksum = bq27220_get_checksum(buffer, size + 2);
|
||||||
// 4. Write the check sum to 0x60 and the total length of (address + parameter data + check sum + length) to 0x61
|
// Write the check sum to 0x60 and the total length of (address + parameter data + check sum + length) to 0x61
|
||||||
buffer[0] = checksum;
|
buffer[0] = checksum;
|
||||||
// 2 bytes address, `size` bytes data, 1 byte check sum, 1 byte length
|
// 2 bytes address, `size` bytes data, 1 byte check sum, 1 byte length
|
||||||
buffer[1] = 2 + size + 1 + 1;
|
buffer[1] = 2 + size + 1 + 1;
|
||||||
if(!furi_hal_i2c_write_mem(
|
if(!bq27220_write(handle, CommandMACDataSum, buffer, 2)) {
|
||||||
handle, BQ27220_ADDRESS, CommandMACDataSum, buffer, 2, BQ27220_I2C_TIMEOUT)) {
|
FURI_LOG_E(TAG, "CRC write failed");
|
||||||
FURI_LOG_I(TAG, "CRC write failed");
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
// Final wait as in gm.fs specification
|
||||||
furi_delay_us(10000);
|
furi_delay_us(BQ27220_CONFIG_DELAY_US);
|
||||||
ret = true;
|
ret = true;
|
||||||
} else {
|
} else {
|
||||||
if(!furi_hal_i2c_write_mem(
|
if(!bq27220_write(handle, CommandSelectSubclass, buffer, 2)) {
|
||||||
handle, BQ27220_ADDRESS, CommandSelectSubclass, buffer, 2, BQ27220_I2C_TIMEOUT)) {
|
FURI_LOG_E(TAG, "DM SelectSubclass for read failed");
|
||||||
FURI_LOG_I(TAG, "DM SelectSubclass for read failed");
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!furi_hal_i2c_rx(handle, BQ27220_ADDRESS, old_data, size, BQ27220_I2C_TIMEOUT)) {
|
// bqstudio uses 15ms wait delay here
|
||||||
FURI_LOG_I(TAG, "DM read failed");
|
furi_delay_us(BQ27220_SELECT_DELAY_US);
|
||||||
|
|
||||||
|
if(!bq27220_read_reg(handle, CommandMACData, old_data, size)) {
|
||||||
|
FURI_LOG_E(TAG, "DM read failed");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// bqstudio uses burst reads with continue(CommandSelectSubclass without argument) and ~5ms between burst
|
||||||
|
furi_delay_us(BQ27220_SELECT_DELAY_US);
|
||||||
|
|
||||||
if(*(uint32_t*)&(old_data[0]) != *(uint32_t*)&(buffer[2])) {
|
if(*(uint32_t*)&(old_data[0]) != *(uint32_t*)&(buffer[2])) {
|
||||||
FURI_LOG_W( //-V641
|
FURI_LOG_E( //-V641
|
||||||
TAG,
|
TAG,
|
||||||
"Data at 0x%04x(%zu): 0x%08lx!=0x%08lx",
|
"Data at 0x%04x(%zu): 0x%08lx!=0x%08lx",
|
||||||
address,
|
address,
|
||||||
@ -119,22 +167,34 @@ static bool bq27220_data_memory_check(
|
|||||||
const BQ27220DMData* data_memory,
|
const BQ27220DMData* data_memory,
|
||||||
bool update) {
|
bool update) {
|
||||||
if(update) {
|
if(update) {
|
||||||
if(!bq27220_control(handle, Control_ENTER_CFG_UPDATE)) {
|
const uint16_t cfg_request = Control_ENTER_CFG_UPDATE;
|
||||||
|
if(!bq27220_write(
|
||||||
|
handle, CommandSelectSubclass, (uint8_t*)&cfg_request, sizeof(cfg_request))) {
|
||||||
FURI_LOG_E(TAG, "ENTER_CFG_UPDATE command failed");
|
FURI_LOG_E(TAG, "ENTER_CFG_UPDATE command failed");
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Wait for enter CFG update mode
|
// Wait for enter CFG update mode
|
||||||
uint32_t timeout = 100;
|
uint32_t timeout = BQ27220_TIMEOUT(BQ27220_TIMEOUT_COMMON_US);
|
||||||
OperationStatus status = {0};
|
Bq27220OperationStatus operation_status;
|
||||||
while((status.CFGUPDATE != true) && (timeout-- > 0)) {
|
while(--timeout > 0) {
|
||||||
bq27220_get_operation_status(handle, &status);
|
if(!bq27220_get_operation_status(handle, &operation_status)) {
|
||||||
|
FURI_LOG_W(TAG, "Failed to get operation status, retries left %lu", timeout);
|
||||||
|
} else if(operation_status.CFGUPDATE) {
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
furi_delay_us(BQ27220_TIMEOUT_CYCLE_INTERVAL_US);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(timeout == 0) {
|
if(timeout == 0) {
|
||||||
FURI_LOG_E(TAG, "CFGUPDATE mode failed");
|
FURI_LOG_E(
|
||||||
|
TAG,
|
||||||
|
"Enter CFGUPDATE mode failed, CFG %u, SEC %u",
|
||||||
|
operation_status.CFGUPDATE,
|
||||||
|
operation_status.SEC);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
BQ27220_DEBUG_LOG("Cycles left: %lu", timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process data memory records
|
// Process data memory records
|
||||||
@ -179,43 +239,283 @@ static bool bq27220_data_memory_check(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Finalize configuration update
|
// Finalize configuration update
|
||||||
if(update) {
|
if(update && result) {
|
||||||
bq27220_control(handle, Control_EXIT_CFG_UPDATE_REINIT);
|
bq27220_control(handle, Control_EXIT_CFG_UPDATE_REINIT);
|
||||||
furi_delay_us(10000);
|
|
||||||
|
// Wait for gauge to apply new configuration
|
||||||
|
furi_delay_us(BQ27220_CONFIG_APPLY_US);
|
||||||
|
|
||||||
|
// ensure that we exited config update mode
|
||||||
|
uint32_t timeout = BQ27220_TIMEOUT(BQ27220_TIMEOUT_COMMON_US);
|
||||||
|
Bq27220OperationStatus operation_status;
|
||||||
|
while(--timeout > 0) {
|
||||||
|
if(!bq27220_get_operation_status(handle, &operation_status)) {
|
||||||
|
FURI_LOG_W(TAG, "Failed to get operation status, retries left %lu", timeout);
|
||||||
|
} else if(operation_status.CFGUPDATE != true) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
furi_delay_us(BQ27220_TIMEOUT_CYCLE_INTERVAL_US);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check timeout
|
||||||
|
if(timeout == 0) {
|
||||||
|
FURI_LOG_E(TAG, "Exit CFGUPDATE mode failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
BQ27220_DEBUG_LOG("Cycles left: %lu", timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool bq27220_init(FuriHalI2cBusHandle* handle) {
|
bool bq27220_init(FuriHalI2cBusHandle* handle, const BQ27220DMData* data_memory) {
|
||||||
// Request device number(chip PN)
|
bool result = false;
|
||||||
if(!bq27220_control(handle, Control_DEVICE_NUMBER)) {
|
bool reset_and_provisioning_required = false;
|
||||||
FURI_LOG_E(TAG, "Device is not present");
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
// Check control response
|
|
||||||
uint16_t data = 0;
|
|
||||||
data = bq27220_read_word(handle, CommandControl);
|
|
||||||
if(data != 0xFF00) {
|
|
||||||
FURI_LOG_E(TAG, "Invalid control response: %x", data);
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
|
|
||||||
data = bq27220_read_word(handle, CommandMACData);
|
do {
|
||||||
FURI_LOG_I(TAG, "Device Number %04x", data);
|
// Request device number(chip PN)
|
||||||
|
BQ27220_DEBUG_LOG("Checking device ID");
|
||||||
|
if(!bq27220_control(handle, Control_DEVICE_NUMBER)) {
|
||||||
|
FURI_LOG_E(TAG, "ID: Device is not responding");
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
// Enterprise wait(MAC read fails if less than 500us)
|
||||||
|
// bqstudio uses ~15ms
|
||||||
|
furi_delay_us(BQ27220_SELECT_DELAY_US);
|
||||||
|
// Read id data from MAC scratch space
|
||||||
|
uint16_t data = bq27220_read_word(handle, CommandMACData);
|
||||||
|
if(data != BQ27220_ID) {
|
||||||
|
FURI_LOG_E(TAG, "Invalid Device Number %04x != 0x0220", data);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
return data == 0x0220;
|
// Unseal device since we are going to read protected configuration
|
||||||
|
BQ27220_DEBUG_LOG("Unsealing");
|
||||||
|
if(!bq27220_unseal(handle)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to recover gauge from forever init
|
||||||
|
BQ27220_DEBUG_LOG("Checking initialization status");
|
||||||
|
Bq27220OperationStatus operation_status;
|
||||||
|
if(!bq27220_get_operation_status(handle, &operation_status)) {
|
||||||
|
FURI_LOG_E(TAG, "Failed to get operation status");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(!operation_status.INITCOMP || operation_status.CFGUPDATE) {
|
||||||
|
FURI_LOG_E(TAG, "Incorrect state, reset needed");
|
||||||
|
reset_and_provisioning_required = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure correct profile is selected
|
||||||
|
BQ27220_DEBUG_LOG("Checking chosen profile");
|
||||||
|
Bq27220ControlStatus control_status;
|
||||||
|
if(!bq27220_get_control_status(handle, &control_status)) {
|
||||||
|
FURI_LOG_E(TAG, "Failed to get control status");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(control_status.BATT_ID != 0) {
|
||||||
|
FURI_LOG_E(TAG, "Incorrect profile, reset needed");
|
||||||
|
reset_and_provisioning_required = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure correct configuration loaded into gauge DataMemory
|
||||||
|
// Only if reset is not required, otherwise we don't
|
||||||
|
if(!reset_and_provisioning_required) {
|
||||||
|
BQ27220_DEBUG_LOG("Checking data memory");
|
||||||
|
if(!bq27220_data_memory_check(handle, data_memory, false)) {
|
||||||
|
FURI_LOG_E(TAG, "Incorrect configuration data, reset needed");
|
||||||
|
reset_and_provisioning_required = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset needed
|
||||||
|
if(reset_and_provisioning_required) {
|
||||||
|
FURI_LOG_W(TAG, "Resetting device");
|
||||||
|
if(!bq27220_reset(handle)) {
|
||||||
|
FURI_LOG_E(TAG, "Failed to reset device");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get full access to read and modify parameters
|
||||||
|
// Also it looks like this step is totally unnecessary
|
||||||
|
BQ27220_DEBUG_LOG("Acquiring Full Access");
|
||||||
|
if(!bq27220_full_access(handle)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update memory
|
||||||
|
FURI_LOG_W(TAG, "Updating data memory");
|
||||||
|
bq27220_data_memory_check(handle, data_memory, true);
|
||||||
|
if(!bq27220_data_memory_check(handle, data_memory, false)) {
|
||||||
|
FURI_LOG_E(TAG, "Data memory update failed");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BQ27220_DEBUG_LOG("Sealing");
|
||||||
|
if(!bq27220_seal(handle)) {
|
||||||
|
FURI_LOG_E(TAG, "Seal failed");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = true;
|
||||||
|
} while(0);
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool bq27220_apply_data_memory(FuriHalI2cBusHandle* handle, const BQ27220DMData* data_memory) {
|
bool bq27220_reset(FuriHalI2cBusHandle* handle) {
|
||||||
FURI_LOG_I(TAG, "Verifying data memory");
|
bool result = false;
|
||||||
if(!bq27220_data_memory_check(handle, data_memory, false)) {
|
do {
|
||||||
FURI_LOG_I(TAG, "Updating data memory");
|
if(!bq27220_control(handle, Control_RESET)) {
|
||||||
bq27220_data_memory_check(handle, data_memory, true);
|
FURI_LOG_E(TAG, "Reset request failed");
|
||||||
}
|
break;
|
||||||
FURI_LOG_I(TAG, "Data memory verification complete");
|
};
|
||||||
|
|
||||||
return true;
|
uint32_t timeout = BQ27220_TIMEOUT(BQ27220_TIMEOUT_RESET_US);
|
||||||
|
Bq27220OperationStatus operation_status;
|
||||||
|
while(--timeout > 0) {
|
||||||
|
if(!bq27220_get_operation_status(handle, &operation_status)) {
|
||||||
|
FURI_LOG_W(TAG, "Failed to get operation status, retries left %lu", timeout);
|
||||||
|
} else if(operation_status.INITCOMP == true) {
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
furi_delay_us(BQ27220_TIMEOUT_CYCLE_INTERVAL_US);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(timeout == 0) {
|
||||||
|
FURI_LOG_E(TAG, "INITCOMP timeout after reset");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
BQ27220_DEBUG_LOG("Cycles left: %lu", timeout);
|
||||||
|
|
||||||
|
result = true;
|
||||||
|
} while(0);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool bq27220_seal(FuriHalI2cBusHandle* handle) {
|
||||||
|
Bq27220OperationStatus operation_status = {0};
|
||||||
|
bool result = false;
|
||||||
|
do {
|
||||||
|
if(!bq27220_get_operation_status(handle, &operation_status)) {
|
||||||
|
FURI_LOG_E(TAG, "Status query failed");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(operation_status.SEC == Bq27220OperationStatusSecSealed) {
|
||||||
|
result = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!bq27220_control(handle, Control_SEALED)) {
|
||||||
|
FURI_LOG_E(TAG, "Seal request failed");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
furi_delay_us(BQ27220_SELECT_DELAY_US);
|
||||||
|
|
||||||
|
if(!bq27220_get_operation_status(handle, &operation_status)) {
|
||||||
|
FURI_LOG_E(TAG, "Status query failed");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(operation_status.SEC != Bq27220OperationStatusSecSealed) {
|
||||||
|
FURI_LOG_E(TAG, "Seal failed");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = true;
|
||||||
|
} while(0);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool bq27220_unseal(FuriHalI2cBusHandle* handle) {
|
||||||
|
Bq27220OperationStatus operation_status = {0};
|
||||||
|
bool result = false;
|
||||||
|
do {
|
||||||
|
if(!bq27220_get_operation_status(handle, &operation_status)) {
|
||||||
|
FURI_LOG_E(TAG, "Status query failed");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(operation_status.SEC != Bq27220OperationStatusSecSealed) {
|
||||||
|
result = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hai, Kazuma desu
|
||||||
|
bq27220_control(handle, UnsealKey1);
|
||||||
|
furi_delay_us(BQ27220_MAGIC_DELAY_US);
|
||||||
|
bq27220_control(handle, UnsealKey2);
|
||||||
|
furi_delay_us(BQ27220_MAGIC_DELAY_US);
|
||||||
|
|
||||||
|
if(!bq27220_get_operation_status(handle, &operation_status)) {
|
||||||
|
FURI_LOG_E(TAG, "Status query failed");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(operation_status.SEC != Bq27220OperationStatusSecUnsealed) {
|
||||||
|
FURI_LOG_E(TAG, "Unseal failed %u", operation_status.SEC);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = true;
|
||||||
|
} while(0);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool bq27220_full_access(FuriHalI2cBusHandle* handle) {
|
||||||
|
bool result = false;
|
||||||
|
|
||||||
|
do {
|
||||||
|
uint32_t timeout = BQ27220_TIMEOUT(BQ27220_TIMEOUT_COMMON_US);
|
||||||
|
Bq27220OperationStatus operation_status;
|
||||||
|
while(--timeout > 0) {
|
||||||
|
if(!bq27220_get_operation_status(handle, &operation_status)) {
|
||||||
|
FURI_LOG_W(TAG, "Failed to get operation status, retries left %lu", timeout);
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
furi_delay_us(BQ27220_TIMEOUT_CYCLE_INTERVAL_US);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(timeout == 0) {
|
||||||
|
FURI_LOG_E(TAG, "Failed to get operation status");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
BQ27220_DEBUG_LOG("Cycles left: %lu", timeout);
|
||||||
|
|
||||||
|
// Already full access
|
||||||
|
if(operation_status.SEC == Bq27220OperationStatusSecFull) {
|
||||||
|
result = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// Must be unsealed to get full access
|
||||||
|
if(operation_status.SEC != Bq27220OperationStatusSecUnsealed) {
|
||||||
|
FURI_LOG_E(TAG, "Not in unsealed state");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Explosion!!!
|
||||||
|
bq27220_control(handle, FullAccessKey); //-V760
|
||||||
|
furi_delay_us(BQ27220_MAGIC_DELAY_US);
|
||||||
|
bq27220_control(handle, FullAccessKey);
|
||||||
|
furi_delay_us(BQ27220_MAGIC_DELAY_US);
|
||||||
|
|
||||||
|
if(!bq27220_get_operation_status(handle, &operation_status)) {
|
||||||
|
FURI_LOG_E(TAG, "Status query failed");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(operation_status.SEC != Bq27220OperationStatusSecFull) {
|
||||||
|
FURI_LOG_E(TAG, "Full access failed %u", operation_status.SEC);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = true;
|
||||||
|
} while(0);
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t bq27220_get_voltage(FuriHalI2cBusHandle* handle) {
|
uint16_t bq27220_get_voltage(FuriHalI2cBusHandle* handle) {
|
||||||
@ -226,24 +526,30 @@ int16_t bq27220_get_current(FuriHalI2cBusHandle* handle) {
|
|||||||
return bq27220_read_word(handle, CommandCurrent);
|
return bq27220_read_word(handle, CommandCurrent);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool bq27220_get_battery_status(FuriHalI2cBusHandle* handle, BatteryStatus* battery_status) {
|
bool bq27220_get_control_status(FuriHalI2cBusHandle* handle, Bq27220ControlStatus* control_status) {
|
||||||
uint16_t data = bq27220_read_word(handle, CommandBatteryStatus);
|
return bq27220_read_reg(handle, CommandControl, (uint8_t*)control_status, 2);
|
||||||
if(data == BQ27220_ERROR) {
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
*(uint16_t*)battery_status = data;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool bq27220_get_operation_status(FuriHalI2cBusHandle* handle, OperationStatus* operation_status) {
|
bool bq27220_get_battery_status(FuriHalI2cBusHandle* handle, Bq27220BatteryStatus* battery_status) {
|
||||||
uint16_t data = bq27220_read_word(handle, CommandOperationStatus);
|
return bq27220_read_reg(handle, CommandBatteryStatus, (uint8_t*)battery_status, 2);
|
||||||
if(data == BQ27220_ERROR) {
|
}
|
||||||
|
|
||||||
|
bool bq27220_get_operation_status(
|
||||||
|
FuriHalI2cBusHandle* handle,
|
||||||
|
Bq27220OperationStatus* operation_status) {
|
||||||
|
return bq27220_read_reg(handle, CommandOperationStatus, (uint8_t*)operation_status, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool bq27220_get_gauging_status(FuriHalI2cBusHandle* handle, Bq27220GaugingStatus* gauging_status) {
|
||||||
|
// Request gauging data to be loaded to MAC
|
||||||
|
if(!bq27220_control(handle, Control_GAUGING_STATUS)) {
|
||||||
|
FURI_LOG_E(TAG, "DM SelectSubclass for read failed");
|
||||||
return false;
|
return false;
|
||||||
} else {
|
|
||||||
*(uint16_t*)operation_status = data;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
// Wait for data being loaded to MAC
|
||||||
|
furi_delay_us(BQ27220_SELECT_DELAY_US);
|
||||||
|
// Read id data from MAC scratch space
|
||||||
|
return bq27220_read_reg(handle, CommandMACData, (uint8_t*)gauging_status, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t bq27220_get_temperature(FuriHalI2cBusHandle* handle) {
|
uint16_t bq27220_get_temperature(FuriHalI2cBusHandle* handle) {
|
||||||
|
@ -1,3 +1,31 @@
|
|||||||
|
/**
|
||||||
|
* @file bq27220.h
|
||||||
|
*
|
||||||
|
* Quite problematic chip with quite bad documentation.
|
||||||
|
*
|
||||||
|
* Couple things to keep in mind:
|
||||||
|
*
|
||||||
|
* - Datasheet and technical reference manual are full of bullshit
|
||||||
|
* - bqstudio is ignoring them
|
||||||
|
* - bqstudio i2c exchange tracing gives some ideas on timings that works, but there is a catch
|
||||||
|
* - bqstudio timings contradicts to gm.fs file specification
|
||||||
|
* - it's impossible to reproduce all situations in bqstudio
|
||||||
|
* - experiments with blackbox can not cover all edge cases
|
||||||
|
* - final timings are kinda blend between all of those sources
|
||||||
|
* - device behavior differs depending on i2c clock speed
|
||||||
|
* - The Hero Himmel would not have used this gauge in the first place
|
||||||
|
*
|
||||||
|
* Couple advises if you'll need to modify this driver:
|
||||||
|
* - Reset and wait for INITCOMP if something is not right.
|
||||||
|
* - Do not do partial config update, it takes unpredictable amount of time to apply.
|
||||||
|
* - Don't forget to reset chip before writing new config.
|
||||||
|
* - If something fails at config update stage, wait for 4 seconds before doing next cycle.
|
||||||
|
* - If you can program and lock chip at factory stage - do it. It will save you a lot of time.
|
||||||
|
* - Keep sealed or strange things may happen.
|
||||||
|
* - There is a condition when it may stuck at INITCOMP state, just "press reset button".
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
@ -9,26 +37,45 @@
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
// Low byte, Low bit first
|
// Low byte, Low bit first
|
||||||
bool DSG : 1; // The device is in DISCHARGE
|
uint8_t BATT_ID : 3; /**< Battery Identification */
|
||||||
bool SYSDWN : 1; // System down bit indicating the system should shut down
|
bool SNOOZE : 1; /**< SNOOZE mode is enabled */
|
||||||
bool TDA : 1; // Terminate Discharge Alarm
|
bool BCA : 1; /**< fuel gauge board calibration routine is active */
|
||||||
bool BATTPRES : 1; // Battery Present detected
|
bool CCA : 1; /**< Coulomb Counter Calibration routine is active */
|
||||||
bool AUTH_GD : 1; // Detect inserted battery
|
uint8_t RSVD0 : 2; /**< Reserved */
|
||||||
bool OCVGD : 1; // Good OCV measurement taken
|
|
||||||
bool TCA : 1; // Terminate Charge Alarm
|
|
||||||
bool RSVD : 1; // Reserved
|
|
||||||
// High byte, Low bit first
|
// High byte, Low bit first
|
||||||
bool CHGINH : 1; // Charge inhibit
|
uint8_t RSVD1; /**< Reserved */
|
||||||
bool FC : 1; // Full-charged is detected
|
} Bq27220ControlStatus;
|
||||||
bool OTD : 1; // Overtemperature in discharge condition is detected
|
|
||||||
bool OTC : 1; // Overtemperature in charge condition is detected
|
|
||||||
bool SLEEP : 1; // Device is operating in SLEEP mode when set
|
|
||||||
bool OCVFAIL : 1; // Status bit indicating that the OCV reading failed due to current
|
|
||||||
bool OCVCOMP : 1; // An OCV measurement update is complete
|
|
||||||
bool FD : 1; // Full-discharge is detected
|
|
||||||
} BatteryStatus;
|
|
||||||
|
|
||||||
_Static_assert(sizeof(BatteryStatus) == 2, "Incorrect structure size");
|
_Static_assert(sizeof(Bq27220ControlStatus) == 2, "Incorrect Bq27220ControlStatus structure size");
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
// Low byte, Low bit first
|
||||||
|
bool DSG : 1; /**< The device is in DISCHARGE */
|
||||||
|
bool SYSDWN : 1; /**< System down bit indicating the system should shut down */
|
||||||
|
bool TDA : 1; /**< Terminate Discharge Alarm */
|
||||||
|
bool BATTPRES : 1; /**< Battery Present detected */
|
||||||
|
bool AUTH_GD : 1; /**< Detect inserted battery */
|
||||||
|
bool OCVGD : 1; /**< Good OCV measurement taken */
|
||||||
|
bool TCA : 1; /**< Terminate Charge Alarm */
|
||||||
|
bool RSVD : 1; /**< Reserved */
|
||||||
|
// High byte, Low bit first
|
||||||
|
bool CHGINH : 1; /**< Charge inhibit */
|
||||||
|
bool FC : 1; /**< Full-charged is detected */
|
||||||
|
bool OTD : 1; /**< Overtemperature in discharge condition is detected */
|
||||||
|
bool OTC : 1; /**< Overtemperature in charge condition is detected */
|
||||||
|
bool SLEEP : 1; /**< Device is operating in SLEEP mode when set */
|
||||||
|
bool OCVFAIL : 1; /**< Status bit indicating that the OCV reading failed due to current */
|
||||||
|
bool OCVCOMP : 1; /**< An OCV measurement update is complete */
|
||||||
|
bool FD : 1; /**< Full-discharge is detected */
|
||||||
|
} Bq27220BatteryStatus;
|
||||||
|
|
||||||
|
_Static_assert(sizeof(Bq27220BatteryStatus) == 2, "Incorrect Bq27220BatteryStatus structure size");
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
Bq27220OperationStatusSecSealed = 0b11,
|
||||||
|
Bq27220OperationStatusSecUnsealed = 0b10,
|
||||||
|
Bq27220OperationStatusSecFull = 0b01,
|
||||||
|
} Bq27220OperationStatusSec;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
// Low byte, Low bit first
|
// Low byte, Low bit first
|
||||||
@ -40,53 +87,189 @@ typedef struct {
|
|||||||
bool SMTH : 1; /**< RemainingCapacity is scaled by smooth engine */
|
bool SMTH : 1; /**< RemainingCapacity is scaled by smooth engine */
|
||||||
bool BTPINT : 1; /**< BTP threshold has been crossed */
|
bool BTPINT : 1; /**< BTP threshold has been crossed */
|
||||||
// High byte, Low bit first
|
// High byte, Low bit first
|
||||||
uint8_t RSVD1 : 2;
|
uint8_t RSVD1 : 2; /**< Reserved */
|
||||||
bool CFGUPDATE : 1; /**< Gauge is in CONFIG UPDATE mode */
|
bool CFGUPDATE : 1; /**< Gauge is in CONFIG UPDATE mode */
|
||||||
uint8_t RSVD0 : 5;
|
uint8_t RSVD0 : 5; /**< Reserved */
|
||||||
} OperationStatus;
|
} Bq27220OperationStatus;
|
||||||
|
|
||||||
_Static_assert(sizeof(OperationStatus) == 2, "Incorrect structure size");
|
_Static_assert(
|
||||||
|
sizeof(Bq27220OperationStatus) == 2,
|
||||||
|
"Incorrect Bq27220OperationStatus structure size");
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
// Low byte, Low bit first
|
||||||
|
bool FD : 1; /**< Full Discharge */
|
||||||
|
bool FC : 1; /**< Full Charge */
|
||||||
|
bool TD : 1; /**< Terminate Discharge */
|
||||||
|
bool TC : 1; /**< Terminate Charge */
|
||||||
|
bool RSVD0 : 1; /**< Reserved */
|
||||||
|
bool EDV : 1; /**< Cell voltage is above or below EDV0 threshold */
|
||||||
|
bool DSG : 1; /**< DISCHARGE or RELAXATION */
|
||||||
|
bool CF : 1; /**< Battery conditioning is needed */
|
||||||
|
// High byte, Low bit first
|
||||||
|
uint8_t RSVD1 : 2; /**< Reserved */
|
||||||
|
bool FCCX : 1; /**< fcc1hz clock going into CC: 0 = 1 Hz, 1 = 16 Hz*/
|
||||||
|
uint8_t RSVD2 : 2; /**< Reserved */
|
||||||
|
bool EDV1 : 1; /**< Cell voltage is above or below EDV1 threshold */
|
||||||
|
bool EDV2 : 1; /**< Cell voltage is above or below EDV2 threshold */
|
||||||
|
bool VDQ : 1; /**< Charge cycle FCC update qualification */
|
||||||
|
} Bq27220GaugingStatus;
|
||||||
|
|
||||||
|
_Static_assert(sizeof(Bq27220GaugingStatus) == 2, "Incorrect Bq27220GaugingStatus structure size");
|
||||||
|
|
||||||
typedef struct BQ27220DMData BQ27220DMData;
|
typedef struct BQ27220DMData BQ27220DMData;
|
||||||
|
|
||||||
/** Initialize Driver
|
/** Initialize Driver
|
||||||
* @return true on success, false otherwise
|
*
|
||||||
|
* This routine performs a lot of things under the hood:
|
||||||
|
* - Verifies that gauge is present on i2c bus and got correct ID(0220)
|
||||||
|
* - Unseals gauge
|
||||||
|
* - Checks various internal statuses
|
||||||
|
* - Checks that current profile is 0
|
||||||
|
* - Checks configuration again provided data_memory
|
||||||
|
* - Reset gauge if something on previous stages was fishy
|
||||||
|
* - Updates configuration if needed
|
||||||
|
* - Sealing gauge to prevent configuration and state from accidental damage
|
||||||
|
*
|
||||||
|
* @param handle The I2C Bus handle
|
||||||
|
* @param[in] data_memory The data memory to be uploaded into gauge
|
||||||
|
*
|
||||||
|
* @return true on success, false otherwise
|
||||||
*/
|
*/
|
||||||
bool bq27220_init(FuriHalI2cBusHandle* handle);
|
bool bq27220_init(FuriHalI2cBusHandle* handle, const BQ27220DMData* data_memory);
|
||||||
|
|
||||||
/** Initialize Driver
|
/** Reset gauge
|
||||||
* @return true on success, false otherwise
|
*
|
||||||
|
* @param handle The I2C Bus handle
|
||||||
|
*
|
||||||
|
* @return true on success, false otherwise
|
||||||
*/
|
*/
|
||||||
bool bq27220_apply_data_memory(FuriHalI2cBusHandle* handle, const BQ27220DMData* data_memory);
|
bool bq27220_reset(FuriHalI2cBusHandle* handle);
|
||||||
|
|
||||||
/** Get battery voltage in mV or error */
|
/** Seal gauge access
|
||||||
|
*
|
||||||
|
* @param handle The I2C Bus handle
|
||||||
|
*
|
||||||
|
* @return true on success, false otherwise
|
||||||
|
*/
|
||||||
|
bool bq27220_seal(FuriHalI2cBusHandle* handle);
|
||||||
|
|
||||||
|
/** Unseal gauge access
|
||||||
|
*
|
||||||
|
* @param handle The I2C Bus handle
|
||||||
|
*
|
||||||
|
* @return true on success, false otherwise
|
||||||
|
*/
|
||||||
|
bool bq27220_unseal(FuriHalI2cBusHandle* handle);
|
||||||
|
|
||||||
|
/** Get full access
|
||||||
|
*
|
||||||
|
* @warning must be done in unsealed state
|
||||||
|
*
|
||||||
|
* @param handle The I2C Bus handle
|
||||||
|
*
|
||||||
|
* @return true on success, false otherwise
|
||||||
|
*/
|
||||||
|
bool bq27220_full_access(FuriHalI2cBusHandle* handle);
|
||||||
|
|
||||||
|
/** Get battery voltage
|
||||||
|
*
|
||||||
|
* @param handle The I2C Bus handle
|
||||||
|
*
|
||||||
|
* @return voltage in mV or BQ27220_ERROR
|
||||||
|
*/
|
||||||
uint16_t bq27220_get_voltage(FuriHalI2cBusHandle* handle);
|
uint16_t bq27220_get_voltage(FuriHalI2cBusHandle* handle);
|
||||||
|
|
||||||
/** Get current in mA or error*/
|
/** Get current
|
||||||
|
*
|
||||||
|
* @param handle The I2C Bus handle
|
||||||
|
*
|
||||||
|
* @return current in mA or BQ27220_ERROR
|
||||||
|
*/
|
||||||
int16_t bq27220_get_current(FuriHalI2cBusHandle* handle);
|
int16_t bq27220_get_current(FuriHalI2cBusHandle* handle);
|
||||||
|
|
||||||
/** Get battery status */
|
/** Get control status
|
||||||
bool bq27220_get_battery_status(FuriHalI2cBusHandle* handle, BatteryStatus* battery_status);
|
*
|
||||||
|
* @param handle The handle
|
||||||
|
* @param control_status The control status
|
||||||
|
*
|
||||||
|
* @return true on success, false otherwise
|
||||||
|
*/
|
||||||
|
bool bq27220_get_control_status(FuriHalI2cBusHandle* handle, Bq27220ControlStatus* control_status);
|
||||||
|
|
||||||
/** Get operation status */
|
/** Get battery status
|
||||||
bool bq27220_get_operation_status(FuriHalI2cBusHandle* handle, OperationStatus* operation_status);
|
*
|
||||||
|
* @param handle The handle
|
||||||
|
* @param battery_status The battery status
|
||||||
|
*
|
||||||
|
* @return true on success, false otherwise
|
||||||
|
*/
|
||||||
|
bool bq27220_get_battery_status(FuriHalI2cBusHandle* handle, Bq27220BatteryStatus* battery_status);
|
||||||
|
|
||||||
/** Get temperature in units of 0.1°K */
|
/** Get operation status
|
||||||
|
*
|
||||||
|
* @param handle The handle
|
||||||
|
* @param operation_status The operation status
|
||||||
|
*
|
||||||
|
* @return true on success, false otherwise
|
||||||
|
*/
|
||||||
|
bool bq27220_get_operation_status(
|
||||||
|
FuriHalI2cBusHandle* handle,
|
||||||
|
Bq27220OperationStatus* operation_status);
|
||||||
|
|
||||||
|
/** Get gauging status
|
||||||
|
*
|
||||||
|
* @param handle The handle
|
||||||
|
* @param gauging_status The gauging status
|
||||||
|
*
|
||||||
|
* @return true on success, false otherwise
|
||||||
|
*/
|
||||||
|
bool bq27220_get_gauging_status(FuriHalI2cBusHandle* handle, Bq27220GaugingStatus* gauging_status);
|
||||||
|
|
||||||
|
/** Get temperature
|
||||||
|
*
|
||||||
|
* @param handle The I2C Bus handle
|
||||||
|
*
|
||||||
|
* @return temperature in units of 0.1°K
|
||||||
|
*/
|
||||||
uint16_t bq27220_get_temperature(FuriHalI2cBusHandle* handle);
|
uint16_t bq27220_get_temperature(FuriHalI2cBusHandle* handle);
|
||||||
|
|
||||||
/** Get compensated full charge capacity in in mAh */
|
/** Get compensated full charge capacity
|
||||||
|
*
|
||||||
|
* @param handle The I2C Bus handle
|
||||||
|
*
|
||||||
|
* @return full charge capacity in mAh or BQ27220_ERROR
|
||||||
|
*/
|
||||||
uint16_t bq27220_get_full_charge_capacity(FuriHalI2cBusHandle* handle);
|
uint16_t bq27220_get_full_charge_capacity(FuriHalI2cBusHandle* handle);
|
||||||
|
|
||||||
/** Get design capacity in mAh */
|
/** Get design capacity
|
||||||
|
*
|
||||||
|
* @param handle The I2C Bus handle
|
||||||
|
*
|
||||||
|
* @return design capacity in mAh or BQ27220_ERROR
|
||||||
|
*/
|
||||||
uint16_t bq27220_get_design_capacity(FuriHalI2cBusHandle* handle);
|
uint16_t bq27220_get_design_capacity(FuriHalI2cBusHandle* handle);
|
||||||
|
|
||||||
/** Get remaining capacity in in mAh */
|
/** Get remaining capacity
|
||||||
|
*
|
||||||
|
* @param handle The I2C Bus handle
|
||||||
|
*
|
||||||
|
* @return remaining capacity in mAh or BQ27220_ERROR
|
||||||
|
*/
|
||||||
uint16_t bq27220_get_remaining_capacity(FuriHalI2cBusHandle* handle);
|
uint16_t bq27220_get_remaining_capacity(FuriHalI2cBusHandle* handle);
|
||||||
|
|
||||||
/** Get predicted remaining battery capacity in percents */
|
/** Get predicted remaining battery capacity
|
||||||
|
*
|
||||||
|
* @param handle The I2C Bus handle
|
||||||
|
*
|
||||||
|
* @return state of charge in percents or BQ27220_ERROR
|
||||||
|
*/
|
||||||
uint16_t bq27220_get_state_of_charge(FuriHalI2cBusHandle* handle);
|
uint16_t bq27220_get_state_of_charge(FuriHalI2cBusHandle* handle);
|
||||||
|
|
||||||
/** Get ratio of full charge capacity over design capacity in percents */
|
/** Get ratio of full charge capacity over design capacity
|
||||||
|
*
|
||||||
|
* @param handle The I2C Bus handle
|
||||||
|
*
|
||||||
|
* @return state of health in percents or BQ27220_ERROR
|
||||||
|
*/
|
||||||
uint16_t bq27220_get_state_of_health(FuriHalI2cBusHandle* handle);
|
uint16_t bq27220_get_state_of_health(FuriHalI2cBusHandle* handle);
|
||||||
|
|
||||||
void bq27220_change_design_capacity(FuriHalI2cBusHandle* handle, uint16_t capacity);
|
|
||||||
|
@ -82,3 +82,5 @@ typedef struct {
|
|||||||
const bool SME0 : 1;
|
const bool SME0 : 1;
|
||||||
const uint8_t RSVD3 : 3;
|
const uint8_t RSVD3 : 3;
|
||||||
} BQ27220DMGaugingConfig;
|
} BQ27220DMGaugingConfig;
|
||||||
|
|
||||||
|
_Static_assert(sizeof(BQ27220DMGaugingConfig) == 2, "Incorrect structure size");
|
||||||
|
@ -1,68 +1,76 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#define BQ27220_ADDRESS 0xAA
|
#define BQ27220_ADDRESS (0xAAu)
|
||||||
#define BQ27220_I2C_TIMEOUT 50
|
#define BQ27220_I2C_TIMEOUT (50u)
|
||||||
|
|
||||||
#define CommandControl 0x00
|
#define CommandControl (0x00u)
|
||||||
#define CommandAtRate 0x02
|
#define CommandAtRate (0x02u)
|
||||||
#define CommandAtRateTimeToEmpty 0x04
|
#define CommandAtRateTimeToEmpty (0x04u)
|
||||||
#define CommandTemperature 0x06
|
#define CommandTemperature (0x06u)
|
||||||
#define CommandVoltage 0x08
|
#define CommandVoltage (0x08u)
|
||||||
#define CommandBatteryStatus 0x0A
|
#define CommandBatteryStatus (0x0Au)
|
||||||
#define CommandCurrent 0x0C
|
#define CommandCurrent (0x0Cu)
|
||||||
#define CommandRemainingCapacity 0x10
|
#define CommandRemainingCapacity (0x10u)
|
||||||
#define CommandFullChargeCapacity 0x12
|
#define CommandFullChargeCapacity (0x12u)
|
||||||
#define CommandAverageCurrent 0x14
|
#define CommandAverageCurrent (0x14u)
|
||||||
#define CommandTimeToEmpty 0x16
|
#define CommandTimeToEmpty (0x16u)
|
||||||
#define CommandTimeToFull 0x18
|
#define CommandTimeToFull (0x18u)
|
||||||
#define CommandStandbyCurrent 0x1A
|
#define CommandStandbyCurrent (0x1Au)
|
||||||
#define CommandStandbyTimeToEmpty 0x1C
|
#define CommandStandbyTimeToEmpty (0x1Cu)
|
||||||
#define CommandMaxLoadCurrent 0x1E
|
#define CommandMaxLoadCurrent (0x1Eu)
|
||||||
#define CommandMaxLoadTimeToEmpty 0x20
|
#define CommandMaxLoadTimeToEmpty (0x20u)
|
||||||
#define CommandRawCoulombCount 0x22
|
#define CommandRawCoulombCount (0x22u)
|
||||||
#define CommandAveragePower 0x24
|
#define CommandAveragePower (0x24u)
|
||||||
#define CommandInternalTemperature 0x28
|
#define CommandInternalTemperature (0x28u)
|
||||||
#define CommandCycleCount 0x2A
|
#define CommandCycleCount (0x2Au)
|
||||||
#define CommandStateOfCharge 0x2C
|
#define CommandStateOfCharge (0x2Cu)
|
||||||
#define CommandStateOfHealth 0x2E
|
#define CommandStateOfHealth (0x2Eu)
|
||||||
#define CommandChargeVoltage 0x30
|
#define CommandChargeVoltage (0x30u)
|
||||||
#define CommandChargeCurrent 0x32
|
#define CommandChargeCurrent (0x32u)
|
||||||
#define CommandBTPDischargeSet 0x34
|
#define CommandBTPDischargeSet (0x34u)
|
||||||
#define CommandBTPChargeSet 0x36
|
#define CommandBTPChargeSet (0x36u)
|
||||||
#define CommandOperationStatus 0x3A
|
#define CommandOperationStatus (0x3Au)
|
||||||
#define CommandDesignCapacity 0x3C
|
#define CommandDesignCapacity (0x3Cu)
|
||||||
#define CommandSelectSubclass 0x3E
|
#define CommandSelectSubclass (0x3Eu)
|
||||||
#define CommandMACData 0x40
|
#define CommandMACData (0x40u)
|
||||||
#define CommandMACDataSum 0x60
|
#define CommandMACDataSum (0x60u)
|
||||||
#define CommandMACDataLen 0x61
|
#define CommandMACDataLen (0x61u)
|
||||||
#define CommandAnalogCount 0x79
|
#define CommandAnalogCount (0x79u)
|
||||||
#define CommandRawCurrent 0x7A
|
#define CommandRawCurrent (0x7Au)
|
||||||
#define CommandRawVoltage 0x7C
|
#define CommandRawVoltage (0x7Cu)
|
||||||
#define CommandRawIntTemp 0x7E
|
#define CommandRawIntTemp (0x7Eu)
|
||||||
|
|
||||||
#define Control_CONTROL_STATUS 0x0000
|
#define Control_CONTROL_STATUS (0x0000u)
|
||||||
#define Control_DEVICE_NUMBER 0x0001
|
#define Control_DEVICE_NUMBER (0x0001u)
|
||||||
#define Control_FW_VERSION 0x0002
|
#define Control_FW_VERSION (0x0002u)
|
||||||
#define Control_BOARD_OFFSET 0x0009
|
#define Control_HW_VERSION (0x0003u)
|
||||||
#define Control_CC_OFFSET 0x000A
|
#define Control_BOARD_OFFSET (0x0009u)
|
||||||
#define Control_CC_OFFSET_SAVE 0x000B
|
#define Control_CC_OFFSET (0x000Au)
|
||||||
#define Control_OCV_CMD 0x000C
|
#define Control_CC_OFFSET_SAVE (0x000Bu)
|
||||||
#define Control_BAT_INSERT 0x000D
|
#define Control_OCV_CMD (0x000Cu)
|
||||||
#define Control_BAT_REMOVE 0x000E
|
#define Control_BAT_INSERT (0x000Du)
|
||||||
#define Control_SET_SNOOZE 0x0013
|
#define Control_BAT_REMOVE (0x000Eu)
|
||||||
#define Control_CLEAR_SNOOZE 0x0014
|
#define Control_SET_SNOOZE (0x0013u)
|
||||||
#define Control_SET_PROFILE_1 0x0015
|
#define Control_CLEAR_SNOOZE (0x0014u)
|
||||||
#define Control_SET_PROFILE_2 0x0016
|
#define Control_SET_PROFILE_1 (0x0015u)
|
||||||
#define Control_SET_PROFILE_3 0x0017
|
#define Control_SET_PROFILE_2 (0x0016u)
|
||||||
#define Control_SET_PROFILE_4 0x0018
|
#define Control_SET_PROFILE_3 (0x0017u)
|
||||||
#define Control_SET_PROFILE_5 0x0019
|
#define Control_SET_PROFILE_4 (0x0018u)
|
||||||
#define Control_SET_PROFILE_6 0x001A
|
#define Control_SET_PROFILE_5 (0x0019u)
|
||||||
#define Control_CAL_TOGGLE 0x002D
|
#define Control_SET_PROFILE_6 (0x001Au)
|
||||||
#define Control_SEALED 0x0030
|
#define Control_CAL_TOGGLE (0x002Du)
|
||||||
#define Control_RESET 0x0041
|
#define Control_SEALED (0x0030u)
|
||||||
#define Control_EXIT_CAL 0x0080
|
#define Control_RESET (0x0041u)
|
||||||
#define Control_ENTER_CAL 0x0081
|
#define Control_OERATION_STATUS (0x0054u)
|
||||||
#define Control_ENTER_CFG_UPDATE 0x0090
|
#define Control_GAUGING_STATUS (0x0056u)
|
||||||
#define Control_EXIT_CFG_UPDATE_REINIT 0x0091
|
#define Control_EXIT_CAL (0x0080u)
|
||||||
#define Control_EXIT_CFG_UPDATE 0x0092
|
#define Control_ENTER_CAL (0x0081u)
|
||||||
#define Control_RETURN_TO_ROM 0x0F00
|
#define Control_ENTER_CFG_UPDATE (0x0090u)
|
||||||
|
#define Control_EXIT_CFG_UPDATE_REINIT (0x0091u)
|
||||||
|
#define Control_EXIT_CFG_UPDATE (0x0092u)
|
||||||
|
#define Control_RETURN_TO_ROM (0x0F00u)
|
||||||
|
|
||||||
|
#define UnsealKey1 (0x0414u)
|
||||||
|
#define UnsealKey2 (0x3672u)
|
||||||
|
|
||||||
|
#define FullAccessKey (0xffffu)
|
||||||
|
@ -47,7 +47,7 @@ def generate(env):
|
|||||||
PVSOPTIONS=[
|
PVSOPTIONS=[
|
||||||
"@.pvsoptions",
|
"@.pvsoptions",
|
||||||
"-j${PVSNCORES}",
|
"-j${PVSNCORES}",
|
||||||
"--disableLicenseExpirationCheck",
|
# "--disableLicenseExpirationCheck",
|
||||||
# "--incremental", # kinda broken on PVS side
|
# "--incremental", # kinda broken on PVS side
|
||||||
],
|
],
|
||||||
PVSCONVOPTIONS=[
|
PVSCONVOPTIONS=[
|
||||||
|
@ -73,18 +73,14 @@ void furi_hal_power_init(void) {
|
|||||||
// Find and init gauge
|
// Find and init gauge
|
||||||
size_t retry = 2;
|
size_t retry = 2;
|
||||||
while(retry > 0) {
|
while(retry > 0) {
|
||||||
furi_hal_power.gauge_ok = bq27220_init(&furi_hal_i2c_handle_power);
|
furi_hal_power.gauge_ok =
|
||||||
if(furi_hal_power.gauge_ok) {
|
bq27220_init(&furi_hal_i2c_handle_power, furi_hal_power_gauge_data_memory);
|
||||||
furi_hal_power.gauge_ok = bq27220_apply_data_memory(
|
|
||||||
&furi_hal_i2c_handle_power, furi_hal_power_gauge_data_memory);
|
|
||||||
}
|
|
||||||
if(furi_hal_power.gauge_ok) {
|
if(furi_hal_power.gauge_ok) {
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
// Normal startup time is 250ms
|
// Gauge need some time to think about it's behavior
|
||||||
// But if we try to access gauge at that stage it will become unresponsive
|
// We must wait, otherwise next init cycle will fail at unseal stage
|
||||||
// 2 seconds timeout needed to restart communication
|
furi_delay_us(4000000);
|
||||||
furi_delay_us(2020202);
|
|
||||||
}
|
}
|
||||||
retry--;
|
retry--;
|
||||||
}
|
}
|
||||||
@ -110,8 +106,8 @@ void furi_hal_power_init(void) {
|
|||||||
bool furi_hal_power_gauge_is_ok(void) {
|
bool furi_hal_power_gauge_is_ok(void) {
|
||||||
bool ret = true;
|
bool ret = true;
|
||||||
|
|
||||||
BatteryStatus battery_status;
|
Bq27220BatteryStatus battery_status;
|
||||||
OperationStatus operation_status;
|
Bq27220OperationStatus operation_status;
|
||||||
|
|
||||||
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
||||||
|
|
||||||
@ -132,7 +128,7 @@ bool furi_hal_power_gauge_is_ok(void) {
|
|||||||
bool furi_hal_power_is_shutdown_requested(void) {
|
bool furi_hal_power_is_shutdown_requested(void) {
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
|
|
||||||
BatteryStatus battery_status;
|
Bq27220BatteryStatus battery_status;
|
||||||
|
|
||||||
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
||||||
|
|
||||||
@ -593,8 +589,8 @@ void furi_hal_power_debug_get(PropertyValueCallback out, void* context) {
|
|||||||
PropertyValueContext property_context = {
|
PropertyValueContext property_context = {
|
||||||
.key = key, .value = value, .out = out, .sep = '.', .last = false, .context = context};
|
.key = key, .value = value, .out = out, .sep = '.', .last = false, .context = context};
|
||||||
|
|
||||||
BatteryStatus battery_status;
|
Bq27220BatteryStatus battery_status;
|
||||||
OperationStatus operation_status;
|
Bq27220OperationStatus operation_status;
|
||||||
|
|
||||||
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user