u-blox / battery-charger-bq24295

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "greentea-client/test_env.h"
00003 #include "unity.h"
00004 #include "utest.h"
00005 #include "battery_charger_bq24295.h"
00006 
00007 using namespace utest::v1;
00008 
00009 // ----------------------------------------------------------------
00010 // COMPILE-TIME MACROS
00011 // ----------------------------------------------------------------
00012 
00013 // Minimum and maximum numbers
00014 #define MAX_INPUT_VOLTAGE_LIMIT_MV  5080
00015 #define MIN_INPUT_VOLTAGE_LIMIT_MV  3880
00016 #define MAX_INPUT_CURRENT_LIMIT_MA  3000
00017 #define MIN_INPUT_CURRENT_LIMIT_MA  100
00018 #define MAX_SYSTEM_VOLTAGE_MV  3700
00019 #define MIN_SYSTEM_VOLTAGE_MV  3000
00020 #define MAX_FAST_CHARGING_CURRENT_LIMIT_MA 3008
00021 #define MIN_FAST_CHARGING_CURRENT_LIMIT_MA 512
00022 #define MAX_PRECHARGING_CURRENT_LIMIT_MA 2048
00023 #define MIN_PRECHARGING_CURRENT_LIMIT_MA 128
00024 #define MAX_CHARGING_TERMINATION_CURRENT_MA 2048
00025 #define MIN_CHARGING_TERMINATION_CURRENT_MA 128
00026 #define MAX_CHARGING_VOLTAGE_LIMIT_MV  4400
00027 #define MIN_CHARGING_VOLTAGE_LIMIT_MV  3504
00028 #define MIN_BOOST_VOLTAGE_MV 4550
00029 #define MAX_BOOST_VOLTAGE_MV 5510
00030 
00031 #ifndef NUM_RAND_ITERATIONS
00032 // The number of iterations of random input values in various tests
00033 #define NUM_RAND_ITERATIONS 50
00034 #endif
00035 
00036 // ----------------------------------------------------------------
00037 // PRIVATE VARIABLES
00038 // ----------------------------------------------------------------
00039 
00040 // I2C interface
00041 I2C * gpI2C = new I2C(I2C_SDA_B, I2C_SCL_B);
00042 
00043 // ----------------------------------------------------------------
00044 // TESTS
00045 // ----------------------------------------------------------------
00046 
00047 // Test that the BQ24295 battery charger can be initialised
00048 void test_init() {
00049     BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
00050     
00051     TEST_ASSERT_FALSE(pBatteryCharger->init(NULL));
00052     TEST_ASSERT(pBatteryCharger->init(gpI2C));
00053 }
00054 
00055 // Test that we can read the charger state from the BQ24295 battery charger
00056 void test_charger_state() {
00057     BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
00058     BatteryChargerBq24295::ChargerState chargerState = BatteryChargerBq24295::CHARGER_STATE_UNKNOWN;
00059     
00060     // Call should fail if the battery charger has not been initialised
00061     chargerState = pBatteryCharger->getChargerState();
00062     TEST_ASSERT(chargerState == BatteryChargerBq24295::CHARGER_STATE_UNKNOWN);
00063     
00064     // Normal case
00065     TEST_ASSERT(pBatteryCharger->init(gpI2C));
00066     chargerState = pBatteryCharger->getChargerState();
00067     printf ("Charger state is %d.\n", chargerState);
00068     // Range check
00069     TEST_ASSERT(chargerState != BatteryChargerBq24295::CHARGER_STATE_UNKNOWN);
00070     TEST_ASSERT(chargerState < BatteryChargerBq24295::MAX_NUM_CHARGER_STATES);
00071 }
00072 
00073 // Test that we can read whether external power is present or not
00074 // according to the BQ24295 battery charger
00075 void test_external_power_present() {
00076     BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
00077     
00078     // Call should return false if the battery charger has not been initialised
00079     TEST_ASSERT_FALSE(pBatteryCharger->isExternalPowerPresent());
00080     
00081     // Normal case: must return true as the USB cable is plugged
00082     // in when running these tests
00083     TEST_ASSERT(pBatteryCharger->init(gpI2C));
00084     TEST_ASSERT(pBatteryCharger->isExternalPowerPresent());
00085 }
00086 
00087 // Test that we can read the charger fault from the BQ24295 battery charger
00088 // NOTE: if this test fails, make sure you haven't got an actual fault. Better
00089 // to reset the chip/board entirely before running these tests so that one
00090 // doesn't occur.
00091 void test_charger_fault() {
00092     BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
00093     char bitmap = (char) BatteryChargerBq24295::CHARGER_FAULT_NONE;
00094     
00095     // Call should return no faults if the battery charger has not been initialised
00096     bitmap = pBatteryCharger->getChargerFaults();
00097     TEST_ASSERT_EQUAL_INT8((char) BatteryChargerBq24295::CHARGER_FAULT_NONE, bitmap);
00098     
00099     // Normal case
00100     TEST_ASSERT(pBatteryCharger->init(gpI2C));
00101     bitmap = pBatteryCharger->getChargerFaults();
00102     printf ("Charger fault is 0x%02x.\n", bitmap);
00103     // Should be just the watchdog as we are in host mode and are not servicing the watchdog
00104     // however other faults can also be present so just look for a non zero value.
00105     // TODO: find a way to test other faults
00106     TEST_ASSERT((unsigned char) bitmap > 0);
00107 }
00108 
00109 // Test that we can read and change the input voltage and current limits
00110 void test_input_limits() {
00111     BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
00112     int32_t voltageOriginal;
00113     int32_t currentOriginal;
00114     bool enabledOriginal;
00115     int32_t setValue;
00116     int32_t getValue;
00117     
00118     // Calls should return false if the battery charger has not been initialised
00119     TEST_ASSERT_FALSE(pBatteryCharger->getInputCurrentLimit(&getValue));
00120     TEST_ASSERT_FALSE(pBatteryCharger->setInputVoltageLimit(getValue));
00121     TEST_ASSERT_FALSE(pBatteryCharger->getInputCurrentLimit(&getValue));
00122     TEST_ASSERT_FALSE(pBatteryCharger->setInputVoltageLimit(getValue));
00123     TEST_ASSERT_FALSE(pBatteryCharger->enableInputLimits());
00124     TEST_ASSERT_FALSE(pBatteryCharger->disableInputLimits());
00125     
00126     // Initialise the battery charger
00127     TEST_ASSERT(pBatteryCharger->init(gpI2C));
00128     
00129     // Save the initial values
00130     TEST_ASSERT(pBatteryCharger->getInputVoltageLimit(&voltageOriginal));
00131     TEST_ASSERT(pBatteryCharger->getInputCurrentLimit(&currentOriginal));
00132     enabledOriginal = pBatteryCharger->areInputLimitsEnabled();
00133 
00134     // Voltage and current beyond the limits
00135     TEST_ASSERT_FALSE(pBatteryCharger->setInputVoltageLimit(MIN_INPUT_VOLTAGE_LIMIT_MV - 1));
00136     TEST_ASSERT_FALSE(pBatteryCharger->setInputVoltageLimit(MAX_INPUT_VOLTAGE_LIMIT_MV + 1));
00137     TEST_ASSERT_FALSE(pBatteryCharger->setInputCurrentLimit(MIN_INPUT_CURRENT_LIMIT_MA - 1));
00138     TEST_ASSERT_FALSE(pBatteryCharger->setInputCurrentLimit(MAX_INPUT_CURRENT_LIMIT_MA + 1));
00139     
00140     // Voltage and current at the limits
00141     TEST_ASSERT(pBatteryCharger->setInputVoltageLimit(MIN_INPUT_VOLTAGE_LIMIT_MV));
00142     TEST_ASSERT(pBatteryCharger->getInputVoltageLimit(&getValue));
00143     TEST_ASSERT_EQUAL_INT32(MIN_INPUT_VOLTAGE_LIMIT_MV, getValue);
00144     TEST_ASSERT(pBatteryCharger->setInputVoltageLimit(MAX_INPUT_VOLTAGE_LIMIT_MV));
00145     TEST_ASSERT(pBatteryCharger->getInputVoltageLimit(&getValue));
00146     TEST_ASSERT_EQUAL_INT32(MAX_INPUT_VOLTAGE_LIMIT_MV, getValue);
00147     TEST_ASSERT(pBatteryCharger->setInputCurrentLimit(MIN_INPUT_CURRENT_LIMIT_MA));
00148     TEST_ASSERT(pBatteryCharger->getInputCurrentLimit(&getValue));
00149     TEST_ASSERT_EQUAL_INT32(MIN_INPUT_CURRENT_LIMIT_MA, getValue);
00150     TEST_ASSERT(pBatteryCharger->setInputCurrentLimit(MAX_INPUT_CURRENT_LIMIT_MA));
00151     TEST_ASSERT(pBatteryCharger->getInputCurrentLimit(&getValue));
00152     TEST_ASSERT_EQUAL_INT32(MAX_INPUT_CURRENT_LIMIT_MA, getValue);
00153 
00154     // The voltage limit read back should not be more than 80 mV below the value requested
00155     for (uint32_t x = 0; x < NUM_RAND_ITERATIONS; x++) {
00156         setValue = MIN_INPUT_VOLTAGE_LIMIT_MV + rand() % (MAX_INPUT_VOLTAGE_LIMIT_MV - MIN_INPUT_VOLTAGE_LIMIT_MV + 1);
00157         TEST_ASSERT(pBatteryCharger->setInputVoltageLimit(setValue));
00158         getValue = -1;
00159         TEST_ASSERT(pBatteryCharger->getInputVoltageLimit(&getValue));
00160         TEST_ASSERT((getValue > setValue - 80) && (getValue <= setValue));
00161         TEST_ASSERT((getValue >= MIN_INPUT_VOLTAGE_LIMIT_MV) && (getValue <= MAX_INPUT_VOLTAGE_LIMIT_MV));
00162     }
00163 
00164     // The current limit read back should always be less than or equal to the set value
00165     for (uint32_t x = 0; x < NUM_RAND_ITERATIONS; x++) {
00166         setValue = MIN_INPUT_CURRENT_LIMIT_MA + rand() % (MAX_INPUT_CURRENT_LIMIT_MA - MIN_INPUT_CURRENT_LIMIT_MA + 1);
00167         TEST_ASSERT(pBatteryCharger->setInputCurrentLimit(setValue));
00168         getValue = -1;
00169         TEST_ASSERT(pBatteryCharger->getInputCurrentLimit(&getValue));
00170         TEST_ASSERT(getValue <= setValue);
00171         TEST_ASSERT((getValue >= MIN_INPUT_CURRENT_LIMIT_MA) && (getValue <= MAX_INPUT_CURRENT_LIMIT_MA));
00172     }
00173     
00174     // Enable and disable the limits
00175     TEST_ASSERT(pBatteryCharger->enableInputLimits());
00176     TEST_ASSERT(pBatteryCharger->areInputLimitsEnabled());
00177     TEST_ASSERT(pBatteryCharger->disableInputLimits());
00178     TEST_ASSERT_FALSE(pBatteryCharger->areInputLimitsEnabled());
00179     TEST_ASSERT(pBatteryCharger->enableInputLimits());
00180     TEST_ASSERT(pBatteryCharger->areInputLimitsEnabled());
00181     
00182     // Parameters can be NULL
00183     TEST_ASSERT(pBatteryCharger->getInputVoltageLimit(NULL));
00184     TEST_ASSERT(pBatteryCharger->getInputCurrentLimit(NULL));
00185 
00186     // Put the initial values back when we're done
00187     TEST_ASSERT(pBatteryCharger->setInputVoltageLimit(voltageOriginal));
00188     TEST_ASSERT(pBatteryCharger->setInputCurrentLimit(currentOriginal));
00189     if (enabledOriginal) {
00190         pBatteryCharger->enableInputLimits();
00191     } else {
00192         pBatteryCharger->disableInputLimits();
00193     }
00194 }
00195 
00196 // Test that we enable and disable OTG and normal charging
00197 void test_charging_enable() {
00198     BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
00199     bool otgEnabled;
00200     bool chargingEnabled;
00201     
00202     // Call should fail if the battery charger has not been initialised
00203     TEST_ASSERT_FALSE(pBatteryCharger->enableOtg());
00204     TEST_ASSERT_FALSE(pBatteryCharger->disableOtg());
00205     TEST_ASSERT_FALSE(pBatteryCharger->enableCharging());
00206     TEST_ASSERT_FALSE(pBatteryCharger->disableCharging());
00207     
00208     // Initialise the battery charger
00209     TEST_ASSERT(pBatteryCharger->init(gpI2C));
00210     
00211     // Save the initial values
00212     otgEnabled = pBatteryCharger->isOtgEnabled();
00213     chargingEnabled = pBatteryCharger->isChargingEnabled();
00214 
00215     // Enable and disable OTG
00216     TEST_ASSERT(pBatteryCharger->enableOtg());
00217     TEST_ASSERT(pBatteryCharger->isOtgEnabled());
00218     TEST_ASSERT(pBatteryCharger->disableOtg());
00219     TEST_ASSERT_FALSE(pBatteryCharger->isOtgEnabled());
00220     TEST_ASSERT(pBatteryCharger->enableOtg());
00221     TEST_ASSERT(pBatteryCharger->isOtgEnabled());
00222     
00223     // Enable and disable charging
00224     TEST_ASSERT(pBatteryCharger->enableCharging());
00225     TEST_ASSERT(pBatteryCharger->isChargingEnabled());
00226     TEST_ASSERT(pBatteryCharger->disableCharging());
00227     TEST_ASSERT_FALSE(pBatteryCharger->isChargingEnabled());
00228     TEST_ASSERT(pBatteryCharger->enableCharging());
00229     TEST_ASSERT(pBatteryCharger->isChargingEnabled());
00230     
00231     // Put the initial values back when we're done
00232     if (otgEnabled) {
00233         pBatteryCharger->enableOtg();
00234     } else {
00235         pBatteryCharger->disableOtg();
00236     }
00237     if (chargingEnabled) {
00238         pBatteryCharger->enableCharging();
00239     } else {
00240         pBatteryCharger->disableCharging();
00241     }
00242 }
00243 
00244 // Test that we can read and change the system voltage
00245 void test_system_voltage() {
00246     BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
00247     int32_t voltageOriginal;
00248     int32_t setValue;
00249     int32_t getValue;
00250     
00251     // Calls should return false if the battery charger has not been initialised
00252     TEST_ASSERT_FALSE(pBatteryCharger->getSystemVoltage(&getValue));
00253     TEST_ASSERT_FALSE(pBatteryCharger->setSystemVoltage(getValue));
00254     
00255     // Initialise the battery charger
00256     TEST_ASSERT(pBatteryCharger->init(gpI2C));
00257     
00258     // Save the initial value
00259     TEST_ASSERT(pBatteryCharger->getSystemVoltage(&voltageOriginal));
00260 
00261     // Beyond the limits
00262     TEST_ASSERT_FALSE(pBatteryCharger->setSystemVoltage(MIN_SYSTEM_VOLTAGE_MV - 1));
00263     TEST_ASSERT_FALSE(pBatteryCharger->setSystemVoltage(MAX_SYSTEM_VOLTAGE_MV + 1));
00264     
00265     // At the limits
00266     TEST_ASSERT(pBatteryCharger->setSystemVoltage(MIN_SYSTEM_VOLTAGE_MV));
00267     TEST_ASSERT(pBatteryCharger->getSystemVoltage(&getValue));
00268     TEST_ASSERT_EQUAL_INT32(MIN_SYSTEM_VOLTAGE_MV, getValue);
00269     TEST_ASSERT(pBatteryCharger->setSystemVoltage(MAX_SYSTEM_VOLTAGE_MV));
00270     TEST_ASSERT(pBatteryCharger->getSystemVoltage(&getValue));
00271     TEST_ASSERT_EQUAL_INT32(MAX_SYSTEM_VOLTAGE_MV, getValue);
00272 
00273     // The voltage read back should be at least the value requested and
00274     // not more than 100 mV greater
00275     for (uint32_t x = 0; x < NUM_RAND_ITERATIONS; x++) {
00276         setValue = MIN_SYSTEM_VOLTAGE_MV + rand() % (MAX_SYSTEM_VOLTAGE_MV - MIN_SYSTEM_VOLTAGE_MV + 1);
00277         TEST_ASSERT(pBatteryCharger->setSystemVoltage(setValue));
00278         getValue = -1;
00279         TEST_ASSERT(pBatteryCharger->getSystemVoltage(&getValue));
00280         TEST_ASSERT((getValue < setValue + 100) && (getValue >= setValue));
00281         TEST_ASSERT((getValue >= MIN_SYSTEM_VOLTAGE_MV) && (getValue <= MAX_SYSTEM_VOLTAGE_MV));
00282     }
00283 
00284     // Parameter can be NULL
00285     TEST_ASSERT(pBatteryCharger->getSystemVoltage(NULL));
00286 
00287     // Put the initial value back when we're done
00288     TEST_ASSERT(pBatteryCharger->setSystemVoltage(voltageOriginal));
00289 }
00290 
00291 // Test that we can read and change the fast charging current limits
00292 void test_fast_charging_current_limits() {
00293     BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
00294     int32_t currentOriginal;
00295     int32_t setValue;
00296     int32_t getValue;
00297     
00298     // Calls should return false if the battery charger has not been initialised
00299     TEST_ASSERT_FALSE(pBatteryCharger->getFastChargingCurrentLimit(&getValue));
00300     TEST_ASSERT_FALSE(pBatteryCharger->setFastChargingCurrentLimit(getValue));
00301     
00302     // Initialise the battery charger
00303     TEST_ASSERT(pBatteryCharger->init(gpI2C));
00304     
00305     // Save the initial value
00306     TEST_ASSERT(pBatteryCharger->getFastChargingCurrentLimit(&currentOriginal));
00307 
00308     // Beyond the limits
00309     TEST_ASSERT_FALSE(pBatteryCharger->setFastChargingCurrentLimit(MIN_FAST_CHARGING_CURRENT_LIMIT_MA - 1));
00310     TEST_ASSERT_FALSE(pBatteryCharger->setFastChargingCurrentLimit(MAX_FAST_CHARGING_CURRENT_LIMIT_MA + 1));
00311     
00312     // At the limits
00313     TEST_ASSERT(pBatteryCharger->setFastChargingCurrentLimit(MIN_FAST_CHARGING_CURRENT_LIMIT_MA));
00314     TEST_ASSERT(pBatteryCharger->getFastChargingCurrentLimit(&getValue));
00315     TEST_ASSERT_EQUAL_INT32(MIN_FAST_CHARGING_CURRENT_LIMIT_MA, getValue);
00316     TEST_ASSERT(pBatteryCharger->setFastChargingCurrentLimit(MAX_FAST_CHARGING_CURRENT_LIMIT_MA));
00317     TEST_ASSERT(pBatteryCharger->getFastChargingCurrentLimit(&getValue));
00318     TEST_ASSERT_EQUAL_INT32(MAX_FAST_CHARGING_CURRENT_LIMIT_MA, getValue);
00319 
00320     // The current limit read back should not be more than 64 mA below the value requested
00321     for (uint32_t x = 0; x < NUM_RAND_ITERATIONS; x++) {
00322         setValue = MIN_FAST_CHARGING_CURRENT_LIMIT_MA + rand() % (MAX_FAST_CHARGING_CURRENT_LIMIT_MA - MIN_FAST_CHARGING_CURRENT_LIMIT_MA + 1);
00323         TEST_ASSERT(pBatteryCharger->setFastChargingCurrentLimit(setValue));
00324         getValue = -1;
00325         TEST_ASSERT(pBatteryCharger->getFastChargingCurrentLimit(&getValue));
00326         TEST_ASSERT(getValue <= setValue);
00327         TEST_ASSERT((getValue >= MIN_FAST_CHARGING_CURRENT_LIMIT_MA) && (getValue <= MAX_FAST_CHARGING_CURRENT_LIMIT_MA));
00328     }
00329     
00330     // Parameter can be NULL
00331     TEST_ASSERT(pBatteryCharger->getFastChargingCurrentLimit(NULL));
00332 
00333     // Put the initial value back when we're done
00334     TEST_ASSERT(pBatteryCharger->setFastChargingCurrentLimit(currentOriginal));
00335 }
00336 
00337 // Test that we can enable and disable the ICGH/IPRECH margin
00338 void test_icgh_iprech_margin() {
00339     BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
00340     bool marginEnabled;
00341     
00342     // Call should fail if the battery charger has not been initialised
00343     TEST_ASSERT_FALSE(pBatteryCharger->isIcghIprechMarginEnabled());
00344     TEST_ASSERT_FALSE(pBatteryCharger->enableIcghIprechMargin());
00345     TEST_ASSERT_FALSE(pBatteryCharger->disableIcghIprechMargin());
00346     
00347     // Initialise the battery charger
00348     TEST_ASSERT(pBatteryCharger->init(gpI2C));
00349     
00350     // Save the initial value
00351     marginEnabled = pBatteryCharger->isIcghIprechMarginEnabled();
00352 
00353     // Enable and disable the margin
00354     TEST_ASSERT(pBatteryCharger->enableIcghIprechMargin());
00355     TEST_ASSERT(pBatteryCharger->isIcghIprechMarginEnabled());
00356     TEST_ASSERT(pBatteryCharger->disableIcghIprechMargin());
00357     TEST_ASSERT_FALSE(pBatteryCharger->isIcghIprechMarginEnabled());
00358     TEST_ASSERT(pBatteryCharger->enableIcghIprechMargin());
00359     TEST_ASSERT(pBatteryCharger->isIcghIprechMarginEnabled());
00360     
00361     // Put the initial value back when we're done
00362     if (marginEnabled) {
00363         pBatteryCharger->enableIcghIprechMargin();
00364     } else {
00365         pBatteryCharger->disableIcghIprechMargin();
00366     }
00367 }
00368 
00369 // Test that we can read and change the pre-charging current limits
00370 void test_precharging_current_limits() {
00371     BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
00372     int32_t currentOriginal;
00373     int32_t setValue;
00374     int32_t getValue;
00375     
00376     // Calls should return false if the battery charger has not been initialised
00377     TEST_ASSERT_FALSE(pBatteryCharger->getPrechargingCurrentLimit(&getValue));
00378     TEST_ASSERT_FALSE(pBatteryCharger->setPrechargingCurrentLimit(getValue));
00379     
00380     // Initialise the battery charger
00381     TEST_ASSERT(pBatteryCharger->init(gpI2C));
00382     
00383     // Save the initial value
00384     TEST_ASSERT(pBatteryCharger->getPrechargingCurrentLimit(&currentOriginal));
00385 
00386     // Beyond the limits
00387     TEST_ASSERT_FALSE(pBatteryCharger->setPrechargingCurrentLimit(MIN_PRECHARGING_CURRENT_LIMIT_MA - 1));
00388     TEST_ASSERT_FALSE(pBatteryCharger->setPrechargingCurrentLimit(MAX_PRECHARGING_CURRENT_LIMIT_MA + 1));
00389     
00390     // At the limits
00391     TEST_ASSERT(pBatteryCharger->setPrechargingCurrentLimit(MIN_PRECHARGING_CURRENT_LIMIT_MA));
00392     TEST_ASSERT(pBatteryCharger->getPrechargingCurrentLimit(&getValue));
00393     TEST_ASSERT_EQUAL_INT32(MIN_PRECHARGING_CURRENT_LIMIT_MA, getValue);
00394     TEST_ASSERT(pBatteryCharger->setPrechargingCurrentLimit(MAX_PRECHARGING_CURRENT_LIMIT_MA));
00395     TEST_ASSERT(pBatteryCharger->getPrechargingCurrentLimit(&getValue));
00396     TEST_ASSERT_EQUAL_INT32(MAX_PRECHARGING_CURRENT_LIMIT_MA, getValue);
00397 
00398     // The current limit read back should not be more than 128 mA below the value requested
00399     for (uint32_t x = 0; x < NUM_RAND_ITERATIONS; x++) {
00400         setValue = MIN_PRECHARGING_CURRENT_LIMIT_MA + rand() % (MAX_PRECHARGING_CURRENT_LIMIT_MA - MIN_PRECHARGING_CURRENT_LIMIT_MA + 1);
00401         TEST_ASSERT(pBatteryCharger->setPrechargingCurrentLimit(setValue));
00402         getValue = -1;
00403         TEST_ASSERT(pBatteryCharger->getPrechargingCurrentLimit(&getValue));
00404         TEST_ASSERT(getValue <= setValue);
00405         TEST_ASSERT((getValue >= MIN_PRECHARGING_CURRENT_LIMIT_MA) && (getValue <= MAX_PRECHARGING_CURRENT_LIMIT_MA));
00406     }
00407     
00408     // Parameter can be NULL
00409     TEST_ASSERT(pBatteryCharger->getPrechargingCurrentLimit(NULL));
00410 
00411     // Put the initial value back when we're done
00412     TEST_ASSERT(pBatteryCharger->setPrechargingCurrentLimit(currentOriginal));
00413 }
00414 
00415 // Test that we can read, change and enable/disable the charging termination current
00416 void test_charging_termination_current() {
00417     BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
00418     int32_t currentOriginal;
00419     bool terminationEnabled;
00420     int32_t setValue;
00421     int32_t getValue;
00422     
00423     // Calls should return false if the battery charger has not been initialised
00424     TEST_ASSERT_FALSE(pBatteryCharger->getChargingTerminationCurrent(&getValue));
00425     TEST_ASSERT_FALSE(pBatteryCharger->setChargingTerminationCurrent(getValue));
00426     TEST_ASSERT_FALSE(pBatteryCharger->enableChargingTermination());
00427     TEST_ASSERT_FALSE(pBatteryCharger->disableChargingTermination());
00428     
00429     // Initialise the battery charger
00430     TEST_ASSERT(pBatteryCharger->init(gpI2C));
00431     
00432     // Save the initial values
00433     TEST_ASSERT(pBatteryCharger->getChargingTerminationCurrent(&currentOriginal));
00434     terminationEnabled = pBatteryCharger->isChargingTerminationEnabled();
00435 
00436     // Beyond the limits
00437     TEST_ASSERT_FALSE(pBatteryCharger->setChargingTerminationCurrent(MIN_CHARGING_TERMINATION_CURRENT_MA - 1));
00438     TEST_ASSERT_FALSE(pBatteryCharger->setChargingTerminationCurrent(MAX_CHARGING_TERMINATION_CURRENT_MA + 1));
00439     
00440     // At the limits
00441     TEST_ASSERT(pBatteryCharger->setChargingTerminationCurrent(MIN_CHARGING_TERMINATION_CURRENT_MA));
00442     TEST_ASSERT(pBatteryCharger->getChargingTerminationCurrent(&getValue));
00443     TEST_ASSERT_EQUAL_INT32(MIN_CHARGING_TERMINATION_CURRENT_MA, getValue);
00444     TEST_ASSERT(pBatteryCharger->setChargingTerminationCurrent(MAX_CHARGING_TERMINATION_CURRENT_MA));
00445     TEST_ASSERT(pBatteryCharger->getChargingTerminationCurrent(&getValue));
00446     TEST_ASSERT_EQUAL_INT32(MAX_CHARGING_TERMINATION_CURRENT_MA, getValue);
00447 
00448     // The current limit read back should not be more than 128 mA below the value requested
00449     for (uint32_t x = 0; x < NUM_RAND_ITERATIONS; x++) {
00450         setValue = MIN_CHARGING_TERMINATION_CURRENT_MA + rand() % (MAX_CHARGING_TERMINATION_CURRENT_MA - MIN_CHARGING_TERMINATION_CURRENT_MA + 1);
00451         TEST_ASSERT(pBatteryCharger->setChargingTerminationCurrent(setValue));
00452         getValue = -1;
00453         TEST_ASSERT(pBatteryCharger->getChargingTerminationCurrent(&getValue));
00454         TEST_ASSERT(getValue <= setValue);
00455         TEST_ASSERT((getValue >= MIN_CHARGING_TERMINATION_CURRENT_MA) && (getValue <= MAX_CHARGING_TERMINATION_CURRENT_MA));
00456     }
00457     
00458     // Enable and disable the margin
00459     TEST_ASSERT(pBatteryCharger->enableChargingTermination());
00460     TEST_ASSERT(pBatteryCharger->isChargingTerminationEnabled());
00461     TEST_ASSERT(pBatteryCharger->disableChargingTermination());
00462     TEST_ASSERT_FALSE(pBatteryCharger->isChargingTerminationEnabled());
00463     TEST_ASSERT(pBatteryCharger->enableChargingTermination());
00464     TEST_ASSERT(pBatteryCharger->isChargingTerminationEnabled());
00465     
00466     // Parameter can be NULL
00467     TEST_ASSERT(pBatteryCharger->getChargingTerminationCurrent(NULL));
00468 
00469     // Put the initial value back when we're done
00470     TEST_ASSERT(pBatteryCharger->setChargingTerminationCurrent(currentOriginal));
00471     if (terminationEnabled) {
00472         pBatteryCharger->enableChargingTermination();
00473     } else {
00474         pBatteryCharger->disableChargingTermination();
00475     }
00476 }
00477 
00478 // Test that we can read and change the various charging voltage limits
00479 void test_charging_voltage_limits() {
00480     BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
00481     int32_t chargingOriginal;
00482     int32_t fastChargingOriginal;
00483     int32_t rechargingOriginal;
00484     int32_t setValue;
00485     int32_t getValue;
00486     
00487     // Calls should return false if the battery charger has not been initialised
00488     TEST_ASSERT_FALSE(pBatteryCharger->getChargingVoltageLimit(&getValue));
00489     TEST_ASSERT_FALSE(pBatteryCharger->setChargingVoltageLimit(getValue));
00490     TEST_ASSERT_FALSE(pBatteryCharger->getFastChargingVoltageThreshold(&getValue));
00491     TEST_ASSERT_FALSE(pBatteryCharger->setFastChargingVoltageThreshold(getValue));
00492     TEST_ASSERT_FALSE(pBatteryCharger->getRechargingVoltageThreshold(&getValue));
00493     TEST_ASSERT_FALSE(pBatteryCharger->setRechargingVoltageThreshold(getValue));
00494     
00495     // Initialise the battery charger
00496     TEST_ASSERT(pBatteryCharger->init(gpI2C));
00497     
00498     // Save the initial values
00499     TEST_ASSERT(pBatteryCharger->getChargingVoltageLimit(&chargingOriginal));
00500     TEST_ASSERT(pBatteryCharger->getFastChargingVoltageThreshold(&fastChargingOriginal));
00501     TEST_ASSERT(pBatteryCharger->getRechargingVoltageThreshold(&rechargingOriginal));
00502 
00503     // Beyond the limits for the charging voltage
00504     TEST_ASSERT_FALSE(pBatteryCharger->setChargingVoltageLimit(MIN_CHARGING_VOLTAGE_LIMIT_MV - 1));
00505     TEST_ASSERT_FALSE(pBatteryCharger->setChargingVoltageLimit(MAX_CHARGING_VOLTAGE_LIMIT_MV + 1));
00506     
00507     // At the limits for the charging voltage
00508     TEST_ASSERT(pBatteryCharger->setChargingVoltageLimit(MIN_CHARGING_VOLTAGE_LIMIT_MV));
00509     TEST_ASSERT(pBatteryCharger->getChargingVoltageLimit(&getValue));
00510     TEST_ASSERT_EQUAL_INT32(MIN_CHARGING_VOLTAGE_LIMIT_MV, getValue);
00511     TEST_ASSERT(pBatteryCharger->setChargingVoltageLimit(MAX_CHARGING_VOLTAGE_LIMIT_MV));
00512     TEST_ASSERT(pBatteryCharger->getChargingVoltageLimit(&getValue));
00513     TEST_ASSERT_EQUAL_INT32(MAX_CHARGING_VOLTAGE_LIMIT_MV, getValue);
00514     
00515     // The charging voltage limit read back should not be more than 16 mV below the value requested
00516     for (uint32_t x = 0; x < NUM_RAND_ITERATIONS; x++) {
00517         setValue = MIN_CHARGING_VOLTAGE_LIMIT_MV + rand() % (MAX_CHARGING_VOLTAGE_LIMIT_MV - MIN_CHARGING_VOLTAGE_LIMIT_MV + 1);
00518         TEST_ASSERT(pBatteryCharger->setChargingVoltageLimit(setValue));
00519         getValue = -1;
00520         TEST_ASSERT(pBatteryCharger->getChargingVoltageLimit(&getValue));
00521         TEST_ASSERT((getValue > setValue - 16) && (getValue <= setValue));
00522         TEST_ASSERT((getValue >= MIN_CHARGING_VOLTAGE_LIMIT_MV) && (getValue <= MAX_CHARGING_VOLTAGE_LIMIT_MV));
00523     }
00524 
00525     // Fast charging threshold is 2.8 V or 3.0 V
00526     TEST_ASSERT(pBatteryCharger->setFastChargingVoltageThreshold(2799));
00527     TEST_ASSERT(pBatteryCharger->getFastChargingVoltageThreshold(&getValue));
00528     TEST_ASSERT_EQUAL_INT32(2800, getValue);
00529     TEST_ASSERT(pBatteryCharger->setFastChargingVoltageThreshold(2800));
00530     TEST_ASSERT(pBatteryCharger->getFastChargingVoltageThreshold(&getValue));
00531     TEST_ASSERT_EQUAL_INT32(2800, getValue);
00532     TEST_ASSERT(pBatteryCharger->setFastChargingVoltageThreshold(2801));
00533     TEST_ASSERT(pBatteryCharger->getFastChargingVoltageThreshold(&getValue));
00534     TEST_ASSERT_EQUAL_INT32(3000, getValue);
00535     TEST_ASSERT(pBatteryCharger->setFastChargingVoltageThreshold(3000));
00536     TEST_ASSERT(pBatteryCharger->getFastChargingVoltageThreshold(&getValue));
00537     TEST_ASSERT_EQUAL_INT32(3000, getValue);
00538     TEST_ASSERT(pBatteryCharger->setFastChargingVoltageThreshold(3001));
00539     TEST_ASSERT(pBatteryCharger->getFastChargingVoltageThreshold(&getValue));
00540     TEST_ASSERT_EQUAL_INT32(3000, getValue);
00541     
00542     // Recharging threshold is 100 mV or 300 mV
00543     TEST_ASSERT(pBatteryCharger->setRechargingVoltageThreshold(99));
00544     TEST_ASSERT(pBatteryCharger->getRechargingVoltageThreshold(&getValue));
00545     TEST_ASSERT_EQUAL_INT32(100, getValue);
00546     TEST_ASSERT(pBatteryCharger->setRechargingVoltageThreshold(100));
00547     TEST_ASSERT(pBatteryCharger->getRechargingVoltageThreshold(&getValue));
00548     TEST_ASSERT_EQUAL_INT32(100, getValue);
00549     TEST_ASSERT(pBatteryCharger->setRechargingVoltageThreshold(101));
00550     TEST_ASSERT(pBatteryCharger->getRechargingVoltageThreshold(&getValue));
00551     TEST_ASSERT_EQUAL_INT32(300, getValue);
00552     TEST_ASSERT(pBatteryCharger->setRechargingVoltageThreshold(300));
00553     TEST_ASSERT(pBatteryCharger->getRechargingVoltageThreshold(&getValue));
00554     TEST_ASSERT_EQUAL_INT32(300, getValue);
00555     TEST_ASSERT(pBatteryCharger->setRechargingVoltageThreshold(301));
00556     TEST_ASSERT(pBatteryCharger->getRechargingVoltageThreshold(&getValue));
00557     TEST_ASSERT_EQUAL_INT32(300, getValue);
00558     
00559     // Parameters can be NULL
00560     TEST_ASSERT(pBatteryCharger->getChargingVoltageLimit(NULL));
00561     TEST_ASSERT(pBatteryCharger->getFastChargingVoltageThreshold(NULL));
00562     TEST_ASSERT(pBatteryCharger->getRechargingVoltageThreshold(NULL));
00563 
00564     // Put the initial values back when we're done
00565     TEST_ASSERT(pBatteryCharger->setChargingVoltageLimit(chargingOriginal));
00566     TEST_ASSERT(pBatteryCharger->setFastChargingVoltageThreshold(fastChargingOriginal));
00567     TEST_ASSERT(pBatteryCharger->setRechargingVoltageThreshold(rechargingOriginal));
00568 }
00569 
00570 // Test that we can read and change the fast charging safety timer
00571 void test_fast_charging_safety_timer() {
00572     BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
00573     int32_t timerOriginal;
00574     int32_t getValue;
00575     
00576     // Calls should return false if the battery charger has not been initialised
00577     TEST_ASSERT_FALSE(pBatteryCharger->getFastChargingSafetyTimer(&getValue));
00578     TEST_ASSERT_FALSE(pBatteryCharger->setFastChargingSafetyTimer(getValue));
00579     
00580     // Initialise the battery charger
00581     TEST_ASSERT(pBatteryCharger->init(gpI2C));
00582     
00583     // Save the initial value
00584     TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&timerOriginal));
00585 
00586     // Beyond the limit
00587     TEST_ASSERT_FALSE(pBatteryCharger->setFastChargingSafetyTimer(-1));
00588     
00589     // There are permissible values are 0, 5, 8, 12 and 20 so test them and the
00590     // boundaries around them
00591     TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(0));
00592     TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue));
00593     TEST_ASSERT_EQUAL_INT32(0, getValue);
00594     TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(1));
00595     TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue));
00596     TEST_ASSERT_EQUAL_INT32(5, getValue);
00597     TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(4));
00598     TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue));
00599     TEST_ASSERT_EQUAL_INT32(5, getValue);
00600     TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(6));
00601     TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue));
00602     TEST_ASSERT_EQUAL_INT32(5, getValue);
00603     TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(7));
00604     TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue));
00605     TEST_ASSERT_EQUAL_INT32(5, getValue);
00606     TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(8));
00607     TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue));
00608     TEST_ASSERT_EQUAL_INT32(8, getValue);
00609     TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(11));
00610     TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue));
00611     TEST_ASSERT_EQUAL_INT32(8, getValue);
00612     TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(12));
00613     TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue));
00614     TEST_ASSERT_EQUAL_INT32(12, getValue);
00615     TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(19));
00616     TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue));
00617     TEST_ASSERT_EQUAL_INT32(12, getValue);
00618     TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(20));
00619     TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue));
00620     TEST_ASSERT_EQUAL_INT32(20, getValue);
00621 
00622     // Parameter can be NULL
00623     TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(NULL));
00624 
00625     // Put the initial value back when we're done
00626     TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(timerOriginal));
00627 }
00628 
00629 // Test setting the boost mode voltage and temperature limits
00630 void test_boost_limits() {
00631     BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
00632     int32_t originalVoltage;
00633     int32_t originalUpperTemperature;
00634     bool upperTemperatureEnabled;
00635     int32_t originalLowerTemperature;
00636     int32_t setValue;
00637     int32_t getValue;
00638     
00639     // Calls should return false if the battery charger has not been initialised
00640     TEST_ASSERT_FALSE(pBatteryCharger->getBoostVoltage(&getValue));
00641     TEST_ASSERT_FALSE(pBatteryCharger->setBoostVoltage(getValue));
00642     TEST_ASSERT_FALSE(pBatteryCharger->getBoostUpperTemperatureLimit(&getValue));
00643     TEST_ASSERT_FALSE(pBatteryCharger->setBoostUpperTemperatureLimit(getValue));
00644     TEST_ASSERT_FALSE(pBatteryCharger->disableBoostUpperTemperatureLimit());
00645     TEST_ASSERT_FALSE(pBatteryCharger->getBoostLowerTemperatureLimit(&getValue));
00646     TEST_ASSERT_FALSE(pBatteryCharger->setBoostLowerTemperatureLimit(getValue));
00647     
00648     // Initialise the battery charger
00649     TEST_ASSERT(pBatteryCharger->init(gpI2C));
00650     
00651     // Save the initial values
00652     TEST_ASSERT(pBatteryCharger->getBoostVoltage(&originalVoltage));
00653     upperTemperatureEnabled = pBatteryCharger->isBoostUpperTemperatureLimitEnabled();
00654     if (upperTemperatureEnabled) {
00655         TEST_ASSERT(pBatteryCharger->getBoostUpperTemperatureLimit(&originalUpperTemperature));
00656     }
00657     TEST_ASSERT(pBatteryCharger->getBoostLowerTemperatureLimit(&originalLowerTemperature));
00658 
00659     // Beyond the limits for the voltage
00660     TEST_ASSERT_FALSE(pBatteryCharger->setBoostVoltage(MIN_BOOST_VOLTAGE_MV - 1));
00661     TEST_ASSERT_FALSE(pBatteryCharger->setBoostVoltage(MAX_BOOST_VOLTAGE_MV + 1));
00662     
00663     // At the limits for the voltage
00664     TEST_ASSERT(pBatteryCharger->setBoostVoltage(MIN_BOOST_VOLTAGE_MV));
00665     TEST_ASSERT(pBatteryCharger->getBoostVoltage(&getValue));
00666     TEST_ASSERT_EQUAL_INT32(MIN_BOOST_VOLTAGE_MV, getValue);
00667     TEST_ASSERT(pBatteryCharger->setBoostVoltage(MAX_BOOST_VOLTAGE_MV));
00668     TEST_ASSERT(pBatteryCharger->getBoostVoltage(&getValue));
00669     TEST_ASSERT_EQUAL_INT32(MAX_BOOST_VOLTAGE_MV, getValue);
00670     
00671     // The voltage read back should be at least the value requested and
00672     // not more than 64 mV greater
00673     for (uint32_t x = 0; x < NUM_RAND_ITERATIONS; x++) {
00674         setValue = MIN_BOOST_VOLTAGE_MV + rand() % (MAX_BOOST_VOLTAGE_MV - MIN_BOOST_VOLTAGE_MV + 1);
00675         TEST_ASSERT(pBatteryCharger->setBoostVoltage(setValue));
00676         getValue = -1;
00677         TEST_ASSERT(pBatteryCharger->getBoostVoltage(&getValue));
00678         TEST_ASSERT((getValue < setValue + 64) && (getValue >= setValue));
00679         TEST_ASSERT((getValue >= MIN_BOOST_VOLTAGE_MV) && (getValue <= MAX_BOOST_VOLTAGE_MV));
00680     }
00681 
00682     // Disable the upper temperature limit and check that the get function returns false
00683     TEST_ASSERT(pBatteryCharger->disableBoostUpperTemperatureLimit());
00684     TEST_ASSERT_FALSE(pBatteryCharger->isBoostUpperTemperatureLimitEnabled());
00685     TEST_ASSERT_FALSE(pBatteryCharger->getBoostUpperTemperatureLimit(&getValue));
00686     
00687     // The boost upper temperature limit is either 55, 60 or 65
00688     TEST_ASSERT(pBatteryCharger->setBoostUpperTemperatureLimit(-1));
00689     TEST_ASSERT(pBatteryCharger->getBoostUpperTemperatureLimit(&getValue));
00690     TEST_ASSERT_EQUAL_INT32(55, getValue);
00691     TEST_ASSERT(pBatteryCharger->setBoostUpperTemperatureLimit(0));
00692     TEST_ASSERT(pBatteryCharger->getBoostUpperTemperatureLimit(&getValue));
00693     TEST_ASSERT_EQUAL_INT32(55, getValue);
00694     TEST_ASSERT(pBatteryCharger->setBoostUpperTemperatureLimit(59));
00695     TEST_ASSERT(pBatteryCharger->getBoostUpperTemperatureLimit(&getValue));
00696     TEST_ASSERT_EQUAL_INT32(55, getValue);
00697     TEST_ASSERT(pBatteryCharger->setBoostUpperTemperatureLimit(60));
00698     TEST_ASSERT(pBatteryCharger->getBoostUpperTemperatureLimit(&getValue));
00699     TEST_ASSERT_EQUAL_INT32(60, getValue);
00700     TEST_ASSERT(pBatteryCharger->setBoostUpperTemperatureLimit(64));
00701     TEST_ASSERT(pBatteryCharger->getBoostUpperTemperatureLimit(&getValue));
00702     TEST_ASSERT_EQUAL_INT32(60, getValue);
00703     TEST_ASSERT(pBatteryCharger->setBoostUpperTemperatureLimit(65));
00704     TEST_ASSERT(pBatteryCharger->getBoostUpperTemperatureLimit(&getValue));
00705     TEST_ASSERT_EQUAL_INT32(65, getValue);
00706     TEST_ASSERT(pBatteryCharger->setBoostUpperTemperatureLimit(100));
00707     TEST_ASSERT(pBatteryCharger->getBoostUpperTemperatureLimit(&getValue));
00708     TEST_ASSERT_EQUAL_INT32(65, getValue);
00709     
00710     // The boost lower temperature is either -10 or -20
00711     TEST_ASSERT(pBatteryCharger->setBoostLowerTemperatureLimit(1));
00712     TEST_ASSERT(pBatteryCharger->getBoostLowerTemperatureLimit(&getValue));
00713     TEST_ASSERT_EQUAL_INT32(-10, getValue);
00714     TEST_ASSERT(pBatteryCharger->setBoostLowerTemperatureLimit(0));
00715     TEST_ASSERT(pBatteryCharger->getBoostLowerTemperatureLimit(&getValue));
00716     TEST_ASSERT_EQUAL_INT32(-10, getValue);
00717     TEST_ASSERT(pBatteryCharger->setBoostLowerTemperatureLimit(-0));
00718     TEST_ASSERT(pBatteryCharger->getBoostLowerTemperatureLimit(&getValue));
00719     TEST_ASSERT_EQUAL_INT32(-10, getValue);
00720     TEST_ASSERT(pBatteryCharger->setBoostLowerTemperatureLimit(-10));
00721     TEST_ASSERT(pBatteryCharger->getBoostLowerTemperatureLimit(&getValue));
00722     TEST_ASSERT_EQUAL_INT32(-10, getValue);
00723     TEST_ASSERT(pBatteryCharger->setBoostLowerTemperatureLimit(-11));
00724     TEST_ASSERT(pBatteryCharger->getBoostLowerTemperatureLimit(&getValue));
00725     TEST_ASSERT_EQUAL_INT32(-20, getValue);
00726     TEST_ASSERT(pBatteryCharger->setBoostLowerTemperatureLimit(-20));
00727     TEST_ASSERT(pBatteryCharger->getBoostLowerTemperatureLimit(&getValue));
00728     TEST_ASSERT_EQUAL_INT32(-20, getValue);
00729     TEST_ASSERT(pBatteryCharger->setBoostLowerTemperatureLimit(-100));
00730     TEST_ASSERT(pBatteryCharger->getBoostLowerTemperatureLimit(&getValue));
00731     TEST_ASSERT_EQUAL_INT32(-20, getValue);
00732     
00733     // Parameter can be NULL
00734     TEST_ASSERT(pBatteryCharger->getBoostVoltage(NULL));
00735     TEST_ASSERT(pBatteryCharger->getBoostUpperTemperatureLimit(NULL));
00736     TEST_ASSERT(pBatteryCharger->getBoostLowerTemperatureLimit(NULL));
00737 
00738     // Put the initial values back when we're done
00739     TEST_ASSERT(pBatteryCharger->setBoostVoltage(originalVoltage));    
00740     if (upperTemperatureEnabled) {
00741         TEST_ASSERT(pBatteryCharger->setBoostUpperTemperatureLimit(originalUpperTemperature));
00742     } else {
00743         TEST_ASSERT(pBatteryCharger->disableBoostUpperTemperatureLimit());
00744     }
00745     TEST_ASSERT(pBatteryCharger->setBoostLowerTemperatureLimit(originalLowerTemperature));    
00746 }
00747 
00748 // Test setting the chip's thermal regulation threshold
00749 void test_chip_thermal_regulation_threshold() {
00750     BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
00751     int32_t originalTemperature;
00752     int32_t getValue;
00753     
00754     // Calls should return false if the battery charger has not been initialised
00755     TEST_ASSERT_FALSE(pBatteryCharger->getChipThermalRegulationThreshold(&getValue));
00756     TEST_ASSERT_FALSE(pBatteryCharger->setChipThermalRegulationThreshold(getValue));
00757     
00758     // Initialise the battery charger
00759     TEST_ASSERT(pBatteryCharger->init(gpI2C));
00760     
00761     // Save the initial value
00762     TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&originalTemperature));
00763 
00764     // The thermal regulation threshold is one of 60C, 80C, 100C or 120C
00765     TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(-1));
00766     TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue));
00767     TEST_ASSERT_EQUAL_INT32(60, getValue);
00768     TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(0));
00769     TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue));
00770     TEST_ASSERT_EQUAL_INT32(60, getValue);
00771     TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(79));
00772     TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue));
00773     TEST_ASSERT_EQUAL_INT32(60, getValue);
00774     TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(80));
00775     TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue));
00776     TEST_ASSERT_EQUAL_INT32(80, getValue);
00777     TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(99));
00778     TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue));
00779     TEST_ASSERT_EQUAL_INT32(80, getValue);
00780     TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(100));
00781     TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue));
00782     TEST_ASSERT_EQUAL_INT32(100, getValue);
00783     TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(101));
00784     TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue));
00785     TEST_ASSERT_EQUAL_INT32(100, getValue);
00786     TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(119));
00787     TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue));
00788     TEST_ASSERT_EQUAL_INT32(100, getValue);
00789     TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(120));
00790     TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue));
00791     TEST_ASSERT_EQUAL_INT32(120, getValue);
00792     TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(121));
00793     TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue));
00794     TEST_ASSERT_EQUAL_INT32(120, getValue);
00795     TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(200));
00796     TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue));
00797     TEST_ASSERT_EQUAL_INT32(120, getValue);
00798 
00799     // Parameter can be NULL
00800     TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(NULL));
00801 
00802     // Put the initial value back when we're done
00803     TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(originalTemperature));    
00804 }
00805 
00806 // Test that we can kick and set the watchdog
00807 void test_watchdog() {
00808     BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
00809     int32_t initValue;
00810     int32_t getValue = 0;
00811     
00812     // Call should fail if the battery charger has not been initialised
00813     TEST_ASSERT_FALSE(pBatteryCharger->feedWatchdog());
00814     TEST_ASSERT_FALSE(pBatteryCharger->getWatchdog(&getValue));
00815     TEST_ASSERT_FALSE(pBatteryCharger->setWatchdog(0));
00816     
00817     // Initialise the battery charger
00818     TEST_ASSERT(pBatteryCharger->init(gpI2C));
00819     
00820     // Save the initial value
00821     TEST_ASSERT(pBatteryCharger->getWatchdog(&initValue));
00822 
00823     // Check that we can feed the watchdog
00824     TEST_ASSERT(pBatteryCharger->feedWatchdog());
00825     
00826     // Check that we can set all the watchdog values
00827     TEST_ASSERT(pBatteryCharger->setWatchdog(81));
00828     TEST_ASSERT(pBatteryCharger->getWatchdog(&getValue));
00829     TEST_ASSERT_EQUAL_INT32(160, getValue);
00830     TEST_ASSERT(pBatteryCharger->setWatchdog(80));
00831     TEST_ASSERT(pBatteryCharger->getWatchdog(&getValue));
00832     TEST_ASSERT_EQUAL_INT32(80, getValue);
00833     TEST_ASSERT(pBatteryCharger->setWatchdog(41));
00834     TEST_ASSERT(pBatteryCharger->getWatchdog(&getValue));
00835     TEST_ASSERT_EQUAL_INT32(80, getValue);
00836     TEST_ASSERT(pBatteryCharger->setWatchdog(40));
00837     TEST_ASSERT(pBatteryCharger->getWatchdog(&getValue));
00838     TEST_ASSERT_EQUAL_INT32(40, getValue);
00839     TEST_ASSERT(pBatteryCharger->setWatchdog(1));
00840     TEST_ASSERT(pBatteryCharger->getWatchdog(&getValue));
00841     TEST_ASSERT_EQUAL_INT32(40, getValue);
00842     TEST_ASSERT(pBatteryCharger->setWatchdog(0));
00843     TEST_ASSERT(pBatteryCharger->getWatchdog(&getValue));
00844     TEST_ASSERT_EQUAL_INT32(0, getValue);
00845 
00846     // Put the initial value back when we're done
00847     TEST_ASSERT (pBatteryCharger->setWatchdog(initValue));
00848 }
00849 
00850 // Test that we can enable and disable shipping mode
00851 void test_shipping_mode() {
00852     BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
00853     bool shippingModeEnabled;
00854     
00855     // Call should fail if the battery charger has not been initialised
00856     TEST_ASSERT_FALSE(pBatteryCharger->enableShippingMode());
00857     TEST_ASSERT_FALSE(pBatteryCharger->disableShippingMode());
00858     TEST_ASSERT_FALSE(pBatteryCharger->isShippingModeEnabled());
00859     
00860     // Initialise the battery charger
00861     TEST_ASSERT(pBatteryCharger->init(gpI2C));
00862     
00863     // Save the initial values
00864     shippingModeEnabled = pBatteryCharger->isShippingModeEnabled();
00865 
00866     // Enable and disable shipping mode
00867     TEST_ASSERT(pBatteryCharger->enableShippingMode());
00868     TEST_ASSERT(pBatteryCharger->isShippingModeEnabled());
00869     TEST_ASSERT(pBatteryCharger->disableShippingMode());
00870     TEST_ASSERT_FALSE(pBatteryCharger->isShippingModeEnabled());
00871     TEST_ASSERT(pBatteryCharger->enableShippingMode());
00872     TEST_ASSERT(pBatteryCharger->isShippingModeEnabled());
00873     
00874     // Put the initial value back when we're done
00875     if (shippingModeEnabled) {
00876         pBatteryCharger->enableShippingMode();
00877     } else {
00878         pBatteryCharger->disableShippingMode();
00879     }
00880 }
00881 
00882 // Test the advanced functions
00883 void test_advanced() {
00884     BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
00885     char originalValue;
00886     uint8_t address = 0x03; // REG03, the pre-charge/termination current control register
00887     char getValue;
00888     int32_t precharge;
00889     int32_t termination;
00890     
00891     // Calls should return false if the battery charger has not been initialised
00892     TEST_ASSERT_FALSE(pBatteryCharger->advancedGet(address, &getValue));
00893     TEST_ASSERT_FALSE(pBatteryCharger->advancedSet(address, getValue));
00894     
00895     // Initialise the battery charger
00896     TEST_ASSERT(pBatteryCharger->init(gpI2C));
00897     
00898     // Save the initial value from address
00899     TEST_ASSERT(pBatteryCharger->advancedGet(address, &originalValue));
00900 
00901     // REG03 contains the pre-charge current limit in the upper four bits
00902     // and the termination current in the lower four bits, so we can read
00903     // those and work out what the raw register value is.
00904     TEST_ASSERT(pBatteryCharger->getPrechargingCurrentLimit(&precharge));
00905     TEST_ASSERT(pBatteryCharger->getChargingTerminationCurrent(&termination));
00906     
00907     precharge = (precharge - MIN_PRECHARGING_CURRENT_LIMIT_MA) / 128;
00908     termination = (termination - MIN_CHARGING_TERMINATION_CURRENT_MA) / 128;
00909     TEST_ASSERT_EQUAL_INT8(((precharge << 4) | (termination & 0x0f)), originalValue);
00910     
00911     // Now write something else to the register and check that it changes
00912     TEST_ASSERT(pBatteryCharger->advancedSet(address, 0x01));
00913     TEST_ASSERT(pBatteryCharger->advancedGet(address, &getValue));
00914     TEST_ASSERT_EQUAL_INT8(0x01, getValue);
00915 
00916     // Parameter can be NULL
00917     TEST_ASSERT(pBatteryCharger->advancedGet(address, NULL));
00918 
00919     // Put the initial value back now that we're done
00920     TEST_ASSERT(pBatteryCharger->advancedSet(address, originalValue));    
00921 }
00922 
00923 // ----------------------------------------------------------------
00924 // TEST ENVIRONMENT
00925 // ----------------------------------------------------------------
00926 
00927 // Setup the test environment
00928 utest::v1::status_t test_setup(const size_t number_of_cases) {
00929     // Setup Greentea, timeout is long enough to run these tests with
00930     // DEBUG_BQ24295 defined
00931     GREENTEA_SETUP(240, "default_auto");
00932     return verbose_test_setup_handler(number_of_cases);
00933 }
00934 
00935 // Test cases
00936 Case cases[] = {
00937     Case("Initialisation", test_init),
00938     Case("Charger state read", test_charger_state),
00939     Case("External power presence", test_external_power_present),
00940     Case("Charger fault", test_charger_fault),
00941     Case("Input limits", test_input_limits),
00942     Case("Charging enable", test_charging_enable),
00943     Case("System voltage", test_system_voltage),
00944     Case("Fast charging current limits", test_fast_charging_current_limits),
00945     Case("Icgh/Iprech margin", test_icgh_iprech_margin),
00946     Case("Pre-charging current limits", test_precharging_current_limits),
00947     Case("Charging termination current", test_charging_termination_current),
00948     Case("Charging voltage limits", test_charging_voltage_limits),
00949     Case("Fast charging safety timer", test_fast_charging_safety_timer),
00950     Case("Boost limits", test_boost_limits),
00951     Case("Chip thermal regulation threshold", test_chip_thermal_regulation_threshold),
00952     Case("Watchdog", test_watchdog),
00953     Case("Shipping mode", test_shipping_mode),
00954     Case("Advanced", test_advanced)
00955 };
00956 
00957 Specification specification(test_setup, cases);
00958 
00959 // ----------------------------------------------------------------
00960 // MAIN
00961 // ----------------------------------------------------------------
00962 
00963 // Entry point into the tests
00964 int main() {    
00965     bool success = false;
00966     
00967     if (gpI2C != NULL) {        
00968         success = !Harness::run(specification);
00969     } else {
00970         printf ("Unable to instantiate I2C interface.\n");
00971     }
00972     
00973     return success;
00974 }
00975 
00976 // End Of File