@@ -99,6 +99,15 @@ static int battery_check_disconnect(void)
9999 return BATTERY_NOT_DISCONNECTED ;
100100}
101101
102+ enum battery_present board_batt_is_present (void )
103+ {
104+ /*
105+ * Due to adc_read_channel() will clear the task event,
106+ * we should get the battery status without read adc channel again.
107+ */
108+ return batt_pres_prev ;
109+ }
110+
102111enum battery_present battery_is_present (void )
103112{
104113 enum battery_present batt_pres ;
@@ -269,7 +278,7 @@ static void fix_single_param(int flag, int *cached, int *curr)
269278 * cached = * curr ;
270279}
271280
272- #define CACHE_INVALIDATION_TIME_US (5 * SECOND)
281+ #define CACHE_INVALIDATION_TIME_US (3 * SECOND)
273282
274283/*
275284 * f any value in batt_params is bad, replace it with a cached
@@ -282,14 +291,43 @@ __override void board_battery_compensate_params(struct batt_params *batt)
282291 static timestamp_t deadline ;
283292
284293 /*
285- * If battery keeps failing for 5 seconds, stop hiding the error and
294+ * If battery keeps failing for 3 seconds, stop hiding the error and
286295 * report back to host.
287296 */
288- if (batt -> flags & BATT_FLAG_BAD_ANY ) {
289- if (timestamp_expired (deadline , NULL ))
297+
298+ if (batt -> flags & BATT_FLAG_RESPONSIVE ) {
299+ if (batt -> flags & BATT_FLAG_BAD_ANY ) {
300+ if (timestamp_expired (deadline , NULL ))
301+ return ;
302+ } else
303+ deadline .val = get_time ().val + CACHE_INVALIDATION_TIME_US ;
304+ } else if (!(batt -> flags & BATT_FLAG_RESPONSIVE )) {
305+ /**
306+ * There are 4 situations for battery is not repsonsed
307+ * 1. Darin battery (first time)
308+ * 2. Dead battery (first time)
309+ * 3. No battery (is preset)
310+ * 4. Others
311+ */
312+ /* we don't need to cache the value when battery is not present */
313+ if (!batt -> is_present ) {
314+ batt_cache .flags &= ~BATT_FLAG_RESPONSIVE ;
290315 return ;
291- } else {
292- deadline .val = get_time ().val + CACHE_INVALIDATION_TIME_US ;
316+ }
317+
318+ /* we don't need to cache the value when we read the battery first time*/
319+ if (!(batt_cache .flags & BATT_FLAG_RESPONSIVE ))
320+ return ;
321+
322+ /**
323+ * If battery keeps no responsing for 3 seconds, stop hiding the error and
324+ * back to host.
325+ */
326+ if (timestamp_expired (deadline , NULL )) {
327+ batt_cache .flags &= ~BATT_FLAG_RESPONSIVE ;
328+ return ;
329+ }
330+
293331 }
294332
295333 /* return cached values for at most CACHE_INVALIDATION_TIME_US */
@@ -330,6 +368,8 @@ __override void board_battery_compensate_params(struct batt_params *batt)
330368
331369 /* remove bad flags after applying cached values */
332370 batt -> flags &= ~BATT_FLAG_BAD_ANY ;
371+ batt -> flags |= BATT_FLAG_RESPONSIVE ;
372+ batt_cache .flags |= BATT_FLAG_RESPONSIVE ;
333373}
334374
335375/*****************************************************************************/
0 commit comments