This class provides APIs to all of the registers of the TI BQ35100 battery gauge, as used on the u-blox C030 primary battery shield.
Dependents: example-battery-gauge-bq35100
Diff: TESTS/unit_tests/default/main.cpp
- Revision:
- 2:4c699a813451
- Parent:
- 1:ee7cc8d75283
diff -r ee7cc8d75283 -r 4c699a813451 TESTS/unit_tests/default/main.cpp --- a/TESTS/unit_tests/default/main.cpp Thu Nov 09 22:55:13 2017 +0000 +++ b/TESTS/unit_tests/default/main.cpp Fri Nov 10 17:07:06 2017 +0000 @@ -21,8 +21,9 @@ #define MAX_CURRENT_READING_MA 2000 #define MIN_CURRENT_READING_MA -2000 #define MIN_CAPACITY_READING_UAH 0 -#define MAX_CAPACITY_READING_UAH 30000000 -#define SET_DESIGN_CAPACITY_MAH 32177 +#define MAX_CAPACITY_READING_UAH 32177000 // Some randomly chosen +#define SET_DESIGN_CAPACITY_MAH 32177 // values that match + // ---------------------------------------------------------------- // PRIVATE VARIABLES @@ -199,6 +200,158 @@ TEST_ASSERT(pBatteryGauge->getRemainingPercentage(NULL)); } +// Test behaviours with gauging on +void test_gauging() { + BatteryGaugeBq35100 * pBatteryGauge = new BatteryGaugeBq35100(); + uint32_t capacityUAh = MIN_CAPACITY_READING_UAH - 1; + + // Call should fail if the battery gauge has not been initialised + TEST_ASSERT_FALSE(pBatteryGauge->enableGauge()); + TEST_ASSERT_FALSE(pBatteryGauge->disableGauge()); + TEST_ASSERT_FALSE(pBatteryGauge->isGaugeEnabled()); + + // Normal case, gauging should be off to begin with + TEST_ASSERT(pBatteryGauge->init(gpI2C, GAUGE_ENABLE_PIN)); + TEST_ASSERT_FALSE(pBatteryGauge->isGaugeEnabled()); + + // Enable gauge, without non-volatile storage + + TEST_ASSERT(pBatteryGauge->enableGauge()); + TEST_ASSERT(pBatteryGauge->getUsedCapacity(&capacityUAh)); + printf ("Used capacity %.3f mAh.\n", ((float) capacityUAh) / 1000); + // Range check + TEST_ASSERT((capacityUAh >= MIN_CAPACITY_READING_UAH) && (capacityUAh <= MAX_CAPACITY_READING_UAH)); + TEST_ASSERT(pBatteryGauge->isGaugeEnabled()); + TEST_ASSERT(pBatteryGauge->disableGauge()); + TEST_ASSERT_FALSE(pBatteryGauge->isGaugeEnabled()); + + // Enable gauge, with non-volatile storage + TEST_ASSERT(pBatteryGauge->enableGauge(true)); + TEST_ASSERT(pBatteryGauge->getUsedCapacity(&capacityUAh)); + printf ("Used capacity %.3f mAh.\n", ((float) capacityUAh) / 1000); + // Range check + // TODO: any way to check that the non-volatileness has worked? + TEST_ASSERT((capacityUAh >= MIN_CAPACITY_READING_UAH) && (capacityUAh <= MAX_CAPACITY_READING_UAH)); + TEST_ASSERT(pBatteryGauge->isGaugeEnabled()); + TEST_ASSERT(pBatteryGauge->disableGauge()); + TEST_ASSERT_FALSE(pBatteryGauge->isGaugeEnabled()); +} + +// Test the new battery call +void test_new_battery() { + BatteryGaugeBq35100 * pBatteryGauge = new BatteryGaugeBq35100(); + uint32_t originalDesignCapacity; + uint32_t newDesignCapacity = SET_DESIGN_CAPACITY_MAH; + uint32_t readDesignCapacity = 0; + + // Call should fail if the battery gauge has not been initialised + TEST_ASSERT_FALSE(pBatteryGauge->newBattery(1000)); + + // Normal case + TEST_ASSERT(pBatteryGauge->init(gpI2C, GAUGE_ENABLE_PIN)); + // Get the original design capacity so that we can set it back + // at the end + TEST_ASSERT(pBatteryGauge->getDesignCapacity(&originalDesignCapacity)); + printf ("Design capacity was originally %d mAh.\n", (unsigned int) originalDesignCapacity); + + // Avoid the old and new values being the same + if (originalDesignCapacity == newDesignCapacity) { + newDesignCapacity--; + } + + // Now add the new battery + TEST_ASSERT(pBatteryGauge->newBattery(newDesignCapacity)); + printf ("New battery added with design capacity %d mAh.\n", (unsigned int) newDesignCapacity); + + // Read the value back and check that it's been set + TEST_ASSERT(pBatteryGauge->getDesignCapacity(&readDesignCapacity)); + printf ("Design capacity was read as %d mAh.\n", (unsigned int) readDesignCapacity); + TEST_ASSERT(readDesignCapacity = newDesignCapacity) + + // Put the original value back + TEST_ASSERT(pBatteryGauge->setDesignCapacity(originalDesignCapacity)); + printf ("Design capacity returned to %d mAh.\n", (unsigned int) originalDesignCapacity); +} + +// Test get/set config +void test_advanced_get_set_config() { + BatteryGaugeBq35100 * pBatteryGauge = new BatteryGaugeBq35100(); + int32_t address = 0x4036; // Manufacturer Info Block A01, a 1 byte field + char originalValue; + char oldValue; + char newValue; + + // Calls should fail if the battery gauge has not been initialised + TEST_ASSERT_FALSE(pBatteryGauge->advancedGetConfig(address, &originalValue, sizeof(originalValue))); + TEST_ASSERT_FALSE(pBatteryGauge->advancedSetConfig(address, &newValue, sizeof(newValue))); + + // Initialise the battery gauge + TEST_ASSERT(pBatteryGauge->init(gpI2C, GAUGE_ENABLE_PIN)); + + // Normal case + TEST_ASSERT(pBatteryGauge->advancedGetConfig(address, &originalValue, sizeof(originalValue))); + // Invert the result and write it back + oldValue = originalValue; + printf ("Original value was 0x%02x.\n", oldValue); + newValue = ~oldValue; + printf ("Setting it to 0x%02x.\n", newValue); + TEST_ASSERT(pBatteryGauge->advancedSetConfig(address, &newValue, sizeof(newValue))); + // Read it and check that it has changed + TEST_ASSERT(pBatteryGauge->advancedGetConfig(address, &oldValue, sizeof(oldValue))); + printf ("Read back 0x%02x.\n", oldValue); + TEST_ASSERT_EQUAL_UINT8(newValue, oldValue); + + // Put the original value back again + TEST_ASSERT(pBatteryGauge->advancedSetConfig(address, &originalValue, sizeof(originalValue))); +} + +// Send a control word +void test_advanced_control() { + BatteryGaugeBq35100 * pBatteryGauge = new BatteryGaugeBq35100(); + uint16_t controlWord = 0x0003; // get HW version + uint16_t response = 0; + + // Call should fail if the battery gauge has not been initialised + TEST_ASSERT_FALSE(pBatteryGauge->advancedSendControlWord(controlWord, &response)); + + // Initialise the battery gauge + TEST_ASSERT(pBatteryGauge->init(gpI2C, GAUGE_ENABLE_PIN)); + + // Normal case + TEST_ASSERT(pBatteryGauge->advancedSendControlWord(controlWord, &response)); + // FW version must be 0x00a8 + TEST_ASSERT_EQUAL_UINT16(0x00a8, response); + + // The parameter is allowed to be null + TEST_ASSERT(pBatteryGauge->advancedSendControlWord(controlWord, NULL)); +} + +// Read using a standard command +void test_advanced_get() { + BatteryGaugeBq35100 * pBatteryGauge = new BatteryGaugeBq35100(); + uint8_t address = 0x06; // Temperature + uint16_t value = 0; + int32_t temperatureC = -1; + + // Call should fail if the battery gauge has not been initialised + TEST_ASSERT_FALSE(pBatteryGauge->advancedGet(address, &value)); + + // Initialise the battery gauge + TEST_ASSERT(pBatteryGauge->init(gpI2C, GAUGE_ENABLE_PIN)); + + // Normal case + TEST_ASSERT(pBatteryGauge->advancedGet(address, &value)); + // Get the temperature via the standard API command + TEST_ASSERT(pBatteryGauge->getTemperature(&temperatureC)); + // Convert the value returned into a temperature reading and compare + // it with the real answer, allowing a 1 degree tolerance in case + // it has changed between readings. + TEST_ASSERT_INT32_WITHIN (1, temperatureC, ((int32_t) value / 10) - 273); + + // The parameter is allowed to be null + TEST_ASSERT(pBatteryGauge->advancedGet(address, NULL)); +} + // Test that the security mode of the chip can be changed void test_advanced_security_mode() { BatteryGaugeBq35100 * pBatteryGauge = new BatteryGaugeBq35100(); @@ -263,7 +416,7 @@ TEST_ASSERT(pBatteryGauge->advancedSetSecurityMode(securityMode)); } -// Reset the BQ35100 battery gauge chip at the outset +// Reset the BQ35100 battery gauge chip void test_advanced_reset() { BatteryGaugeBq35100 * pBatteryGauge = new BatteryGaugeBq35100(); BatteryGaugeBq35100::SecurityMode securityMode; @@ -299,7 +452,7 @@ utest::v1::status_t test_setup(const size_t number_of_cases) { // Setup Greentea, timeout is long enough to run these tests with // DEBUG_BQ35100 defined - GREENTEA_SETUP(480, "default_auto"); + GREENTEA_SETUP(250, "default_auto"); return verbose_test_setup_handler(number_of_cases); } @@ -313,6 +466,11 @@ Case("Design capacity read/set", test_design_capacity), Case("Remaining capacity read", test_remaining_capacity), Case("Remaining precentage read", test_remaining_percentage), + Case("Gauging", test_gauging), + Case("New battery", test_new_battery), + Case("Advanced get/set config", test_advanced_get_set_config), + Case("Advanced control", test_advanced_control), + Case("Advanced get", test_advanced_get), Case("Advanced security mode", test_advanced_security_mode), Case("Advanced reset", test_advanced_reset) };