Skip to content

Commit abbc54f

Browse files
committed
nicla-system: Disable charging while taking a battery reading.
1 parent 6286728 commit abbc54f

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

libraries/Nicla_System/src/Nicla_System.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,13 +284,23 @@ int8_t nicla::getBatteryVoltagePercentage(bool useLatchedValue) {
284284
uint8_t faults = _pmic.getFaultsRegister();
285285
if(faults & BAT_UVLO_FAULT) return -1; // Battery is not connected or voltage is too low
286286

287-
// Write 1 to VBMON_READ to trigger a new reading
288-
// TODO: Disable charging while reading battery percentage. SEE chapter 9.3.4
289287

290288
if(!useLatchedValue){
289+
// Disable charging while reading battery percentage. SEE chapter 9.3.4
290+
bool chargingEnabled = (_fastChargeRegisterData & 0b10) == 0; // Bit 1 is 0 if charging is enabled.
291+
292+
if(chargingEnabled) {
293+
disableCharging();
294+
}
291295
// Write 1 to VBMON_READ to trigger a new reading
292296
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_BATT_MON, 1);
293297
delay(3); // According to datasheet, 2ms is enough, but we add 1ms for safety
298+
299+
if(chargingEnabled) {
300+
// Re-enable charging by setting bit 1 to 0
301+
_fastChargeRegisterData &= 0b11111101;
302+
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_FAST_CHG, _fastChargeRegisterData);
303+
}
294304
}
295305
uint8_t data = _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_BATT_MON);
296306

0 commit comments

Comments
 (0)