Skip to content

Commit

Permalink
power: Also ask charger if charge done
Browse files Browse the repository at this point in the history
Also check the actual charge controller bq25896's CHRG_STAT to see if
charging is complete.  Don't only rely on the battery fuel gauge
bq27220 reaching 100% to indicate charging is finished.

This more accurately reflects the actual charging status and
simplifies optionally reducing the battery charging voltage limit.
Keeping the fuel gauge check ensures that dis/connecting from USB will
still show as fully charged at default settings (no charge limit),
since the bq25896 takes a few seconds to detect charging is complete.

This adds a new function to the API:
furi_hal_power_is_charging_done()

Helps with #1158.
  • Loading branch information
digitalcircuit committed Sep 19, 2022
1 parent f5ff643 commit ce78d3b
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion applications/services/power/power_service/power.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void power_free(Power* power) {

static void power_check_charging_state(Power* power) {
if(furi_hal_power_is_charging()) {
if(power->info.charge == 100) {
if((power->info.charge == 100) || (furi_hal_power_is_charging_done())) {
if(power->state != PowerStateCharged) {
notification_internal_message(power->notification, &sequence_charged);
power->state = PowerStateCharged;
Expand Down
3 changes: 2 additions & 1 deletion firmware/targets/f7/api_symbols.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
entry,status,name,type,params
Version,+,1.6,,
Version,+,1.7,,
Header,+,applications/services/bt/bt_service/bt.h,,
Header,+,applications/services/cli/cli.h,,
Header,+,applications/services/cli/cli_vcp.h,,
Expand Down Expand Up @@ -1141,6 +1141,7 @@ Function,+,furi_hal_power_insomnia_enter,void,
Function,+,furi_hal_power_insomnia_exit,void,
Function,-,furi_hal_power_insomnia_level,uint16_t,
Function,+,furi_hal_power_is_charging,_Bool,
Function,+,furi_hal_power_is_charging_done,_Bool,
Function,+,furi_hal_power_is_otg_enabled,_Bool,
Function,+,furi_hal_power_off,void,
Function,+,furi_hal_power_reset,void,
Expand Down
7 changes: 7 additions & 0 deletions firmware/targets/f7/furi_hal/furi_hal_power.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,13 @@ bool furi_hal_power_is_charging() {
return ret;
}

bool furi_hal_power_is_charging_done() {
furi_hal_i2c_acquire(&furi_hal_i2c_handle_power);
bool ret = bq25896_is_charging_done(&furi_hal_i2c_handle_power);
furi_hal_i2c_release(&furi_hal_i2c_handle_power);
return ret;
}

void furi_hal_power_shutdown() {
furi_hal_power_insomnia_enter();

Expand Down
6 changes: 6 additions & 0 deletions firmware/targets/furi_hal_include/furi_hal_power.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ uint8_t furi_hal_power_get_bat_health_pct();
*/
bool furi_hal_power_is_charging();

/** Get charge complete status
*
* @return true if done charging and connected to charger
*/
bool furi_hal_power_is_charging_done();

/** Switch MCU to SHUTDOWN */
void furi_hal_power_shutdown();

Expand Down
13 changes: 11 additions & 2 deletions lib/drivers/bq25896.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void bq25896_poweroff(FuriHalI2cBusHandle* handle) {
handle, BQ25896_ADDRESS, 0x09, *(uint8_t*)&bq25896_regs.r09, BQ25896_I2C_TIMEOUT);
}

bool bq25896_is_charging(FuriHalI2cBusHandle* handle) {
ChrgStat bq25896_get_charge_status(FuriHalI2cBusHandle* handle) {
furi_hal_i2c_read_mem(
handle,
BQ25896_ADDRESS,
Expand All @@ -91,7 +91,16 @@ bool bq25896_is_charging(FuriHalI2cBusHandle* handle) {
BQ25896_I2C_TIMEOUT);
furi_hal_i2c_read_reg_8(
handle, BQ25896_ADDRESS, 0x0B, (uint8_t*)&bq25896_regs.r0B, BQ25896_I2C_TIMEOUT);
return bq25896_regs.r0B.CHRG_STAT != ChrgStatNo;
return bq25896_regs.r0B.CHRG_STAT;
}

bool bq25896_is_charging(FuriHalI2cBusHandle* handle) {
// Include precharge, fast charging, and charging termination done as "charging"
return bq25896_get_charge_status(handle) != ChrgStatNo;
}

bool bq25896_is_charging_done(FuriHalI2cBusHandle* handle) {
return bq25896_get_charge_status(handle) == ChrgStatDone;
}

void bq25896_enable_charging(FuriHalI2cBusHandle* handle) {
Expand Down
8 changes: 8 additions & 0 deletions lib/drivers/bq25896.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "bq25896_reg.h"

#include <stdbool.h>
#include <stdint.h>
#include <furi_hal_i2c.h>
Expand All @@ -10,9 +12,15 @@ void bq25896_init(FuriHalI2cBusHandle* handle);
/** Send device into shipping mode */
void bq25896_poweroff(FuriHalI2cBusHandle* handle);

/** Get charging status */
ChrgStat bq25896_get_charge_status(FuriHalI2cBusHandle* handle);

/** Is currently charging */
bool bq25896_is_charging(FuriHalI2cBusHandle* handle);

/** Is charging completed while connected to charger */
bool bq25896_is_charging_done(FuriHalI2cBusHandle* handle);

/** Enable charging */
void bq25896_enable_charging(FuriHalI2cBusHandle* handle);

Expand Down

0 comments on commit ce78d3b

Please sign in to comment.