3

I am trying to read battery information from an NDK app. The only way I know to accomplish this is to read the values out /sys/class/power_supply. My difficulty is that I find multiple entries with type "Battery" and I don't know which one to use.

On a Nexus 10 with KitKat 4.4.4 I see:

$ ls /sys/class/power_supply
ds2784-fuelgauge
manta-battery
smb347-battery
# other non-battery entries

With type=Battery for all three. For example:

$ cat /sys/class/power_supply/ds2784-fuelgauge/type
Battery

In the Android BatteryManager code they just iterate through the battery devices and take the first one that has the entry they want (http://androidxref.com/4.4.4_r1/xref/system/core/healthd/BatteryMonitor.cpp). However, on my Nexus 10 they have different values. For example, for ds2784-fuelgauge/voltage_now and smb347-battery/voltage_now:

$ cat ds2784-fuelgauge/voltage_now
4153100
$ cat smb347-battery/voltage_now
4300000

ds2784-fuelgauge/voltage_now seems to be updated regularly (e.g. it changes when I start a compute-heavy task), but smb347-battery/voltage_now seems to be fixed. I see a similar effect when I compare ds2784-fuelgauge/current_now and smb347-battery/current_now.

My questions are:

  1. why are there multiple entries here with type=battery?
  2. how do I distinguish them in a general way?
  3. which one should I be using?

Edits

  • fixed typo in voltage_now filename
  • emphasize that type=Battery for all three devices
aschmied
  • 908
  • 2
  • 10
  • 26
  • Could volt be the capacity? It never changes right? – Phix Oct 28 '14 at 18:44
  • Ack. That was a typo. They're both `voltage_now` which should be the "momentary/instantaneous values" (https://www.kernel.org/doc/Documentation/power/power_supply_class.txt) – aschmied Oct 28 '14 at 20:08
  • Hm. Well I'd say do more testing to see which changes. I'm betting on the fuelguage being some other param that meaures useage like you said and the battery being the actual value. Test test test! – Phix Oct 28 '14 at 20:34

2 Answers2

5

On android devices you may have more than one battery, like the backup for RTC or for other peripherals. Did you already cat the type file for all the 3 sysfs entries? for all the 3 you get 'Battery'?

Considering that Android expects several parameters from the battery (see below), i may guess that they are split in those 3 sysfs files you got.

For example the fuel gauge is the hardware component that reads the current flow from/to the battery and calculate the remaining capacity, thus reading the capacity file from it should give you the %, but probably the voltage is read from another file.

Battery params (i.e.: /sys/class/power_supply/my_batt/status):

  • status: Charging, Discharging, Not charging, Full, Unknown
  • health (string): Cold, Death, Good, Overheat, Over voltage, Unspecified failure, Unknown
  • present (int): 1 when present
  • capacity (int): 97 for 97%
  • voltage_now (in uV) or batt_volt (in mV): 4205000 for 4205000 uV = 4.205 V
  • temp or batt_temp (in tenth of °C): 305 for 30.5°C
  • technology (string): Li-ion
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Luca Faggianelli
  • 2,032
  • 20
  • 23
  • Thank you for your response. Yes, the `type` file contains `Battery` for all three devices (I will edit the question to make this clearer). It sounds like the fuel gauge is probably what I want. I'll accept your answer since you clarify what the fuel gauge is, but I don't see a way to distinguish it from the other entries other than its symlink name. My current implementation uses the approach from `BatteryMonitor.cpp`: iterate all batteries and pick the first one that has the file you're looking for. – aschmied Dec 16 '14 at 16:09
  • You can see all the possible values used in the kernel [listed here](https://github.com/torvalds/linux/blob/master/drivers/power/power_supply_sysfs.c#L46:L71) – karolba Mar 24 '16 at 10:26
1

Your question was about which source to use, and presumably how to determine it programmatically.

For all android devices, you should use the fuel gauge.

Nexus 10:

For your Nexus 10, you should use the Dallas Semiconductor DS2784, according to this official documentation from source.android.com on Measuring Android Device Power. The SMB347, also listed as a fuel gauge on the same documentation, even though, is probably a battery charger. I say this because of the datasheet for the chip lists it as a battery charger, which would explain why it has a constant voltage (CV). (That the CV source runs at 4.2V, the asymptotic high end of the power curve for a Li-Ion battery.)

SysFS

You were using Android 4.4.4. Android M is in developer preview as of this writing, and Android 5.1.1 has been released already. Because of this, the mapping of your /sys filesystem (SysFS) might not be the same if you upgrade your phone. In other words, YMMV.

ON BKG: Lithium Batteries

  • Almost all Li-Ion batteries have a max / charging voltage of 4.2V.
    Some newer lithium-polymer batteries in 2015 have a max at 4.35V instead of 4.2V. This is a result of the Lithium Ion chemistry (and what's considered acceptable deterioration of the electrodes of the battery due to advances in the field).

  • Li-Ion batteries change voltage a lot as they go from 100% charged to 1% or 0% charged. Expect a range of 3.0V to 4.2V.

  • All lithium batteries are unrecoverably broken and could be a potential fire hazard if they discharge too much. Because of that, devices using Lithium-* batteries have to have some kind of regulatory circuitry, which may in addition be controlled by something in software rather than just hardware / firmware. The cutoff voltage is somewhere between 3.0V and 3.6V. The choice of what cutoff voltage used is determined by safety vs. capacity design choices, though the amount of battery energy stored as the battery discharges in the last few 100mV is quite low. The power curves are a bit weird. See a discharge curve (voltage vs. remaining capacity expressed in mAh), such as one in the datasheet referenced below.

  • Because of this, modern devices using Lithium batteries must report the current voltage using something like a fuel gauge.

References:

Datasheets

Summit SMB347 Datasheet

(Not enough reputation points). See also a 18650 Datasheet, such as the one for the Panasonic 18650, where Panasonic is one among many manufacturers of Lithium-* batteries, and the one Tesla prefers to work with in an industry setting way.

Community
  • 1
  • 1