From 9a257b048e65da34ede19c381f0d511c748e22a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=82=E3=81=8F?= Date: Tue, 22 Dec 2020 21:16:32 +0300 Subject: [PATCH] Power: more info (#274) --- applications/power/power.c | 48 +++++++++++++++---- .../targets/api-hal-include/api-hal-power.h | 23 +++++++-- firmware/targets/f4/api-hal/api-hal-power.c | 42 +++++++++++++--- 3 files changed, 93 insertions(+), 20 deletions(-) diff --git a/applications/power/power.c b/applications/power/power.c index 487a774e..740c30c3 100644 --- a/applications/power/power.c +++ b/applications/power/power.c @@ -23,9 +23,14 @@ struct Power { Cli* cli; MenuItem* menu; - float current; - float voltage; - float temperature; + float current_charger; + float current_gauge; + float voltage_charger; + float voltage_gauge; + uint32_t capacity_remaining; + uint32_t capacity_full; + float temperature_charger; + float temperature_gauge; uint8_t charge; }; @@ -71,20 +76,38 @@ void power_draw_callback(Canvas* canvas, void* context) { char buffer[64]; canvas_set_font(canvas, FontSecondary); - snprintf(buffer, 64, "Current: %ldmA", (int32_t)(power->current * 1000)); + snprintf( + buffer, + 64, + "Current: %ld/%ldmA", + (int32_t)(power->current_gauge * 1000), + (int32_t)(power->current_charger * 1000)); canvas_draw_str(canvas, 5, 22, buffer); - snprintf(buffer, 64, "Voltage: %ldmV", (uint32_t)(power->voltage * 1000)); + snprintf( + buffer, + 64, + "Voltage: %ld/%ldmV", + (uint32_t)(power->voltage_gauge * 1000), + (uint32_t)(power->voltage_charger * 1000)); canvas_draw_str(canvas, 5, 32, buffer); snprintf(buffer, 64, "Charge: %ld%%", (uint32_t)(power->charge)); canvas_draw_str(canvas, 5, 42, buffer); - snprintf(buffer, 64, "Temperature: %ldC", (uint32_t)(power->temperature)); + snprintf( + buffer, 64, "Capacity: %ld of %ldmAh", power->capacity_remaining, power->capacity_full); canvas_draw_str(canvas, 5, 52, buffer); + snprintf( + buffer, + 64, + "Temperature: %ld/%ldC", + (uint32_t)(power->temperature_gauge), + (uint32_t)(power->temperature_charger)); + canvas_draw_str(canvas, 5, 62, buffer); } void power_input_callback(InputEvent* event, void* context) { Power* power = context; - if(!event->state) return; + if(!event->state || event->input != InputBack) return; widget_enabled_set(power->widget, false); } @@ -199,9 +222,14 @@ void power_task(void* p) { while(1) { power->charge = api_hal_power_get_pct(); - power->current = api_hal_power_get_battery_current(); - power->voltage = api_hal_power_get_battery_voltage(); - power->temperature = api_hal_power_get_battery_temperature(); + power->capacity_remaining = api_hal_power_get_battery_remaining_capacity(); + power->capacity_full = api_hal_power_get_battery_full_capacity(); + power->current_charger = api_hal_power_get_battery_current(ApiHalPowerICCharger); + power->current_gauge = api_hal_power_get_battery_current(ApiHalPowerICFuelGauge); + power->voltage_charger = api_hal_power_get_battery_voltage(ApiHalPowerICCharger); + power->voltage_gauge = api_hal_power_get_battery_voltage(ApiHalPowerICFuelGauge); + power->temperature_charger = api_hal_power_get_battery_temperature(ApiHalPowerICCharger); + power->temperature_gauge = api_hal_power_get_battery_temperature(ApiHalPowerICFuelGauge); widget_update(power->widget); widget_enabled_set(power->usb_widget, api_hal_power_is_charging()); osDelay(1000); diff --git a/firmware/targets/api-hal-include/api-hal-power.h b/firmware/targets/api-hal-include/api-hal-power.h index 115102c9..2b55e979 100644 --- a/firmware/targets/api-hal-include/api-hal-power.h +++ b/firmware/targets/api-hal-include/api-hal-power.h @@ -8,6 +8,11 @@ extern "C" { #endif +typedef enum { + ApiHalPowerICCharger, + ApiHalPowerICFuelGauge, +} ApiHalPowerIC; + /* Initialize drivers */ void api_hal_power_init(); @@ -26,14 +31,26 @@ void api_hal_power_enable_otg(); /* OTG disable */ void api_hal_power_disable_otg(); +/* Get remaining battery battery capacity in mAh */ +uint32_t api_hal_power_get_battery_remaining_capacity(); + +/* Get full charge battery capacity in mAh */ +uint32_t api_hal_power_get_battery_full_capacity(); + /* Get battery voltage in V */ -float api_hal_power_get_battery_voltage(); +float api_hal_power_get_battery_voltage(ApiHalPowerIC ic); /* Get battery current in A */ -float api_hal_power_get_battery_current(); +float api_hal_power_get_battery_current(ApiHalPowerIC ic); /* Get temperature in C */ -float api_hal_power_get_battery_temperature(); +float api_hal_power_get_battery_temperature(ApiHalPowerIC ic); + +/* Get System voltage in V */ +float api_hal_power_get_system_voltage(); + +/* Get USB voltage in V */ +float api_hal_power_get_usb_voltage(); /* Get power system component state */ void api_hal_power_dump_state(string_t buffer); diff --git a/firmware/targets/f4/api-hal/api-hal-power.c b/firmware/targets/f4/api-hal/api-hal-power.c index de35858b..7d6cc745 100644 --- a/firmware/targets/f4/api-hal/api-hal-power.c +++ b/firmware/targets/f4/api-hal/api-hal-power.c @@ -34,16 +34,44 @@ void api_hal_power_disable_otg() { bq25896_disable_otg(); } -float api_hal_power_get_battery_voltage() { - return (float)bq27220_get_voltage() / 1000.0f; +uint32_t api_hal_power_get_battery_remaining_capacity() { + return bq27220_get_remaining_capacity(); } -float api_hal_power_get_battery_current() { - return (float)bq27220_get_current() / 1000.0f; +uint32_t api_hal_power_get_battery_full_capacity() { + return bq27220_get_full_charge_capacity(); } -float api_hal_power_get_battery_temperature() { - return ((float)bq27220_get_temperature() - 2731.0f) / 10.0f; +float api_hal_power_get_battery_voltage(ApiHalPowerIC ic) { + if (ic == ApiHalPowerICCharger) { + return (float)bq25896_get_vbat_voltage() / 1000.0f; + } else if (ic == ApiHalPowerICFuelGauge) { + return (float)bq27220_get_voltage() / 1000.0f; + } else { + return 0.0f; + } +} + +float api_hal_power_get_battery_current(ApiHalPowerIC ic) { + if (ic == ApiHalPowerICCharger) { + return (float)bq25896_get_vbat_current() / 1000.0f; + } else if (ic == ApiHalPowerICFuelGauge) { + return (float)bq27220_get_current() / 1000.0f; + } else { + return 0.0f; + } +} + +float api_hal_power_get_battery_temperature(ApiHalPowerIC ic) { + if (ic == ApiHalPowerICCharger) { + // Linear approximation, +/- 5 C + return (71.0f - (float)bq25896_get_ntc_mpct()/1000) / 0.6f; + } else if (ic == ApiHalPowerICFuelGauge) { + return ((float)bq27220_get_temperature() - 2731.0f) / 10.0f; + } else { + return 0.0f; + } + } void api_hal_power_dump_state(string_t buffer) { @@ -81,7 +109,7 @@ void api_hal_power_dump_state(string_t buffer) { ); string_cat_printf(buffer, "bq27220: Voltage: %dmV, Current: %dmA, Temperature: %dC\r\n", - bq27220_get_voltage(), bq27220_get_current(), (int)api_hal_power_get_battery_temperature() + bq27220_get_voltage(), bq27220_get_current(), (int)api_hal_power_get_battery_temperature(ApiHalPowerICFuelGauge) ); }