flipperzero-firmware icon indicating copy to clipboard operation
flipperzero-firmware copied to clipboard

power: Also ask charger if charge done

Open digitalcircuit opened this issue 3 years ago • 2 comments

What's new

  • Also check bq25896's CHRG_STAT to see if charging is complete
    • More accurately reflects charging status
    • Simplifies local custom firmware builds that reduce the battery charging voltage
  • Keep bq27220 fuel gauge check to show as charged at 100%
    • Ensures Flipper shows as charged as soon as it's plugged in if already 100%
  • No changes made to charging procedure
Criteria Rank Reason
Impact ★☆☆ 1/3 More accurate status, groundwork for locally reducing charging voltage
Risk ★☆☆ 1/3 Charging might be shown as complete before it's done
Intrusiveness ★★☆ 2/3 Modifies driver, power HAL, and power interface, might interfere with other pull requests

Verification

Standard charging behavior

  1. Flash firmware to Flipper
  2. Drain Flipper battery below 100%, then connect to charger
  3. Ensure that Flipper shows as charging (red LED)
  4. Charge until full (100%)
  5. Ensure that Flipper shows as full (green LED)
  6. Unplug Flipper from charger
  7. Wait until Flipper shows as disconnected (green LED turns off)
  8. Reconnect Flipper to charger while at 100%
  9. Ensure that Flipper shows as full (green LED) as soon as power is detected

Modified charging behavior

This provides a way to test the "charging done" status from the bq25896 without relying on the fuel gauge. Whether or not an actual UI to implement charge limiting will be added is under discussion in issue #1158.

  1. Modify lib/drivers/bq25896.c to add a hard-coded charge limit for testing
    • Only for testing, not part of this pull request
@@ -56,6 +56,12 @@ void bq25896_init(FuriHalI2cBusHandle* handle) {
     furi_hal_i2c_write_reg_8(
         handle, BQ25896_ADDRESS, 0x02, *(uint8_t*)&bq25896_regs.r02, BQ25896_I2C_TIMEOUT);
 
+    // FIXME: Hard-coded hack to test, move this to an actual function later
+    // WARNING: Do NOT go above 0x17 (23 decimal) - that will overcharge the battery!
+    bq25896_regs.r06.VREG = 0x00; // Charge Voltage Limit: 3840 mV (offset: +3840mV)
+    furi_hal_i2c_write_reg_8(
+        handle, BQ25896_ADDRESS, 0x06, *(uint8_t*)&bq25896_regs.r06, BQ25896_I2C_TIMEOUT);
+
     bq25896_regs.r07.WATCHDOG = WatchdogDisable;
     furi_hal_i2c_write_reg_8(
         handle, BQ25896_ADDRESS, 0x07, *(uint8_t*)&bq25896_regs.r07, BQ25896_I2C_TIMEOUT);
  1. Flash firmware to Flipper
  2. Drain Flipper battery below 3.8v (possibly near 70%), then connect to charger
  3. Ensure that Flipper shows as charging (red LED)
  4. Charge until Flipper shows as full (green LED)
  5. Unplug Flipper from charger
  6. Wait until Flipper shows as disconnected (green LED turns off)
  7. Ensure that Flipper eventually shows as full (green LED)
    • May take up to 1 minute for bq25896 to determine that charging is already done

Code review extras

I'm unsure about adding #include "bq25896_reg.h" to lib/drivers/bq25896.h. It's needed to specify a return type of ChrgStat to bq25896_get_charge_status(), which avoids duplicating code, but that is pulling in another header file at the header level.

If there's a better way to handle this, let me know.

Checklist (For Reviewer)

  • [ ] PR has description of feature/bug or link to Confluence/Jira task
  • [ ] Description contains actions to verify feature/bugfix
  • [ ] I've built this code, uploaded it to the device and verified feature/bugfix

digitalcircuit avatar Jul 07 '22 03:07 digitalcircuit

There are cases when charging will be suppressed. This changes need verification, I'll be able to test it on weekend.

So it's scheduled for next release

skotopes avatar Jul 07 '22 11:07 skotopes

Let me know if there's anything I could do to make this easier to review. No rush implied though; I realize the Flipper team's got much bigger matters to tackle.

I've been running this atop dev plus local changes to actually limit the charge (this PR does not limit charging) and it's been working well. I'll keep rebasing as I update, though if the pushes cause issues I can stop.

digitalcircuit avatar Jul 27 '22 02:07 digitalcircuit

I have updated this with the latest changes to introduce the public Flipper API. This pull request adds the following to the API:

Function,+,furi_hal_power_is_charging_done,_Bool,

When combined with my work-in-progress support for charge limiting, two other functions get added.

EDIT: I noticed the force-push to the dev branch and I've rebased this.

digitalcircuit avatar Sep 14 '22 17:09 digitalcircuit

@skotopes Thank you for picking this up!

I'll refrain from doing further rebases atop dev since you've assigned this to yourself.

digitalcircuit avatar Sep 25 '22 22:09 digitalcircuit