@@ -234,41 +234,53 @@ static int max17048_get_props(const struct device *dev, struct fuel_gauge_get_pr
234
234
return rc ;
235
235
}
236
236
237
- /**
238
- * May take some time until the chip detects the change between discharging to charging
239
- * (and vice versa) especially if your device consumes little power
240
- */
241
- data -> charging = crate > 0 ;
242
-
243
-
244
- /**
245
- * In the following code, we multiply by 1000 the charge to increase the precision. If we
246
- * just truncate the division without this multiplier, the precision lost is very
247
- * significant when converting it into minutes (the value given is in hours)
248
- *
249
- * The value coming from crate is already 1000 times higher (check the function
250
- * max17048_crate to
251
- * see the reason) so the multiplier for the charge
252
- * will be 1000000
253
- */
254
- if (data -> charging ) {
255
- uint8_t percentage_pending = 100 - data -> charge ;
256
- uint32_t hours_pending = percentage_pending * 1000000 / crate ;
257
-
258
- data -> time_to_empty = 0 ;
259
- data -> time_to_full = hours_pending * 60 / 1000 ;
237
+ if (crate != 0 ) {
238
+
239
+ /**
240
+ * May take some time until the chip detects the change between discharging to
241
+ * charging (and vice versa) especially if your device consumes little power
242
+ */
243
+ data -> charging = crate > 0 ;
244
+
245
+
246
+ /**
247
+ * In the following code, we multiply by 1000 the charge to increase the
248
+ * precision. If we just truncate the division without this multiplier,
249
+ * the precision lost is very significant when converting it into minutes
250
+ * (the value given is in hours)
251
+ *
252
+ * The value coming from crate is already 1000 times higher (check the
253
+ * function max17048_crate to see the reason) so the multiplier for the
254
+ * charge will be 1000000
255
+ */
256
+ if (data -> charging ) {
257
+ uint8_t percentage_pending = 100 - data -> charge ;
258
+ uint32_t hours_pending = percentage_pending * 1000000 / crate ;
259
+
260
+ data -> time_to_empty = 0 ;
261
+ data -> time_to_full = hours_pending * 60 / 1000 ;
262
+ } else {
263
+ /* Discharging */
264
+ uint32_t hours_pending = data -> charge * 1000000 / - crate ;
265
+
266
+ data -> time_to_empty = hours_pending * 60 / 1000 ;
267
+ data -> time_to_full = 0 ;
268
+ }
269
+
270
+ for (int i = 0 ; i < len ; i ++ ) {
271
+ int ret = max17048_get_prop (dev , props + i );
272
+
273
+ err_count += ret ? 1 : 0 ;
274
+ }
260
275
} else {
261
- /* Discharging */
262
- uint32_t hours_pending = data -> charge * 1000000 / - crate ;
263
-
264
- data -> time_to_empty = hours_pending * 60 / 1000 ;
276
+ /**
277
+ * This case is to avoid a division by 0 when the charge rate is the same
278
+ * than consumption rate. It could also happen when the sensor is still
279
+ * calibrating the battery
280
+ */
281
+ data -> charging = false;
265
282
data -> time_to_full = 0 ;
266
- }
267
-
268
- for (int i = 0 ; i < len ; i ++ ) {
269
- int ret = max17048_get_prop (dev , props + i );
270
-
271
- err_count += ret ? 1 : 0 ;
283
+ data -> time_to_empty = 0 ;
272
284
}
273
285
274
286
err_count = (err_count == len ) ? -1 : err_count ;
0 commit comments