From cfa76f19d072b9c5fff390d1e7af309dcf9207ab Mon Sep 17 00:00:00 2001 From: gornekich <44112859+gornekich@users.noreply.github.com> Date: Tue, 2 Mar 2021 19:07:26 +0300 Subject: [PATCH] [FL-905] Add battery health state monitoring in power app (#358) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add battery health reading from gauge * add battery health reading to api-hal-power * update battery health in power application Co-authored-by: あく --- applications/power/power.c | 1 + applications/power/power_views.c | 7 ++++++- applications/power/power_views.h | 1 + firmware/targets/api-hal-include/api-hal-power.h | 3 +++ firmware/targets/f4/api-hal/api-hal-power.c | 8 ++++++-- firmware/targets/f5/api-hal/api-hal-power.c | 8 ++++++-- lib/drivers/bq27220.c | 4 ++++ lib/drivers/bq27220.h | 3 +++ 8 files changed, 30 insertions(+), 5 deletions(-) diff --git a/applications/power/power.c b/applications/power/power.c index 1250b141..0047c707 100644 --- a/applications/power/power.c +++ b/applications/power/power.c @@ -205,6 +205,7 @@ int32_t power_task(void* p) { with_view_model( power->info_view, (PowerInfoModel * model) { model->charge = api_hal_power_get_pct(); + model->health = api_hal_power_get_bat_health_pct(); model->capacity_remaining = api_hal_power_get_battery_remaining_capacity(); model->capacity_full = api_hal_power_get_battery_full_capacity(); model->current_charger = api_hal_power_get_battery_current(ApiHalPowerICCharger); diff --git a/applications/power/power_views.c b/applications/power/power_views.c index 1f0dfeb2..f48fa454 100644 --- a/applications/power/power_views.c +++ b/applications/power/power_views.c @@ -24,7 +24,12 @@ void power_info_draw_callback(Canvas* canvas, void* context) { (uint32_t)(data->voltage_gauge * 1000), (uint32_t)(data->voltage_charger * 1000)); canvas_draw_str(canvas, 5, 32, buffer); - snprintf(buffer, 64, "Charge: %ld%%", (uint32_t)(data->charge)); + snprintf( + buffer, + 64, + "Charge: %ld%% Health: %ld%%", + (uint32_t)(data->charge), + (uint32_t)(data->health)); canvas_draw_str(canvas, 5, 42, buffer); snprintf(buffer, 64, "Capacity: %ld of %ldmAh", data->capacity_remaining, data->capacity_full); canvas_draw_str(canvas, 5, 52, buffer); diff --git a/applications/power/power_views.h b/applications/power/power_views.h index c3325219..db2ac308 100644 --- a/applications/power/power_views.h +++ b/applications/power/power_views.h @@ -22,6 +22,7 @@ typedef struct { float temperature_gauge; uint8_t charge; + uint8_t health; } PowerInfoModel; void power_info_draw_callback(Canvas* canvas, void* context); diff --git a/firmware/targets/api-hal-include/api-hal-power.h b/firmware/targets/api-hal-include/api-hal-power.h index 1928342c..178270bc 100644 --- a/firmware/targets/api-hal-include/api-hal-power.h +++ b/firmware/targets/api-hal-include/api-hal-power.h @@ -44,6 +44,9 @@ void api_hal_power_deep_sleep(); /* Get predicted remaining battery capacity in percents */ uint8_t api_hal_power_get_pct(); +/* Get battery health state in percents */ +uint8_t api_hal_power_get_bat_health_pct(); + /* Get charging status */ bool api_hal_power_is_charging(); diff --git a/firmware/targets/f4/api-hal/api-hal-power.c b/firmware/targets/f4/api-hal/api-hal-power.c index d6d8bedb..d58bc1d5 100644 --- a/firmware/targets/f4/api-hal/api-hal-power.c +++ b/firmware/targets/f4/api-hal/api-hal-power.c @@ -90,6 +90,10 @@ uint8_t api_hal_power_get_pct() { return bq27220_get_state_of_charge(); } +uint8_t api_hal_power_get_bat_health_pct() { + return bq27220_get_state_of_health(); +} + bool api_hal_power_is_charging() { return bq25896_is_charging(); } @@ -175,9 +179,9 @@ void api_hal_power_dump_state(string_t buffer) { ); // Voltage and current info string_cat_printf(buffer, - "bq27220: Full capacity: %dmAh, Remaining capacity: %dmAh, State of Charge: %d%%\r\n", + "bq27220: Full capacity: %dmAh, Remaining capacity: %dmAh, State of Charge: %d%%, State of health: %d%%\r\n", bq27220_get_full_charge_capacity(), bq27220_get_remaining_capacity(), - bq27220_get_state_of_charge() + bq27220_get_state_of_charge(), bq27220_get_state_of_health() ); string_cat_printf(buffer, "bq27220: Voltage: %dmV, Current: %dmA, Temperature: %dC\r\n", diff --git a/firmware/targets/f5/api-hal/api-hal-power.c b/firmware/targets/f5/api-hal/api-hal-power.c index d6d8bedb..d58bc1d5 100644 --- a/firmware/targets/f5/api-hal/api-hal-power.c +++ b/firmware/targets/f5/api-hal/api-hal-power.c @@ -90,6 +90,10 @@ uint8_t api_hal_power_get_pct() { return bq27220_get_state_of_charge(); } +uint8_t api_hal_power_get_bat_health_pct() { + return bq27220_get_state_of_health(); +} + bool api_hal_power_is_charging() { return bq25896_is_charging(); } @@ -175,9 +179,9 @@ void api_hal_power_dump_state(string_t buffer) { ); // Voltage and current info string_cat_printf(buffer, - "bq27220: Full capacity: %dmAh, Remaining capacity: %dmAh, State of Charge: %d%%\r\n", + "bq27220: Full capacity: %dmAh, Remaining capacity: %dmAh, State of Charge: %d%%, State of health: %d%%\r\n", bq27220_get_full_charge_capacity(), bq27220_get_remaining_capacity(), - bq27220_get_state_of_charge() + bq27220_get_state_of_charge(), bq27220_get_state_of_health() ); string_cat_printf(buffer, "bq27220: Voltage: %dmV, Current: %dmA, Temperature: %dC\r\n", diff --git a/lib/drivers/bq27220.c b/lib/drivers/bq27220.c index bba7eaef..51d90a63 100644 --- a/lib/drivers/bq27220.c +++ b/lib/drivers/bq27220.c @@ -79,3 +79,7 @@ uint16_t bq27220_get_remaining_capacity() { uint16_t bq27220_get_state_of_charge() { return bq27220_read_word(CommandStateOfCharge); } + +uint16_t bq27220_get_state_of_health() { + return bq27220_read_word(CommandStateOfHealth); +} diff --git a/lib/drivers/bq27220.h b/lib/drivers/bq27220.h index f180e534..bf0f8de1 100644 --- a/lib/drivers/bq27220.h +++ b/lib/drivers/bq27220.h @@ -69,3 +69,6 @@ uint16_t bq27220_get_remaining_capacity(); /* Get predicted remaining battery capacity in percents */ uint16_t bq27220_get_state_of_charge(); + +/* Get ratio of full charge capacity over design capacity in percents */ +uint16_t bq27220_get_state_of_health();