Reading Battery

Quick question. I am using the mangOH red with the WP8548 and i am trying to read the battery level using the Input Power Supply Monitoring API provided by modem Services. The battery level returned by le_ips_GetBatteryLevel always equals to zero, according to the documentation that means the battery is drained or it doesn’t have a battery connected. The battery definetly isn’t drained because i am able to power my platform only using the battery. Also le_ips_GetPowerSource always tells me it is powered by an external source and not the battery (even after disconnecting all usb connections).

Am I using the API wrong or is it because of the mangOH board?

I don’t see problem on mangoh red board with WP8548:

root@fx30:~# cm ips read
[ 53.760750] Alignment trap: netmgrd (823) PC=0x4d374280 Instr=0xe1c980d0 Address=0xb6704ed6 FSR 0x001
[ 53.771127] Alignment trap: netmgrd (823) PC=0x4d374284 Instr=0xe1c320d0 Address=0xb6704ede FSR 0x001
Voltage: 3630 mV
Powered by an external source
root@fx30:~# cm info
Device: WP8548
IMEI: 359377060004735
IMEISV: 2F
FSN: LL537500110203
Firmware Version: SWI9X15Y_07.13.07.00 92cfd2c jenkins 2018/08/22 19:18:52
Bootloader Version: SWI9X15Y_07.13.07.00 92cfd2c jenkins 2018/08/22 19:18:39
[ 56.373660] qup_i2c qup_i2c.0: QUP: I2C status flags :0x1300c8, irq:187
[ 56.379307] qup_i2c qup_i2c.0: I2C slave addr:0x3a not connected
MCU Version: 002.004
PRI Part Number (PN): 9904889
PRI Revision: 01.11
Carrier PRI Name: ATT
Carrier PRI Revision: 001.036_000
SKU: 1102621
Last Reset Cause: Unknown
Resets Count: Expected: 0 Unexpected: 0

I get Pretty much the same:

root@swi-mdm9x15:~# cm ips read
Voltage: 3692 mV
Powered by an external source
root@swi-mdm9x15:~# cm info
Device: WP8548
IMEI: 359377060821351
IMEISV: 30
FSN: LL919601200810
Firmware Version: SWI9X15Y_07.14.01.00 595652d jenkins 2018/10/26 08:34:29
Bootloader Version: SWI9X15Y_07.14.01.00 595652d jenkins 2018/10/26 08:48:35
MCU Version: 002.004
PRI Part Number (PN): 9906351
PRI Revision: 01.01
Carrier PRI Name: GENERIC
Carrier PRI Revision: 001.042_000
SKU: 1103113
Last Reset Cause: Unknown
Resets Count: Expected: -1 Unexpected: -1

But according to: https://docs.legato.io/17_08/toolsTarget_cm.html cm ips read should also display the battery level and specify that it is powered by the battery if one is connected and powering the board

didn’t you say it is returning zero?

I mean after calling:

uint8_t battery;
le_ips_GetBatteryLevel(&battery);

inside an App the variably Battery always contains 0.

API Documentaion: https://docs.legato.io/17_10/le__ips__interface_8h.html#a3e01227136f352b2d67b3c21b383f4d0

ips command is also using this API, you need to investigate why “cm ips” can read value but your app cannot.

But “cm ips” obviously doesn’t display the battery value and displays it is powered by an external source even if it is powered by a battery. Also i can use the API in the app and get the power source and voltage levels.

Edit: With BatteryLevel i mean the battery percentage and not the voltage, sorry if that was unclear

how about using AT!MADC?0

There i get the following:

AT!MADC?0
!MADC: 3.65

OK

How exactly does le_ips_GetBatteryLevel(&battery); calculate the percentage? Maybe i can replicate it myself

seems it cannot really get the percentage…
I found that there maybe already an application for this:

I looked throug the source code and found following function:

static unsigned int ComputePercentage
(
unsigned int mAh
)
{
// Compute the battery charge percentage, rounding up from half a percent or higher.
uint32_t percentTimesTen = 1000UL * mAh / Capacity;
unsigned int percentage = (percentTimesTen / 10);
if ((percentTimesTen % 10) >= 5)
{
percentage += 1;
}

// Clamp at 100%
if (percentage > 100)
{
    LE_WARN("Battery monitor reports available charge (%u mAh) higher than maximum of %u mAh.",
            mAh,
            (unsigned int)Capacity);
    percentage = 100;
}

return percentage;

}

“Capacity” is just the battery Capacity, which i could set myself depending on the battery.

The mAh is the remaining charge calculated in another function using:

char path[256];

int pathLen = snprintf(path, sizeof(path), "%s/%s", MonitorDirPath, ChargeNowFileName);
LE_ASSERT(pathLen < sizeof(path));

int32_t uAh;
le_result_t r = util_ReadIntFromFile(path, &uAh);
if (r == LE_OK)
{
    *charge = uAh / 1000;
}

The weird thing is, “MonitorDirPath” is set via:
static const char MonitorDirPath = “/sys/class/power_supply/LTC2942”;

and it seems i don’t have the path on my mangOH specified in MonitorDirPath

did you install the driver?

I am not sure if it’s worth installing just yet. Because the final version of the Project i am working on will be independent from the mangOH red and the WP will have its own custom carrier board.

Still, thank you for your help!