Skip to content

Commit 633aef9

Browse files
committed
battery error handling
1 parent c7ed7aa commit 633aef9

3 files changed

Lines changed: 12 additions & 8 deletions

File tree

src/system/battery.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ static const struct battery_level_point levels[] = {
263263
#endif
264264
};
265265

266-
unsigned int read_batt()
266+
int read_batt()
267267
{
268268
int rc = battery_measure_enable(true);
269269

@@ -274,17 +274,18 @@ unsigned int read_batt()
274274

275275
int batt_mV = battery_sample();
276276

277+
battery_measure_enable(false);
278+
277279
if (batt_mV < 0) {
278280
LOG_DBG("Failed to read battery voltage: %d",
279281
batt_mV);
282+
return rc;
280283
}
281284

282-
battery_measure_enable(false);
283-
284285
return battery_level_pptt(batt_mV, levels);
285286
}
286287

287-
unsigned int read_batt_mV(int *out)
288+
int read_batt_mV(int *out)
288289
{
289290
int rc = battery_measure_enable(true);
290291

@@ -295,13 +296,14 @@ unsigned int read_batt_mV(int *out)
295296

296297
int batt_mV = battery_sample();
297298

299+
battery_measure_enable(false);
300+
298301
if (batt_mV < 0) {
299302
LOG_DBG("Failed to read battery voltage: %d",
300303
batt_mV);
304+
return rc;
301305
}
302306

303-
battery_measure_enable(false);
304-
305307
*out = batt_mV;
306308
return battery_level_pptt(batt_mV, levels);
307309
}

src/system/battery.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ struct battery_level_point {
5050
unsigned int battery_level_pptt(unsigned int batt_mV,
5151
const struct battery_level_point *curve);
5252

53-
unsigned int read_batt(void);
53+
int read_batt(void);
5454

55-
unsigned int read_batt_mV(int *out);
55+
int read_batt_mV(int *out);
5656

5757
#endif /* APPLICATION_BATTERY_H_ */

src/system/power.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,8 @@ static void power_thread(void)
464464

465465
int battery_mV;
466466
int16_t battery_pptt = read_batt_mV(&battery_mV);
467+
if (battery_level_pptt < 0)
468+
LOG_ERR("Failed to read battery voltage: %d", battery_pptt);
467469
if (samples < BATTERY_SAMPLES)
468470
samples++;
469471

0 commit comments

Comments
 (0)