This class provides APIs to all of the registers of the TI BQ24295 battery charger chip, as used on the u-blox C030 board. This class is not required to charge a battery connected to the C030 board, charging will begin automatically when a battery is connected. This class is only required if the user wishes to monitor the charger's state or change the charger's settings. 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. The chip may then be configured using the API calls. Once the chip is configured, battery charging can be enabled. If battery charging is disabled the chip will once more be put into its lowest power state.

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

Committer:
rob.meades@u-blox.com
Date:
Tue Jun 06 15:56:50 2017 +0100
Revision:
5:bb17656c9b9d
Parent:
3:340d65a1a133
Remove old UTM board pin-outs.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rob.meades@u-blox.com 1:ed57b6ca43ab 1 #include "mbed.h"
rob.meades@u-blox.com 1:ed57b6ca43ab 2 #include "greentea-client/test_env.h"
rob.meades@u-blox.com 1:ed57b6ca43ab 3 #include "unity.h"
rob.meades@u-blox.com 1:ed57b6ca43ab 4 #include "utest.h"
rob.meades@u-blox.com 1:ed57b6ca43ab 5 #include "battery_charger_bq24295.h"
rob.meades@u-blox.com 1:ed57b6ca43ab 6
rob.meades@u-blox.com 1:ed57b6ca43ab 7 using namespace utest::v1;
rob.meades@u-blox.com 1:ed57b6ca43ab 8
rob.meades@u-blox.com 1:ed57b6ca43ab 9 // ----------------------------------------------------------------
rob.meades@u-blox.com 1:ed57b6ca43ab 10 // COMPILE-TIME MACROS
rob.meades@u-blox.com 1:ed57b6ca43ab 11 // ----------------------------------------------------------------
rob.meades@u-blox.com 1:ed57b6ca43ab 12
rob.meades@u-blox.com 1:ed57b6ca43ab 13 // Minimum and maximum numbers
rob.meades@u-blox.com 1:ed57b6ca43ab 14 #define MAX_INPUT_VOLTAGE_LIMIT_MV 5080
rob.meades@u-blox.com 1:ed57b6ca43ab 15 #define MIN_INPUT_VOLTAGE_LIMIT_MV 3880
rob.meades@u-blox.com 1:ed57b6ca43ab 16 #define MAX_INPUT_CURRENT_LIMIT_MA 3000
rob.meades@u-blox.com 1:ed57b6ca43ab 17 #define MIN_INPUT_CURRENT_LIMIT_MA 100
rob.meades@u-blox.com 1:ed57b6ca43ab 18 #define MAX_SYSTEM_VOLTAGE_MV 3700
rob.meades@u-blox.com 1:ed57b6ca43ab 19 #define MIN_SYSTEM_VOLTAGE_MV 3000
rob.meades@u-blox.com 1:ed57b6ca43ab 20 #define MAX_FAST_CHARGING_CURRENT_LIMIT_MA 3008
rob.meades@u-blox.com 1:ed57b6ca43ab 21 #define MIN_FAST_CHARGING_CURRENT_LIMIT_MA 512
rob.meades@u-blox.com 1:ed57b6ca43ab 22 #define MAX_PRECHARGING_CURRENT_LIMIT_MA 2048
rob.meades@u-blox.com 1:ed57b6ca43ab 23 #define MIN_PRECHARGING_CURRENT_LIMIT_MA 128
rob.meades@u-blox.com 1:ed57b6ca43ab 24 #define MAX_CHARGING_TERMINATION_CURRENT_MA 2048
rob.meades@u-blox.com 1:ed57b6ca43ab 25 #define MIN_CHARGING_TERMINATION_CURRENT_MA 128
rob.meades@u-blox.com 1:ed57b6ca43ab 26 #define MAX_CHARGING_VOLTAGE_LIMIT_MV 4400
rob.meades@u-blox.com 1:ed57b6ca43ab 27 #define MIN_CHARGING_VOLTAGE_LIMIT_MV 3504
rob.meades@u-blox.com 1:ed57b6ca43ab 28 #define MIN_BOOST_VOLTAGE_MV 4550
rob.meades@u-blox.com 1:ed57b6ca43ab 29 #define MAX_BOOST_VOLTAGE_MV 5510
rob.meades@u-blox.com 1:ed57b6ca43ab 30
rob.meades@u-blox.com 1:ed57b6ca43ab 31 #ifndef NUM_RAND_ITERATIONS
rob.meades@u-blox.com 1:ed57b6ca43ab 32 // The number of iterations of random input values in various tests
rob.meades@u-blox.com 1:ed57b6ca43ab 33 #define NUM_RAND_ITERATIONS 50
rob.meades@u-blox.com 1:ed57b6ca43ab 34 #endif
rob.meades@u-blox.com 1:ed57b6ca43ab 35
rob.meades@u-blox.com 1:ed57b6ca43ab 36 // ----------------------------------------------------------------
rob.meades@u-blox.com 1:ed57b6ca43ab 37 // PRIVATE VARIABLES
rob.meades@u-blox.com 1:ed57b6ca43ab 38 // ----------------------------------------------------------------
rob.meades@u-blox.com 1:ed57b6ca43ab 39
rob.meades@u-blox.com 1:ed57b6ca43ab 40 // I2C interface
rob.meades@u-blox.com 5:bb17656c9b9d 41 I2C * gpI2C = new I2C(I2C_SDA_B, I2C_SCL_B);
rob.meades@u-blox.com 1:ed57b6ca43ab 42
rob.meades@u-blox.com 1:ed57b6ca43ab 43 // ----------------------------------------------------------------
rob.meades@u-blox.com 1:ed57b6ca43ab 44 // TESTS
rob.meades@u-blox.com 1:ed57b6ca43ab 45 // ----------------------------------------------------------------
rob.meades@u-blox.com 1:ed57b6ca43ab 46
rob.meades@u-blox.com 1:ed57b6ca43ab 47 // Test that the BQ24295 battery charger can be initialised
rob.meades@u-blox.com 1:ed57b6ca43ab 48 void test_init() {
rob.meades@u-blox.com 1:ed57b6ca43ab 49 BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
rob.meades@u-blox.com 1:ed57b6ca43ab 50
rob.meades@u-blox.com 1:ed57b6ca43ab 51 TEST_ASSERT_FALSE(pBatteryCharger->init(NULL));
rob.meades@u-blox.com 1:ed57b6ca43ab 52 TEST_ASSERT(pBatteryCharger->init(gpI2C));
rob.meades@u-blox.com 1:ed57b6ca43ab 53 }
rob.meades@u-blox.com 1:ed57b6ca43ab 54
rob.meades@u-blox.com 1:ed57b6ca43ab 55 // Test that we can read the charger state from the BQ24295 battery charger
rob.meades@u-blox.com 1:ed57b6ca43ab 56 void test_charger_state() {
rob.meades@u-blox.com 1:ed57b6ca43ab 57 BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
rob.meades@u-blox.com 1:ed57b6ca43ab 58 BatteryChargerBq24295::ChargerState chargerState = BatteryChargerBq24295::CHARGER_STATE_UNKNOWN;
rob.meades@u-blox.com 1:ed57b6ca43ab 59
rob.meades@u-blox.com 1:ed57b6ca43ab 60 // Call should fail if the battery charger has not been initialised
rob.meades@u-blox.com 1:ed57b6ca43ab 61 chargerState = pBatteryCharger->getChargerState();
rob.meades@u-blox.com 1:ed57b6ca43ab 62 TEST_ASSERT(chargerState == BatteryChargerBq24295::CHARGER_STATE_UNKNOWN);
rob.meades@u-blox.com 1:ed57b6ca43ab 63
rob.meades@u-blox.com 1:ed57b6ca43ab 64 // Normal case
rob.meades@u-blox.com 1:ed57b6ca43ab 65 TEST_ASSERT(pBatteryCharger->init(gpI2C));
rob.meades@u-blox.com 1:ed57b6ca43ab 66 chargerState = pBatteryCharger->getChargerState();
rob.meades@u-blox.com 1:ed57b6ca43ab 67 printf ("Charger state is %d.\n", chargerState);
rob.meades@u-blox.com 1:ed57b6ca43ab 68 // Range check
rob.meades@u-blox.com 1:ed57b6ca43ab 69 TEST_ASSERT(chargerState != BatteryChargerBq24295::CHARGER_STATE_UNKNOWN);
rob.meades@u-blox.com 1:ed57b6ca43ab 70 TEST_ASSERT(chargerState < BatteryChargerBq24295::MAX_NUM_CHARGER_STATES);
rob.meades@u-blox.com 1:ed57b6ca43ab 71 }
rob.meades@u-blox.com 1:ed57b6ca43ab 72
rob.meades@u-blox.com 1:ed57b6ca43ab 73 // Test that we can read whether external power is present or not
rob.meades@u-blox.com 1:ed57b6ca43ab 74 // according to the BQ24295 battery charger
rob.meades@u-blox.com 1:ed57b6ca43ab 75 void test_external_power_present() {
rob.meades@u-blox.com 1:ed57b6ca43ab 76 BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
rob.meades@u-blox.com 1:ed57b6ca43ab 77
rob.meades@u-blox.com 1:ed57b6ca43ab 78 // Call should return false if the battery charger has not been initialised
rob.meades@u-blox.com 1:ed57b6ca43ab 79 TEST_ASSERT_FALSE(pBatteryCharger->isExternalPowerPresent());
rob.meades@u-blox.com 1:ed57b6ca43ab 80
rob.meades@u-blox.com 1:ed57b6ca43ab 81 // Normal case: must return true as the USB cable is plugged
rob.meades@u-blox.com 1:ed57b6ca43ab 82 // in when running these tests
rob.meades@u-blox.com 1:ed57b6ca43ab 83 TEST_ASSERT(pBatteryCharger->init(gpI2C));
rob.meades@u-blox.com 1:ed57b6ca43ab 84 TEST_ASSERT(pBatteryCharger->isExternalPowerPresent());
rob.meades@u-blox.com 1:ed57b6ca43ab 85 }
rob.meades@u-blox.com 1:ed57b6ca43ab 86
rob.meades@u-blox.com 1:ed57b6ca43ab 87 // Test that we can read the charger fault from the BQ24295 battery charger
rob.meades@u-blox.com 1:ed57b6ca43ab 88 // NOTE: if this test fails, make sure you haven't got an actual fault. Better
rob.meades@u-blox.com 1:ed57b6ca43ab 89 // to reset the chip/board entirely before running these tests so that one
rob.meades@u-blox.com 1:ed57b6ca43ab 90 // doesn't occur.
rob.meades@u-blox.com 1:ed57b6ca43ab 91 void test_charger_fault() {
rob.meades@u-blox.com 1:ed57b6ca43ab 92 BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
rob.meades@u-blox.com 1:ed57b6ca43ab 93 char bitmap = (char) BatteryChargerBq24295::CHARGER_FAULT_NONE;
rob.meades@u-blox.com 1:ed57b6ca43ab 94
rob.meades@u-blox.com 1:ed57b6ca43ab 95 // Call should return no faults if the battery charger has not been initialised
rob.meades@u-blox.com 1:ed57b6ca43ab 96 bitmap = pBatteryCharger->getChargerFaults();
rob.meades@u-blox.com 1:ed57b6ca43ab 97 TEST_ASSERT_EQUAL_INT8((char) BatteryChargerBq24295::CHARGER_FAULT_NONE, bitmap);
rob.meades@u-blox.com 1:ed57b6ca43ab 98
rob.meades@u-blox.com 1:ed57b6ca43ab 99 // Normal case
rob.meades@u-blox.com 1:ed57b6ca43ab 100 TEST_ASSERT(pBatteryCharger->init(gpI2C));
rob.meades@u-blox.com 1:ed57b6ca43ab 101 bitmap = pBatteryCharger->getChargerFaults();
rob.meades@u-blox.com 1:ed57b6ca43ab 102 printf ("Charger fault is 0x%02x.\n", bitmap);
rob.meades@u-blox.com 1:ed57b6ca43ab 103 // Should be just the watchdog as we are in host mode and are not servicing the watchdog
rob.meades@u-blox.com 1:ed57b6ca43ab 104 // however other faults can also be present so just look for a non zero value.
rob.meades@u-blox.com 1:ed57b6ca43ab 105 // TODO: find a way to test other faults
rob.meades@u-blox.com 1:ed57b6ca43ab 106 TEST_ASSERT((unsigned char) bitmap > 0);
rob.meades@u-blox.com 1:ed57b6ca43ab 107 }
rob.meades@u-blox.com 1:ed57b6ca43ab 108
rob.meades@u-blox.com 1:ed57b6ca43ab 109 // Test that we can read and change the input voltage and current limits
rob.meades@u-blox.com 1:ed57b6ca43ab 110 void test_input_limits() {
rob.meades@u-blox.com 1:ed57b6ca43ab 111 BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
rob.meades@u-blox.com 1:ed57b6ca43ab 112 int32_t voltageOriginal;
rob.meades@u-blox.com 1:ed57b6ca43ab 113 int32_t currentOriginal;
rob.meades@u-blox.com 1:ed57b6ca43ab 114 bool enabledOriginal;
rob.meades@u-blox.com 1:ed57b6ca43ab 115 int32_t setValue;
rob.meades@u-blox.com 1:ed57b6ca43ab 116 int32_t getValue;
rob.meades@u-blox.com 1:ed57b6ca43ab 117
rob.meades@u-blox.com 1:ed57b6ca43ab 118 // Calls should return false if the battery charger has not been initialised
rob.meades@u-blox.com 1:ed57b6ca43ab 119 TEST_ASSERT_FALSE(pBatteryCharger->getInputCurrentLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 120 TEST_ASSERT_FALSE(pBatteryCharger->setInputVoltageLimit(getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 121 TEST_ASSERT_FALSE(pBatteryCharger->getInputCurrentLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 122 TEST_ASSERT_FALSE(pBatteryCharger->setInputVoltageLimit(getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 123 TEST_ASSERT_FALSE(pBatteryCharger->enableInputLimits());
rob.meades@u-blox.com 1:ed57b6ca43ab 124 TEST_ASSERT_FALSE(pBatteryCharger->disableInputLimits());
rob.meades@u-blox.com 1:ed57b6ca43ab 125
rob.meades@u-blox.com 1:ed57b6ca43ab 126 // Initialise the battery charger
rob.meades@u-blox.com 1:ed57b6ca43ab 127 TEST_ASSERT(pBatteryCharger->init(gpI2C));
rob.meades@u-blox.com 1:ed57b6ca43ab 128
rob.meades@u-blox.com 1:ed57b6ca43ab 129 // Save the initial values
rob.meades@u-blox.com 1:ed57b6ca43ab 130 TEST_ASSERT(pBatteryCharger->getInputVoltageLimit(&voltageOriginal));
rob.meades@u-blox.com 1:ed57b6ca43ab 131 TEST_ASSERT(pBatteryCharger->getInputCurrentLimit(&currentOriginal));
rob.meades@u-blox.com 1:ed57b6ca43ab 132 enabledOriginal = pBatteryCharger->areInputLimitsEnabled();
rob.meades@u-blox.com 1:ed57b6ca43ab 133
rob.meades@u-blox.com 1:ed57b6ca43ab 134 // Voltage and current beyond the limits
rob.meades@u-blox.com 1:ed57b6ca43ab 135 TEST_ASSERT_FALSE(pBatteryCharger->setInputVoltageLimit(MIN_INPUT_VOLTAGE_LIMIT_MV - 1));
rob.meades@u-blox.com 1:ed57b6ca43ab 136 TEST_ASSERT_FALSE(pBatteryCharger->setInputVoltageLimit(MAX_INPUT_VOLTAGE_LIMIT_MV + 1));
rob.meades@u-blox.com 1:ed57b6ca43ab 137 TEST_ASSERT_FALSE(pBatteryCharger->setInputCurrentLimit(MIN_INPUT_CURRENT_LIMIT_MA - 1));
rob.meades@u-blox.com 1:ed57b6ca43ab 138 TEST_ASSERT_FALSE(pBatteryCharger->setInputCurrentLimit(MAX_INPUT_CURRENT_LIMIT_MA + 1));
rob.meades@u-blox.com 1:ed57b6ca43ab 139
rob.meades@u-blox.com 1:ed57b6ca43ab 140 // Voltage and current at the limits
rob.meades@u-blox.com 1:ed57b6ca43ab 141 TEST_ASSERT(pBatteryCharger->setInputVoltageLimit(MIN_INPUT_VOLTAGE_LIMIT_MV));
rob.meades@u-blox.com 1:ed57b6ca43ab 142 TEST_ASSERT(pBatteryCharger->getInputVoltageLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 143 TEST_ASSERT_EQUAL_INT32(MIN_INPUT_VOLTAGE_LIMIT_MV, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 144 TEST_ASSERT(pBatteryCharger->setInputVoltageLimit(MAX_INPUT_VOLTAGE_LIMIT_MV));
rob.meades@u-blox.com 1:ed57b6ca43ab 145 TEST_ASSERT(pBatteryCharger->getInputVoltageLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 146 TEST_ASSERT_EQUAL_INT32(MAX_INPUT_VOLTAGE_LIMIT_MV, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 147 TEST_ASSERT(pBatteryCharger->setInputCurrentLimit(MIN_INPUT_CURRENT_LIMIT_MA));
rob.meades@u-blox.com 1:ed57b6ca43ab 148 TEST_ASSERT(pBatteryCharger->getInputCurrentLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 149 TEST_ASSERT_EQUAL_INT32(MIN_INPUT_CURRENT_LIMIT_MA, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 150 TEST_ASSERT(pBatteryCharger->setInputCurrentLimit(MAX_INPUT_CURRENT_LIMIT_MA));
rob.meades@u-blox.com 1:ed57b6ca43ab 151 TEST_ASSERT(pBatteryCharger->getInputCurrentLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 152 TEST_ASSERT_EQUAL_INT32(MAX_INPUT_CURRENT_LIMIT_MA, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 153
rob.meades@u-blox.com 1:ed57b6ca43ab 154 // The voltage limit read back should not be more than 80 mV below the value requested
rob.meades@u-blox.com 1:ed57b6ca43ab 155 for (uint32_t x = 0; x < NUM_RAND_ITERATIONS; x++) {
rob.meades@u-blox.com 1:ed57b6ca43ab 156 setValue = MIN_INPUT_VOLTAGE_LIMIT_MV + rand() % (MAX_INPUT_VOLTAGE_LIMIT_MV - MIN_INPUT_VOLTAGE_LIMIT_MV + 1);
rob.meades@u-blox.com 1:ed57b6ca43ab 157 TEST_ASSERT(pBatteryCharger->setInputVoltageLimit(setValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 158 getValue = -1;
rob.meades@u-blox.com 1:ed57b6ca43ab 159 TEST_ASSERT(pBatteryCharger->getInputVoltageLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 160 TEST_ASSERT((getValue > setValue - 80) && (getValue <= setValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 161 TEST_ASSERT((getValue >= MIN_INPUT_VOLTAGE_LIMIT_MV) && (getValue <= MAX_INPUT_VOLTAGE_LIMIT_MV));
rob.meades@u-blox.com 1:ed57b6ca43ab 162 }
rob.meades@u-blox.com 1:ed57b6ca43ab 163
rob.meades@u-blox.com 1:ed57b6ca43ab 164 // The current limit read back should always be less than or equal to the set value
rob.meades@u-blox.com 1:ed57b6ca43ab 165 for (uint32_t x = 0; x < NUM_RAND_ITERATIONS; x++) {
rob.meades@u-blox.com 1:ed57b6ca43ab 166 setValue = MIN_INPUT_CURRENT_LIMIT_MA + rand() % (MAX_INPUT_CURRENT_LIMIT_MA - MIN_INPUT_CURRENT_LIMIT_MA + 1);
rob.meades@u-blox.com 1:ed57b6ca43ab 167 TEST_ASSERT(pBatteryCharger->setInputCurrentLimit(setValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 168 getValue = -1;
rob.meades@u-blox.com 1:ed57b6ca43ab 169 TEST_ASSERT(pBatteryCharger->getInputCurrentLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 170 TEST_ASSERT(getValue <= setValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 171 TEST_ASSERT((getValue >= MIN_INPUT_CURRENT_LIMIT_MA) && (getValue <= MAX_INPUT_CURRENT_LIMIT_MA));
rob.meades@u-blox.com 1:ed57b6ca43ab 172 }
rob.meades@u-blox.com 1:ed57b6ca43ab 173
rob.meades@u-blox.com 1:ed57b6ca43ab 174 // Enable and disable the limits
rob.meades@u-blox.com 1:ed57b6ca43ab 175 TEST_ASSERT(pBatteryCharger->enableInputLimits());
rob.meades@u-blox.com 1:ed57b6ca43ab 176 TEST_ASSERT(pBatteryCharger->areInputLimitsEnabled());
rob.meades@u-blox.com 1:ed57b6ca43ab 177 TEST_ASSERT(pBatteryCharger->disableInputLimits());
rob.meades@u-blox.com 1:ed57b6ca43ab 178 TEST_ASSERT_FALSE(pBatteryCharger->areInputLimitsEnabled());
rob.meades@u-blox.com 1:ed57b6ca43ab 179 TEST_ASSERT(pBatteryCharger->enableInputLimits());
rob.meades@u-blox.com 1:ed57b6ca43ab 180 TEST_ASSERT(pBatteryCharger->areInputLimitsEnabled());
rob.meades@u-blox.com 1:ed57b6ca43ab 181
rob.meades@u-blox.com 1:ed57b6ca43ab 182 // Parameters can be NULL
rob.meades@u-blox.com 1:ed57b6ca43ab 183 TEST_ASSERT(pBatteryCharger->getInputVoltageLimit(NULL));
rob.meades@u-blox.com 1:ed57b6ca43ab 184 TEST_ASSERT(pBatteryCharger->getInputCurrentLimit(NULL));
rob.meades@u-blox.com 1:ed57b6ca43ab 185
rob.meades@u-blox.com 1:ed57b6ca43ab 186 // Put the initial values back when we're done
rob.meades@u-blox.com 1:ed57b6ca43ab 187 TEST_ASSERT(pBatteryCharger->setInputVoltageLimit(voltageOriginal));
rob.meades@u-blox.com 1:ed57b6ca43ab 188 TEST_ASSERT(pBatteryCharger->setInputCurrentLimit(currentOriginal));
rob.meades@u-blox.com 1:ed57b6ca43ab 189 if (enabledOriginal) {
rob.meades@u-blox.com 1:ed57b6ca43ab 190 pBatteryCharger->enableInputLimits();
rob.meades@u-blox.com 1:ed57b6ca43ab 191 } else {
rob.meades@u-blox.com 1:ed57b6ca43ab 192 pBatteryCharger->disableInputLimits();
rob.meades@u-blox.com 1:ed57b6ca43ab 193 }
rob.meades@u-blox.com 1:ed57b6ca43ab 194 }
rob.meades@u-blox.com 1:ed57b6ca43ab 195
rob.meades@u-blox.com 1:ed57b6ca43ab 196 // Test that we enable and disable OTG and normal charging
rob.meades@u-blox.com 1:ed57b6ca43ab 197 void test_charging_enable() {
rob.meades@u-blox.com 1:ed57b6ca43ab 198 BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
rob.meades@u-blox.com 1:ed57b6ca43ab 199 bool otgEnabled;
rob.meades@u-blox.com 1:ed57b6ca43ab 200 bool chargingEnabled;
rob.meades@u-blox.com 1:ed57b6ca43ab 201
rob.meades@u-blox.com 1:ed57b6ca43ab 202 // Call should fail if the battery charger has not been initialised
rob.meades@u-blox.com 1:ed57b6ca43ab 203 TEST_ASSERT_FALSE(pBatteryCharger->enableOtg());
rob.meades@u-blox.com 1:ed57b6ca43ab 204 TEST_ASSERT_FALSE(pBatteryCharger->disableOtg());
rob.meades@u-blox.com 1:ed57b6ca43ab 205 TEST_ASSERT_FALSE(pBatteryCharger->enableCharging());
rob.meades@u-blox.com 1:ed57b6ca43ab 206 TEST_ASSERT_FALSE(pBatteryCharger->disableCharging());
rob.meades@u-blox.com 1:ed57b6ca43ab 207
rob.meades@u-blox.com 1:ed57b6ca43ab 208 // Initialise the battery charger
rob.meades@u-blox.com 1:ed57b6ca43ab 209 TEST_ASSERT(pBatteryCharger->init(gpI2C));
rob.meades@u-blox.com 1:ed57b6ca43ab 210
rob.meades@u-blox.com 1:ed57b6ca43ab 211 // Save the initial values
rob.meades@u-blox.com 1:ed57b6ca43ab 212 otgEnabled = pBatteryCharger->isOtgEnabled();
rob.meades@u-blox.com 1:ed57b6ca43ab 213 chargingEnabled = pBatteryCharger->isChargingEnabled();
rob.meades@u-blox.com 1:ed57b6ca43ab 214
rob.meades@u-blox.com 1:ed57b6ca43ab 215 // Enable and disable OTG
rob.meades@u-blox.com 1:ed57b6ca43ab 216 TEST_ASSERT(pBatteryCharger->enableOtg());
rob.meades@u-blox.com 1:ed57b6ca43ab 217 TEST_ASSERT(pBatteryCharger->isOtgEnabled());
rob.meades@u-blox.com 1:ed57b6ca43ab 218 TEST_ASSERT(pBatteryCharger->disableOtg());
rob.meades@u-blox.com 1:ed57b6ca43ab 219 TEST_ASSERT_FALSE(pBatteryCharger->isOtgEnabled());
rob.meades@u-blox.com 1:ed57b6ca43ab 220 TEST_ASSERT(pBatteryCharger->enableOtg());
rob.meades@u-blox.com 1:ed57b6ca43ab 221 TEST_ASSERT(pBatteryCharger->isOtgEnabled());
rob.meades@u-blox.com 1:ed57b6ca43ab 222
rob.meades@u-blox.com 1:ed57b6ca43ab 223 // Enable and disable charging
rob.meades@u-blox.com 1:ed57b6ca43ab 224 TEST_ASSERT(pBatteryCharger->enableCharging());
rob.meades@u-blox.com 1:ed57b6ca43ab 225 TEST_ASSERT(pBatteryCharger->isChargingEnabled());
rob.meades@u-blox.com 1:ed57b6ca43ab 226 TEST_ASSERT(pBatteryCharger->disableCharging());
rob.meades@u-blox.com 1:ed57b6ca43ab 227 TEST_ASSERT_FALSE(pBatteryCharger->isChargingEnabled());
rob.meades@u-blox.com 1:ed57b6ca43ab 228 TEST_ASSERT(pBatteryCharger->enableCharging());
rob.meades@u-blox.com 1:ed57b6ca43ab 229 TEST_ASSERT(pBatteryCharger->isChargingEnabled());
rob.meades@u-blox.com 1:ed57b6ca43ab 230
rob.meades@u-blox.com 1:ed57b6ca43ab 231 // Put the initial values back when we're done
rob.meades@u-blox.com 1:ed57b6ca43ab 232 if (otgEnabled) {
rob.meades@u-blox.com 1:ed57b6ca43ab 233 pBatteryCharger->enableOtg();
rob.meades@u-blox.com 1:ed57b6ca43ab 234 } else {
rob.meades@u-blox.com 1:ed57b6ca43ab 235 pBatteryCharger->disableOtg();
rob.meades@u-blox.com 1:ed57b6ca43ab 236 }
rob.meades@u-blox.com 1:ed57b6ca43ab 237 if (chargingEnabled) {
rob.meades@u-blox.com 1:ed57b6ca43ab 238 pBatteryCharger->enableCharging();
rob.meades@u-blox.com 1:ed57b6ca43ab 239 } else {
rob.meades@u-blox.com 1:ed57b6ca43ab 240 pBatteryCharger->disableCharging();
rob.meades@u-blox.com 1:ed57b6ca43ab 241 }
rob.meades@u-blox.com 1:ed57b6ca43ab 242 }
rob.meades@u-blox.com 1:ed57b6ca43ab 243
rob.meades@u-blox.com 1:ed57b6ca43ab 244 // Test that we can read and change the system voltage
rob.meades@u-blox.com 1:ed57b6ca43ab 245 void test_system_voltage() {
rob.meades@u-blox.com 1:ed57b6ca43ab 246 BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
rob.meades@u-blox.com 1:ed57b6ca43ab 247 int32_t voltageOriginal;
rob.meades@u-blox.com 1:ed57b6ca43ab 248 int32_t setValue;
rob.meades@u-blox.com 1:ed57b6ca43ab 249 int32_t getValue;
rob.meades@u-blox.com 1:ed57b6ca43ab 250
rob.meades@u-blox.com 1:ed57b6ca43ab 251 // Calls should return false if the battery charger has not been initialised
rob.meades@u-blox.com 1:ed57b6ca43ab 252 TEST_ASSERT_FALSE(pBatteryCharger->getSystemVoltage(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 253 TEST_ASSERT_FALSE(pBatteryCharger->setSystemVoltage(getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 254
rob.meades@u-blox.com 1:ed57b6ca43ab 255 // Initialise the battery charger
rob.meades@u-blox.com 1:ed57b6ca43ab 256 TEST_ASSERT(pBatteryCharger->init(gpI2C));
rob.meades@u-blox.com 1:ed57b6ca43ab 257
rob.meades@u-blox.com 1:ed57b6ca43ab 258 // Save the initial value
rob.meades@u-blox.com 1:ed57b6ca43ab 259 TEST_ASSERT(pBatteryCharger->getSystemVoltage(&voltageOriginal));
rob.meades@u-blox.com 1:ed57b6ca43ab 260
rob.meades@u-blox.com 1:ed57b6ca43ab 261 // Beyond the limits
rob.meades@u-blox.com 1:ed57b6ca43ab 262 TEST_ASSERT_FALSE(pBatteryCharger->setSystemVoltage(MIN_SYSTEM_VOLTAGE_MV - 1));
rob.meades@u-blox.com 1:ed57b6ca43ab 263 TEST_ASSERT_FALSE(pBatteryCharger->setSystemVoltage(MAX_SYSTEM_VOLTAGE_MV + 1));
rob.meades@u-blox.com 1:ed57b6ca43ab 264
rob.meades@u-blox.com 1:ed57b6ca43ab 265 // At the limits
rob.meades@u-blox.com 1:ed57b6ca43ab 266 TEST_ASSERT(pBatteryCharger->setSystemVoltage(MIN_SYSTEM_VOLTAGE_MV));
rob.meades@u-blox.com 1:ed57b6ca43ab 267 TEST_ASSERT(pBatteryCharger->getSystemVoltage(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 268 TEST_ASSERT_EQUAL_INT32(MIN_SYSTEM_VOLTAGE_MV, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 269 TEST_ASSERT(pBatteryCharger->setSystemVoltage(MAX_SYSTEM_VOLTAGE_MV));
rob.meades@u-blox.com 1:ed57b6ca43ab 270 TEST_ASSERT(pBatteryCharger->getSystemVoltage(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 271 TEST_ASSERT_EQUAL_INT32(MAX_SYSTEM_VOLTAGE_MV, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 272
rob.meades@u-blox.com 1:ed57b6ca43ab 273 // The voltage read back should be at least the value requested and
rob.meades@u-blox.com 1:ed57b6ca43ab 274 // not more than 100 mV greater
rob.meades@u-blox.com 1:ed57b6ca43ab 275 for (uint32_t x = 0; x < NUM_RAND_ITERATIONS; x++) {
rob.meades@u-blox.com 1:ed57b6ca43ab 276 setValue = MIN_SYSTEM_VOLTAGE_MV + rand() % (MAX_SYSTEM_VOLTAGE_MV - MIN_SYSTEM_VOLTAGE_MV + 1);
rob.meades@u-blox.com 1:ed57b6ca43ab 277 TEST_ASSERT(pBatteryCharger->setSystemVoltage(setValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 278 getValue = -1;
rob.meades@u-blox.com 1:ed57b6ca43ab 279 TEST_ASSERT(pBatteryCharger->getSystemVoltage(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 280 TEST_ASSERT((getValue < setValue + 100) && (getValue >= setValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 281 TEST_ASSERT((getValue >= MIN_SYSTEM_VOLTAGE_MV) && (getValue <= MAX_SYSTEM_VOLTAGE_MV));
rob.meades@u-blox.com 1:ed57b6ca43ab 282 }
rob.meades@u-blox.com 1:ed57b6ca43ab 283
rob.meades@u-blox.com 1:ed57b6ca43ab 284 // Parameter can be NULL
rob.meades@u-blox.com 1:ed57b6ca43ab 285 TEST_ASSERT(pBatteryCharger->getSystemVoltage(NULL));
rob.meades@u-blox.com 1:ed57b6ca43ab 286
rob.meades@u-blox.com 1:ed57b6ca43ab 287 // Put the initial value back when we're done
rob.meades@u-blox.com 1:ed57b6ca43ab 288 TEST_ASSERT(pBatteryCharger->setSystemVoltage(voltageOriginal));
rob.meades@u-blox.com 1:ed57b6ca43ab 289 }
rob.meades@u-blox.com 1:ed57b6ca43ab 290
rob.meades@u-blox.com 1:ed57b6ca43ab 291 // Test that we can read and change the fast charging current limits
rob.meades@u-blox.com 1:ed57b6ca43ab 292 void test_fast_charging_current_limits() {
rob.meades@u-blox.com 1:ed57b6ca43ab 293 BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
rob.meades@u-blox.com 1:ed57b6ca43ab 294 int32_t currentOriginal;
rob.meades@u-blox.com 1:ed57b6ca43ab 295 int32_t setValue;
rob.meades@u-blox.com 1:ed57b6ca43ab 296 int32_t getValue;
rob.meades@u-blox.com 1:ed57b6ca43ab 297
rob.meades@u-blox.com 1:ed57b6ca43ab 298 // Calls should return false if the battery charger has not been initialised
rob.meades@u-blox.com 1:ed57b6ca43ab 299 TEST_ASSERT_FALSE(pBatteryCharger->getFastChargingCurrentLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 300 TEST_ASSERT_FALSE(pBatteryCharger->setFastChargingCurrentLimit(getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 301
rob.meades@u-blox.com 1:ed57b6ca43ab 302 // Initialise the battery charger
rob.meades@u-blox.com 1:ed57b6ca43ab 303 TEST_ASSERT(pBatteryCharger->init(gpI2C));
rob.meades@u-blox.com 1:ed57b6ca43ab 304
rob.meades@u-blox.com 1:ed57b6ca43ab 305 // Save the initial value
rob.meades@u-blox.com 1:ed57b6ca43ab 306 TEST_ASSERT(pBatteryCharger->getFastChargingCurrentLimit(&currentOriginal));
rob.meades@u-blox.com 1:ed57b6ca43ab 307
rob.meades@u-blox.com 1:ed57b6ca43ab 308 // Beyond the limits
rob.meades@u-blox.com 1:ed57b6ca43ab 309 TEST_ASSERT_FALSE(pBatteryCharger->setFastChargingCurrentLimit(MIN_FAST_CHARGING_CURRENT_LIMIT_MA - 1));
rob.meades@u-blox.com 1:ed57b6ca43ab 310 TEST_ASSERT_FALSE(pBatteryCharger->setFastChargingCurrentLimit(MAX_FAST_CHARGING_CURRENT_LIMIT_MA + 1));
rob.meades@u-blox.com 1:ed57b6ca43ab 311
rob.meades@u-blox.com 1:ed57b6ca43ab 312 // At the limits
rob.meades@u-blox.com 1:ed57b6ca43ab 313 TEST_ASSERT(pBatteryCharger->setFastChargingCurrentLimit(MIN_FAST_CHARGING_CURRENT_LIMIT_MA));
rob.meades@u-blox.com 1:ed57b6ca43ab 314 TEST_ASSERT(pBatteryCharger->getFastChargingCurrentLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 315 TEST_ASSERT_EQUAL_INT32(MIN_FAST_CHARGING_CURRENT_LIMIT_MA, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 316 TEST_ASSERT(pBatteryCharger->setFastChargingCurrentLimit(MAX_FAST_CHARGING_CURRENT_LIMIT_MA));
rob.meades@u-blox.com 1:ed57b6ca43ab 317 TEST_ASSERT(pBatteryCharger->getFastChargingCurrentLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 318 TEST_ASSERT_EQUAL_INT32(MAX_FAST_CHARGING_CURRENT_LIMIT_MA, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 319
rob.meades@u-blox.com 1:ed57b6ca43ab 320 // The current limit read back should not be more than 64 mA below the value requested
rob.meades@u-blox.com 1:ed57b6ca43ab 321 for (uint32_t x = 0; x < NUM_RAND_ITERATIONS; x++) {
rob.meades@u-blox.com 1:ed57b6ca43ab 322 setValue = MIN_FAST_CHARGING_CURRENT_LIMIT_MA + rand() % (MAX_FAST_CHARGING_CURRENT_LIMIT_MA - MIN_FAST_CHARGING_CURRENT_LIMIT_MA + 1);
rob.meades@u-blox.com 1:ed57b6ca43ab 323 TEST_ASSERT(pBatteryCharger->setFastChargingCurrentLimit(setValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 324 getValue = -1;
rob.meades@u-blox.com 1:ed57b6ca43ab 325 TEST_ASSERT(pBatteryCharger->getFastChargingCurrentLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 326 TEST_ASSERT(getValue <= setValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 327 TEST_ASSERT((getValue >= MIN_FAST_CHARGING_CURRENT_LIMIT_MA) && (getValue <= MAX_FAST_CHARGING_CURRENT_LIMIT_MA));
rob.meades@u-blox.com 1:ed57b6ca43ab 328 }
rob.meades@u-blox.com 1:ed57b6ca43ab 329
rob.meades@u-blox.com 1:ed57b6ca43ab 330 // Parameter can be NULL
rob.meades@u-blox.com 1:ed57b6ca43ab 331 TEST_ASSERT(pBatteryCharger->getFastChargingCurrentLimit(NULL));
rob.meades@u-blox.com 1:ed57b6ca43ab 332
rob.meades@u-blox.com 1:ed57b6ca43ab 333 // Put the initial value back when we're done
rob.meades@u-blox.com 1:ed57b6ca43ab 334 TEST_ASSERT(pBatteryCharger->setFastChargingCurrentLimit(currentOriginal));
rob.meades@u-blox.com 1:ed57b6ca43ab 335 }
rob.meades@u-blox.com 1:ed57b6ca43ab 336
rob.meades@u-blox.com 1:ed57b6ca43ab 337 // Test that we can enable and disable the ICGH/IPRECH margin
rob.meades@u-blox.com 1:ed57b6ca43ab 338 void test_icgh_iprech_margin() {
rob.meades@u-blox.com 1:ed57b6ca43ab 339 BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
rob.meades@u-blox.com 1:ed57b6ca43ab 340 bool marginEnabled;
rob.meades@u-blox.com 1:ed57b6ca43ab 341
rob.meades@u-blox.com 1:ed57b6ca43ab 342 // Call should fail if the battery charger has not been initialised
rob.meades@u-blox.com 1:ed57b6ca43ab 343 TEST_ASSERT_FALSE(pBatteryCharger->isIcghIprechMarginEnabled());
rob.meades@u-blox.com 1:ed57b6ca43ab 344 TEST_ASSERT_FALSE(pBatteryCharger->enableIcghIprechMargin());
rob.meades@u-blox.com 1:ed57b6ca43ab 345 TEST_ASSERT_FALSE(pBatteryCharger->disableIcghIprechMargin());
rob.meades@u-blox.com 1:ed57b6ca43ab 346
rob.meades@u-blox.com 1:ed57b6ca43ab 347 // Initialise the battery charger
rob.meades@u-blox.com 1:ed57b6ca43ab 348 TEST_ASSERT(pBatteryCharger->init(gpI2C));
rob.meades@u-blox.com 1:ed57b6ca43ab 349
rob.meades@u-blox.com 1:ed57b6ca43ab 350 // Save the initial value
rob.meades@u-blox.com 1:ed57b6ca43ab 351 marginEnabled = pBatteryCharger->isIcghIprechMarginEnabled();
rob.meades@u-blox.com 1:ed57b6ca43ab 352
rob.meades@u-blox.com 1:ed57b6ca43ab 353 // Enable and disable the margin
rob.meades@u-blox.com 1:ed57b6ca43ab 354 TEST_ASSERT(pBatteryCharger->enableIcghIprechMargin());
rob.meades@u-blox.com 1:ed57b6ca43ab 355 TEST_ASSERT(pBatteryCharger->isIcghIprechMarginEnabled());
rob.meades@u-blox.com 1:ed57b6ca43ab 356 TEST_ASSERT(pBatteryCharger->disableIcghIprechMargin());
rob.meades@u-blox.com 1:ed57b6ca43ab 357 TEST_ASSERT_FALSE(pBatteryCharger->isIcghIprechMarginEnabled());
rob.meades@u-blox.com 1:ed57b6ca43ab 358 TEST_ASSERT(pBatteryCharger->enableIcghIprechMargin());
rob.meades@u-blox.com 1:ed57b6ca43ab 359 TEST_ASSERT(pBatteryCharger->isIcghIprechMarginEnabled());
rob.meades@u-blox.com 1:ed57b6ca43ab 360
rob.meades@u-blox.com 1:ed57b6ca43ab 361 // Put the initial value back when we're done
rob.meades@u-blox.com 1:ed57b6ca43ab 362 if (marginEnabled) {
rob.meades@u-blox.com 1:ed57b6ca43ab 363 pBatteryCharger->enableIcghIprechMargin();
rob.meades@u-blox.com 1:ed57b6ca43ab 364 } else {
rob.meades@u-blox.com 1:ed57b6ca43ab 365 pBatteryCharger->disableIcghIprechMargin();
rob.meades@u-blox.com 1:ed57b6ca43ab 366 }
rob.meades@u-blox.com 1:ed57b6ca43ab 367 }
rob.meades@u-blox.com 1:ed57b6ca43ab 368
rob.meades@u-blox.com 1:ed57b6ca43ab 369 // Test that we can read and change the pre-charging current limits
rob.meades@u-blox.com 1:ed57b6ca43ab 370 void test_precharging_current_limits() {
rob.meades@u-blox.com 1:ed57b6ca43ab 371 BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
rob.meades@u-blox.com 1:ed57b6ca43ab 372 int32_t currentOriginal;
rob.meades@u-blox.com 1:ed57b6ca43ab 373 int32_t setValue;
rob.meades@u-blox.com 1:ed57b6ca43ab 374 int32_t getValue;
rob.meades@u-blox.com 1:ed57b6ca43ab 375
rob.meades@u-blox.com 1:ed57b6ca43ab 376 // Calls should return false if the battery charger has not been initialised
rob.meades@u-blox.com 1:ed57b6ca43ab 377 TEST_ASSERT_FALSE(pBatteryCharger->getPrechargingCurrentLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 378 TEST_ASSERT_FALSE(pBatteryCharger->setPrechargingCurrentLimit(getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 379
rob.meades@u-blox.com 1:ed57b6ca43ab 380 // Initialise the battery charger
rob.meades@u-blox.com 1:ed57b6ca43ab 381 TEST_ASSERT(pBatteryCharger->init(gpI2C));
rob.meades@u-blox.com 1:ed57b6ca43ab 382
rob.meades@u-blox.com 1:ed57b6ca43ab 383 // Save the initial value
rob.meades@u-blox.com 1:ed57b6ca43ab 384 TEST_ASSERT(pBatteryCharger->getPrechargingCurrentLimit(&currentOriginal));
rob.meades@u-blox.com 1:ed57b6ca43ab 385
rob.meades@u-blox.com 1:ed57b6ca43ab 386 // Beyond the limits
rob.meades@u-blox.com 1:ed57b6ca43ab 387 TEST_ASSERT_FALSE(pBatteryCharger->setPrechargingCurrentLimit(MIN_PRECHARGING_CURRENT_LIMIT_MA - 1));
rob.meades@u-blox.com 1:ed57b6ca43ab 388 TEST_ASSERT_FALSE(pBatteryCharger->setPrechargingCurrentLimit(MAX_PRECHARGING_CURRENT_LIMIT_MA + 1));
rob.meades@u-blox.com 1:ed57b6ca43ab 389
rob.meades@u-blox.com 1:ed57b6ca43ab 390 // At the limits
rob.meades@u-blox.com 1:ed57b6ca43ab 391 TEST_ASSERT(pBatteryCharger->setPrechargingCurrentLimit(MIN_PRECHARGING_CURRENT_LIMIT_MA));
rob.meades@u-blox.com 1:ed57b6ca43ab 392 TEST_ASSERT(pBatteryCharger->getPrechargingCurrentLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 393 TEST_ASSERT_EQUAL_INT32(MIN_PRECHARGING_CURRENT_LIMIT_MA, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 394 TEST_ASSERT(pBatteryCharger->setPrechargingCurrentLimit(MAX_PRECHARGING_CURRENT_LIMIT_MA));
rob.meades@u-blox.com 1:ed57b6ca43ab 395 TEST_ASSERT(pBatteryCharger->getPrechargingCurrentLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 396 TEST_ASSERT_EQUAL_INT32(MAX_PRECHARGING_CURRENT_LIMIT_MA, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 397
rob.meades@u-blox.com 1:ed57b6ca43ab 398 // The current limit read back should not be more than 128 mA below the value requested
rob.meades@u-blox.com 1:ed57b6ca43ab 399 for (uint32_t x = 0; x < NUM_RAND_ITERATIONS; x++) {
rob.meades@u-blox.com 1:ed57b6ca43ab 400 setValue = MIN_PRECHARGING_CURRENT_LIMIT_MA + rand() % (MAX_PRECHARGING_CURRENT_LIMIT_MA - MIN_PRECHARGING_CURRENT_LIMIT_MA + 1);
rob.meades@u-blox.com 1:ed57b6ca43ab 401 TEST_ASSERT(pBatteryCharger->setPrechargingCurrentLimit(setValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 402 getValue = -1;
rob.meades@u-blox.com 1:ed57b6ca43ab 403 TEST_ASSERT(pBatteryCharger->getPrechargingCurrentLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 404 TEST_ASSERT(getValue <= setValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 405 TEST_ASSERT((getValue >= MIN_PRECHARGING_CURRENT_LIMIT_MA) && (getValue <= MAX_PRECHARGING_CURRENT_LIMIT_MA));
rob.meades@u-blox.com 1:ed57b6ca43ab 406 }
rob.meades@u-blox.com 1:ed57b6ca43ab 407
rob.meades@u-blox.com 1:ed57b6ca43ab 408 // Parameter can be NULL
rob.meades@u-blox.com 1:ed57b6ca43ab 409 TEST_ASSERT(pBatteryCharger->getPrechargingCurrentLimit(NULL));
rob.meades@u-blox.com 1:ed57b6ca43ab 410
rob.meades@u-blox.com 1:ed57b6ca43ab 411 // Put the initial value back when we're done
rob.meades@u-blox.com 1:ed57b6ca43ab 412 TEST_ASSERT(pBatteryCharger->setPrechargingCurrentLimit(currentOriginal));
rob.meades@u-blox.com 1:ed57b6ca43ab 413 }
rob.meades@u-blox.com 1:ed57b6ca43ab 414
rob.meades@u-blox.com 1:ed57b6ca43ab 415 // Test that we can read, change and enable/disable the charging termination current
rob.meades@u-blox.com 1:ed57b6ca43ab 416 void test_charging_termination_current() {
rob.meades@u-blox.com 1:ed57b6ca43ab 417 BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
rob.meades@u-blox.com 1:ed57b6ca43ab 418 int32_t currentOriginal;
rob.meades@u-blox.com 1:ed57b6ca43ab 419 bool terminationEnabled;
rob.meades@u-blox.com 1:ed57b6ca43ab 420 int32_t setValue;
rob.meades@u-blox.com 1:ed57b6ca43ab 421 int32_t getValue;
rob.meades@u-blox.com 1:ed57b6ca43ab 422
rob.meades@u-blox.com 1:ed57b6ca43ab 423 // Calls should return false if the battery charger has not been initialised
rob.meades@u-blox.com 1:ed57b6ca43ab 424 TEST_ASSERT_FALSE(pBatteryCharger->getChargingTerminationCurrent(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 425 TEST_ASSERT_FALSE(pBatteryCharger->setChargingTerminationCurrent(getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 426 TEST_ASSERT_FALSE(pBatteryCharger->enableChargingTermination());
rob.meades@u-blox.com 1:ed57b6ca43ab 427 TEST_ASSERT_FALSE(pBatteryCharger->disableChargingTermination());
rob.meades@u-blox.com 1:ed57b6ca43ab 428
rob.meades@u-blox.com 1:ed57b6ca43ab 429 // Initialise the battery charger
rob.meades@u-blox.com 1:ed57b6ca43ab 430 TEST_ASSERT(pBatteryCharger->init(gpI2C));
rob.meades@u-blox.com 1:ed57b6ca43ab 431
rob.meades@u-blox.com 1:ed57b6ca43ab 432 // Save the initial values
rob.meades@u-blox.com 1:ed57b6ca43ab 433 TEST_ASSERT(pBatteryCharger->getChargingTerminationCurrent(&currentOriginal));
rob.meades@u-blox.com 1:ed57b6ca43ab 434 terminationEnabled = pBatteryCharger->isChargingTerminationEnabled();
rob.meades@u-blox.com 1:ed57b6ca43ab 435
rob.meades@u-blox.com 1:ed57b6ca43ab 436 // Beyond the limits
rob.meades@u-blox.com 1:ed57b6ca43ab 437 TEST_ASSERT_FALSE(pBatteryCharger->setChargingTerminationCurrent(MIN_CHARGING_TERMINATION_CURRENT_MA - 1));
rob.meades@u-blox.com 1:ed57b6ca43ab 438 TEST_ASSERT_FALSE(pBatteryCharger->setChargingTerminationCurrent(MAX_CHARGING_TERMINATION_CURRENT_MA + 1));
rob.meades@u-blox.com 1:ed57b6ca43ab 439
rob.meades@u-blox.com 1:ed57b6ca43ab 440 // At the limits
rob.meades@u-blox.com 1:ed57b6ca43ab 441 TEST_ASSERT(pBatteryCharger->setChargingTerminationCurrent(MIN_CHARGING_TERMINATION_CURRENT_MA));
rob.meades@u-blox.com 1:ed57b6ca43ab 442 TEST_ASSERT(pBatteryCharger->getChargingTerminationCurrent(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 443 TEST_ASSERT_EQUAL_INT32(MIN_CHARGING_TERMINATION_CURRENT_MA, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 444 TEST_ASSERT(pBatteryCharger->setChargingTerminationCurrent(MAX_CHARGING_TERMINATION_CURRENT_MA));
rob.meades@u-blox.com 1:ed57b6ca43ab 445 TEST_ASSERT(pBatteryCharger->getChargingTerminationCurrent(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 446 TEST_ASSERT_EQUAL_INT32(MAX_CHARGING_TERMINATION_CURRENT_MA, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 447
rob.meades@u-blox.com 1:ed57b6ca43ab 448 // The current limit read back should not be more than 128 mA below the value requested
rob.meades@u-blox.com 1:ed57b6ca43ab 449 for (uint32_t x = 0; x < NUM_RAND_ITERATIONS; x++) {
rob.meades@u-blox.com 1:ed57b6ca43ab 450 setValue = MIN_CHARGING_TERMINATION_CURRENT_MA + rand() % (MAX_CHARGING_TERMINATION_CURRENT_MA - MIN_CHARGING_TERMINATION_CURRENT_MA + 1);
rob.meades@u-blox.com 1:ed57b6ca43ab 451 TEST_ASSERT(pBatteryCharger->setChargingTerminationCurrent(setValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 452 getValue = -1;
rob.meades@u-blox.com 1:ed57b6ca43ab 453 TEST_ASSERT(pBatteryCharger->getChargingTerminationCurrent(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 454 TEST_ASSERT(getValue <= setValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 455 TEST_ASSERT((getValue >= MIN_CHARGING_TERMINATION_CURRENT_MA) && (getValue <= MAX_CHARGING_TERMINATION_CURRENT_MA));
rob.meades@u-blox.com 1:ed57b6ca43ab 456 }
rob.meades@u-blox.com 1:ed57b6ca43ab 457
rob.meades@u-blox.com 1:ed57b6ca43ab 458 // Enable and disable the margin
rob.meades@u-blox.com 1:ed57b6ca43ab 459 TEST_ASSERT(pBatteryCharger->enableChargingTermination());
rob.meades@u-blox.com 1:ed57b6ca43ab 460 TEST_ASSERT(pBatteryCharger->isChargingTerminationEnabled());
rob.meades@u-blox.com 1:ed57b6ca43ab 461 TEST_ASSERT(pBatteryCharger->disableChargingTermination());
rob.meades@u-blox.com 1:ed57b6ca43ab 462 TEST_ASSERT_FALSE(pBatteryCharger->isChargingTerminationEnabled());
rob.meades@u-blox.com 1:ed57b6ca43ab 463 TEST_ASSERT(pBatteryCharger->enableChargingTermination());
rob.meades@u-blox.com 1:ed57b6ca43ab 464 TEST_ASSERT(pBatteryCharger->isChargingTerminationEnabled());
rob.meades@u-blox.com 1:ed57b6ca43ab 465
rob.meades@u-blox.com 1:ed57b6ca43ab 466 // Parameter can be NULL
rob.meades@u-blox.com 1:ed57b6ca43ab 467 TEST_ASSERT(pBatteryCharger->getChargingTerminationCurrent(NULL));
rob.meades@u-blox.com 1:ed57b6ca43ab 468
rob.meades@u-blox.com 1:ed57b6ca43ab 469 // Put the initial value back when we're done
rob.meades@u-blox.com 1:ed57b6ca43ab 470 TEST_ASSERT(pBatteryCharger->setChargingTerminationCurrent(currentOriginal));
rob.meades@u-blox.com 1:ed57b6ca43ab 471 if (terminationEnabled) {
rob.meades@u-blox.com 1:ed57b6ca43ab 472 pBatteryCharger->enableChargingTermination();
rob.meades@u-blox.com 1:ed57b6ca43ab 473 } else {
rob.meades@u-blox.com 1:ed57b6ca43ab 474 pBatteryCharger->disableChargingTermination();
rob.meades@u-blox.com 1:ed57b6ca43ab 475 }
rob.meades@u-blox.com 1:ed57b6ca43ab 476 }
rob.meades@u-blox.com 1:ed57b6ca43ab 477
rob.meades@u-blox.com 1:ed57b6ca43ab 478 // Test that we can read and change the various charging voltage limits
rob.meades@u-blox.com 1:ed57b6ca43ab 479 void test_charging_voltage_limits() {
rob.meades@u-blox.com 1:ed57b6ca43ab 480 BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
rob.meades@u-blox.com 1:ed57b6ca43ab 481 int32_t chargingOriginal;
rob.meades@u-blox.com 1:ed57b6ca43ab 482 int32_t fastChargingOriginal;
rob.meades@u-blox.com 1:ed57b6ca43ab 483 int32_t rechargingOriginal;
rob.meades@u-blox.com 1:ed57b6ca43ab 484 int32_t setValue;
rob.meades@u-blox.com 1:ed57b6ca43ab 485 int32_t getValue;
rob.meades@u-blox.com 1:ed57b6ca43ab 486
rob.meades@u-blox.com 1:ed57b6ca43ab 487 // Calls should return false if the battery charger has not been initialised
rob.meades@u-blox.com 1:ed57b6ca43ab 488 TEST_ASSERT_FALSE(pBatteryCharger->getChargingVoltageLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 489 TEST_ASSERT_FALSE(pBatteryCharger->setChargingVoltageLimit(getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 490 TEST_ASSERT_FALSE(pBatteryCharger->getFastChargingVoltageThreshold(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 491 TEST_ASSERT_FALSE(pBatteryCharger->setFastChargingVoltageThreshold(getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 492 TEST_ASSERT_FALSE(pBatteryCharger->getRechargingVoltageThreshold(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 493 TEST_ASSERT_FALSE(pBatteryCharger->setRechargingVoltageThreshold(getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 494
rob.meades@u-blox.com 1:ed57b6ca43ab 495 // Initialise the battery charger
rob.meades@u-blox.com 1:ed57b6ca43ab 496 TEST_ASSERT(pBatteryCharger->init(gpI2C));
rob.meades@u-blox.com 1:ed57b6ca43ab 497
rob.meades@u-blox.com 1:ed57b6ca43ab 498 // Save the initial values
rob.meades@u-blox.com 1:ed57b6ca43ab 499 TEST_ASSERT(pBatteryCharger->getChargingVoltageLimit(&chargingOriginal));
rob.meades@u-blox.com 1:ed57b6ca43ab 500 TEST_ASSERT(pBatteryCharger->getFastChargingVoltageThreshold(&fastChargingOriginal));
rob.meades@u-blox.com 1:ed57b6ca43ab 501 TEST_ASSERT(pBatteryCharger->getRechargingVoltageThreshold(&rechargingOriginal));
rob.meades@u-blox.com 1:ed57b6ca43ab 502
rob.meades@u-blox.com 1:ed57b6ca43ab 503 // Beyond the limits for the charging voltage
rob.meades@u-blox.com 1:ed57b6ca43ab 504 TEST_ASSERT_FALSE(pBatteryCharger->setChargingVoltageLimit(MIN_CHARGING_VOLTAGE_LIMIT_MV - 1));
rob.meades@u-blox.com 1:ed57b6ca43ab 505 TEST_ASSERT_FALSE(pBatteryCharger->setChargingVoltageLimit(MAX_CHARGING_VOLTAGE_LIMIT_MV + 1));
rob.meades@u-blox.com 1:ed57b6ca43ab 506
rob.meades@u-blox.com 1:ed57b6ca43ab 507 // At the limits for the charging voltage
rob.meades@u-blox.com 1:ed57b6ca43ab 508 TEST_ASSERT(pBatteryCharger->setChargingVoltageLimit(MIN_CHARGING_VOLTAGE_LIMIT_MV));
rob.meades@u-blox.com 1:ed57b6ca43ab 509 TEST_ASSERT(pBatteryCharger->getChargingVoltageLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 510 TEST_ASSERT_EQUAL_INT32(MIN_CHARGING_VOLTAGE_LIMIT_MV, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 511 TEST_ASSERT(pBatteryCharger->setChargingVoltageLimit(MAX_CHARGING_VOLTAGE_LIMIT_MV));
rob.meades@u-blox.com 1:ed57b6ca43ab 512 TEST_ASSERT(pBatteryCharger->getChargingVoltageLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 513 TEST_ASSERT_EQUAL_INT32(MAX_CHARGING_VOLTAGE_LIMIT_MV, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 514
rob.meades@u-blox.com 1:ed57b6ca43ab 515 // The charging voltage limit read back should not be more than 16 mV below the value requested
rob.meades@u-blox.com 1:ed57b6ca43ab 516 for (uint32_t x = 0; x < NUM_RAND_ITERATIONS; x++) {
rob.meades@u-blox.com 1:ed57b6ca43ab 517 setValue = MIN_CHARGING_VOLTAGE_LIMIT_MV + rand() % (MAX_CHARGING_VOLTAGE_LIMIT_MV - MIN_CHARGING_VOLTAGE_LIMIT_MV + 1);
rob.meades@u-blox.com 1:ed57b6ca43ab 518 TEST_ASSERT(pBatteryCharger->setChargingVoltageLimit(setValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 519 getValue = -1;
rob.meades@u-blox.com 1:ed57b6ca43ab 520 TEST_ASSERT(pBatteryCharger->getChargingVoltageLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 521 TEST_ASSERT((getValue > setValue - 16) && (getValue <= setValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 522 TEST_ASSERT((getValue >= MIN_CHARGING_VOLTAGE_LIMIT_MV) && (getValue <= MAX_CHARGING_VOLTAGE_LIMIT_MV));
rob.meades@u-blox.com 1:ed57b6ca43ab 523 }
rob.meades@u-blox.com 1:ed57b6ca43ab 524
rob.meades@u-blox.com 1:ed57b6ca43ab 525 // Fast charging threshold is 2.8 V or 3.0 V
rob.meades@u-blox.com 1:ed57b6ca43ab 526 TEST_ASSERT(pBatteryCharger->setFastChargingVoltageThreshold(2799));
rob.meades@u-blox.com 1:ed57b6ca43ab 527 TEST_ASSERT(pBatteryCharger->getFastChargingVoltageThreshold(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 528 TEST_ASSERT_EQUAL_INT32(2800, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 529 TEST_ASSERT(pBatteryCharger->setFastChargingVoltageThreshold(2800));
rob.meades@u-blox.com 1:ed57b6ca43ab 530 TEST_ASSERT(pBatteryCharger->getFastChargingVoltageThreshold(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 531 TEST_ASSERT_EQUAL_INT32(2800, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 532 TEST_ASSERT(pBatteryCharger->setFastChargingVoltageThreshold(2801));
rob.meades@u-blox.com 1:ed57b6ca43ab 533 TEST_ASSERT(pBatteryCharger->getFastChargingVoltageThreshold(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 534 TEST_ASSERT_EQUAL_INT32(3000, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 535 TEST_ASSERT(pBatteryCharger->setFastChargingVoltageThreshold(3000));
rob.meades@u-blox.com 1:ed57b6ca43ab 536 TEST_ASSERT(pBatteryCharger->getFastChargingVoltageThreshold(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 537 TEST_ASSERT_EQUAL_INT32(3000, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 538 TEST_ASSERT(pBatteryCharger->setFastChargingVoltageThreshold(3001));
rob.meades@u-blox.com 1:ed57b6ca43ab 539 TEST_ASSERT(pBatteryCharger->getFastChargingVoltageThreshold(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 540 TEST_ASSERT_EQUAL_INT32(3000, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 541
rob.meades@u-blox.com 1:ed57b6ca43ab 542 // Recharging threshold is 100 mV or 300 mV
rob.meades@u-blox.com 1:ed57b6ca43ab 543 TEST_ASSERT(pBatteryCharger->setRechargingVoltageThreshold(99));
rob.meades@u-blox.com 1:ed57b6ca43ab 544 TEST_ASSERT(pBatteryCharger->getRechargingVoltageThreshold(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 545 TEST_ASSERT_EQUAL_INT32(100, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 546 TEST_ASSERT(pBatteryCharger->setRechargingVoltageThreshold(100));
rob.meades@u-blox.com 1:ed57b6ca43ab 547 TEST_ASSERT(pBatteryCharger->getRechargingVoltageThreshold(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 548 TEST_ASSERT_EQUAL_INT32(100, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 549 TEST_ASSERT(pBatteryCharger->setRechargingVoltageThreshold(101));
rob.meades@u-blox.com 1:ed57b6ca43ab 550 TEST_ASSERT(pBatteryCharger->getRechargingVoltageThreshold(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 551 TEST_ASSERT_EQUAL_INT32(300, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 552 TEST_ASSERT(pBatteryCharger->setRechargingVoltageThreshold(300));
rob.meades@u-blox.com 1:ed57b6ca43ab 553 TEST_ASSERT(pBatteryCharger->getRechargingVoltageThreshold(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 554 TEST_ASSERT_EQUAL_INT32(300, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 555 TEST_ASSERT(pBatteryCharger->setRechargingVoltageThreshold(301));
rob.meades@u-blox.com 1:ed57b6ca43ab 556 TEST_ASSERT(pBatteryCharger->getRechargingVoltageThreshold(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 557 TEST_ASSERT_EQUAL_INT32(300, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 558
rob.meades@u-blox.com 1:ed57b6ca43ab 559 // Parameters can be NULL
rob.meades@u-blox.com 1:ed57b6ca43ab 560 TEST_ASSERT(pBatteryCharger->getChargingVoltageLimit(NULL));
rob.meades@u-blox.com 1:ed57b6ca43ab 561 TEST_ASSERT(pBatteryCharger->getFastChargingVoltageThreshold(NULL));
rob.meades@u-blox.com 1:ed57b6ca43ab 562 TEST_ASSERT(pBatteryCharger->getRechargingVoltageThreshold(NULL));
rob.meades@u-blox.com 1:ed57b6ca43ab 563
rob.meades@u-blox.com 1:ed57b6ca43ab 564 // Put the initial values back when we're done
rob.meades@u-blox.com 1:ed57b6ca43ab 565 TEST_ASSERT(pBatteryCharger->setChargingVoltageLimit(chargingOriginal));
rob.meades@u-blox.com 1:ed57b6ca43ab 566 TEST_ASSERT(pBatteryCharger->setFastChargingVoltageThreshold(fastChargingOriginal));
rob.meades@u-blox.com 1:ed57b6ca43ab 567 TEST_ASSERT(pBatteryCharger->setRechargingVoltageThreshold(rechargingOriginal));
rob.meades@u-blox.com 1:ed57b6ca43ab 568 }
rob.meades@u-blox.com 1:ed57b6ca43ab 569
rob.meades@u-blox.com 1:ed57b6ca43ab 570 // Test that we can read and change the fast charging safety timer
rob.meades@u-blox.com 1:ed57b6ca43ab 571 void test_fast_charging_safety_timer() {
rob.meades@u-blox.com 1:ed57b6ca43ab 572 BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
rob.meades@u-blox.com 1:ed57b6ca43ab 573 int32_t timerOriginal;
rob.meades@u-blox.com 1:ed57b6ca43ab 574 int32_t getValue;
rob.meades@u-blox.com 1:ed57b6ca43ab 575
rob.meades@u-blox.com 1:ed57b6ca43ab 576 // Calls should return false if the battery charger has not been initialised
rob.meades@u-blox.com 1:ed57b6ca43ab 577 TEST_ASSERT_FALSE(pBatteryCharger->getFastChargingSafetyTimer(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 578 TEST_ASSERT_FALSE(pBatteryCharger->setFastChargingSafetyTimer(getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 579
rob.meades@u-blox.com 1:ed57b6ca43ab 580 // Initialise the battery charger
rob.meades@u-blox.com 1:ed57b6ca43ab 581 TEST_ASSERT(pBatteryCharger->init(gpI2C));
rob.meades@u-blox.com 1:ed57b6ca43ab 582
rob.meades@u-blox.com 1:ed57b6ca43ab 583 // Save the initial value
rob.meades@u-blox.com 1:ed57b6ca43ab 584 TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&timerOriginal));
rob.meades@u-blox.com 1:ed57b6ca43ab 585
rob.meades@u-blox.com 1:ed57b6ca43ab 586 // Beyond the limit
rob.meades@u-blox.com 1:ed57b6ca43ab 587 TEST_ASSERT_FALSE(pBatteryCharger->setFastChargingSafetyTimer(-1));
rob.meades@u-blox.com 1:ed57b6ca43ab 588
rob.meades@u-blox.com 1:ed57b6ca43ab 589 // There are permissible values are 0, 5, 8, 12 and 20 so test them and the
rob.meades@u-blox.com 1:ed57b6ca43ab 590 // boundaries around them
rob.meades@u-blox.com 1:ed57b6ca43ab 591 TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(0));
rob.meades@u-blox.com 1:ed57b6ca43ab 592 TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 593 TEST_ASSERT_EQUAL_INT32(0, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 594 TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(1));
rob.meades@u-blox.com 1:ed57b6ca43ab 595 TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 596 TEST_ASSERT_EQUAL_INT32(5, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 597 TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(4));
rob.meades@u-blox.com 1:ed57b6ca43ab 598 TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 599 TEST_ASSERT_EQUAL_INT32(5, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 600 TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(6));
rob.meades@u-blox.com 1:ed57b6ca43ab 601 TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 602 TEST_ASSERT_EQUAL_INT32(5, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 603 TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(7));
rob.meades@u-blox.com 1:ed57b6ca43ab 604 TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 605 TEST_ASSERT_EQUAL_INT32(5, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 606 TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(8));
rob.meades@u-blox.com 1:ed57b6ca43ab 607 TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 608 TEST_ASSERT_EQUAL_INT32(8, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 609 TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(11));
rob.meades@u-blox.com 1:ed57b6ca43ab 610 TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 611 TEST_ASSERT_EQUAL_INT32(8, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 612 TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(12));
rob.meades@u-blox.com 1:ed57b6ca43ab 613 TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 614 TEST_ASSERT_EQUAL_INT32(12, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 615 TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(19));
rob.meades@u-blox.com 1:ed57b6ca43ab 616 TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 617 TEST_ASSERT_EQUAL_INT32(12, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 618 TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(20));
rob.meades@u-blox.com 1:ed57b6ca43ab 619 TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 620 TEST_ASSERT_EQUAL_INT32(20, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 621
rob.meades@u-blox.com 1:ed57b6ca43ab 622 // Parameter can be NULL
rob.meades@u-blox.com 1:ed57b6ca43ab 623 TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(NULL));
rob.meades@u-blox.com 1:ed57b6ca43ab 624
rob.meades@u-blox.com 1:ed57b6ca43ab 625 // Put the initial value back when we're done
rob.meades@u-blox.com 1:ed57b6ca43ab 626 TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(timerOriginal));
rob.meades@u-blox.com 1:ed57b6ca43ab 627 }
rob.meades@u-blox.com 1:ed57b6ca43ab 628
rob.meades@u-blox.com 1:ed57b6ca43ab 629 // Test setting the boost mode voltage and temperature limits
rob.meades@u-blox.com 1:ed57b6ca43ab 630 void test_boost_limits() {
rob.meades@u-blox.com 1:ed57b6ca43ab 631 BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
rob.meades@u-blox.com 1:ed57b6ca43ab 632 int32_t originalVoltage;
rob.meades@u-blox.com 1:ed57b6ca43ab 633 int32_t originalUpperTemperature;
rob.meades@u-blox.com 1:ed57b6ca43ab 634 bool upperTemperatureEnabled;
rob.meades@u-blox.com 1:ed57b6ca43ab 635 int32_t originalLowerTemperature;
rob.meades@u-blox.com 1:ed57b6ca43ab 636 int32_t setValue;
rob.meades@u-blox.com 1:ed57b6ca43ab 637 int32_t getValue;
rob.meades@u-blox.com 1:ed57b6ca43ab 638
rob.meades@u-blox.com 1:ed57b6ca43ab 639 // Calls should return false if the battery charger has not been initialised
rob.meades@u-blox.com 1:ed57b6ca43ab 640 TEST_ASSERT_FALSE(pBatteryCharger->getBoostVoltage(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 641 TEST_ASSERT_FALSE(pBatteryCharger->setBoostVoltage(getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 642 TEST_ASSERT_FALSE(pBatteryCharger->getBoostUpperTemperatureLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 643 TEST_ASSERT_FALSE(pBatteryCharger->setBoostUpperTemperatureLimit(getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 644 TEST_ASSERT_FALSE(pBatteryCharger->disableBoostUpperTemperatureLimit());
rob.meades@u-blox.com 1:ed57b6ca43ab 645 TEST_ASSERT_FALSE(pBatteryCharger->getBoostLowerTemperatureLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 646 TEST_ASSERT_FALSE(pBatteryCharger->setBoostLowerTemperatureLimit(getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 647
rob.meades@u-blox.com 1:ed57b6ca43ab 648 // Initialise the battery charger
rob.meades@u-blox.com 1:ed57b6ca43ab 649 TEST_ASSERT(pBatteryCharger->init(gpI2C));
rob.meades@u-blox.com 1:ed57b6ca43ab 650
rob.meades@u-blox.com 1:ed57b6ca43ab 651 // Save the initial values
rob.meades@u-blox.com 1:ed57b6ca43ab 652 TEST_ASSERT(pBatteryCharger->getBoostVoltage(&originalVoltage));
rob.meades@u-blox.com 1:ed57b6ca43ab 653 upperTemperatureEnabled = pBatteryCharger->isBoostUpperTemperatureLimitEnabled();
rob.meades@u-blox.com 1:ed57b6ca43ab 654 if (upperTemperatureEnabled) {
rob.meades@u-blox.com 1:ed57b6ca43ab 655 TEST_ASSERT(pBatteryCharger->getBoostUpperTemperatureLimit(&originalUpperTemperature));
rob.meades@u-blox.com 1:ed57b6ca43ab 656 }
rob.meades@u-blox.com 1:ed57b6ca43ab 657 TEST_ASSERT(pBatteryCharger->getBoostLowerTemperatureLimit(&originalLowerTemperature));
rob.meades@u-blox.com 1:ed57b6ca43ab 658
rob.meades@u-blox.com 1:ed57b6ca43ab 659 // Beyond the limits for the voltage
rob.meades@u-blox.com 1:ed57b6ca43ab 660 TEST_ASSERT_FALSE(pBatteryCharger->setBoostVoltage(MIN_BOOST_VOLTAGE_MV - 1));
rob.meades@u-blox.com 1:ed57b6ca43ab 661 TEST_ASSERT_FALSE(pBatteryCharger->setBoostVoltage(MAX_BOOST_VOLTAGE_MV + 1));
rob.meades@u-blox.com 1:ed57b6ca43ab 662
rob.meades@u-blox.com 1:ed57b6ca43ab 663 // At the limits for the voltage
rob.meades@u-blox.com 1:ed57b6ca43ab 664 TEST_ASSERT(pBatteryCharger->setBoostVoltage(MIN_BOOST_VOLTAGE_MV));
rob.meades@u-blox.com 1:ed57b6ca43ab 665 TEST_ASSERT(pBatteryCharger->getBoostVoltage(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 666 TEST_ASSERT_EQUAL_INT32(MIN_BOOST_VOLTAGE_MV, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 667 TEST_ASSERT(pBatteryCharger->setBoostVoltage(MAX_BOOST_VOLTAGE_MV));
rob.meades@u-blox.com 1:ed57b6ca43ab 668 TEST_ASSERT(pBatteryCharger->getBoostVoltage(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 669 TEST_ASSERT_EQUAL_INT32(MAX_BOOST_VOLTAGE_MV, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 670
rob.meades@u-blox.com 1:ed57b6ca43ab 671 // The voltage read back should be at least the value requested and
rob.meades@u-blox.com 1:ed57b6ca43ab 672 // not more than 64 mV greater
rob.meades@u-blox.com 1:ed57b6ca43ab 673 for (uint32_t x = 0; x < NUM_RAND_ITERATIONS; x++) {
rob.meades@u-blox.com 1:ed57b6ca43ab 674 setValue = MIN_BOOST_VOLTAGE_MV + rand() % (MAX_BOOST_VOLTAGE_MV - MIN_BOOST_VOLTAGE_MV + 1);
rob.meades@u-blox.com 1:ed57b6ca43ab 675 TEST_ASSERT(pBatteryCharger->setBoostVoltage(setValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 676 getValue = -1;
rob.meades@u-blox.com 1:ed57b6ca43ab 677 TEST_ASSERT(pBatteryCharger->getBoostVoltage(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 678 TEST_ASSERT((getValue < setValue + 64) && (getValue >= setValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 679 TEST_ASSERT((getValue >= MIN_BOOST_VOLTAGE_MV) && (getValue <= MAX_BOOST_VOLTAGE_MV));
rob.meades@u-blox.com 1:ed57b6ca43ab 680 }
rob.meades@u-blox.com 1:ed57b6ca43ab 681
rob.meades@u-blox.com 1:ed57b6ca43ab 682 // Disable the upper temperature limit and check that the get function returns false
rob.meades@u-blox.com 1:ed57b6ca43ab 683 TEST_ASSERT(pBatteryCharger->disableBoostUpperTemperatureLimit());
rob.meades@u-blox.com 1:ed57b6ca43ab 684 TEST_ASSERT_FALSE(pBatteryCharger->isBoostUpperTemperatureLimitEnabled());
rob.meades@u-blox.com 1:ed57b6ca43ab 685 TEST_ASSERT_FALSE(pBatteryCharger->getBoostUpperTemperatureLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 686
rob.meades@u-blox.com 1:ed57b6ca43ab 687 // The boost upper temperature limit is either 55, 60 or 65
rob.meades@u-blox.com 1:ed57b6ca43ab 688 TEST_ASSERT(pBatteryCharger->setBoostUpperTemperatureLimit(-1));
rob.meades@u-blox.com 1:ed57b6ca43ab 689 TEST_ASSERT(pBatteryCharger->getBoostUpperTemperatureLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 690 TEST_ASSERT_EQUAL_INT32(55, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 691 TEST_ASSERT(pBatteryCharger->setBoostUpperTemperatureLimit(0));
rob.meades@u-blox.com 1:ed57b6ca43ab 692 TEST_ASSERT(pBatteryCharger->getBoostUpperTemperatureLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 693 TEST_ASSERT_EQUAL_INT32(55, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 694 TEST_ASSERT(pBatteryCharger->setBoostUpperTemperatureLimit(59));
rob.meades@u-blox.com 1:ed57b6ca43ab 695 TEST_ASSERT(pBatteryCharger->getBoostUpperTemperatureLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 696 TEST_ASSERT_EQUAL_INT32(55, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 697 TEST_ASSERT(pBatteryCharger->setBoostUpperTemperatureLimit(60));
rob.meades@u-blox.com 1:ed57b6ca43ab 698 TEST_ASSERT(pBatteryCharger->getBoostUpperTemperatureLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 699 TEST_ASSERT_EQUAL_INT32(60, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 700 TEST_ASSERT(pBatteryCharger->setBoostUpperTemperatureLimit(64));
rob.meades@u-blox.com 1:ed57b6ca43ab 701 TEST_ASSERT(pBatteryCharger->getBoostUpperTemperatureLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 702 TEST_ASSERT_EQUAL_INT32(60, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 703 TEST_ASSERT(pBatteryCharger->setBoostUpperTemperatureLimit(65));
rob.meades@u-blox.com 1:ed57b6ca43ab 704 TEST_ASSERT(pBatteryCharger->getBoostUpperTemperatureLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 705 TEST_ASSERT_EQUAL_INT32(65, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 706 TEST_ASSERT(pBatteryCharger->setBoostUpperTemperatureLimit(100));
rob.meades@u-blox.com 1:ed57b6ca43ab 707 TEST_ASSERT(pBatteryCharger->getBoostUpperTemperatureLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 708 TEST_ASSERT_EQUAL_INT32(65, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 709
rob.meades@u-blox.com 1:ed57b6ca43ab 710 // The boost lower temperature is either -10 or -20
rob.meades@u-blox.com 1:ed57b6ca43ab 711 TEST_ASSERT(pBatteryCharger->setBoostLowerTemperatureLimit(1));
rob.meades@u-blox.com 1:ed57b6ca43ab 712 TEST_ASSERT(pBatteryCharger->getBoostLowerTemperatureLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 713 TEST_ASSERT_EQUAL_INT32(-10, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 714 TEST_ASSERT(pBatteryCharger->setBoostLowerTemperatureLimit(0));
rob.meades@u-blox.com 1:ed57b6ca43ab 715 TEST_ASSERT(pBatteryCharger->getBoostLowerTemperatureLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 716 TEST_ASSERT_EQUAL_INT32(-10, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 717 TEST_ASSERT(pBatteryCharger->setBoostLowerTemperatureLimit(-0));
rob.meades@u-blox.com 1:ed57b6ca43ab 718 TEST_ASSERT(pBatteryCharger->getBoostLowerTemperatureLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 719 TEST_ASSERT_EQUAL_INT32(-10, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 720 TEST_ASSERT(pBatteryCharger->setBoostLowerTemperatureLimit(-10));
rob.meades@u-blox.com 1:ed57b6ca43ab 721 TEST_ASSERT(pBatteryCharger->getBoostLowerTemperatureLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 722 TEST_ASSERT_EQUAL_INT32(-10, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 723 TEST_ASSERT(pBatteryCharger->setBoostLowerTemperatureLimit(-11));
rob.meades@u-blox.com 1:ed57b6ca43ab 724 TEST_ASSERT(pBatteryCharger->getBoostLowerTemperatureLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 725 TEST_ASSERT_EQUAL_INT32(-20, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 726 TEST_ASSERT(pBatteryCharger->setBoostLowerTemperatureLimit(-20));
rob.meades@u-blox.com 1:ed57b6ca43ab 727 TEST_ASSERT(pBatteryCharger->getBoostLowerTemperatureLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 728 TEST_ASSERT_EQUAL_INT32(-20, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 729 TEST_ASSERT(pBatteryCharger->setBoostLowerTemperatureLimit(-100));
rob.meades@u-blox.com 1:ed57b6ca43ab 730 TEST_ASSERT(pBatteryCharger->getBoostLowerTemperatureLimit(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 731 TEST_ASSERT_EQUAL_INT32(-20, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 732
rob.meades@u-blox.com 1:ed57b6ca43ab 733 // Parameter can be NULL
rob.meades@u-blox.com 1:ed57b6ca43ab 734 TEST_ASSERT(pBatteryCharger->getBoostVoltage(NULL));
rob.meades@u-blox.com 1:ed57b6ca43ab 735 TEST_ASSERT(pBatteryCharger->getBoostUpperTemperatureLimit(NULL));
rob.meades@u-blox.com 1:ed57b6ca43ab 736 TEST_ASSERT(pBatteryCharger->getBoostLowerTemperatureLimit(NULL));
rob.meades@u-blox.com 1:ed57b6ca43ab 737
rob.meades@u-blox.com 1:ed57b6ca43ab 738 // Put the initial values back when we're done
rob.meades@u-blox.com 1:ed57b6ca43ab 739 TEST_ASSERT(pBatteryCharger->setBoostVoltage(originalVoltage));
rob.meades@u-blox.com 1:ed57b6ca43ab 740 if (upperTemperatureEnabled) {
rob.meades@u-blox.com 1:ed57b6ca43ab 741 TEST_ASSERT(pBatteryCharger->setBoostUpperTemperatureLimit(originalUpperTemperature));
rob.meades@u-blox.com 1:ed57b6ca43ab 742 } else {
rob.meades@u-blox.com 1:ed57b6ca43ab 743 TEST_ASSERT(pBatteryCharger->disableBoostUpperTemperatureLimit());
rob.meades@u-blox.com 1:ed57b6ca43ab 744 }
rob.meades@u-blox.com 1:ed57b6ca43ab 745 TEST_ASSERT(pBatteryCharger->setBoostLowerTemperatureLimit(originalLowerTemperature));
rob.meades@u-blox.com 1:ed57b6ca43ab 746 }
rob.meades@u-blox.com 1:ed57b6ca43ab 747
rob.meades@u-blox.com 1:ed57b6ca43ab 748 // Test setting the chip's thermal regulation threshold
rob.meades@u-blox.com 1:ed57b6ca43ab 749 void test_chip_thermal_regulation_threshold() {
rob.meades@u-blox.com 1:ed57b6ca43ab 750 BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
rob.meades@u-blox.com 1:ed57b6ca43ab 751 int32_t originalTemperature;
rob.meades@u-blox.com 1:ed57b6ca43ab 752 int32_t getValue;
rob.meades@u-blox.com 1:ed57b6ca43ab 753
rob.meades@u-blox.com 1:ed57b6ca43ab 754 // Calls should return false if the battery charger has not been initialised
rob.meades@u-blox.com 1:ed57b6ca43ab 755 TEST_ASSERT_FALSE(pBatteryCharger->getChipThermalRegulationThreshold(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 756 TEST_ASSERT_FALSE(pBatteryCharger->setChipThermalRegulationThreshold(getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 757
rob.meades@u-blox.com 1:ed57b6ca43ab 758 // Initialise the battery charger
rob.meades@u-blox.com 1:ed57b6ca43ab 759 TEST_ASSERT(pBatteryCharger->init(gpI2C));
rob.meades@u-blox.com 1:ed57b6ca43ab 760
rob.meades@u-blox.com 1:ed57b6ca43ab 761 // Save the initial value
rob.meades@u-blox.com 1:ed57b6ca43ab 762 TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&originalTemperature));
rob.meades@u-blox.com 1:ed57b6ca43ab 763
rob.meades@u-blox.com 1:ed57b6ca43ab 764 // The thermal regulation threshold is one of 60C, 80C, 100C or 120C
rob.meades@u-blox.com 1:ed57b6ca43ab 765 TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(-1));
rob.meades@u-blox.com 1:ed57b6ca43ab 766 TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 767 TEST_ASSERT_EQUAL_INT32(60, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 768 TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(0));
rob.meades@u-blox.com 1:ed57b6ca43ab 769 TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 770 TEST_ASSERT_EQUAL_INT32(60, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 771 TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(79));
rob.meades@u-blox.com 1:ed57b6ca43ab 772 TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 773 TEST_ASSERT_EQUAL_INT32(60, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 774 TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(80));
rob.meades@u-blox.com 1:ed57b6ca43ab 775 TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 776 TEST_ASSERT_EQUAL_INT32(80, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 777 TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(99));
rob.meades@u-blox.com 1:ed57b6ca43ab 778 TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 779 TEST_ASSERT_EQUAL_INT32(80, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 780 TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(100));
rob.meades@u-blox.com 1:ed57b6ca43ab 781 TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 782 TEST_ASSERT_EQUAL_INT32(100, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 783 TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(101));
rob.meades@u-blox.com 1:ed57b6ca43ab 784 TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 785 TEST_ASSERT_EQUAL_INT32(100, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 786 TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(119));
rob.meades@u-blox.com 1:ed57b6ca43ab 787 TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 788 TEST_ASSERT_EQUAL_INT32(100, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 789 TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(120));
rob.meades@u-blox.com 1:ed57b6ca43ab 790 TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 791 TEST_ASSERT_EQUAL_INT32(120, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 792 TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(121));
rob.meades@u-blox.com 1:ed57b6ca43ab 793 TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 794 TEST_ASSERT_EQUAL_INT32(120, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 795 TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(200));
rob.meades@u-blox.com 1:ed57b6ca43ab 796 TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 797 TEST_ASSERT_EQUAL_INT32(120, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 798
rob.meades@u-blox.com 1:ed57b6ca43ab 799 // Parameter can be NULL
rob.meades@u-blox.com 1:ed57b6ca43ab 800 TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(NULL));
rob.meades@u-blox.com 1:ed57b6ca43ab 801
rob.meades@u-blox.com 1:ed57b6ca43ab 802 // Put the initial value back when we're done
rob.meades@u-blox.com 1:ed57b6ca43ab 803 TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(originalTemperature));
rob.meades@u-blox.com 1:ed57b6ca43ab 804 }
rob.meades@u-blox.com 1:ed57b6ca43ab 805
rob.meades@u-blox.com 3:340d65a1a133 806 // Test that we can kick and set the watchdog
rob.meades@u-blox.com 3:340d65a1a133 807 void test_watchdog() {
rob.meades@u-blox.com 3:340d65a1a133 808 BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
rob.meades@u-blox.com 3:340d65a1a133 809 int32_t initValue;
rob.meades@u-blox.com 3:340d65a1a133 810 int32_t getValue = 0;
rob.meades@u-blox.com 3:340d65a1a133 811
rob.meades@u-blox.com 3:340d65a1a133 812 // Call should fail if the battery charger has not been initialised
rob.meades@u-blox.com 3:340d65a1a133 813 TEST_ASSERT_FALSE(pBatteryCharger->feedWatchdog());
rob.meades@u-blox.com 3:340d65a1a133 814 TEST_ASSERT_FALSE(pBatteryCharger->getWatchdog(&getValue));
rob.meades@u-blox.com 3:340d65a1a133 815 TEST_ASSERT_FALSE(pBatteryCharger->setWatchdog(0));
rob.meades@u-blox.com 3:340d65a1a133 816
rob.meades@u-blox.com 3:340d65a1a133 817 // Initialise the battery charger
rob.meades@u-blox.com 3:340d65a1a133 818 TEST_ASSERT(pBatteryCharger->init(gpI2C));
rob.meades@u-blox.com 3:340d65a1a133 819
rob.meades@u-blox.com 3:340d65a1a133 820 // Save the initial value
rob.meades@u-blox.com 3:340d65a1a133 821 TEST_ASSERT(pBatteryCharger->getWatchdog(&initValue));
rob.meades@u-blox.com 3:340d65a1a133 822
rob.meades@u-blox.com 3:340d65a1a133 823 // Check that we can feed the watchdog
rob.meades@u-blox.com 3:340d65a1a133 824 TEST_ASSERT(pBatteryCharger->feedWatchdog());
rob.meades@u-blox.com 3:340d65a1a133 825
rob.meades@u-blox.com 3:340d65a1a133 826 // Check that we can set all the watchdog values
rob.meades@u-blox.com 3:340d65a1a133 827 TEST_ASSERT(pBatteryCharger->setWatchdog(81));
rob.meades@u-blox.com 3:340d65a1a133 828 TEST_ASSERT(pBatteryCharger->getWatchdog(&getValue));
rob.meades@u-blox.com 3:340d65a1a133 829 TEST_ASSERT_EQUAL_INT32(160, getValue);
rob.meades@u-blox.com 3:340d65a1a133 830 TEST_ASSERT(pBatteryCharger->setWatchdog(80));
rob.meades@u-blox.com 3:340d65a1a133 831 TEST_ASSERT(pBatteryCharger->getWatchdog(&getValue));
rob.meades@u-blox.com 3:340d65a1a133 832 TEST_ASSERT_EQUAL_INT32(80, getValue);
rob.meades@u-blox.com 3:340d65a1a133 833 TEST_ASSERT(pBatteryCharger->setWatchdog(41));
rob.meades@u-blox.com 3:340d65a1a133 834 TEST_ASSERT(pBatteryCharger->getWatchdog(&getValue));
rob.meades@u-blox.com 3:340d65a1a133 835 TEST_ASSERT_EQUAL_INT32(80, getValue);
rob.meades@u-blox.com 3:340d65a1a133 836 TEST_ASSERT(pBatteryCharger->setWatchdog(40));
rob.meades@u-blox.com 3:340d65a1a133 837 TEST_ASSERT(pBatteryCharger->getWatchdog(&getValue));
rob.meades@u-blox.com 3:340d65a1a133 838 TEST_ASSERT_EQUAL_INT32(40, getValue);
rob.meades@u-blox.com 3:340d65a1a133 839 TEST_ASSERT(pBatteryCharger->setWatchdog(1));
rob.meades@u-blox.com 3:340d65a1a133 840 TEST_ASSERT(pBatteryCharger->getWatchdog(&getValue));
rob.meades@u-blox.com 3:340d65a1a133 841 TEST_ASSERT_EQUAL_INT32(40, getValue);
rob.meades@u-blox.com 3:340d65a1a133 842 TEST_ASSERT(pBatteryCharger->setWatchdog(0));
rob.meades@u-blox.com 3:340d65a1a133 843 TEST_ASSERT(pBatteryCharger->getWatchdog(&getValue));
rob.meades@u-blox.com 3:340d65a1a133 844 TEST_ASSERT_EQUAL_INT32(0, getValue);
rob.meades@u-blox.com 3:340d65a1a133 845
rob.meades@u-blox.com 3:340d65a1a133 846 // Put the initial value back when we're done
rob.meades@u-blox.com 3:340d65a1a133 847 TEST_ASSERT (pBatteryCharger->setWatchdog(initValue));
rob.meades@u-blox.com 3:340d65a1a133 848 }
rob.meades@u-blox.com 3:340d65a1a133 849
rob.meades@u-blox.com 1:ed57b6ca43ab 850 // Test that we can enable and disable shipping mode
rob.meades@u-blox.com 1:ed57b6ca43ab 851 void test_shipping_mode() {
rob.meades@u-blox.com 1:ed57b6ca43ab 852 BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
rob.meades@u-blox.com 1:ed57b6ca43ab 853 bool shippingModeEnabled;
rob.meades@u-blox.com 1:ed57b6ca43ab 854
rob.meades@u-blox.com 1:ed57b6ca43ab 855 // Call should fail if the battery charger has not been initialised
rob.meades@u-blox.com 1:ed57b6ca43ab 856 TEST_ASSERT_FALSE(pBatteryCharger->enableShippingMode());
rob.meades@u-blox.com 1:ed57b6ca43ab 857 TEST_ASSERT_FALSE(pBatteryCharger->disableShippingMode());
rob.meades@u-blox.com 1:ed57b6ca43ab 858 TEST_ASSERT_FALSE(pBatteryCharger->isShippingModeEnabled());
rob.meades@u-blox.com 1:ed57b6ca43ab 859
rob.meades@u-blox.com 1:ed57b6ca43ab 860 // Initialise the battery charger
rob.meades@u-blox.com 1:ed57b6ca43ab 861 TEST_ASSERT(pBatteryCharger->init(gpI2C));
rob.meades@u-blox.com 1:ed57b6ca43ab 862
rob.meades@u-blox.com 1:ed57b6ca43ab 863 // Save the initial values
rob.meades@u-blox.com 1:ed57b6ca43ab 864 shippingModeEnabled = pBatteryCharger->isShippingModeEnabled();
rob.meades@u-blox.com 1:ed57b6ca43ab 865
rob.meades@u-blox.com 1:ed57b6ca43ab 866 // Enable and disable shipping mode
rob.meades@u-blox.com 1:ed57b6ca43ab 867 TEST_ASSERT(pBatteryCharger->enableShippingMode());
rob.meades@u-blox.com 1:ed57b6ca43ab 868 TEST_ASSERT(pBatteryCharger->isShippingModeEnabled());
rob.meades@u-blox.com 1:ed57b6ca43ab 869 TEST_ASSERT(pBatteryCharger->disableShippingMode());
rob.meades@u-blox.com 1:ed57b6ca43ab 870 TEST_ASSERT_FALSE(pBatteryCharger->isShippingModeEnabled());
rob.meades@u-blox.com 1:ed57b6ca43ab 871 TEST_ASSERT(pBatteryCharger->enableShippingMode());
rob.meades@u-blox.com 1:ed57b6ca43ab 872 TEST_ASSERT(pBatteryCharger->isShippingModeEnabled());
rob.meades@u-blox.com 1:ed57b6ca43ab 873
rob.meades@u-blox.com 1:ed57b6ca43ab 874 // Put the initial value back when we're done
rob.meades@u-blox.com 1:ed57b6ca43ab 875 if (shippingModeEnabled) {
rob.meades@u-blox.com 1:ed57b6ca43ab 876 pBatteryCharger->enableShippingMode();
rob.meades@u-blox.com 1:ed57b6ca43ab 877 } else {
rob.meades@u-blox.com 1:ed57b6ca43ab 878 pBatteryCharger->disableShippingMode();
rob.meades@u-blox.com 1:ed57b6ca43ab 879 }
rob.meades@u-blox.com 1:ed57b6ca43ab 880 }
rob.meades@u-blox.com 1:ed57b6ca43ab 881
rob.meades@u-blox.com 1:ed57b6ca43ab 882 // Test the advanced functions
rob.meades@u-blox.com 1:ed57b6ca43ab 883 void test_advanced() {
rob.meades@u-blox.com 1:ed57b6ca43ab 884 BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295();
rob.meades@u-blox.com 1:ed57b6ca43ab 885 char originalValue;
rob.meades@u-blox.com 1:ed57b6ca43ab 886 uint8_t address = 0x03; // REG03, the pre-charge/termination current control register
rob.meades@u-blox.com 1:ed57b6ca43ab 887 char getValue;
rob.meades@u-blox.com 1:ed57b6ca43ab 888 int32_t precharge;
rob.meades@u-blox.com 1:ed57b6ca43ab 889 int32_t termination;
rob.meades@u-blox.com 1:ed57b6ca43ab 890
rob.meades@u-blox.com 1:ed57b6ca43ab 891 // Calls should return false if the battery charger has not been initialised
rob.meades@u-blox.com 1:ed57b6ca43ab 892 TEST_ASSERT_FALSE(pBatteryCharger->advancedGet(address, &getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 893 TEST_ASSERT_FALSE(pBatteryCharger->advancedSet(address, getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 894
rob.meades@u-blox.com 1:ed57b6ca43ab 895 // Initialise the battery charger
rob.meades@u-blox.com 1:ed57b6ca43ab 896 TEST_ASSERT(pBatteryCharger->init(gpI2C));
rob.meades@u-blox.com 1:ed57b6ca43ab 897
rob.meades@u-blox.com 1:ed57b6ca43ab 898 // Save the initial value from address
rob.meades@u-blox.com 1:ed57b6ca43ab 899 TEST_ASSERT(pBatteryCharger->advancedGet(address, &originalValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 900
rob.meades@u-blox.com 1:ed57b6ca43ab 901 // REG03 contains the pre-charge current limit in the upper four bits
rob.meades@u-blox.com 1:ed57b6ca43ab 902 // and the termination current in the lower four bits, so we can read
rob.meades@u-blox.com 1:ed57b6ca43ab 903 // those and work out what the raw register value is.
rob.meades@u-blox.com 1:ed57b6ca43ab 904 TEST_ASSERT(pBatteryCharger->getPrechargingCurrentLimit(&precharge));
rob.meades@u-blox.com 1:ed57b6ca43ab 905 TEST_ASSERT(pBatteryCharger->getChargingTerminationCurrent(&termination));
rob.meades@u-blox.com 1:ed57b6ca43ab 906
rob.meades@u-blox.com 1:ed57b6ca43ab 907 precharge = (precharge - MIN_PRECHARGING_CURRENT_LIMIT_MA) / 128;
rob.meades@u-blox.com 1:ed57b6ca43ab 908 termination = (termination - MIN_CHARGING_TERMINATION_CURRENT_MA) / 128;
rob.meades@u-blox.com 1:ed57b6ca43ab 909 TEST_ASSERT_EQUAL_INT8(((precharge << 4) | (termination & 0x0f)), originalValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 910
rob.meades@u-blox.com 1:ed57b6ca43ab 911 // Now write something else to the register and check that it changes
rob.meades@u-blox.com 1:ed57b6ca43ab 912 TEST_ASSERT(pBatteryCharger->advancedSet(address, 0x01));
rob.meades@u-blox.com 1:ed57b6ca43ab 913 TEST_ASSERT(pBatteryCharger->advancedGet(address, &getValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 914 TEST_ASSERT_EQUAL_INT8(0x01, getValue);
rob.meades@u-blox.com 1:ed57b6ca43ab 915
rob.meades@u-blox.com 1:ed57b6ca43ab 916 // Parameter can be NULL
rob.meades@u-blox.com 1:ed57b6ca43ab 917 TEST_ASSERT(pBatteryCharger->advancedGet(address, NULL));
rob.meades@u-blox.com 1:ed57b6ca43ab 918
rob.meades@u-blox.com 1:ed57b6ca43ab 919 // Put the initial value back now that we're done
rob.meades@u-blox.com 1:ed57b6ca43ab 920 TEST_ASSERT(pBatteryCharger->advancedSet(address, originalValue));
rob.meades@u-blox.com 1:ed57b6ca43ab 921 }
rob.meades@u-blox.com 1:ed57b6ca43ab 922
rob.meades@u-blox.com 1:ed57b6ca43ab 923 // ----------------------------------------------------------------
rob.meades@u-blox.com 1:ed57b6ca43ab 924 // TEST ENVIRONMENT
rob.meades@u-blox.com 1:ed57b6ca43ab 925 // ----------------------------------------------------------------
rob.meades@u-blox.com 1:ed57b6ca43ab 926
rob.meades@u-blox.com 1:ed57b6ca43ab 927 // Setup the test environment
rob.meades@u-blox.com 1:ed57b6ca43ab 928 utest::v1::status_t test_setup(const size_t number_of_cases) {
rob.meades@u-blox.com 1:ed57b6ca43ab 929 // Setup Greentea, timeout is long enough to run these tests with
rob.meades@u-blox.com 1:ed57b6ca43ab 930 // DEBUG_BQ24295 defined
rob.meades@u-blox.com 1:ed57b6ca43ab 931 GREENTEA_SETUP(240, "default_auto");
rob.meades@u-blox.com 1:ed57b6ca43ab 932 return verbose_test_setup_handler(number_of_cases);
rob.meades@u-blox.com 1:ed57b6ca43ab 933 }
rob.meades@u-blox.com 1:ed57b6ca43ab 934
rob.meades@u-blox.com 1:ed57b6ca43ab 935 // Test cases
rob.meades@u-blox.com 1:ed57b6ca43ab 936 Case cases[] = {
rob.meades@u-blox.com 1:ed57b6ca43ab 937 Case("Initialisation", test_init),
rob.meades@u-blox.com 1:ed57b6ca43ab 938 Case("Charger state read", test_charger_state),
rob.meades@u-blox.com 1:ed57b6ca43ab 939 Case("External power presence", test_external_power_present),
rob.meades@u-blox.com 1:ed57b6ca43ab 940 Case("Charger fault", test_charger_fault),
rob.meades@u-blox.com 1:ed57b6ca43ab 941 Case("Input limits", test_input_limits),
rob.meades@u-blox.com 1:ed57b6ca43ab 942 Case("Charging enable", test_charging_enable),
rob.meades@u-blox.com 1:ed57b6ca43ab 943 Case("System voltage", test_system_voltage),
rob.meades@u-blox.com 1:ed57b6ca43ab 944 Case("Fast charging current limits", test_fast_charging_current_limits),
rob.meades@u-blox.com 1:ed57b6ca43ab 945 Case("Icgh/Iprech margin", test_icgh_iprech_margin),
rob.meades@u-blox.com 1:ed57b6ca43ab 946 Case("Pre-charging current limits", test_precharging_current_limits),
rob.meades@u-blox.com 1:ed57b6ca43ab 947 Case("Charging termination current", test_charging_termination_current),
rob.meades@u-blox.com 1:ed57b6ca43ab 948 Case("Charging voltage limits", test_charging_voltage_limits),
rob.meades@u-blox.com 1:ed57b6ca43ab 949 Case("Fast charging safety timer", test_fast_charging_safety_timer),
rob.meades@u-blox.com 1:ed57b6ca43ab 950 Case("Boost limits", test_boost_limits),
rob.meades@u-blox.com 1:ed57b6ca43ab 951 Case("Chip thermal regulation threshold", test_chip_thermal_regulation_threshold),
rob.meades@u-blox.com 3:340d65a1a133 952 Case("Watchdog", test_watchdog),
rob.meades@u-blox.com 1:ed57b6ca43ab 953 Case("Shipping mode", test_shipping_mode),
rob.meades@u-blox.com 1:ed57b6ca43ab 954 Case("Advanced", test_advanced)
rob.meades@u-blox.com 1:ed57b6ca43ab 955 };
rob.meades@u-blox.com 1:ed57b6ca43ab 956
rob.meades@u-blox.com 1:ed57b6ca43ab 957 Specification specification(test_setup, cases);
rob.meades@u-blox.com 1:ed57b6ca43ab 958
rob.meades@u-blox.com 1:ed57b6ca43ab 959 // ----------------------------------------------------------------
rob.meades@u-blox.com 1:ed57b6ca43ab 960 // MAIN
rob.meades@u-blox.com 1:ed57b6ca43ab 961 // ----------------------------------------------------------------
rob.meades@u-blox.com 1:ed57b6ca43ab 962
rob.meades@u-blox.com 1:ed57b6ca43ab 963 // Entry point into the tests
rob.meades@u-blox.com 1:ed57b6ca43ab 964 int main() {
rob.meades@u-blox.com 1:ed57b6ca43ab 965 bool success = false;
rob.meades@u-blox.com 1:ed57b6ca43ab 966
rob.meades@u-blox.com 1:ed57b6ca43ab 967 if (gpI2C != NULL) {
rob.meades@u-blox.com 1:ed57b6ca43ab 968 success = !Harness::run(specification);
rob.meades@u-blox.com 1:ed57b6ca43ab 969 } else {
rob.meades@u-blox.com 1:ed57b6ca43ab 970 printf ("Unable to instantiate I2C interface.\n");
rob.meades@u-blox.com 1:ed57b6ca43ab 971 }
rob.meades@u-blox.com 1:ed57b6ca43ab 972
rob.meades@u-blox.com 1:ed57b6ca43ab 973 return success;
rob.meades@u-blox.com 1:ed57b6ca43ab 974 }
rob.meades@u-blox.com 1:ed57b6ca43ab 975
rob.meades@u-blox.com 1:ed57b6ca43ab 976 // End Of File