Skip to content

Commit 1c55bb5

Browse files
committed
nicla-system: Remove battery status function.
1 parent 03cbd92 commit 1c55bb5

File tree

2 files changed

+43
-49
lines changed

2 files changed

+43
-49
lines changed

libraries/Nicla_System/src/Nicla_System.cpp

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -265,58 +265,53 @@ int8_t nicla::getBatteryPercentage(bool useLatchedValue) {
265265
}
266266

267267
uint8_t nicla::getBatteryChargeLevel() {
268-
return getBatteryStatus() & BATTERY_CHARGE_MASK;
269-
}
270-
271-
uint8_t nicla::getBatteryStatus() {
272268
auto percent = getBatteryPercentage();
273-
int res = BATTERY_UNKNOWN;
274269

275270
if (percent >= 98) {
276-
res = BATTERY_FULL;
271+
return BATTERY_FULL;
277272
} else if (percent >= 94){
278-
res = BATTERY_ALMOST_FULL;
273+
return BATTERY_ALMOST_FULL;
279274
} else if (percent >= 90){
280-
res = BATTERY_HALF;
275+
return BATTERY_HALF;
281276
} else if (percent >= 86){
282-
res = BATTERY_ALMOST_EMPTY;
277+
return BATTERY_ALMOST_EMPTY;
283278
} else if(percent < 86 && percent > 0) {
284279
// < 84% is considered empty
285-
res = BATTERY_EMPTY;
286-
}
287-
288-
if (_ntcEnabled) {
289-
// Extract bits 5 and 6 (TS_FAULT0 and TS_FAULT1)
290-
uint8_t temperatureSenseFault = _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_TS_CONTROL) >> 5 & 0b11;
291-
292-
/*
293-
+------+-------------------------------------------------------------+
294-
| Bits | Description |
295-
+------+-------------------------------------------------------------+
296-
| 00 | Normal, No TS fault |
297-
| 01 | TS temp < TCOLD or TS temp > THOT (Charging suspended) |
298-
| 10 | TCOOL > TS temp > TCOLD (Charging current reduced by half) |
299-
| 11 | TWARM < TS temp < THOT (Charging voltage reduced by 140 mV) |
300-
+------+-------------------------------------------------------------+
301-
*/
302-
303-
if(temperatureSenseFault == 0){
304-
res |= BATTERY_TEMPERATURE_NORMAL;
305-
} else if (temperatureSenseFault == 1) {
306-
res |= BATTERY_TEMPERATURE_EXTREME;
307-
} else if (temperatureSenseFault == 2) {
308-
res |= BATTERY_TEMPERTURE_COOL;
309-
} else if (temperatureSenseFault == 3) {
310-
res |= BATTERY_TEMPERTURE_WARM;
311-
}
280+
return BATTERY_EMPTY;
281+
} else {
282+
// Battery status could not be read
283+
return BATTERY_UNKNOWN;
312284
}
313-
314-
return res;
315285
}
316286

317287
uint8_t nicla::getBatteryTemperature() {
318288
if(!_ntcEnabled) return BATTERY_TEMPERATURE_NORMAL;
319-
return getBatteryStatus() & BATTERY_TEMPERATURE_MASK;
289+
290+
// Extract bits 5 and 6 (TS_FAULT0 and TS_FAULT1)
291+
uint8_t temperatureSenseFault = _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_TS_CONTROL) >> 5 & 0b11;
292+
293+
/*
294+
+------+-------------------------------------------------------------+
295+
| Bits | Description |
296+
+------+-------------------------------------------------------------+
297+
| 00 | Normal, No TS fault |
298+
| 01 | TS temp < TCOLD or TS temp > THOT (Charging suspended) |
299+
| 10 | TCOOL > TS temp > TCOLD (Charging current reduced by half) |
300+
| 11 | TWARM < TS temp < THOT (Charging voltage reduced by 140 mV) |
301+
+------+-------------------------------------------------------------+
302+
*/
303+
304+
if(temperatureSenseFault == 0){
305+
return BATTERY_TEMPERATURE_NORMAL;
306+
} else if (temperatureSenseFault == 1) {
307+
return BATTERY_TEMPERATURE_EXTREME;
308+
} else if (temperatureSenseFault == 2) {
309+
return BATTERY_TEMPERTURE_COOL;
310+
} else if (temperatureSenseFault == 3) {
311+
return BATTERY_TEMPERTURE_WARM;
312+
}
313+
314+
return BATTERY_TEMPERATURE_NORMAL;
320315
}
321316

322317

libraries/Nicla_System/src/Nicla_System.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ enum class OperatingStatus {
2626
#define BATTERY_EMPTY 1 // Bit pattern: 001
2727
#define BATTERY_UNKNOWN 0 // Bit pattern: 000
2828

29+
enum class BatteryChargeLevel {
30+
Unknown = 0b000,
31+
Empty = 0b001,
32+
AlmostEmpty = 0b010,
33+
HalfFull = 0b011,
34+
AlmostFull = 0b100,
35+
Full = 0b101
36+
};
37+
2938
// 2 bits are used to indicate the battery temperature
3039
#define BATTERY_TEMPERATURE_MASK 0b00011000
3140
#define BATTERY_TEMPERATURE_NORMAL (0 << 4)
@@ -114,16 +123,6 @@ class nicla {
114123
*/
115124
static uint8_t getBatteryTemperature();
116125

117-
/**
118-
* @brief Get the Battery Status (charge level and temperature).
119-
* The first 3 bits indicate the battery charge level. They can be retrieved using the BATTERY_CHARGE_MASK.
120-
* The 4th and 5th bit indicate the battery temperature. They can be retrieved using the BATTERY_TEMPERATURE_MASK.
121-
* @see getBatteryChargeLevel()
122-
* @see getBatteryTemperature()
123-
* @return uint8_t The battery status containing the charge level and temperature.
124-
*/
125-
static uint8_t getBatteryStatus();
126-
127126
/**
128127
* @brief Returns potential battery faults retrieved from the fault register.
129128
*

0 commit comments

Comments
 (0)