Merge remote-tracking branch 'OFW/dev' into dev

This commit is contained in:
MX 2024-10-07 04:47:24 +03:00
commit 306e34c64b
No known key found for this signature in database
GPG key ID: 7CCC66B7DBDD1C83
17 changed files with 941 additions and 1248 deletions

View file

@ -69,7 +69,6 @@ static const BadUsbHidApi hid_api_usb = {
.release_all = hid_usb_release_all,
.get_led_state = hid_usb_get_led_state,
};
const BadUsbHidApi* bad_usb_hid_get_interface(BadUsbHidInterface interface) {
UNUSED(interface);
const BadUsbHidApi* bad_usb_hid_get_interface() {
return &hid_api_usb;
}

View file

@ -7,10 +7,6 @@ extern "C" {
#include <furi.h>
#include <furi_hal.h>
typedef enum {
BadUsbHidInterfaceUsb,
} BadUsbHidInterface;
typedef struct {
void* (*init)(FuriHalUsbHidConfig* hid_cfg);
void (*deinit)(void* inst);
@ -25,7 +21,7 @@ typedef struct {
uint8_t (*get_led_state)(void* inst);
} BadUsbHidApi;
const BadUsbHidApi* bad_usb_hid_get_interface(BadUsbHidInterface interface);
const BadUsbHidApi* bad_usb_hid_get_interface();
void bad_usb_hid_ble_remove_pairing(void);

View file

@ -654,7 +654,7 @@ static void bad_usb_script_set_default_keyboard_layout(BadUsbScript* bad_usb) {
memcpy(bad_usb->layout, hid_asciimap, MIN(sizeof(hid_asciimap), sizeof(bad_usb->layout)));
}
BadUsbScript* bad_usb_script_open(FuriString* file_path, BadUsbHidInterface interface) {
BadUsbScript* bad_usb_script_open(FuriString* file_path) {
furi_assert(file_path);
BadUsbScript* bad_usb = malloc(sizeof(BadUsbScript));
@ -664,7 +664,7 @@ BadUsbScript* bad_usb_script_open(FuriString* file_path, BadUsbHidInterface inte
bad_usb->st.state = BadUsbStateInit;
bad_usb->st.error[0] = '\0';
bad_usb->hid = bad_usb_hid_get_interface(interface);
bad_usb->hid = bad_usb_hid_get_interface();
bad_usb->thread = furi_thread_alloc_ex("BadUsbWorker", 2048, bad_usb_worker, bad_usb);
furi_thread_start(bad_usb->thread);

View file

@ -34,7 +34,7 @@ typedef struct {
typedef struct BadUsbScript BadUsbScript;
BadUsbScript* bad_usb_script_open(FuriString* file_path, BadUsbHidInterface interface);
BadUsbScript* bad_usb_script_open(FuriString* file_path);
void bad_usb_script_close(BadUsbScript* bad_usb);

View file

@ -39,7 +39,7 @@ bool bad_usb_scene_work_on_event(void* context, SceneManagerEvent event) {
void bad_usb_scene_work_on_enter(void* context) {
BadUsbApp* app = context;
app->bad_usb_script = bad_usb_script_open(app->file_path, BadUsbHidInterfaceUsb);
app->bad_usb_script = bad_usb_script_open(app->file_path);
bad_usb_script_set_keyboard_layout(app->bad_usb_script, app->keyboard_layout);
FuriString* file_name;

View file

@ -2,6 +2,20 @@
#define TAG "Mosgortrans"
void render_section_header(
FuriString* str,
const char* name,
uint8_t prefix_separator_cnt,
uint8_t suffix_separator_cnt) {
for(uint8_t i = 0; i < prefix_separator_cnt; i++) {
furi_string_cat_printf(str, ":");
}
furi_string_cat_printf(str, "[ %s ]", name);
for(uint8_t i = 0; i < suffix_separator_cnt; i++) {
furi_string_cat_printf(str, ":");
}
}
void from_days_to_datetime(uint32_t days, DateTime* datetime, uint16_t start_year) {
uint32_t timestamp = days * 24 * 60 * 60;
DateTime start_datetime = {0};

View file

@ -10,6 +10,11 @@
extern "C" {
#endif
void render_section_header(
FuriString* str,
const char* name,
uint8_t prefix_separator_cnt,
uint8_t suffix_separator_cnt);
bool mosgortrans_parse_transport_block(const MfClassicBlock* block, FuriString* result);
#ifdef __cplusplus

View file

@ -15,4 +15,11 @@ static constexpr auto nfc_app_api_table = sort(create_array_t<sym_entry>(
API_METHOD(
mosgortrans_parse_transport_block,
bool,
(const MfClassicBlock* block, FuriString* result))));
(const MfClassicBlock* block, FuriString* result)),
API_METHOD(
render_section_header,
void,
(FuriString * str,
const char* name,
uint8_t prefix_separator_cnt,
uint8_t suffix_separator_cnt))));

View file

@ -1,12 +1,24 @@
#include "nfc_supported_card_plugin.h"
#include <flipper_application.h>
#include <flipper_application/flipper_application.h>
#include <nfc/nfc_device.h>
#include <bit_lib/bit_lib.h>
#include <datetime.h>
#include <nfc/protocols/mf_classic/mf_classic_poller_sync.h>
#include <bit_lib.h>
#define TAG "Plantain"
void from_minutes_to_datetime(uint32_t minutes, DateTime* datetime, uint16_t start_year) {
uint32_t timestamp = minutes * 60;
DateTime start_datetime = {0};
start_datetime.year = start_year - 1;
start_datetime.month = 12;
start_datetime.day = 31;
timestamp += datetime_datetime_to_timestamp(&start_datetime);
datetime_timestamp_to_datetime(timestamp, datetime);
}
typedef struct {
uint64_t a;
uint64_t b;
@ -207,29 +219,92 @@ static bool plantain_parse(const NfcDevice* device, FuriString* parsed_data) {
bit_lib_bytes_to_num_be(sec_tr->key_a.data, COUNT_OF(sec_tr->key_a.data));
if(key != cfg.keys[cfg.data_sector].a) break;
// Point to block 0 of sector 4, value 0
const uint8_t* temp_ptr = data->block[16].data;
// Read first 4 bytes of block 0 of sector 4 from last to first and convert them to uint32_t
// 38 18 00 00 becomes 00 00 18 38, and equals to 6200 decimal
uint32_t balance =
((temp_ptr[3] << 24) | (temp_ptr[2] << 16) | (temp_ptr[1] << 8) | temp_ptr[0]) / 100;
// Read card number
// Point to block 0 of sector 0, value 0
temp_ptr = data->block[0].data;
// Read first 7 bytes of block 0 of sector 0 from last to first and convert them to uint64_t
// 04 31 16 8A 23 5C 80 becomes 80 5C 23 8A 16 31 04, and equals to 36130104729284868 decimal
uint8_t card_number_arr[7];
for(size_t i = 0; i < 7; i++) {
card_number_arr[i] = temp_ptr[6 - i];
}
// Copy card number to uint64_t
furi_string_printf(parsed_data, "\e#Plantain card\n");
uint64_t card_number = 0;
for(size_t i = 0; i < 7; i++) {
card_number = (card_number << 8) | card_number_arr[i];
card_number = (card_number << 8) | data->block[0].data[6 - i];
}
furi_string_printf(
parsed_data, "\e#Plantain\nNo.: %lluX\nBalance: %lu\n", card_number, balance);
// Print card number with 4-digit groups
furi_string_cat_printf(parsed_data, "Number: ");
FuriString* card_number_s = furi_string_alloc();
furi_string_cat_printf(card_number_s, "%llu", card_number);
FuriString* tmp_s = furi_string_alloc_set_str("9643 3078 ");
for(uint8_t i = 0; i < 24; i += 4) {
for(uint8_t j = 0; j < 4; j++) {
furi_string_push_back(tmp_s, furi_string_get_char(card_number_s, i + j));
}
furi_string_push_back(tmp_s, ' ');
}
furi_string_cat_printf(parsed_data, "%s\n", furi_string_get_cstr(tmp_s));
if(data->type == MfClassicType1k) {
//balance
uint32_t balance = 0;
for(uint8_t i = 0; i < 4; i++) {
balance = (balance << 8) | data->block[16].data[3 - i];
}
furi_string_cat_printf(parsed_data, "Balance: %ld rub\n", balance / 100);
//trips
uint8_t trips_metro = data->block[21].data[0];
uint8_t trips_ground = data->block[21].data[1];
furi_string_cat_printf(parsed_data, "Trips: %d\n", trips_metro + trips_ground);
//trip time
uint32_t last_trip_timestamp = 0;
for(uint8_t i = 0; i < 3; i++) {
last_trip_timestamp = (last_trip_timestamp << 8) | data->block[21].data[4 - i];
}
DateTime last_trip = {0};
from_minutes_to_datetime(last_trip_timestamp + 24 * 60, &last_trip, 2010);
furi_string_cat_printf(
parsed_data,
"Trip start: %02d.%02d.%04d %02d:%02d\n",
last_trip.day,
last_trip.month,
last_trip.year,
last_trip.hour,
last_trip.minute);
//validator
uint16_t validator = (data->block[20].data[5] << 8) | data->block[20].data[4];
furi_string_cat_printf(parsed_data, "Validator: %d\n", validator);
//tariff
uint16_t fare = (data->block[20].data[7] << 8) | data->block[20].data[6];
furi_string_cat_printf(parsed_data, "Tariff: %d rub\n", fare / 100);
//trips in metro
furi_string_cat_printf(parsed_data, "Trips (Metro): %d\n", trips_metro);
//trips on ground
furi_string_cat_printf(parsed_data, "Trips (Ground): %d\n", trips_ground);
//last payment
uint32_t last_payment_timestamp = 0;
for(uint8_t i = 0; i < 3; i++) {
last_payment_timestamp = (last_payment_timestamp << 8) |
data->block[18].data[4 - i];
}
DateTime last_payment_date = {0};
from_minutes_to_datetime(last_payment_timestamp + 24 * 60, &last_payment_date, 2010);
furi_string_cat_printf(
parsed_data,
"Last pay: %02d.%02d.%04d %02d:%02d\n",
last_payment_date.day,
last_payment_date.month,
last_payment_date.year,
last_payment_date.hour,
last_payment_date.minute);
//payment summ
uint16_t last_payment = (data->block[18].data[9] << 8) | data->block[18].data[8];
furi_string_cat_printf(parsed_data, "Amount: %d rub", last_payment / 100);
furi_string_free(card_number_s);
furi_string_free(tmp_s);
} else if(data->type == MfClassicType4k) {
//trips
uint8_t trips_metro = data->block[36].data[0];
uint8_t trips_ground = data->block[36].data[1];
furi_string_cat_printf(parsed_data, "Trips: %d\n", trips_metro + trips_ground);
//trips in metro
furi_string_cat_printf(parsed_data, "Trips (Metro): %d\n", trips_metro);
//trips on ground
furi_string_cat_printf(parsed_data, "Trips (Ground): %d\n", trips_ground);
}
parsed = true;
} while(false);

File diff suppressed because it is too large Load diff

View file

@ -82,20 +82,6 @@ static const MfClassicKeyPair troika_4k_keys[] = {
{.a = 0xBB52F8CCE07F, .b = 0x6B6119752C70}, //40
};
static void troika_render_section_header(
FuriString* str,
const char* name,
uint8_t prefix_separator_cnt,
uint8_t suffix_separator_cnt) {
for(uint8_t i = 0; i < prefix_separator_cnt; i++) {
furi_string_cat_printf(str, ":");
}
furi_string_cat_printf(str, "[ %s ]", name);
for(uint8_t i = 0; i < suffix_separator_cnt; i++) {
furi_string_cat_printf(str, ":");
}
}
static bool troika_get_card_config(TroikaCardConfig* config, MfClassicType type) {
bool success = true;
@ -212,23 +198,25 @@ static bool troika_parse(const NfcDevice* device, FuriString* parsed_data) {
FuriString* ground_result = furi_string_alloc();
FuriString* tat_result = furi_string_alloc();
bool result1 = mosgortrans_parse_transport_block(&data->block[32], metro_result);
bool result2 = mosgortrans_parse_transport_block(&data->block[28], ground_result);
bool result3 = mosgortrans_parse_transport_block(&data->block[16], tat_result);
bool is_metro_data_present =
mosgortrans_parse_transport_block(&data->block[32], metro_result);
bool is_ground_data_present =
mosgortrans_parse_transport_block(&data->block[28], ground_result);
bool is_tat_data_present = mosgortrans_parse_transport_block(&data->block[16], tat_result);
furi_string_cat_printf(parsed_data, "\e#Troyka card\n");
if(result1 && !furi_string_empty(metro_result)) {
troika_render_section_header(parsed_data, "Metro", 22, 21);
if(is_metro_data_present && !furi_string_empty(metro_result)) {
render_section_header(parsed_data, "Metro", 22, 21);
furi_string_cat_printf(parsed_data, "%s\n", furi_string_get_cstr(metro_result));
}
if(result2 && !furi_string_empty(ground_result)) {
troika_render_section_header(parsed_data, "Ediny", 22, 22);
if(is_ground_data_present && !furi_string_empty(ground_result)) {
render_section_header(parsed_data, "Ediny", 22, 22);
furi_string_cat_printf(parsed_data, "%s\n", furi_string_get_cstr(ground_result));
}
if(result3 && !furi_string_empty(tat_result)) {
troika_render_section_header(parsed_data, "TAT", 24, 23);
if(is_tat_data_present && !furi_string_empty(tat_result)) {
render_section_header(parsed_data, "TAT", 24, 23);
furi_string_cat_printf(parsed_data, "%s\n", furi_string_get_cstr(tat_result));
}
@ -236,7 +224,7 @@ static bool troika_parse(const NfcDevice* device, FuriString* parsed_data) {
furi_string_free(ground_result);
furi_string_free(metro_result);
parsed = result1 || result2 || result3;
parsed = is_metro_data_present || is_ground_data_present || is_tat_data_present;
} while(false);
return parsed;

View file

@ -1,29 +1,77 @@
#include "bq27220.h"
#include "bq27220_reg.h"
#include "bq27220_data_memory.h"
_Static_assert(sizeof(BQ27220DMGaugingConfig) == 2, "Incorrect structure size");
#include <furi.h>
#include <stdbool.h>
#define TAG "Gauge"
static uint16_t bq27220_read_word(FuriHalI2cBusHandle* handle, uint8_t address) {
uint16_t buf = 0;
#define BQ27220_ID (0x0220u)
furi_hal_i2c_read_mem(
handle, BQ27220_ADDRESS, address, (uint8_t*)&buf, 2, BQ27220_I2C_TIMEOUT);
/** Delay between 2 writes into Subclass/MAC area. Fails at ~120us. */
#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) {
bool ret = furi_hal_i2c_write_mem(
handle, BQ27220_ADDRESS, CommandControl, (uint8_t*)&control, 2, BQ27220_I2C_TIMEOUT);
static inline bool bq27220_write(
FuriHalI2cBusHandle* handle,
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) {
@ -56,49 +104,49 @@ static bool bq27220_parameter_check(
if(update) {
// 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
// Also see note in the header
// 2. Write the address AND the parameter data to 0x3E+ (auto increment)
if(!furi_hal_i2c_write_mem(
handle,
BQ27220_ADDRESS,
CommandSelectSubclass,
buffer,
size + 2,
BQ27220_I2C_TIMEOUT)) {
FURI_LOG_I(TAG, "DM write failed");
// Write the address AND the parameter data to 0x3E+ (auto increment)
if(!bq27220_write(handle, CommandSelectSubclass, buffer, size + 2)) {
FURI_LOG_E(TAG, "DM write failed");
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);
// 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;
// 2 bytes address, `size` bytes data, 1 byte check sum, 1 byte length
buffer[1] = 2 + size + 1 + 1;
if(!furi_hal_i2c_write_mem(
handle, BQ27220_ADDRESS, CommandMACDataSum, buffer, 2, BQ27220_I2C_TIMEOUT)) {
FURI_LOG_I(TAG, "CRC write failed");
if(!bq27220_write(handle, CommandMACDataSum, buffer, 2)) {
FURI_LOG_E(TAG, "CRC write failed");
break;
}
furi_delay_us(10000);
// Final wait as in gm.fs specification
furi_delay_us(BQ27220_CONFIG_DELAY_US);
ret = true;
} else {
if(!furi_hal_i2c_write_mem(
handle, BQ27220_ADDRESS, CommandSelectSubclass, buffer, 2, BQ27220_I2C_TIMEOUT)) {
FURI_LOG_I(TAG, "DM SelectSubclass for read failed");
if(!bq27220_write(handle, CommandSelectSubclass, buffer, 2)) {
FURI_LOG_E(TAG, "DM SelectSubclass for read failed");
break;
}
if(!furi_hal_i2c_rx(handle, BQ27220_ADDRESS, old_data, size, BQ27220_I2C_TIMEOUT)) {
FURI_LOG_I(TAG, "DM read failed");
// bqstudio uses 15ms wait delay here
furi_delay_us(BQ27220_SELECT_DELAY_US);
if(!bq27220_read_reg(handle, CommandMACData, old_data, size)) {
FURI_LOG_E(TAG, "DM read failed");
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])) {
FURI_LOG_W( //-V641
FURI_LOG_E( //-V641
TAG,
"Data at 0x%04x(%zu): 0x%08lx!=0x%08lx",
address,
@ -119,22 +167,34 @@ static bool bq27220_data_memory_check(
const BQ27220DMData* data_memory,
bool 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");
return false;
};
// Wait for enter CFG update mode
uint32_t timeout = 100;
OperationStatus status = {0};
while((status.CFGUPDATE != true) && (timeout-- > 0)) {
bq27220_get_operation_status(handle, &status);
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) {
break;
};
furi_delay_us(BQ27220_TIMEOUT_CYCLE_INTERVAL_US);
}
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;
}
BQ27220_DEBUG_LOG("Cycles left: %lu", timeout);
}
// Process data memory records
@ -179,43 +239,283 @@ static bool bq27220_data_memory_check(
}
// Finalize configuration update
if(update) {
if(update && result) {
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;
}
bool bq27220_init(FuriHalI2cBusHandle* handle) {
bool bq27220_init(FuriHalI2cBusHandle* handle, const BQ27220DMData* data_memory) {
bool result = false;
bool reset_and_provisioning_required = false;
do {
// Request device number(chip PN)
BQ27220_DEBUG_LOG("Checking device ID");
if(!bq27220_control(handle, Control_DEVICE_NUMBER)) {
FURI_LOG_E(TAG, "Device is not present");
return false;
FURI_LOG_E(TAG, "ID: Device is not responding");
break;
};
// 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);
FURI_LOG_I(TAG, "Device Number %04x", data);
return data == 0x0220;
// 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;
}
bool bq27220_apply_data_memory(FuriHalI2cBusHandle* handle, const BQ27220DMData* data_memory) {
FURI_LOG_I(TAG, "Verifying data memory");
// 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_I(TAG, "Updating data memory");
bq27220_data_memory_check(handle, data_memory, true);
FURI_LOG_E(TAG, "Incorrect configuration data, reset needed");
reset_and_provisioning_required = true;
}
}
FURI_LOG_I(TAG, "Data memory verification complete");
return 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_reset(FuriHalI2cBusHandle* handle) {
bool result = false;
do {
if(!bq27220_control(handle, Control_RESET)) {
FURI_LOG_E(TAG, "Reset request failed");
break;
};
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) {
@ -226,24 +526,30 @@ int16_t bq27220_get_current(FuriHalI2cBusHandle* handle) {
return bq27220_read_word(handle, CommandCurrent);
}
bool bq27220_get_battery_status(FuriHalI2cBusHandle* handle, BatteryStatus* battery_status) {
uint16_t data = bq27220_read_word(handle, CommandBatteryStatus);
if(data == BQ27220_ERROR) {
return false;
} else {
*(uint16_t*)battery_status = data;
return true;
}
bool bq27220_get_control_status(FuriHalI2cBusHandle* handle, Bq27220ControlStatus* control_status) {
return bq27220_read_reg(handle, CommandControl, (uint8_t*)control_status, 2);
}
bool bq27220_get_operation_status(FuriHalI2cBusHandle* handle, OperationStatus* operation_status) {
uint16_t data = bq27220_read_word(handle, CommandOperationStatus);
if(data == BQ27220_ERROR) {
return false;
} else {
*(uint16_t*)operation_status = data;
return true;
bool bq27220_get_battery_status(FuriHalI2cBusHandle* handle, Bq27220BatteryStatus* battery_status) {
return bq27220_read_reg(handle, CommandBatteryStatus, (uint8_t*)battery_status, 2);
}
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;
}
// 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) {

View file

@ -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
#include <stdint.h>
@ -9,26 +37,45 @@
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
uint8_t BATT_ID : 3; /**< Battery Identification */
bool SNOOZE : 1; /**< SNOOZE mode is enabled */
bool BCA : 1; /**< fuel gauge board calibration routine is active */
bool CCA : 1; /**< Coulomb Counter Calibration routine is active */
uint8_t RSVD0 : 2; /**< 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
} BatteryStatus;
uint8_t RSVD1; /**< Reserved */
} Bq27220ControlStatus;
_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 {
// Low byte, Low bit first
@ -40,53 +87,189 @@ typedef struct {
bool SMTH : 1; /**< RemainingCapacity is scaled by smooth engine */
bool BTPINT : 1; /**< BTP threshold has been crossed */
// High byte, Low bit first
uint8_t RSVD1 : 2;
uint8_t RSVD1 : 2; /**< Reserved */
bool CFGUPDATE : 1; /**< Gauge is in CONFIG UPDATE mode */
uint8_t RSVD0 : 5;
} OperationStatus;
uint8_t RSVD0 : 5; /**< Reserved */
} 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;
/** Initialize Driver
*
* 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
*
* @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);
/** 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);
/** Get battery status */
bool bq27220_get_battery_status(FuriHalI2cBusHandle* handle, BatteryStatus* battery_status);
/** Get control 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 */
bool bq27220_get_operation_status(FuriHalI2cBusHandle* handle, OperationStatus* operation_status);
/** Get battery 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);
/** 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);
/** 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);
/** 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);
/** 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);
/** 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);
void bq27220_change_design_capacity(FuriHalI2cBusHandle* handle, uint16_t capacity);

View file

@ -82,3 +82,5 @@ typedef struct {
const bool SME0 : 1;
const uint8_t RSVD3 : 3;
} BQ27220DMGaugingConfig;
_Static_assert(sizeof(BQ27220DMGaugingConfig) == 2, "Incorrect structure size");

View file

@ -1,68 +1,76 @@
#pragma once
#define BQ27220_ADDRESS 0xAA
#define BQ27220_I2C_TIMEOUT 50
#define BQ27220_ADDRESS (0xAAu)
#define BQ27220_I2C_TIMEOUT (50u)
#define CommandControl 0x00
#define CommandAtRate 0x02
#define CommandAtRateTimeToEmpty 0x04
#define CommandTemperature 0x06
#define CommandVoltage 0x08
#define CommandBatteryStatus 0x0A
#define CommandCurrent 0x0C
#define CommandRemainingCapacity 0x10
#define CommandFullChargeCapacity 0x12
#define CommandAverageCurrent 0x14
#define CommandTimeToEmpty 0x16
#define CommandTimeToFull 0x18
#define CommandStandbyCurrent 0x1A
#define CommandStandbyTimeToEmpty 0x1C
#define CommandMaxLoadCurrent 0x1E
#define CommandMaxLoadTimeToEmpty 0x20
#define CommandRawCoulombCount 0x22
#define CommandAveragePower 0x24
#define CommandInternalTemperature 0x28
#define CommandCycleCount 0x2A
#define CommandStateOfCharge 0x2C
#define CommandStateOfHealth 0x2E
#define CommandChargeVoltage 0x30
#define CommandChargeCurrent 0x32
#define CommandBTPDischargeSet 0x34
#define CommandBTPChargeSet 0x36
#define CommandOperationStatus 0x3A
#define CommandDesignCapacity 0x3C
#define CommandSelectSubclass 0x3E
#define CommandMACData 0x40
#define CommandMACDataSum 0x60
#define CommandMACDataLen 0x61
#define CommandAnalogCount 0x79
#define CommandRawCurrent 0x7A
#define CommandRawVoltage 0x7C
#define CommandRawIntTemp 0x7E
#define CommandControl (0x00u)
#define CommandAtRate (0x02u)
#define CommandAtRateTimeToEmpty (0x04u)
#define CommandTemperature (0x06u)
#define CommandVoltage (0x08u)
#define CommandBatteryStatus (0x0Au)
#define CommandCurrent (0x0Cu)
#define CommandRemainingCapacity (0x10u)
#define CommandFullChargeCapacity (0x12u)
#define CommandAverageCurrent (0x14u)
#define CommandTimeToEmpty (0x16u)
#define CommandTimeToFull (0x18u)
#define CommandStandbyCurrent (0x1Au)
#define CommandStandbyTimeToEmpty (0x1Cu)
#define CommandMaxLoadCurrent (0x1Eu)
#define CommandMaxLoadTimeToEmpty (0x20u)
#define CommandRawCoulombCount (0x22u)
#define CommandAveragePower (0x24u)
#define CommandInternalTemperature (0x28u)
#define CommandCycleCount (0x2Au)
#define CommandStateOfCharge (0x2Cu)
#define CommandStateOfHealth (0x2Eu)
#define CommandChargeVoltage (0x30u)
#define CommandChargeCurrent (0x32u)
#define CommandBTPDischargeSet (0x34u)
#define CommandBTPChargeSet (0x36u)
#define CommandOperationStatus (0x3Au)
#define CommandDesignCapacity (0x3Cu)
#define CommandSelectSubclass (0x3Eu)
#define CommandMACData (0x40u)
#define CommandMACDataSum (0x60u)
#define CommandMACDataLen (0x61u)
#define CommandAnalogCount (0x79u)
#define CommandRawCurrent (0x7Au)
#define CommandRawVoltage (0x7Cu)
#define CommandRawIntTemp (0x7Eu)
#define Control_CONTROL_STATUS 0x0000
#define Control_DEVICE_NUMBER 0x0001
#define Control_FW_VERSION 0x0002
#define Control_BOARD_OFFSET 0x0009
#define Control_CC_OFFSET 0x000A
#define Control_CC_OFFSET_SAVE 0x000B
#define Control_OCV_CMD 0x000C
#define Control_BAT_INSERT 0x000D
#define Control_BAT_REMOVE 0x000E
#define Control_SET_SNOOZE 0x0013
#define Control_CLEAR_SNOOZE 0x0014
#define Control_SET_PROFILE_1 0x0015
#define Control_SET_PROFILE_2 0x0016
#define Control_SET_PROFILE_3 0x0017
#define Control_SET_PROFILE_4 0x0018
#define Control_SET_PROFILE_5 0x0019
#define Control_SET_PROFILE_6 0x001A
#define Control_CAL_TOGGLE 0x002D
#define Control_SEALED 0x0030
#define Control_RESET 0x0041
#define Control_EXIT_CAL 0x0080
#define Control_ENTER_CAL 0x0081
#define Control_ENTER_CFG_UPDATE 0x0090
#define Control_EXIT_CFG_UPDATE_REINIT 0x0091
#define Control_EXIT_CFG_UPDATE 0x0092
#define Control_RETURN_TO_ROM 0x0F00
#define Control_CONTROL_STATUS (0x0000u)
#define Control_DEVICE_NUMBER (0x0001u)
#define Control_FW_VERSION (0x0002u)
#define Control_HW_VERSION (0x0003u)
#define Control_BOARD_OFFSET (0x0009u)
#define Control_CC_OFFSET (0x000Au)
#define Control_CC_OFFSET_SAVE (0x000Bu)
#define Control_OCV_CMD (0x000Cu)
#define Control_BAT_INSERT (0x000Du)
#define Control_BAT_REMOVE (0x000Eu)
#define Control_SET_SNOOZE (0x0013u)
#define Control_CLEAR_SNOOZE (0x0014u)
#define Control_SET_PROFILE_1 (0x0015u)
#define Control_SET_PROFILE_2 (0x0016u)
#define Control_SET_PROFILE_3 (0x0017u)
#define Control_SET_PROFILE_4 (0x0018u)
#define Control_SET_PROFILE_5 (0x0019u)
#define Control_SET_PROFILE_6 (0x001Au)
#define Control_CAL_TOGGLE (0x002Du)
#define Control_SEALED (0x0030u)
#define Control_RESET (0x0041u)
#define Control_OERATION_STATUS (0x0054u)
#define Control_GAUGING_STATUS (0x0056u)
#define Control_EXIT_CAL (0x0080u)
#define Control_ENTER_CAL (0x0081u)
#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)

View file

@ -47,7 +47,7 @@ def generate(env):
PVSOPTIONS=[
"@.pvsoptions",
"-j${PVSNCORES}",
"--disableLicenseExpirationCheck",
# "--disableLicenseExpirationCheck",
# "--incremental", # kinda broken on PVS side
],
PVSCONVOPTIONS=[

View file

@ -73,18 +73,14 @@ void furi_hal_power_init(void) {
// Find and init gauge
size_t retry = 2;
while(retry > 0) {
furi_hal_power.gauge_ok = bq27220_init(&furi_hal_i2c_handle_power);
if(furi_hal_power.gauge_ok) {
furi_hal_power.gauge_ok = bq27220_apply_data_memory(
&furi_hal_i2c_handle_power, furi_hal_power_gauge_data_memory);
}
furi_hal_power.gauge_ok =
bq27220_init(&furi_hal_i2c_handle_power, furi_hal_power_gauge_data_memory);
if(furi_hal_power.gauge_ok) {
break;
} else {
// Normal startup time is 250ms
// But if we try to access gauge at that stage it will become unresponsive
// 2 seconds timeout needed to restart communication
furi_delay_us(2020202);
// Gauge need some time to think about it's behavior
// We must wait, otherwise next init cycle will fail at unseal stage
furi_delay_us(4000000);
}
retry--;
}
@ -110,8 +106,8 @@ void furi_hal_power_init(void) {
bool furi_hal_power_gauge_is_ok(void) {
bool ret = true;
BatteryStatus battery_status;
OperationStatus operation_status;
Bq27220BatteryStatus battery_status;
Bq27220OperationStatus operation_status;
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 ret = false;
BatteryStatus battery_status;
Bq27220BatteryStatus battery_status;
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 = {
.key = key, .value = value, .out = out, .sep = '.', .last = false, .context = context};
BatteryStatus battery_status;
OperationStatus operation_status;
Bq27220BatteryStatus battery_status;
Bq27220OperationStatus operation_status;
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);