u-blox / battery-charger-bq24295

Dependents:   example-battery-charger-bq24295 example-C030-out-of-box-demo example-C030-out-of-box-demo Amit

Files at this revision

API Documentation at this revision

Comitter:
rob.meades@u-blox.com
Date:
Tue Jun 06 12:59:11 2017 +0100
Parent:
2:f0bbe0269d67
Child:
4:26b46760481d
Commit message:
Add ability to feed(), set() and get() watchdog timer.

Changed in this revision

TESTS/unit_tests/default/main.cpp Show annotated file Show diff for this revision Revisions of this file
battery_charger_bq24295.h Show annotated file Show diff for this revision Revisions of this file
bq24295.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/TESTS/unit_tests/default/main.cpp	Tue Apr 11 09:56:38 2017 +0000
+++ b/TESTS/unit_tests/default/main.cpp	Tue Jun 06 12:59:11 2017 +0100
@@ -817,6 +817,50 @@
     TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(originalTemperature));    
 }
 
+// Test that we can kick and set the watchdog
+void test_watchdog() {
+    BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
+    int32_t initValue;
+    int32_t getValue = 0;
+    
+    // Call should fail if the battery charger has not been initialised
+    TEST_ASSERT_FALSE(pBatteryCharger->feedWatchdog());
+    TEST_ASSERT_FALSE(pBatteryCharger->getWatchdog(&getValue));
+    TEST_ASSERT_FALSE(pBatteryCharger->setWatchdog(0));
+    
+    // Initialise the battery charger
+    TEST_ASSERT(pBatteryCharger->init(gpI2C));
+    
+    // Save the initial value
+    TEST_ASSERT(pBatteryCharger->getWatchdog(&initValue));
+
+    // Check that we can feed the watchdog
+    TEST_ASSERT(pBatteryCharger->feedWatchdog());
+    
+    // Check that we can set all the watchdog values
+    TEST_ASSERT(pBatteryCharger->setWatchdog(81));
+    TEST_ASSERT(pBatteryCharger->getWatchdog(&getValue));
+    TEST_ASSERT_EQUAL_INT32(160, getValue);
+    TEST_ASSERT(pBatteryCharger->setWatchdog(80));
+    TEST_ASSERT(pBatteryCharger->getWatchdog(&getValue));
+    TEST_ASSERT_EQUAL_INT32(80, getValue);
+    TEST_ASSERT(pBatteryCharger->setWatchdog(41));
+    TEST_ASSERT(pBatteryCharger->getWatchdog(&getValue));
+    TEST_ASSERT_EQUAL_INT32(80, getValue);
+    TEST_ASSERT(pBatteryCharger->setWatchdog(40));
+    TEST_ASSERT(pBatteryCharger->getWatchdog(&getValue));
+    TEST_ASSERT_EQUAL_INT32(40, getValue);
+    TEST_ASSERT(pBatteryCharger->setWatchdog(1));
+    TEST_ASSERT(pBatteryCharger->getWatchdog(&getValue));
+    TEST_ASSERT_EQUAL_INT32(40, getValue);
+    TEST_ASSERT(pBatteryCharger->setWatchdog(0));
+    TEST_ASSERT(pBatteryCharger->getWatchdog(&getValue));
+    TEST_ASSERT_EQUAL_INT32(0, getValue);
+
+    // Put the initial value back when we're done
+    TEST_ASSERT (pBatteryCharger->setWatchdog(initValue));
+}
+
 // Test that we can enable and disable shipping mode
 void test_shipping_mode() {
     BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
@@ -919,6 +963,7 @@
     Case("Fast charging safety timer", test_fast_charging_safety_timer),
     Case("Boost limits", test_boost_limits),
     Case("Chip thermal regulation threshold", test_chip_thermal_regulation_threshold),
+    Case("Watchdog", test_watchdog),
     Case("Shipping mode", test_shipping_mode),
     Case("Advanced", test_advanced)
 };
--- a/battery_charger_bq24295.h	Tue Apr 11 09:56:38 2017 +0000
+++ b/battery_charger_bq24295.h	Tue Jun 06 12:59:11 2017 +0100
@@ -71,8 +71,11 @@
     /** Initialise the BQ24295 chip.
     * After initialisation the chip will be put into its lowest
     * power state and should be configured if the default settings
-    * are not satisfactory.  Once the chip is correctly configured,
-    * charging should be enabled.
+    * are not satisfactory.
+    * Note: the BQ24295 charging chip will automonously charge a
+    * LiPo cell that is connected to it, without host interaction.
+    * This class is only required where the configuration of the
+    * chip needs to be changed or the charger state is to be monitored.
     * @param pI2c a pointer to the I2C instance to use.
     * @param address 7-bit I2C address of the battery charger chip.
     * @return true if successful, otherwise false.
@@ -397,6 +400,26 @@
     */
     char getChargerFaults(void);
     
+    /** Feed the watchdog timer.
+    * Use this if it is necessary to keep the BQ24295 chip in Host
+    * mode (see section 8.4.1 of the data sheet for details).
+    * @return true if successful, otherwise false.
+    */
+    bool feedWatchdog(void);
+    
+    /** Get the watchdog timer of the BQ24295 chip.
+    * @param pWatchdogS a place to put the watchdog timer (in seconds).
+    * @return true if successful, otherwise false.
+    */
+    bool getWatchdog (int32_t *pWatchdogS);
+    
+    /** Set the watchdog timer of the BQ24295 chip.
+    * @param watchdogS the watchdog timer (in seconds), 0 to disable,
+    *                  max 160 seconds.
+    * @return true if successful, otherwise false.
+    */
+    bool setWatchdog (int32_t watchdogS);
+    
     /** Enable shipping mode.
     * In shipping mode the battery is disconnected from the system
     * to avoid leakage.  Default is disabled.
--- a/bq24295.cpp	Tue Apr 11 09:56:38 2017 +0000
+++ b/bq24295.cpp	Tue Jun 06 12:59:11 2017 +0100
@@ -1665,6 +1665,103 @@
     return bitmap;
 }
 
+// Feed the watchdog timer of the BQ24295 chip.
+bool BatteryChargerBq24295::feedWatchdog (void)
+{
+    bool success = false;
+
+    if (gReady && (gpI2c != NULL)) {
+        gpI2c->lock();
+        // Watchdog reset is done by setting bit 6 of the power on control register
+        // to 1
+        success = setRegisterBits(0x01, (1 << 6));
+#ifdef DEBUG_BQ24295
+        if (success) {
+            printf("BatteryChargerBq24295 (I2C 0x%02x): watchdog fed.\r\n", gAddress >> 1);
+        }
+#endif
+        gpI2c->unlock();
+    }
+
+    return success;
+}
+
+// Get the watchdog timer of the BQ24295 chip.
+bool BatteryChargerBq24295::getWatchdog (int32_t *pWatchdogS)
+{
+    bool success = false;
+    char reg;
+
+    if (gReady && (gpI2c != NULL)) {
+        gpI2c->lock();
+        // Watchdog timer is in bits 4 and 5 of the charger termination/timer control
+        // register, where 0 is disabled 01 is 40 secs, 10 is 80 secs and 11 is 160 secs
+        success = getRegister(0x05, &reg);
+        if (pWatchdogS != NULL) {
+            switch ((reg >> 4) & 0x03) {
+                case 0x00:
+                    *pWatchdogS = 0;
+                break;
+                case 0x01:
+                    *pWatchdogS = 40;
+                break;
+                case 0x02:
+                    *pWatchdogS = 80;
+                break;
+                case 0x03:
+                    *pWatchdogS = 160;
+                break;
+                default:
+                    MBED_ASSERT(false);
+                break;
+            }
+#ifdef DEBUG_BQ24295
+            if (success) {
+                printf("BatteryChargerBq24295 (I2C 0x%02x): watchdog is %d seconds.\r\n", gAddress >> 1, *pWatchdogS);
+            }
+#endif
+        }
+        gpI2c->unlock();
+    }
+
+    return success;
+}
+
+// Set the watchdog timer of the BQ24295 chip.
+bool BatteryChargerBq24295::setWatchdog (int32_t watchdogS)
+{
+    bool success = false;
+    char reg;
+    char regValue = 0;
+
+    if (gReady && (gpI2c != NULL)) {
+        gpI2c->lock();
+        // Watchdog timer is in bits 4 and 5 of the charger termination/timer control
+        // register, where 0 is disabled 01 is 40 secs, 10 is 80 secs and 11 is 160 secs
+        if (watchdogS > 80) {
+            regValue = 0x03;
+        } else if (watchdogS > 40) {
+            regValue = 0x02;
+        } else if (watchdogS > 0) {
+            regValue = 0x01;
+        }
+        if (getRegister(0x05, &reg)) {
+            // Clear the bits then set them
+            reg &= ~(0x03 << 4);
+            reg |= regValue << 4;
+            success = setRegister(0x05, reg);
+#ifdef DEBUG_BQ24295
+            if (success) {
+                printf("BatteryChargerBq24295 (I2C 0x%02x): watchdog set to %d seconds.\r\n", gAddress >> 1, watchdogS);
+            }
+#endif
+        }
+        gpI2c->unlock();
+    }
+
+    return success;
+}
+
 // Enable shipping mode.
 bool BatteryChargerBq24295::enableShippingMode (void)
 {