alp@vilnius: ~ up since 2019 · Vilnius Download cv.pdf

← ALP-OS

alp ~ $ work battery

The battery gauge that took 3 hours to say 10%

Three battery complaints turned out to be three different bugs. One gauge took 3 hours to show its first 10%, and now takes 1.5. Another got stuck at 99%; it now reaches 100%, proven with a simulated −40 mV ADC offset.

3 h  to 1.5 hfor the gauge to show the first 10%

Three battery complaints came in from the field. On one model, the gauge took 3 hours to show its first 10%. Some devices stopped charging below 4.15 V. Others sat at 99% forever after a full charge.

It looked like one battery problem. It was three separate bugs.

The wrong table

The slow model was using the percentage table for a 170 mAh cell on a 1,000 mAh battery. I built new tables for 170, 1,000 and 1,800 mAh cells. I also wrote and documented an algorithm that moves toward the measured value and can’t get stuck.

Time for the gauge to show the first 10%

Believing the charger too early

The early stop came from the charger chip reporting DONE too early, and the firmware believing it. Now the firmware retries up to 3 early DONE signals before it accepts one. The noisy charger status lines also got a 3-sample debounce with hysteresis.

Stuck at 99%

The charger stops at 4,125–4,140 mV, but the table needs at least 4,148 mV for 100%. So a full battery always came out 8–23 mV short. On top of that, an older guard blocked the 10-minute recalculation, so the gauge never moved off 99%. Now, when the firmware accepts the battery as full, it sets 100%.

Charger DONE retries, and why a full battery read 99% Part 1, the charger state machine. Before, the first DONE from the charger IC turned charging off for good, below 4.15 V. Now, while charging, a DONE with fewer than 3 retries toggles the charger and adds one retry, then charging resumes. Retries reset to 0 whenever the charger reports CHARGING or STANDBY. A DONE after 3 retries is accepted as full, and accepting full now sets 100%. Part 2, to scale on a millivolt axis from 4080 to 4160: the charger IC ends the charge between 4,125 and 4,140 mV, but the percentage table needs at least 4,148 mV for 100%, so the reading is 8 to 23 mV short of 100%. The test image emulates a −40 mV ADC offset, as in field readings. Part 3: before the fix, the table said 99% and an old guard blocked the 10-minute recalculation, so the gauge stayed at 99% forever. After the fix, accepting full sets 100%, and the fixed build held 100% with the −40 mV offset, which proved the fix. THE CHARGER REPORTS DONE TOO EARLY Before, the first DONE turned charging off for good, below 4.15 V. Now: CHARGING charger: DONE after 3 retries accept full set 100% new fewer than 3 retries toggle charger, retries+1 Retries reset to 0 whenever the charger reports CHARGING or STANDBY. WHY A FULL BATTERY READ 99% · to scale 100% needs ≥ 4,148 mV charge ends 4,125–4,140 mV 8–23 mV short of 100% test image emulated −40 mV ADC offset 4080 4100 4120 4140 4160 mV WHAT THE GAUGE SHOWED AFTER A FULL CHARGE before table says 99% 10-min recalc blocked by guard 99% forever after accept full set 100% held 100% with the −40 mV offset
Charger DONE retries, and why a full battery read 99%

Testing with field conditions

To test the 99% fix, I built a test image with a simulated −40 mV ADC offset, like the readings from the field. The fixed build held 100%. Each fix also has a unit test.

Conclusion

The gauge now shows 10% after 1.5 hours instead of 3, holds a steady 100% without flickering back to 99, and batteries charge to 4.15 V again.

Splitting the complaints apart early meant each bug got its own fix and its own test.

Tags power testing