This class provides APIs to all of the registers of the TI BQ27441 battery gauge, as used on the u-blox C030 board. The caller should instantiate an I2C interface and pass this to init(), which will initialise the chip and place it into its lowest power state. When battery gauging is enabled, the getRemainingCapacity()/getRemainingPercentage() API calls may be used; otherwise the chip will be maintained in its lowest power state until a voltage/current/temperature reading is requested.

Dependents:   example-battery-gauge-bq27441

Revision:
6:998cc334f8f2
Parent:
5:63b325f2c21a
--- a/bq27441.cpp	Wed Jun 14 17:11:40 2017 +0100
+++ b/bq27441.cpp	Wed Dec 13 17:14:51 2017 +0000
@@ -82,6 +82,29 @@
     return success;
 }
 
+bool BatteryGaugeBq27441::getTwoBytesSigned (uint8_t registerAddress, int16_t *pBytes)
+{
+    bool success = false;
+    char data[3];
+
+    if (gpI2c != NULL) {
+        // Send a command to read from registerAddress
+        data[0] = registerAddress;
+        data[1] = 0;
+        data[2] = 0;
+
+        if ((gpI2c->write(gAddress, &(data[0]), 1) == 0) &&
+            (gpI2c->read(gAddress, &(data[1]), 2) == 0)) {
+            success = true;
+            if (pBytes) {
+                *pBytes = (((int16_t) data[2]) << 8) + data[1];
+            }
+        }
+    }
+
+    return success;
+}
+
 // Compute the checksum over a block of data.
 uint8_t BatteryGaugeBq27441::computeChecksum(const char * pData)
 {
@@ -941,28 +964,56 @@
     return success;
 }
 
+// Get the power output of the battery
+bool BatteryGaugeBq27441::getPower (int32_t *pPowerMW)
+{
+    bool success = false;
+    int16_t data = 0;
+
+    if (gReady && (gpI2c != NULL)) {
+        // Make sure there's a recent reading
+        if (gGaugeOn || makeAdcReading()) {            
+            gpI2c->lock();
+            // Read from the power register address
+            if (getTwoBytesSigned (0x18, &data)) {
+                success = true;
+
+                // The answer is in mW
+                if (pPowerMW) {
+                    *pPowerMW = (int32_t) data;
+                }
+
+            }
+
+            // Return to sleep if we are allowed to
+            if (!gGaugeOn && !setHibernate()) {
+                success = false;
+            }
+
+            gpI2c->unlock();
+        }
+    }
+    
+    return success;
+}
+
 // Get the current flowing from the battery.
 bool BatteryGaugeBq27441::getCurrent (int32_t *pCurrentMA)
 {
     bool success = false;
-    int32_t currentMA = 0;
-    uint16_t data;
+    int16_t data;
 
     if (gReady && (gpI2c != NULL)) {
         // Make sure there's a recent reading
         if (gGaugeOn || makeAdcReading()) {            
             gpI2c->lock();            
             // Read from the average current register address
-            if (getTwoBytes (0x10, &data)) {
+            if (getTwoBytesSigned  (0x10, &data)) {
                 success = true;
 
                 if (pCurrentMA) {
-                    *pCurrentMA = currentMA;
+                    *pCurrentMA = (int32_t) data;
                 }
-
-#ifdef DEBUG_BQ27441
-                printf("BatteryGaugeBq27441 (I2C 0x%02x): current %d mA.\n", gAddress >> 1, (int) currentMA);
-#endif
             }
 
             // Return to sleep if we are allowed to