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:
Mon Apr 10 11:04:15 2017 +0100
Revision:
1:ed57b6ca43ab
Child:
3:340d65a1a133
Add files to repo, removing README.md as that description is now in the repo description of the on-line IDE.

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