Cellular module AT Serial interface passthrough to Debug COM port. This program can be used on the C030 boards excluding the C030 N2xx version.
Revision 2:7764d92551ff, committed 2019-08-30
- Comitter:
- mudassar0121
- Date:
- Fri Aug 30 12:51:50 2019 +0500
- Parent:
- 1:f39e92fcdf5e
- Child:
- 3:924ed55e8397
- Commit message:
- Removed battery-charger-bq24295
Changed in this revision
--- a/battery-charger-bq24295/TESTS/unit_tests/default/main.cpp Fri Aug 30 11:26:10 2019 +0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,976 +0,0 @@ -#include "mbed.h" -#include "greentea-client/test_env.h" -#include "unity.h" -#include "utest.h" -#include "battery_charger_bq24295.h" - -using namespace utest::v1; - -// ---------------------------------------------------------------- -// COMPILE-TIME MACROS -// ---------------------------------------------------------------- - -// Minimum and maximum numbers -#define MAX_INPUT_VOLTAGE_LIMIT_MV 5080 -#define MIN_INPUT_VOLTAGE_LIMIT_MV 3880 -#define MAX_INPUT_CURRENT_LIMIT_MA 3000 -#define MIN_INPUT_CURRENT_LIMIT_MA 100 -#define MAX_SYSTEM_VOLTAGE_MV 3700 -#define MIN_SYSTEM_VOLTAGE_MV 3000 -#define MAX_FAST_CHARGING_CURRENT_LIMIT_MA 3008 -#define MIN_FAST_CHARGING_CURRENT_LIMIT_MA 512 -#define MAX_PRECHARGING_CURRENT_LIMIT_MA 2048 -#define MIN_PRECHARGING_CURRENT_LIMIT_MA 128 -#define MAX_CHARGING_TERMINATION_CURRENT_MA 2048 -#define MIN_CHARGING_TERMINATION_CURRENT_MA 128 -#define MAX_CHARGING_VOLTAGE_LIMIT_MV 4400 -#define MIN_CHARGING_VOLTAGE_LIMIT_MV 3504 -#define MIN_BOOST_VOLTAGE_MV 4550 -#define MAX_BOOST_VOLTAGE_MV 5510 - -#ifndef NUM_RAND_ITERATIONS -// The number of iterations of random input values in various tests -#define NUM_RAND_ITERATIONS 50 -#endif - -// ---------------------------------------------------------------- -// PRIVATE VARIABLES -// ---------------------------------------------------------------- - -// I2C interface -I2C * gpI2C = new I2C(I2C_SDA_B, I2C_SCL_B); - -// ---------------------------------------------------------------- -// TESTS -// ---------------------------------------------------------------- - -// Test that the BQ24295 battery charger can be initialised -void test_init() { - BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295(); - - TEST_ASSERT_FALSE(pBatteryCharger->init(NULL)); - TEST_ASSERT(pBatteryCharger->init(gpI2C)); -} - -// Test that we can read the charger state from the BQ24295 battery charger -void test_charger_state() { - BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295(); - BatteryChargerBq24295::ChargerState chargerState = BatteryChargerBq24295::CHARGER_STATE_UNKNOWN; - - // Call should fail if the battery charger has not been initialised - chargerState = pBatteryCharger->getChargerState(); - TEST_ASSERT(chargerState == BatteryChargerBq24295::CHARGER_STATE_UNKNOWN); - - // Normal case - TEST_ASSERT(pBatteryCharger->init(gpI2C)); - chargerState = pBatteryCharger->getChargerState(); - printf ("Charger state is %d.\n", chargerState); - // Range check - TEST_ASSERT(chargerState != BatteryChargerBq24295::CHARGER_STATE_UNKNOWN); - TEST_ASSERT(chargerState < BatteryChargerBq24295::MAX_NUM_CHARGER_STATES); -} - -// Test that we can read whether external power is present or not -// according to the BQ24295 battery charger -void test_external_power_present() { - BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295(); - - // Call should return false if the battery charger has not been initialised - TEST_ASSERT_FALSE(pBatteryCharger->isExternalPowerPresent()); - - // Normal case: must return true as the USB cable is plugged - // in when running these tests - TEST_ASSERT(pBatteryCharger->init(gpI2C)); - TEST_ASSERT(pBatteryCharger->isExternalPowerPresent()); -} - -// Test that we can read the charger fault from the BQ24295 battery charger -// NOTE: if this test fails, make sure you haven't got an actual fault. Better -// to reset the chip/board entirely before running these tests so that one -// doesn't occur. -void test_charger_fault() { - BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295(); - char bitmap = (char) BatteryChargerBq24295::CHARGER_FAULT_NONE; - - // Call should return no faults if the battery charger has not been initialised - bitmap = pBatteryCharger->getChargerFaults(); - TEST_ASSERT_EQUAL_INT8((char) BatteryChargerBq24295::CHARGER_FAULT_NONE, bitmap); - - // Normal case - TEST_ASSERT(pBatteryCharger->init(gpI2C)); - bitmap = pBatteryCharger->getChargerFaults(); - printf ("Charger fault is 0x%02x.\n", bitmap); - // Should be just the watchdog as we are in host mode and are not servicing the watchdog - // however other faults can also be present so just look for a non zero value. - // TODO: find a way to test other faults - TEST_ASSERT((unsigned char) bitmap > 0); -} - -// Test that we can read and change the input voltage and current limits -void test_input_limits() { - BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295(); - int32_t voltageOriginal; - int32_t currentOriginal; - bool enabledOriginal; - int32_t setValue; - int32_t getValue; - - // Calls should return false if the battery charger has not been initialised - TEST_ASSERT_FALSE(pBatteryCharger->getInputCurrentLimit(&getValue)); - TEST_ASSERT_FALSE(pBatteryCharger->setInputVoltageLimit(getValue)); - TEST_ASSERT_FALSE(pBatteryCharger->getInputCurrentLimit(&getValue)); - TEST_ASSERT_FALSE(pBatteryCharger->setInputVoltageLimit(getValue)); - TEST_ASSERT_FALSE(pBatteryCharger->enableInputLimits()); - TEST_ASSERT_FALSE(pBatteryCharger->disableInputLimits()); - - // Initialise the battery charger - TEST_ASSERT(pBatteryCharger->init(gpI2C)); - - // Save the initial values - TEST_ASSERT(pBatteryCharger->getInputVoltageLimit(&voltageOriginal)); - TEST_ASSERT(pBatteryCharger->getInputCurrentLimit(¤tOriginal)); - enabledOriginal = pBatteryCharger->areInputLimitsEnabled(); - - // Voltage and current beyond the limits - TEST_ASSERT_FALSE(pBatteryCharger->setInputVoltageLimit(MIN_INPUT_VOLTAGE_LIMIT_MV - 1)); - TEST_ASSERT_FALSE(pBatteryCharger->setInputVoltageLimit(MAX_INPUT_VOLTAGE_LIMIT_MV + 1)); - TEST_ASSERT_FALSE(pBatteryCharger->setInputCurrentLimit(MIN_INPUT_CURRENT_LIMIT_MA - 1)); - TEST_ASSERT_FALSE(pBatteryCharger->setInputCurrentLimit(MAX_INPUT_CURRENT_LIMIT_MA + 1)); - - // Voltage and current at the limits - TEST_ASSERT(pBatteryCharger->setInputVoltageLimit(MIN_INPUT_VOLTAGE_LIMIT_MV)); - TEST_ASSERT(pBatteryCharger->getInputVoltageLimit(&getValue)); - TEST_ASSERT_EQUAL_INT32(MIN_INPUT_VOLTAGE_LIMIT_MV, getValue); - TEST_ASSERT(pBatteryCharger->setInputVoltageLimit(MAX_INPUT_VOLTAGE_LIMIT_MV)); - TEST_ASSERT(pBatteryCharger->getInputVoltageLimit(&getValue)); - TEST_ASSERT_EQUAL_INT32(MAX_INPUT_VOLTAGE_LIMIT_MV, getValue); - TEST_ASSERT(pBatteryCharger->setInputCurrentLimit(MIN_INPUT_CURRENT_LIMIT_MA)); - TEST_ASSERT(pBatteryCharger->getInputCurrentLimit(&getValue)); - TEST_ASSERT_EQUAL_INT32(MIN_INPUT_CURRENT_LIMIT_MA, getValue); - TEST_ASSERT(pBatteryCharger->setInputCurrentLimit(MAX_INPUT_CURRENT_LIMIT_MA)); - TEST_ASSERT(pBatteryCharger->getInputCurrentLimit(&getValue)); - TEST_ASSERT_EQUAL_INT32(MAX_INPUT_CURRENT_LIMIT_MA, getValue); - - // The voltage limit read back should not be more than 80 mV below the value requested - for (uint32_t x = 0; x < NUM_RAND_ITERATIONS; x++) { - setValue = MIN_INPUT_VOLTAGE_LIMIT_MV + rand() % (MAX_INPUT_VOLTAGE_LIMIT_MV - MIN_INPUT_VOLTAGE_LIMIT_MV + 1); - TEST_ASSERT(pBatteryCharger->setInputVoltageLimit(setValue)); - getValue = -1; - TEST_ASSERT(pBatteryCharger->getInputVoltageLimit(&getValue)); - TEST_ASSERT((getValue > setValue - 80) && (getValue <= setValue)); - TEST_ASSERT((getValue >= MIN_INPUT_VOLTAGE_LIMIT_MV) && (getValue <= MAX_INPUT_VOLTAGE_LIMIT_MV)); - } - - // The current limit read back should always be less than or equal to the set value - for (uint32_t x = 0; x < NUM_RAND_ITERATIONS; x++) { - setValue = MIN_INPUT_CURRENT_LIMIT_MA + rand() % (MAX_INPUT_CURRENT_LIMIT_MA - MIN_INPUT_CURRENT_LIMIT_MA + 1); - TEST_ASSERT(pBatteryCharger->setInputCurrentLimit(setValue)); - getValue = -1; - TEST_ASSERT(pBatteryCharger->getInputCurrentLimit(&getValue)); - TEST_ASSERT(getValue <= setValue); - TEST_ASSERT((getValue >= MIN_INPUT_CURRENT_LIMIT_MA) && (getValue <= MAX_INPUT_CURRENT_LIMIT_MA)); - } - - // Enable and disable the limits - TEST_ASSERT(pBatteryCharger->enableInputLimits()); - TEST_ASSERT(pBatteryCharger->areInputLimitsEnabled()); - TEST_ASSERT(pBatteryCharger->disableInputLimits()); - TEST_ASSERT_FALSE(pBatteryCharger->areInputLimitsEnabled()); - TEST_ASSERT(pBatteryCharger->enableInputLimits()); - TEST_ASSERT(pBatteryCharger->areInputLimitsEnabled()); - - // Parameters can be NULL - TEST_ASSERT(pBatteryCharger->getInputVoltageLimit(NULL)); - TEST_ASSERT(pBatteryCharger->getInputCurrentLimit(NULL)); - - // Put the initial values back when we're done - TEST_ASSERT(pBatteryCharger->setInputVoltageLimit(voltageOriginal)); - TEST_ASSERT(pBatteryCharger->setInputCurrentLimit(currentOriginal)); - if (enabledOriginal) { - pBatteryCharger->enableInputLimits(); - } else { - pBatteryCharger->disableInputLimits(); - } -} - -// Test that we enable and disable OTG and normal charging -void test_charging_enable() { - BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295(); - bool otgEnabled; - bool chargingEnabled; - - // Call should fail if the battery charger has not been initialised - TEST_ASSERT_FALSE(pBatteryCharger->enableOtg()); - TEST_ASSERT_FALSE(pBatteryCharger->disableOtg()); - TEST_ASSERT_FALSE(pBatteryCharger->enableCharging()); - TEST_ASSERT_FALSE(pBatteryCharger->disableCharging()); - - // Initialise the battery charger - TEST_ASSERT(pBatteryCharger->init(gpI2C)); - - // Save the initial values - otgEnabled = pBatteryCharger->isOtgEnabled(); - chargingEnabled = pBatteryCharger->isChargingEnabled(); - - // Enable and disable OTG - TEST_ASSERT(pBatteryCharger->enableOtg()); - TEST_ASSERT(pBatteryCharger->isOtgEnabled()); - TEST_ASSERT(pBatteryCharger->disableOtg()); - TEST_ASSERT_FALSE(pBatteryCharger->isOtgEnabled()); - TEST_ASSERT(pBatteryCharger->enableOtg()); - TEST_ASSERT(pBatteryCharger->isOtgEnabled()); - - // Enable and disable charging - TEST_ASSERT(pBatteryCharger->enableCharging()); - TEST_ASSERT(pBatteryCharger->isChargingEnabled()); - TEST_ASSERT(pBatteryCharger->disableCharging()); - TEST_ASSERT_FALSE(pBatteryCharger->isChargingEnabled()); - TEST_ASSERT(pBatteryCharger->enableCharging()); - TEST_ASSERT(pBatteryCharger->isChargingEnabled()); - - // Put the initial values back when we're done - if (otgEnabled) { - pBatteryCharger->enableOtg(); - } else { - pBatteryCharger->disableOtg(); - } - if (chargingEnabled) { - pBatteryCharger->enableCharging(); - } else { - pBatteryCharger->disableCharging(); - } -} - -// Test that we can read and change the system voltage -void test_system_voltage() { - BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295(); - int32_t voltageOriginal; - int32_t setValue; - int32_t getValue; - - // Calls should return false if the battery charger has not been initialised - TEST_ASSERT_FALSE(pBatteryCharger->getSystemVoltage(&getValue)); - TEST_ASSERT_FALSE(pBatteryCharger->setSystemVoltage(getValue)); - - // Initialise the battery charger - TEST_ASSERT(pBatteryCharger->init(gpI2C)); - - // Save the initial value - TEST_ASSERT(pBatteryCharger->getSystemVoltage(&voltageOriginal)); - - // Beyond the limits - TEST_ASSERT_FALSE(pBatteryCharger->setSystemVoltage(MIN_SYSTEM_VOLTAGE_MV - 1)); - TEST_ASSERT_FALSE(pBatteryCharger->setSystemVoltage(MAX_SYSTEM_VOLTAGE_MV + 1)); - - // At the limits - TEST_ASSERT(pBatteryCharger->setSystemVoltage(MIN_SYSTEM_VOLTAGE_MV)); - TEST_ASSERT(pBatteryCharger->getSystemVoltage(&getValue)); - TEST_ASSERT_EQUAL_INT32(MIN_SYSTEM_VOLTAGE_MV, getValue); - TEST_ASSERT(pBatteryCharger->setSystemVoltage(MAX_SYSTEM_VOLTAGE_MV)); - TEST_ASSERT(pBatteryCharger->getSystemVoltage(&getValue)); - TEST_ASSERT_EQUAL_INT32(MAX_SYSTEM_VOLTAGE_MV, getValue); - - // The voltage read back should be at least the value requested and - // not more than 100 mV greater - for (uint32_t x = 0; x < NUM_RAND_ITERATIONS; x++) { - setValue = MIN_SYSTEM_VOLTAGE_MV + rand() % (MAX_SYSTEM_VOLTAGE_MV - MIN_SYSTEM_VOLTAGE_MV + 1); - TEST_ASSERT(pBatteryCharger->setSystemVoltage(setValue)); - getValue = -1; - TEST_ASSERT(pBatteryCharger->getSystemVoltage(&getValue)); - TEST_ASSERT((getValue < setValue + 100) && (getValue >= setValue)); - TEST_ASSERT((getValue >= MIN_SYSTEM_VOLTAGE_MV) && (getValue <= MAX_SYSTEM_VOLTAGE_MV)); - } - - // Parameter can be NULL - TEST_ASSERT(pBatteryCharger->getSystemVoltage(NULL)); - - // Put the initial value back when we're done - TEST_ASSERT(pBatteryCharger->setSystemVoltage(voltageOriginal)); -} - -// Test that we can read and change the fast charging current limits -void test_fast_charging_current_limits() { - BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295(); - int32_t currentOriginal; - int32_t setValue; - int32_t getValue; - - // Calls should return false if the battery charger has not been initialised - TEST_ASSERT_FALSE(pBatteryCharger->getFastChargingCurrentLimit(&getValue)); - TEST_ASSERT_FALSE(pBatteryCharger->setFastChargingCurrentLimit(getValue)); - - // Initialise the battery charger - TEST_ASSERT(pBatteryCharger->init(gpI2C)); - - // Save the initial value - TEST_ASSERT(pBatteryCharger->getFastChargingCurrentLimit(¤tOriginal)); - - // Beyond the limits - TEST_ASSERT_FALSE(pBatteryCharger->setFastChargingCurrentLimit(MIN_FAST_CHARGING_CURRENT_LIMIT_MA - 1)); - TEST_ASSERT_FALSE(pBatteryCharger->setFastChargingCurrentLimit(MAX_FAST_CHARGING_CURRENT_LIMIT_MA + 1)); - - // At the limits - TEST_ASSERT(pBatteryCharger->setFastChargingCurrentLimit(MIN_FAST_CHARGING_CURRENT_LIMIT_MA)); - TEST_ASSERT(pBatteryCharger->getFastChargingCurrentLimit(&getValue)); - TEST_ASSERT_EQUAL_INT32(MIN_FAST_CHARGING_CURRENT_LIMIT_MA, getValue); - TEST_ASSERT(pBatteryCharger->setFastChargingCurrentLimit(MAX_FAST_CHARGING_CURRENT_LIMIT_MA)); - TEST_ASSERT(pBatteryCharger->getFastChargingCurrentLimit(&getValue)); - TEST_ASSERT_EQUAL_INT32(MAX_FAST_CHARGING_CURRENT_LIMIT_MA, getValue); - - // The current limit read back should not be more than 64 mA below the value requested - for (uint32_t x = 0; x < NUM_RAND_ITERATIONS; x++) { - setValue = MIN_FAST_CHARGING_CURRENT_LIMIT_MA + rand() % (MAX_FAST_CHARGING_CURRENT_LIMIT_MA - MIN_FAST_CHARGING_CURRENT_LIMIT_MA + 1); - TEST_ASSERT(pBatteryCharger->setFastChargingCurrentLimit(setValue)); - getValue = -1; - TEST_ASSERT(pBatteryCharger->getFastChargingCurrentLimit(&getValue)); - TEST_ASSERT(getValue <= setValue); - TEST_ASSERT((getValue >= MIN_FAST_CHARGING_CURRENT_LIMIT_MA) && (getValue <= MAX_FAST_CHARGING_CURRENT_LIMIT_MA)); - } - - // Parameter can be NULL - TEST_ASSERT(pBatteryCharger->getFastChargingCurrentLimit(NULL)); - - // Put the initial value back when we're done - TEST_ASSERT(pBatteryCharger->setFastChargingCurrentLimit(currentOriginal)); -} - -// Test that we can enable and disable the ICGH/IPRECH margin -void test_icgh_iprech_margin() { - BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295(); - bool marginEnabled; - - // Call should fail if the battery charger has not been initialised - TEST_ASSERT_FALSE(pBatteryCharger->isIcghIprechMarginEnabled()); - TEST_ASSERT_FALSE(pBatteryCharger->enableIcghIprechMargin()); - TEST_ASSERT_FALSE(pBatteryCharger->disableIcghIprechMargin()); - - // Initialise the battery charger - TEST_ASSERT(pBatteryCharger->init(gpI2C)); - - // Save the initial value - marginEnabled = pBatteryCharger->isIcghIprechMarginEnabled(); - - // Enable and disable the margin - TEST_ASSERT(pBatteryCharger->enableIcghIprechMargin()); - TEST_ASSERT(pBatteryCharger->isIcghIprechMarginEnabled()); - TEST_ASSERT(pBatteryCharger->disableIcghIprechMargin()); - TEST_ASSERT_FALSE(pBatteryCharger->isIcghIprechMarginEnabled()); - TEST_ASSERT(pBatteryCharger->enableIcghIprechMargin()); - TEST_ASSERT(pBatteryCharger->isIcghIprechMarginEnabled()); - - // Put the initial value back when we're done - if (marginEnabled) { - pBatteryCharger->enableIcghIprechMargin(); - } else { - pBatteryCharger->disableIcghIprechMargin(); - } -} - -// Test that we can read and change the pre-charging current limits -void test_precharging_current_limits() { - BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295(); - int32_t currentOriginal; - int32_t setValue; - int32_t getValue; - - // Calls should return false if the battery charger has not been initialised - TEST_ASSERT_FALSE(pBatteryCharger->getPrechargingCurrentLimit(&getValue)); - TEST_ASSERT_FALSE(pBatteryCharger->setPrechargingCurrentLimit(getValue)); - - // Initialise the battery charger - TEST_ASSERT(pBatteryCharger->init(gpI2C)); - - // Save the initial value - TEST_ASSERT(pBatteryCharger->getPrechargingCurrentLimit(¤tOriginal)); - - // Beyond the limits - TEST_ASSERT_FALSE(pBatteryCharger->setPrechargingCurrentLimit(MIN_PRECHARGING_CURRENT_LIMIT_MA - 1)); - TEST_ASSERT_FALSE(pBatteryCharger->setPrechargingCurrentLimit(MAX_PRECHARGING_CURRENT_LIMIT_MA + 1)); - - // At the limits - TEST_ASSERT(pBatteryCharger->setPrechargingCurrentLimit(MIN_PRECHARGING_CURRENT_LIMIT_MA)); - TEST_ASSERT(pBatteryCharger->getPrechargingCurrentLimit(&getValue)); - TEST_ASSERT_EQUAL_INT32(MIN_PRECHARGING_CURRENT_LIMIT_MA, getValue); - TEST_ASSERT(pBatteryCharger->setPrechargingCurrentLimit(MAX_PRECHARGING_CURRENT_LIMIT_MA)); - TEST_ASSERT(pBatteryCharger->getPrechargingCurrentLimit(&getValue)); - TEST_ASSERT_EQUAL_INT32(MAX_PRECHARGING_CURRENT_LIMIT_MA, getValue); - - // The current limit read back should not be more than 128 mA below the value requested - for (uint32_t x = 0; x < NUM_RAND_ITERATIONS; x++) { - setValue = MIN_PRECHARGING_CURRENT_LIMIT_MA + rand() % (MAX_PRECHARGING_CURRENT_LIMIT_MA - MIN_PRECHARGING_CURRENT_LIMIT_MA + 1); - TEST_ASSERT(pBatteryCharger->setPrechargingCurrentLimit(setValue)); - getValue = -1; - TEST_ASSERT(pBatteryCharger->getPrechargingCurrentLimit(&getValue)); - TEST_ASSERT(getValue <= setValue); - TEST_ASSERT((getValue >= MIN_PRECHARGING_CURRENT_LIMIT_MA) && (getValue <= MAX_PRECHARGING_CURRENT_LIMIT_MA)); - } - - // Parameter can be NULL - TEST_ASSERT(pBatteryCharger->getPrechargingCurrentLimit(NULL)); - - // Put the initial value back when we're done - TEST_ASSERT(pBatteryCharger->setPrechargingCurrentLimit(currentOriginal)); -} - -// Test that we can read, change and enable/disable the charging termination current -void test_charging_termination_current() { - BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295(); - int32_t currentOriginal; - bool terminationEnabled; - int32_t setValue; - int32_t getValue; - - // Calls should return false if the battery charger has not been initialised - TEST_ASSERT_FALSE(pBatteryCharger->getChargingTerminationCurrent(&getValue)); - TEST_ASSERT_FALSE(pBatteryCharger->setChargingTerminationCurrent(getValue)); - TEST_ASSERT_FALSE(pBatteryCharger->enableChargingTermination()); - TEST_ASSERT_FALSE(pBatteryCharger->disableChargingTermination()); - - // Initialise the battery charger - TEST_ASSERT(pBatteryCharger->init(gpI2C)); - - // Save the initial values - TEST_ASSERT(pBatteryCharger->getChargingTerminationCurrent(¤tOriginal)); - terminationEnabled = pBatteryCharger->isChargingTerminationEnabled(); - - // Beyond the limits - TEST_ASSERT_FALSE(pBatteryCharger->setChargingTerminationCurrent(MIN_CHARGING_TERMINATION_CURRENT_MA - 1)); - TEST_ASSERT_FALSE(pBatteryCharger->setChargingTerminationCurrent(MAX_CHARGING_TERMINATION_CURRENT_MA + 1)); - - // At the limits - TEST_ASSERT(pBatteryCharger->setChargingTerminationCurrent(MIN_CHARGING_TERMINATION_CURRENT_MA)); - TEST_ASSERT(pBatteryCharger->getChargingTerminationCurrent(&getValue)); - TEST_ASSERT_EQUAL_INT32(MIN_CHARGING_TERMINATION_CURRENT_MA, getValue); - TEST_ASSERT(pBatteryCharger->setChargingTerminationCurrent(MAX_CHARGING_TERMINATION_CURRENT_MA)); - TEST_ASSERT(pBatteryCharger->getChargingTerminationCurrent(&getValue)); - TEST_ASSERT_EQUAL_INT32(MAX_CHARGING_TERMINATION_CURRENT_MA, getValue); - - // The current limit read back should not be more than 128 mA below the value requested - for (uint32_t x = 0; x < NUM_RAND_ITERATIONS; x++) { - setValue = MIN_CHARGING_TERMINATION_CURRENT_MA + rand() % (MAX_CHARGING_TERMINATION_CURRENT_MA - MIN_CHARGING_TERMINATION_CURRENT_MA + 1); - TEST_ASSERT(pBatteryCharger->setChargingTerminationCurrent(setValue)); - getValue = -1; - TEST_ASSERT(pBatteryCharger->getChargingTerminationCurrent(&getValue)); - TEST_ASSERT(getValue <= setValue); - TEST_ASSERT((getValue >= MIN_CHARGING_TERMINATION_CURRENT_MA) && (getValue <= MAX_CHARGING_TERMINATION_CURRENT_MA)); - } - - // Enable and disable the margin - TEST_ASSERT(pBatteryCharger->enableChargingTermination()); - TEST_ASSERT(pBatteryCharger->isChargingTerminationEnabled()); - TEST_ASSERT(pBatteryCharger->disableChargingTermination()); - TEST_ASSERT_FALSE(pBatteryCharger->isChargingTerminationEnabled()); - TEST_ASSERT(pBatteryCharger->enableChargingTermination()); - TEST_ASSERT(pBatteryCharger->isChargingTerminationEnabled()); - - // Parameter can be NULL - TEST_ASSERT(pBatteryCharger->getChargingTerminationCurrent(NULL)); - - // Put the initial value back when we're done - TEST_ASSERT(pBatteryCharger->setChargingTerminationCurrent(currentOriginal)); - if (terminationEnabled) { - pBatteryCharger->enableChargingTermination(); - } else { - pBatteryCharger->disableChargingTermination(); - } -} - -// Test that we can read and change the various charging voltage limits -void test_charging_voltage_limits() { - BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295(); - int32_t chargingOriginal; - int32_t fastChargingOriginal; - int32_t rechargingOriginal; - int32_t setValue; - int32_t getValue; - - // Calls should return false if the battery charger has not been initialised - TEST_ASSERT_FALSE(pBatteryCharger->getChargingVoltageLimit(&getValue)); - TEST_ASSERT_FALSE(pBatteryCharger->setChargingVoltageLimit(getValue)); - TEST_ASSERT_FALSE(pBatteryCharger->getFastChargingVoltageThreshold(&getValue)); - TEST_ASSERT_FALSE(pBatteryCharger->setFastChargingVoltageThreshold(getValue)); - TEST_ASSERT_FALSE(pBatteryCharger->getRechargingVoltageThreshold(&getValue)); - TEST_ASSERT_FALSE(pBatteryCharger->setRechargingVoltageThreshold(getValue)); - - // Initialise the battery charger - TEST_ASSERT(pBatteryCharger->init(gpI2C)); - - // Save the initial values - TEST_ASSERT(pBatteryCharger->getChargingVoltageLimit(&chargingOriginal)); - TEST_ASSERT(pBatteryCharger->getFastChargingVoltageThreshold(&fastChargingOriginal)); - TEST_ASSERT(pBatteryCharger->getRechargingVoltageThreshold(&rechargingOriginal)); - - // Beyond the limits for the charging voltage - TEST_ASSERT_FALSE(pBatteryCharger->setChargingVoltageLimit(MIN_CHARGING_VOLTAGE_LIMIT_MV - 1)); - TEST_ASSERT_FALSE(pBatteryCharger->setChargingVoltageLimit(MAX_CHARGING_VOLTAGE_LIMIT_MV + 1)); - - // At the limits for the charging voltage - TEST_ASSERT(pBatteryCharger->setChargingVoltageLimit(MIN_CHARGING_VOLTAGE_LIMIT_MV)); - TEST_ASSERT(pBatteryCharger->getChargingVoltageLimit(&getValue)); - TEST_ASSERT_EQUAL_INT32(MIN_CHARGING_VOLTAGE_LIMIT_MV, getValue); - TEST_ASSERT(pBatteryCharger->setChargingVoltageLimit(MAX_CHARGING_VOLTAGE_LIMIT_MV)); - TEST_ASSERT(pBatteryCharger->getChargingVoltageLimit(&getValue)); - TEST_ASSERT_EQUAL_INT32(MAX_CHARGING_VOLTAGE_LIMIT_MV, getValue); - - // The charging voltage limit read back should not be more than 16 mV below the value requested - for (uint32_t x = 0; x < NUM_RAND_ITERATIONS; x++) { - setValue = MIN_CHARGING_VOLTAGE_LIMIT_MV + rand() % (MAX_CHARGING_VOLTAGE_LIMIT_MV - MIN_CHARGING_VOLTAGE_LIMIT_MV + 1); - TEST_ASSERT(pBatteryCharger->setChargingVoltageLimit(setValue)); - getValue = -1; - TEST_ASSERT(pBatteryCharger->getChargingVoltageLimit(&getValue)); - TEST_ASSERT((getValue > setValue - 16) && (getValue <= setValue)); - TEST_ASSERT((getValue >= MIN_CHARGING_VOLTAGE_LIMIT_MV) && (getValue <= MAX_CHARGING_VOLTAGE_LIMIT_MV)); - } - - // Fast charging threshold is 2.8 V or 3.0 V - TEST_ASSERT(pBatteryCharger->setFastChargingVoltageThreshold(2799)); - TEST_ASSERT(pBatteryCharger->getFastChargingVoltageThreshold(&getValue)); - TEST_ASSERT_EQUAL_INT32(2800, getValue); - TEST_ASSERT(pBatteryCharger->setFastChargingVoltageThreshold(2800)); - TEST_ASSERT(pBatteryCharger->getFastChargingVoltageThreshold(&getValue)); - TEST_ASSERT_EQUAL_INT32(2800, getValue); - TEST_ASSERT(pBatteryCharger->setFastChargingVoltageThreshold(2801)); - TEST_ASSERT(pBatteryCharger->getFastChargingVoltageThreshold(&getValue)); - TEST_ASSERT_EQUAL_INT32(3000, getValue); - TEST_ASSERT(pBatteryCharger->setFastChargingVoltageThreshold(3000)); - TEST_ASSERT(pBatteryCharger->getFastChargingVoltageThreshold(&getValue)); - TEST_ASSERT_EQUAL_INT32(3000, getValue); - TEST_ASSERT(pBatteryCharger->setFastChargingVoltageThreshold(3001)); - TEST_ASSERT(pBatteryCharger->getFastChargingVoltageThreshold(&getValue)); - TEST_ASSERT_EQUAL_INT32(3000, getValue); - - // Recharging threshold is 100 mV or 300 mV - TEST_ASSERT(pBatteryCharger->setRechargingVoltageThreshold(99)); - TEST_ASSERT(pBatteryCharger->getRechargingVoltageThreshold(&getValue)); - TEST_ASSERT_EQUAL_INT32(100, getValue); - TEST_ASSERT(pBatteryCharger->setRechargingVoltageThreshold(100)); - TEST_ASSERT(pBatteryCharger->getRechargingVoltageThreshold(&getValue)); - TEST_ASSERT_EQUAL_INT32(100, getValue); - TEST_ASSERT(pBatteryCharger->setRechargingVoltageThreshold(101)); - TEST_ASSERT(pBatteryCharger->getRechargingVoltageThreshold(&getValue)); - TEST_ASSERT_EQUAL_INT32(300, getValue); - TEST_ASSERT(pBatteryCharger->setRechargingVoltageThreshold(300)); - TEST_ASSERT(pBatteryCharger->getRechargingVoltageThreshold(&getValue)); - TEST_ASSERT_EQUAL_INT32(300, getValue); - TEST_ASSERT(pBatteryCharger->setRechargingVoltageThreshold(301)); - TEST_ASSERT(pBatteryCharger->getRechargingVoltageThreshold(&getValue)); - TEST_ASSERT_EQUAL_INT32(300, getValue); - - // Parameters can be NULL - TEST_ASSERT(pBatteryCharger->getChargingVoltageLimit(NULL)); - TEST_ASSERT(pBatteryCharger->getFastChargingVoltageThreshold(NULL)); - TEST_ASSERT(pBatteryCharger->getRechargingVoltageThreshold(NULL)); - - // Put the initial values back when we're done - TEST_ASSERT(pBatteryCharger->setChargingVoltageLimit(chargingOriginal)); - TEST_ASSERT(pBatteryCharger->setFastChargingVoltageThreshold(fastChargingOriginal)); - TEST_ASSERT(pBatteryCharger->setRechargingVoltageThreshold(rechargingOriginal)); -} - -// Test that we can read and change the fast charging safety timer -void test_fast_charging_safety_timer() { - BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295(); - int32_t timerOriginal; - int32_t getValue; - - // Calls should return false if the battery charger has not been initialised - TEST_ASSERT_FALSE(pBatteryCharger->getFastChargingSafetyTimer(&getValue)); - TEST_ASSERT_FALSE(pBatteryCharger->setFastChargingSafetyTimer(getValue)); - - // Initialise the battery charger - TEST_ASSERT(pBatteryCharger->init(gpI2C)); - - // Save the initial value - TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&timerOriginal)); - - // Beyond the limit - TEST_ASSERT_FALSE(pBatteryCharger->setFastChargingSafetyTimer(-1)); - - // There are permissible values are 0, 5, 8, 12 and 20 so test them and the - // boundaries around them - TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(0)); - TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue)); - TEST_ASSERT_EQUAL_INT32(0, getValue); - TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(1)); - TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue)); - TEST_ASSERT_EQUAL_INT32(5, getValue); - TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(4)); - TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue)); - TEST_ASSERT_EQUAL_INT32(5, getValue); - TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(6)); - TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue)); - TEST_ASSERT_EQUAL_INT32(5, getValue); - TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(7)); - TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue)); - TEST_ASSERT_EQUAL_INT32(5, getValue); - TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(8)); - TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue)); - TEST_ASSERT_EQUAL_INT32(8, getValue); - TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(11)); - TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue)); - TEST_ASSERT_EQUAL_INT32(8, getValue); - TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(12)); - TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue)); - TEST_ASSERT_EQUAL_INT32(12, getValue); - TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(19)); - TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue)); - TEST_ASSERT_EQUAL_INT32(12, getValue); - TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(20)); - TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(&getValue)); - TEST_ASSERT_EQUAL_INT32(20, getValue); - - // Parameter can be NULL - TEST_ASSERT(pBatteryCharger->getFastChargingSafetyTimer(NULL)); - - // Put the initial value back when we're done - TEST_ASSERT(pBatteryCharger->setFastChargingSafetyTimer(timerOriginal)); -} - -// Test setting the boost mode voltage and temperature limits -void test_boost_limits() { - BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295(); - int32_t originalVoltage; - int32_t originalUpperTemperature; - bool upperTemperatureEnabled; - int32_t originalLowerTemperature; - int32_t setValue; - int32_t getValue; - - // Calls should return false if the battery charger has not been initialised - TEST_ASSERT_FALSE(pBatteryCharger->getBoostVoltage(&getValue)); - TEST_ASSERT_FALSE(pBatteryCharger->setBoostVoltage(getValue)); - TEST_ASSERT_FALSE(pBatteryCharger->getBoostUpperTemperatureLimit(&getValue)); - TEST_ASSERT_FALSE(pBatteryCharger->setBoostUpperTemperatureLimit(getValue)); - TEST_ASSERT_FALSE(pBatteryCharger->disableBoostUpperTemperatureLimit()); - TEST_ASSERT_FALSE(pBatteryCharger->getBoostLowerTemperatureLimit(&getValue)); - TEST_ASSERT_FALSE(pBatteryCharger->setBoostLowerTemperatureLimit(getValue)); - - // Initialise the battery charger - TEST_ASSERT(pBatteryCharger->init(gpI2C)); - - // Save the initial values - TEST_ASSERT(pBatteryCharger->getBoostVoltage(&originalVoltage)); - upperTemperatureEnabled = pBatteryCharger->isBoostUpperTemperatureLimitEnabled(); - if (upperTemperatureEnabled) { - TEST_ASSERT(pBatteryCharger->getBoostUpperTemperatureLimit(&originalUpperTemperature)); - } - TEST_ASSERT(pBatteryCharger->getBoostLowerTemperatureLimit(&originalLowerTemperature)); - - // Beyond the limits for the voltage - TEST_ASSERT_FALSE(pBatteryCharger->setBoostVoltage(MIN_BOOST_VOLTAGE_MV - 1)); - TEST_ASSERT_FALSE(pBatteryCharger->setBoostVoltage(MAX_BOOST_VOLTAGE_MV + 1)); - - // At the limits for the voltage - TEST_ASSERT(pBatteryCharger->setBoostVoltage(MIN_BOOST_VOLTAGE_MV)); - TEST_ASSERT(pBatteryCharger->getBoostVoltage(&getValue)); - TEST_ASSERT_EQUAL_INT32(MIN_BOOST_VOLTAGE_MV, getValue); - TEST_ASSERT(pBatteryCharger->setBoostVoltage(MAX_BOOST_VOLTAGE_MV)); - TEST_ASSERT(pBatteryCharger->getBoostVoltage(&getValue)); - TEST_ASSERT_EQUAL_INT32(MAX_BOOST_VOLTAGE_MV, getValue); - - // The voltage read back should be at least the value requested and - // not more than 64 mV greater - for (uint32_t x = 0; x < NUM_RAND_ITERATIONS; x++) { - setValue = MIN_BOOST_VOLTAGE_MV + rand() % (MAX_BOOST_VOLTAGE_MV - MIN_BOOST_VOLTAGE_MV + 1); - TEST_ASSERT(pBatteryCharger->setBoostVoltage(setValue)); - getValue = -1; - TEST_ASSERT(pBatteryCharger->getBoostVoltage(&getValue)); - TEST_ASSERT((getValue < setValue + 64) && (getValue >= setValue)); - TEST_ASSERT((getValue >= MIN_BOOST_VOLTAGE_MV) && (getValue <= MAX_BOOST_VOLTAGE_MV)); - } - - // Disable the upper temperature limit and check that the get function returns false - TEST_ASSERT(pBatteryCharger->disableBoostUpperTemperatureLimit()); - TEST_ASSERT_FALSE(pBatteryCharger->isBoostUpperTemperatureLimitEnabled()); - TEST_ASSERT_FALSE(pBatteryCharger->getBoostUpperTemperatureLimit(&getValue)); - - // The boost upper temperature limit is either 55, 60 or 65 - TEST_ASSERT(pBatteryCharger->setBoostUpperTemperatureLimit(-1)); - TEST_ASSERT(pBatteryCharger->getBoostUpperTemperatureLimit(&getValue)); - TEST_ASSERT_EQUAL_INT32(55, getValue); - TEST_ASSERT(pBatteryCharger->setBoostUpperTemperatureLimit(0)); - TEST_ASSERT(pBatteryCharger->getBoostUpperTemperatureLimit(&getValue)); - TEST_ASSERT_EQUAL_INT32(55, getValue); - TEST_ASSERT(pBatteryCharger->setBoostUpperTemperatureLimit(59)); - TEST_ASSERT(pBatteryCharger->getBoostUpperTemperatureLimit(&getValue)); - TEST_ASSERT_EQUAL_INT32(55, getValue); - TEST_ASSERT(pBatteryCharger->setBoostUpperTemperatureLimit(60)); - TEST_ASSERT(pBatteryCharger->getBoostUpperTemperatureLimit(&getValue)); - TEST_ASSERT_EQUAL_INT32(60, getValue); - TEST_ASSERT(pBatteryCharger->setBoostUpperTemperatureLimit(64)); - TEST_ASSERT(pBatteryCharger->getBoostUpperTemperatureLimit(&getValue)); - TEST_ASSERT_EQUAL_INT32(60, getValue); - TEST_ASSERT(pBatteryCharger->setBoostUpperTemperatureLimit(65)); - TEST_ASSERT(pBatteryCharger->getBoostUpperTemperatureLimit(&getValue)); - TEST_ASSERT_EQUAL_INT32(65, getValue); - TEST_ASSERT(pBatteryCharger->setBoostUpperTemperatureLimit(100)); - TEST_ASSERT(pBatteryCharger->getBoostUpperTemperatureLimit(&getValue)); - TEST_ASSERT_EQUAL_INT32(65, getValue); - - // The boost lower temperature is either -10 or -20 - TEST_ASSERT(pBatteryCharger->setBoostLowerTemperatureLimit(1)); - TEST_ASSERT(pBatteryCharger->getBoostLowerTemperatureLimit(&getValue)); - TEST_ASSERT_EQUAL_INT32(-10, getValue); - TEST_ASSERT(pBatteryCharger->setBoostLowerTemperatureLimit(0)); - TEST_ASSERT(pBatteryCharger->getBoostLowerTemperatureLimit(&getValue)); - TEST_ASSERT_EQUAL_INT32(-10, getValue); - TEST_ASSERT(pBatteryCharger->setBoostLowerTemperatureLimit(-0)); - TEST_ASSERT(pBatteryCharger->getBoostLowerTemperatureLimit(&getValue)); - TEST_ASSERT_EQUAL_INT32(-10, getValue); - TEST_ASSERT(pBatteryCharger->setBoostLowerTemperatureLimit(-10)); - TEST_ASSERT(pBatteryCharger->getBoostLowerTemperatureLimit(&getValue)); - TEST_ASSERT_EQUAL_INT32(-10, getValue); - TEST_ASSERT(pBatteryCharger->setBoostLowerTemperatureLimit(-11)); - TEST_ASSERT(pBatteryCharger->getBoostLowerTemperatureLimit(&getValue)); - TEST_ASSERT_EQUAL_INT32(-20, getValue); - TEST_ASSERT(pBatteryCharger->setBoostLowerTemperatureLimit(-20)); - TEST_ASSERT(pBatteryCharger->getBoostLowerTemperatureLimit(&getValue)); - TEST_ASSERT_EQUAL_INT32(-20, getValue); - TEST_ASSERT(pBatteryCharger->setBoostLowerTemperatureLimit(-100)); - TEST_ASSERT(pBatteryCharger->getBoostLowerTemperatureLimit(&getValue)); - TEST_ASSERT_EQUAL_INT32(-20, getValue); - - // Parameter can be NULL - TEST_ASSERT(pBatteryCharger->getBoostVoltage(NULL)); - TEST_ASSERT(pBatteryCharger->getBoostUpperTemperatureLimit(NULL)); - TEST_ASSERT(pBatteryCharger->getBoostLowerTemperatureLimit(NULL)); - - // Put the initial values back when we're done - TEST_ASSERT(pBatteryCharger->setBoostVoltage(originalVoltage)); - if (upperTemperatureEnabled) { - TEST_ASSERT(pBatteryCharger->setBoostUpperTemperatureLimit(originalUpperTemperature)); - } else { - TEST_ASSERT(pBatteryCharger->disableBoostUpperTemperatureLimit()); - } - TEST_ASSERT(pBatteryCharger->setBoostLowerTemperatureLimit(originalLowerTemperature)); -} - -// Test setting the chip's thermal regulation threshold -void test_chip_thermal_regulation_threshold() { - BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295(); - int32_t originalTemperature; - int32_t getValue; - - // Calls should return false if the battery charger has not been initialised - TEST_ASSERT_FALSE(pBatteryCharger->getChipThermalRegulationThreshold(&getValue)); - TEST_ASSERT_FALSE(pBatteryCharger->setChipThermalRegulationThreshold(getValue)); - - // Initialise the battery charger - TEST_ASSERT(pBatteryCharger->init(gpI2C)); - - // Save the initial value - TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&originalTemperature)); - - // The thermal regulation threshold is one of 60C, 80C, 100C or 120C - TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(-1)); - TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue)); - TEST_ASSERT_EQUAL_INT32(60, getValue); - TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(0)); - TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue)); - TEST_ASSERT_EQUAL_INT32(60, getValue); - TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(79)); - TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue)); - TEST_ASSERT_EQUAL_INT32(60, getValue); - TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(80)); - TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue)); - TEST_ASSERT_EQUAL_INT32(80, getValue); - TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(99)); - TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue)); - TEST_ASSERT_EQUAL_INT32(80, getValue); - TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(100)); - TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue)); - TEST_ASSERT_EQUAL_INT32(100, getValue); - TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(101)); - TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue)); - TEST_ASSERT_EQUAL_INT32(100, getValue); - TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(119)); - TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue)); - TEST_ASSERT_EQUAL_INT32(100, getValue); - TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(120)); - TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue)); - TEST_ASSERT_EQUAL_INT32(120, getValue); - TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(121)); - TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue)); - TEST_ASSERT_EQUAL_INT32(120, getValue); - TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(200)); - TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(&getValue)); - TEST_ASSERT_EQUAL_INT32(120, getValue); - - // Parameter can be NULL - TEST_ASSERT(pBatteryCharger->getChipThermalRegulationThreshold(NULL)); - - // Put the initial value back when we're done - TEST_ASSERT(pBatteryCharger->setChipThermalRegulationThreshold(originalTemperature)); -} - -// Test that we can kick and set the watchdog -void test_watchdog() { - BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295(); - int32_t initValue; - int32_t getValue = 0; - - // Call should fail if the battery charger has not been initialised - TEST_ASSERT_FALSE(pBatteryCharger->feedWatchdog()); - TEST_ASSERT_FALSE(pBatteryCharger->getWatchdog(&getValue)); - TEST_ASSERT_FALSE(pBatteryCharger->setWatchdog(0)); - - // Initialise the battery charger - TEST_ASSERT(pBatteryCharger->init(gpI2C)); - - // Save the initial value - TEST_ASSERT(pBatteryCharger->getWatchdog(&initValue)); - - // Check that we can feed the watchdog - TEST_ASSERT(pBatteryCharger->feedWatchdog()); - - // Check that we can set all the watchdog values - TEST_ASSERT(pBatteryCharger->setWatchdog(81)); - TEST_ASSERT(pBatteryCharger->getWatchdog(&getValue)); - TEST_ASSERT_EQUAL_INT32(160, getValue); - TEST_ASSERT(pBatteryCharger->setWatchdog(80)); - TEST_ASSERT(pBatteryCharger->getWatchdog(&getValue)); - TEST_ASSERT_EQUAL_INT32(80, getValue); - TEST_ASSERT(pBatteryCharger->setWatchdog(41)); - TEST_ASSERT(pBatteryCharger->getWatchdog(&getValue)); - TEST_ASSERT_EQUAL_INT32(80, getValue); - TEST_ASSERT(pBatteryCharger->setWatchdog(40)); - TEST_ASSERT(pBatteryCharger->getWatchdog(&getValue)); - TEST_ASSERT_EQUAL_INT32(40, getValue); - TEST_ASSERT(pBatteryCharger->setWatchdog(1)); - TEST_ASSERT(pBatteryCharger->getWatchdog(&getValue)); - TEST_ASSERT_EQUAL_INT32(40, getValue); - TEST_ASSERT(pBatteryCharger->setWatchdog(0)); - TEST_ASSERT(pBatteryCharger->getWatchdog(&getValue)); - TEST_ASSERT_EQUAL_INT32(0, getValue); - - // Put the initial value back when we're done - TEST_ASSERT (pBatteryCharger->setWatchdog(initValue)); -} - -// Test that we can enable and disable shipping mode -void test_shipping_mode() { - BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295(); - bool shippingModeEnabled; - - // Call should fail if the battery charger has not been initialised - TEST_ASSERT_FALSE(pBatteryCharger->enableShippingMode()); - TEST_ASSERT_FALSE(pBatteryCharger->disableShippingMode()); - TEST_ASSERT_FALSE(pBatteryCharger->isShippingModeEnabled()); - - // Initialise the battery charger - TEST_ASSERT(pBatteryCharger->init(gpI2C)); - - // Save the initial values - shippingModeEnabled = pBatteryCharger->isShippingModeEnabled(); - - // Enable and disable shipping mode - TEST_ASSERT(pBatteryCharger->enableShippingMode()); - TEST_ASSERT(pBatteryCharger->isShippingModeEnabled()); - TEST_ASSERT(pBatteryCharger->disableShippingMode()); - TEST_ASSERT_FALSE(pBatteryCharger->isShippingModeEnabled()); - TEST_ASSERT(pBatteryCharger->enableShippingMode()); - TEST_ASSERT(pBatteryCharger->isShippingModeEnabled()); - - // Put the initial value back when we're done - if (shippingModeEnabled) { - pBatteryCharger->enableShippingMode(); - } else { - pBatteryCharger->disableShippingMode(); - } -} - -// Test the advanced functions -void test_advanced() { - BatteryChargerBq24295 * pBatteryCharger = new BatteryChargerBq24295(); - char originalValue; - uint8_t address = 0x03; // REG03, the pre-charge/termination current control register - char getValue; - int32_t precharge; - int32_t termination; - - // Calls should return false if the battery charger has not been initialised - TEST_ASSERT_FALSE(pBatteryCharger->advancedGet(address, &getValue)); - TEST_ASSERT_FALSE(pBatteryCharger->advancedSet(address, getValue)); - - // Initialise the battery charger - TEST_ASSERT(pBatteryCharger->init(gpI2C)); - - // Save the initial value from address - TEST_ASSERT(pBatteryCharger->advancedGet(address, &originalValue)); - - // REG03 contains the pre-charge current limit in the upper four bits - // and the termination current in the lower four bits, so we can read - // those and work out what the raw register value is. - TEST_ASSERT(pBatteryCharger->getPrechargingCurrentLimit(&precharge)); - TEST_ASSERT(pBatteryCharger->getChargingTerminationCurrent(&termination)); - - precharge = (precharge - MIN_PRECHARGING_CURRENT_LIMIT_MA) / 128; - termination = (termination - MIN_CHARGING_TERMINATION_CURRENT_MA) / 128; - TEST_ASSERT_EQUAL_INT8(((precharge << 4) | (termination & 0x0f)), originalValue); - - // Now write something else to the register and check that it changes - TEST_ASSERT(pBatteryCharger->advancedSet(address, 0x01)); - TEST_ASSERT(pBatteryCharger->advancedGet(address, &getValue)); - TEST_ASSERT_EQUAL_INT8(0x01, getValue); - - // Parameter can be NULL - TEST_ASSERT(pBatteryCharger->advancedGet(address, NULL)); - - // Put the initial value back now that we're done - TEST_ASSERT(pBatteryCharger->advancedSet(address, originalValue)); -} - -// ---------------------------------------------------------------- -// TEST ENVIRONMENT -// ---------------------------------------------------------------- - -// Setup the test environment -utest::v1::status_t test_setup(const size_t number_of_cases) { - // Setup Greentea, timeout is long enough to run these tests with - // DEBUG_BQ24295 defined - GREENTEA_SETUP(240, "default_auto"); - return verbose_test_setup_handler(number_of_cases); -} - -// Test cases -Case cases[] = { - Case("Initialisation", test_init), - Case("Charger state read", test_charger_state), - Case("External power presence", test_external_power_present), - Case("Charger fault", test_charger_fault), - Case("Input limits", test_input_limits), - Case("Charging enable", test_charging_enable), - Case("System voltage", test_system_voltage), - Case("Fast charging current limits", test_fast_charging_current_limits), - Case("Icgh/Iprech margin", test_icgh_iprech_margin), - Case("Pre-charging current limits", test_precharging_current_limits), - Case("Charging termination current", test_charging_termination_current), - Case("Charging voltage limits", test_charging_voltage_limits), - Case("Fast charging safety timer", test_fast_charging_safety_timer), - Case("Boost limits", test_boost_limits), - Case("Chip thermal regulation threshold", test_chip_thermal_regulation_threshold), - Case("Watchdog", test_watchdog), - Case("Shipping mode", test_shipping_mode), - Case("Advanced", test_advanced) -}; - -Specification specification(test_setup, cases); - -// ---------------------------------------------------------------- -// MAIN -// ---------------------------------------------------------------- - -// Entry point into the tests -int main() { - bool success = false; - - if (gpI2C != NULL) { - success = !Harness::run(specification); - } else { - printf ("Unable to instantiate I2C interface.\n"); - } - - return success; -} - -// End Of File
--- a/battery-charger-bq24295/battery_charger_bq24295.h Fri Aug 30 11:26:10 2019 +0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,502 +0,0 @@ -/* mbed Microcontroller Library - * Copyright (c) 2017 u-blox - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef BATTERY_CHARGER_BQ24295_H -#define BATTERY_CHARGER_BQ24295_H - -/** - * @file battery_charger_bq24295.h - * This file defines the API to the TI BQ24295 battery charger chip. - */ - -/* ---------------------------------------------------------------- - * COMPILE-TIME MACROS - * -------------------------------------------------------------- */ - -/** Device I2C address. */ -#define BATTERY_CHARGER_BQ24295_ADDRESS 0x6B - -/* ---------------------------------------------------------------- - * CLASSES - * -------------------------------------------------------------- */ - -/** BQ27441 battery charger driver. */ -class BatteryChargerBq24295 { -public: - /** Charger state. */ - typedef enum { - CHARGER_STATE_UNKNOWN, - CHARGER_STATE_DISABLED, - CHARGER_STATE_NO_EXTERNAL_POWER, - CHARGER_STATE_NOT_CHARGING, - CHARGER_STATE_PRECHARGE, - CHARGER_STATE_FAST_CHARGE, - CHARGER_STATE_COMPLETE, - MAX_NUM_CHARGER_STATES - } ChargerState; - - /** Charger faults as a bitmap that matches the chip REG09 definitions. */ - typedef enum { - CHARGER_FAULT_NONE = 0x00, - CHARGER_FAULT_THERMISTOR_TOO_HOT = 0x01, - CHARGER_FAULT_THERMISTOR_TOO_COLD = 0x02, - // Value 0x04 is reserved - CHARGER_FAULT_BATTERY_OVER_VOLTAGE = 0x08, - CHARGER_FAULT_INPUT_FAULT = 0x10, //!< Note that the value of CHARGER_FAULT_CHARGE_TIMER_EXPIRED overlaps this, be careful when testing the bitmap. - CHARGER_FAULT_THERMAL_SHUTDOWN = 0x20, //!< Note that the value of CHARGER_FAULT_CHARGE_TIMER_EXPIRED overlaps this, be careful when testing the bitmap. - CHARGER_FAULT_CHARGE_TIMER_EXPIRED = 0x30, //!< This looks odd as it overlaps the two above but it matches the register meaning as defined by the chip. - CHARGER_FAULT_OTG = 0x40, - CHARGER_FAULT_WATCHDOG_EXPIRED = 0x80, - MAX_NUM_CHARGER_FAULTS - } ChargerFault; - - /** Constructor. */ - BatteryChargerBq24295(void); - /** Destructor. */ - ~BatteryChargerBq24295(void); - - /** Initialise the BQ24295 chip. - * After initialisation the chip will be put into its lowest - * power state and should be configured if the default settings - * are not satisfactory. - * Note: the BQ24295 charging chip will automonously charge a - * LiPo cell that is connected to it, without host interaction. - * This class is only required where the configuration of the - * chip needs to be changed or the charger state is to be monitored. - * @param pI2c a pointer to the I2C instance to use. - * @param address 7-bit I2C address of the battery charger chip. - * @return true if successful, otherwise false. - */ - bool init (I2C * pI2c, uint8_t address = BATTERY_CHARGER_BQ24295_ADDRESS); - - /** Get the charger state. - * Note: on a u-blox C030 board with no battery connected this will report - * CHARGER_STATE_COMPLETE or CHARGER_STATE_FAST_CHARGE rather than - * CHARGER_STATE_NOT_CHARGING. - * @return the charge state. - */ - ChargerState getChargerState(void); - - /** Get whether external power is present or not. - * @return true if external power is present, otherwise false. - */ - bool isExternalPowerPresent(void); - - /** Enable charging. - * Default is disabled. - * @return true if successful, otherwise false. - */ - bool enableCharging (void); - - /** Disable charging. - * Default is disabled. - * @return true if successful, otherwise false. - */ - bool disableCharging (void); - - /** Get the state of charging (enabled or disabled). - * @return true if charging is enabled, otherwise false. - */ - bool isChargingEnabled (void); - - /** Enable OTG charging. - * Default is enabled. - * @return true if successful, otherwise false. - */ - bool enableOtg (void); - - /** Disable OTG charging. - * Default is enabled. - * @return true if successful, otherwise false. - */ - bool disableOtg (void); - - /** Determine whether OTG charging is enabled or not. - * @return true if OTG charging is enabled, otherwise false. - */ - bool isOtgEnabled (void); - - /** Set the system voltage (the voltage which the - * chip will attempt to maintain the system at). - * @param voltageMV the voltage limit, in milliVolts. - * Range is 3000 mV to 3700 mV, default 3500 mV. - * @return true if successful, otherwise false. - */ - bool setSystemVoltage (int32_t voltageMV); - - /** Get the system voltage. - * @param pVoltageMV a place to put the system voltage limit. - * @return true if successful, otherwise false. - */ - bool getSystemVoltage (int32_t *pVoltageMV); - - /** Set the fast charging current limit. - * @param currentMA the fast charging current limit, in milliAmps. - * Range is 512 mA to 3008 mA, default 1024 mA. - * @return true if successful, otherwise false. - */ - bool setFastChargingCurrentLimit (int32_t currentMA); - - /** Get the fast charging current limit. - * @param pCurrentMA a place to put the fast charging current limit. - * @return true if successful, otherwise false. - */ - bool getFastChargingCurrentLimit (int32_t *pCurrentMA); - - /** Set the fast charging safety timer. - * @param timerHours the charging safety timer value. - * Use a value of 0 to indicate that the timer should be disabled. - * Timer values will be translated to the nearest (lower) value - * out of 5, 8, 12, and 20 hours, default 12 hours. - * @return true if successful, otherwise false. - */ - bool setFastChargingSafetyTimer (int32_t timerHours); - - /** Get the fast charging safety timer value. - * @param pTimerHours a place to put the charging safety timer value. - * Returned value is zero if the fast charging safety timer is disabled. - * @return true if charging termination is enabled, otherwise false. - */ - bool getFastChargingSafetyTimer (int32_t *pTimerHours); - - /** Set ICHG/IPRECH margin (see section 8.3.3.5 of the chip data sheet). - * Default is disabled. - * @return true if successful, otherwise false. - */ - bool enableIcghIprechMargin (void); - - /** Clear the ICHG/IPRECH margin (see section 8.3.3.5 of the chip data sheet). - * Default is disabled. - * @return true if successful, otherwise false. - */ - bool disableIcghIprechMargin (void); - - /** Check if the ICHG/IPRECH margin is set (see section 8.3.3.5 of - * the chip data sheet). - * @return true if the ICHG/IPRECH margin is enabled, otherwise false. - */ - bool isIcghIprechMarginEnabled (void); - - /** Set the charging termination current. - * @param currentMA the charging termination current, in milliAmps. - * Range is 128 mA to 2048 mA, default is 256 mA. - * @return true if successful, otherwise false. - */ - bool setChargingTerminationCurrent (int32_t currentMA); - - /** Get the charging termination current. - * @param pCurrentMA a place to put the charging termination current. - * @return true if successful, otherwise false. - */ - bool getChargingTerminationCurrent (int32_t *pCurrentMA); - - /** Enable charging termination. - * Default is enabled. - * @return true if successful, otherwise false. - */ - bool enableChargingTermination (void); - - /** Disable charging termination. - * Default is enabled. - * @return true if successful, otherwise false. - */ - bool disableChargingTermination (void); - - /** Get the state of charging termination (enabled or disabled). - * @return true if charging termination is enabled, otherwise false. - */ - bool isChargingTerminationEnabled (void); - - /** Set the pre-charging current limit. - * @param currentMA the pre-charging current limit, in milliAmps. - * Range is 128 mA to 2048 mA, default is 256 mA. - * @return true if successful, otherwise false. - */ - bool setPrechargingCurrentLimit (int32_t currentMA); - - /** Get the pre-charging current limit. - * @param pCurrentMA a place to put the pre-charging current limit. - * @return true if successful, otherwise false. - */ - bool getPrechargingCurrentLimit (int32_t *pCurrentMA); - - /** Set the charging voltage limit. - * @param voltageMV the charging voltage limit, in milliVolts. - * Range is 3504 mV to 4400 mV, default is 4208 mV. - * @return true if successful, otherwise false. - */ - bool setChargingVoltageLimit (int32_t voltageMV); - - /** Get the charging voltage limit. - * @param pVoltageMV a place to put the charging voltage limit, - * in milliVolts. - * @return true if successful, otherwise false. - */ - bool getChargingVoltageLimit (int32_t *pVoltageMV); - - /** Set the pre-charge to fast-charge voltage threshold. - * @param voltageMV the threshold, in milliVolts. - * Values will be translated to the nearest (highest) - * voltage out of 2800 mV and 3000 mV, default is 3000 mV. - * @return true if successful, otherwise false. - */ - bool setFastChargingVoltageThreshold (int32_t voltageMV); - - /** Get the pre-charge to fast-charge voltage threshold. - * @param pVoltageMV a place to put the threshold, in milliVolts. - * @return true if successful, otherwise false. - */ - bool getFastChargingVoltageThreshold (int32_t *pVoltageMV); - - /** Set the recharging voltage threshold. - * @param voltageMV the recharging voltage threshold, in milliVolts. - * Values will be translated to the nearest (highest) - * voltage out of 100 mV and 300 mV, default is 100 mV. - * @return true if successful, otherwise false. - */ - bool setRechargingVoltageThreshold (int32_t voltageMV); - - /** Get the recharging voltage threshold. - * @param pVoltageMV a place to put the charging voltage threshold, in milliVolts. - * @return true if successful, otherwise false. - */ - bool getRechargingVoltageThreshold (int32_t *pVoltageMV); - - /** Set the boost voltage. - * @param voltageMV the boost voltage, in milliVolts. - * Range is 4550 mV to 5510 mV, default is 5126 mV. - * @return true if successful, otherwise false. - */ - bool setBoostVoltage (int32_t voltageMV); - - /** Get the boost voltage. - * @param pVoltageMV a place to put the boost voltage, in milliVolts. - * @return true if successful, otherwise false. - */ - bool getBoostVoltage (int32_t *pVoltageMV); - - /** Set the boost mode upper temperature limit. - * @param temperatureC the temperature in C. - * Values will be translated to the nearest (lower) - * of 55 C, 60 C and 65 C (disabled by default). - * @return true if successful, otherwise false. - */ - bool setBoostUpperTemperatureLimit (int32_t temperatureC); - - /** Get the boost mode upper temperature limit. - * If the boost mode upper temperature limit is not - * enabled then pTemperatureC will remain untouched and false - * will be returned. - * @param pTemperatureC a place to put the temperature. - * @return true if successful and a limit was set, otherwise false. - */ - bool getBoostUpperTemperatureLimit (int32_t *pTemperatureC); - - /** Check whether the boost mode upper temperature limit is enabled. - * @return true if successful, otherwise false. - */ - bool isBoostUpperTemperatureLimitEnabled (void); - - /** Disable the boost mode upper temperature limit. - * Default is disabled. - * @return true if successful, otherwise false. - */ - bool disableBoostUpperTemperatureLimit (void); - - /** Set the boost mode low temperature limit. - * @param temperatureC the temperature in C. - * Values will be translated to the nearest (higher) - * of -10 C and -20 C, default is -10 C. - * @return true if successful, otherwise false. - */ - bool setBoostLowerTemperatureLimit (int32_t temperatureC); - - /** Get the boost mode low temperature limit. - * @param pTemperatureC a place to put the temperature. - * @return true if successful, otherwise false. - */ - bool getBoostLowerTemperatureLimit (int32_t *pTemperatureC); - - /** Set the input voltage limit. If the input falls below - * this level then charging will be ramped down. The limit - * does not take effect until enableInputLimits() is called - * (default setting is disabled). - * @param voltageMV the input voltage limit, in milliVolts. - * Range is 3880 mV to 5080 mV, default is 4760 mV. - * @return true if successful, otherwise false. - */ - bool setInputVoltageLimit (int32_t voltageMV); - - /** Get the input voltage limit. - * @param pVoltageMV a place to put the input voltage limit. - * @return true if successful, otherwise false. - */ - bool getInputVoltageLimit (int32_t *pVoltageMV); - - /** Set the input current limit. If the current drawn - * goes above this limit then charging will be ramped down. - * The limit does not take effect until enableInputLimits() - * is called (default setting is disabled). - * @param currentMA the input current limit, in milliAmps. - * Range is 100 mA to 3000 mA, default depends upon - * hardware configuration, see section 8.3.1.4.3 of - * the data sheet. - * @return true if successful, otherwise false. - */ - bool setInputCurrentLimit (int32_t currentMA); - - /** Get the input current limit. - * @param pCurrentMA a place to put the input current limit. - * @return true if successful, otherwise false. - */ - bool getInputCurrentLimit (int32_t *pCurrentMA); - - /** Enable input voltage and current limits. - * Default is disabled. - * @return true if successful, otherwise false. - */ - bool enableInputLimits (void); - - /** Remove any input voltage or current limits. - * Default is disabled. - * @return true if successful, otherwise false. - */ - bool disableInputLimits (void); - - /** Check whether input limits are enabled. - * @return true if input limits are enabled, otherwise false. - */ - bool areInputLimitsEnabled (void); - - /** Set the thermal regulation threshold for the chip. - * @param temperatureC the temperature in C. - * Values will be translated to the nearest (lower) - * of 60 C, 80 C, 100 C and 120 C, default 120 C. - * @return true if successful, otherwise false. - */ - bool setChipThermalRegulationThreshold (int32_t temperatureC); - - /** Get the thermal regulation threshold for the chip. - * @param pTemperatureC a place to put the temperature. - * @return true if successful, otherwise false. - */ - bool getChipThermalRegulationThreshold (int32_t *pTemperatureC); - - /** Get the charger faults. - * Note: as with all the other API functions here, this should - * not be called from an interrupt function as the comms with the - * chip over I2C will take too long. - * @return a bit-map of that can be tested against ChargerFault. - */ - char getChargerFaults(void); - - /** Feed the watchdog timer. - * Use this if it is necessary to keep the BQ24295 chip in Host - * mode (see section 8.4.1 of the data sheet for details). - * @return true if successful, otherwise false. - */ - bool feedWatchdog(void); - - /** Get the watchdog timer of the BQ24295 chip. - * @param pWatchdogS a place to put the watchdog timer (in seconds). - * @return true if successful, otherwise false. - */ - bool getWatchdog (int32_t *pWatchdogS); - - /** Set the watchdog timer of the BQ24295 chip. - * @param watchdogS the watchdog timer (in seconds), 0 to disable, - * max 160 seconds. - * @return true if successful, otherwise false. - */ - bool setWatchdog (int32_t watchdogS); - - /** Enable shipping mode. - * In shipping mode the battery is disconnected from the system - * to avoid leakage. Default is disabled. - * @return true if successful, otherwise false. - */ - bool enableShippingMode (void); - - /** Disable shipping mode. - * In shipping mode the battery is disconnected from the system - * to avoid leakage. Default is disabled. - * @return true if successful, otherwise false. - */ - bool disableShippingMode (void); - - /** Check whether shipping mode is enabled. - * @return true if input limits are enabled, otherwise false. - */ - bool isShippingModeEnabled (void); - - /** Advanced function to read a register on the chip. - * @param address the address to read from. - * @param pValue a place to put the returned value. - * @return true if successful, otherwise false. - */ - bool advancedGet(char address, char *pValue); - - /** Advanced function to set a register on the chip. - * @param address the address to write to. - * @param value the value to write. - * @return true if successful, otherwise false. - */ - bool advancedSet(char address, char value); - -protected: - /** Pointer to the I2C interface. */ - I2C * gpI2c; - /** The address of the device. */ - uint8_t gAddress; - /** Flag to indicate device is ready */ - bool gReady; - - /** Read a register. - * Note: gpI2c should be locked before this is called. - * @param address the address to read from. - * @param pValue a place to put the returned value. - * @return true if successful, otherwise false. - */ - bool getRegister(char address, char *pValue); - - /** Set a register. - * Note: gpI2c should be locked before this is called. - * @param address the address to write to. - * @param value the value to write. - * @return true if successful, otherwise false. - */ - bool setRegister(char address, char value); - - /** Set a mask of bits in a register. - * Note: gpI2c should be locked before this is called. - * @param address the address to write to. - * @param mask the mask of bits to set. - * @return true if successful, otherwise false. - */ - bool setRegisterBits(char address, char mask); - - /** Clear a mask of bits in a register. - * Note: gpI2c should be locked before this is called. - * @param address the address to write to. - * @param mask the mask of bits to clear. - * @return true if successful, otherwise false. - */ - bool clearRegisterBits(char address, char mask); -}; - -#endif - -/* End Of File */
--- a/battery-charger-bq24295/bq24295.cpp Fri Aug 30 11:26:10 2019 +0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1887 +0,0 @@ -/* mbed Microcontroller Library - * Copyright (c) 2017 u-blox - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * @file bq24295.cpp - * This file defines the API to the TI BQ24295 battery charging chip. - */ - -// Define this to print debug information -//#define DEBUG_BQ24295 - -#include <mbed.h> -#include <battery_charger_bq24295.h> - -#ifdef DEBUG_BQ24295 -# include <stdio.h> -#endif - -/* ---------------------------------------------------------------- - * COMPILE-TIME MACROS - * -------------------------------------------------------------- */ - -/* ---------------------------------------------------------------- - * PRIVATE VARIABLES - * -------------------------------------------------------------- */ - -/* ---------------------------------------------------------------- - * GENERIC PRIVATE FUNCTIONS - * -------------------------------------------------------------- */ - -// Read from a register. -bool BatteryChargerBq24295::getRegister(char address, char *pValue) -{ - bool success = false; - - if (gpI2c != NULL) { - // Move the address pointer - if (gpI2c->write(gAddress, &address, 1) == 0) { - if (pValue != NULL) { - // Read from the address - if (gpI2c->read(gAddress, pValue, 1) == 0) { - success = true; -#ifdef DEBUG_BQ24295 - printf("BatteryChargerBq24295 (I2C 0x%02x): read 0x%02x from register 0x%02x.\r\n", gAddress >> 1, *pValue, address); -#endif - } - } else { - success = true; - } - } - } - - return success; -} - -// Write to a register. -bool BatteryChargerBq24295::setRegister(char address, char value) -{ - bool success = false; - char data[2]; - - if (gpI2c != NULL) { - data[0] = address; - data[1] = value; - if (gpI2c->write(gAddress, &(data[0]), 2) == 0) { - success = true; -#ifdef DEBUG_BQ24295 - printf("BatteryChargerBq24295 (I2C 0x%02x): wrote 0x%02x to register 0x%02x.\r\n", gAddress >> 1, value, address); -#endif - } - } - - return success; -} - -// Set a mask of bits in a register. -bool BatteryChargerBq24295::setRegisterBits(char address, char mask) -{ - bool success = false; - char value; - - if (getRegister(address, &value)) { - value |= mask; - if (setRegister(address, value)) { - success = true; - } - } - - return success; -} - -// Clear a mask of bits in a register. -bool BatteryChargerBq24295::clearRegisterBits(char address, char mask) -{ - bool success = false; - char value; - - if (getRegister(address, &value)) { - value &= ~mask; - if (setRegister(address, value)) { - success = true; - } - } - - return success; -} - -/* ---------------------------------------------------------------- - * PUBLIC FUNCTIONS - * -------------------------------------------------------------- */ - -// Constructor. -BatteryChargerBq24295::BatteryChargerBq24295(void) -{ - gpI2c = NULL; - gReady = false; -} - -// Destructor. -BatteryChargerBq24295::~BatteryChargerBq24295(void) -{ -} - -// Initialise ourselves. -bool BatteryChargerBq24295::init (I2C * pI2c, uint8_t address) -{ - char reg; - - gpI2c = pI2c; - gAddress = address << 1; - - if (gpI2c != NULL) { - gpI2c->lock(); - - // Read the revision status register - if (getRegister(0x0a, ®)) { - // The expected response is 0xc0 - if (reg == 0xc0) { - gReady = true; - } - } - gpI2c->unlock(); - } - -#ifdef DEBUG_BQ24295 - if (gReady) { - printf("BatteryChargerBq24295 (I2C 0x%02x): handler initialised.\r\n", gAddress >> 1); - } else { - printf("BatteryChargerBq24295 (I2C 0x%02x): chip NOT initialised.\r\n", gAddress >> 1); - } -#endif - - return gReady; -} - -// Get the charge state. -BatteryChargerBq24295::ChargerState BatteryChargerBq24295::getChargerState(void) -{ - BatteryChargerBq24295::ChargerState chargerState = CHARGER_STATE_UNKNOWN; - char powerOnConfiguration; - char systemStatus; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the power-on configuration register - if (getRegister(0x01, &powerOnConfiguration)) { - // Read the system status register - if (getRegister(0x08, &systemStatus)) { - // Check the charge enable bit - if ((powerOnConfiguration & (1 << 4)) == 0) { - chargerState = CHARGER_STATE_DISABLED; -#ifdef DEBUG_BQ24295 - printf("BatteryChargerBq24295 (I2C 0x%02x): charging is disabled.\r\n", gAddress >> 1); -#endif - } else { - // BatteryCharger is not disabled, so see if we have - // external power (bit 3) - if ((systemStatus & 0x04) == 0) { - chargerState = CHARGER_STATE_NO_EXTERNAL_POWER; -#ifdef DEBUG_BQ24295 - printf("BatteryChargerBq24295 (I2C 0x%02x): no external power.\r\n", gAddress >> 1); -#endif - } else { - // Have power, so see how we're cooking (bits 4 & 5) - switch ((systemStatus >> 4) & 0x03) { - case 0: - chargerState = CHARGER_STATE_NOT_CHARGING; -#ifdef DEBUG_BQ24295 - printf("BatteryChargerBq24295 (I2C 0x%02x): not charging.\r\n", gAddress >> 1); -#endif - break; - case 1: - chargerState = CHARGER_STATE_PRECHARGE; -#ifdef DEBUG_BQ24295 - printf("BatteryChargerBq24295 (I2C 0x%02x): pre-charge.\r\n", gAddress >> 1); -#endif - break; - case 2: - chargerState = CHARGER_STATE_FAST_CHARGE; -#ifdef DEBUG_BQ24295 - printf("BatteryChargerBq24295 (I2C 0x%02x): fast charge.\r\n", gAddress >> 1); -#endif - break; - case 3: - chargerState = CHARGER_STATE_COMPLETE; -#ifdef DEBUG_BQ24295 - printf("BatteryChargerBq24295 (I2C 0x%02x): charging complete.\r\n", gAddress >> 1); -#endif - break; - default: - break; - } - } - } - } - } - gpI2c->unlock(); - } - - return chargerState; -} - -// Get whether external power is present or not. -bool BatteryChargerBq24295::isExternalPowerPresent(void) -{ - bool isPresent = false; - char reg; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the system status register - if (getRegister(0x08, ®)) { - // See if we have external power (bit 3) - if ((reg & 0x04) != 0) { - isPresent = true; - } - -#ifdef DEBUG_BQ24295 - if (isPresent) { - printf("BatteryChargerBq24295 (I2C 0x%02x): external power is present.\r\n", gAddress >> 1); - } else { - printf("BatteryChargerBq24295 (I2C 0x%02x): external power is NOT present.\r\n", gAddress >> 1); - } -#endif - } - gpI2c->unlock(); - } - - return isPresent; -} - -// Enable charging. -bool BatteryChargerBq24295::enableCharging (void) -{ - bool success = false; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Charge enable is bit 4 of the power-on configuration register - success = setRegisterBits(0x01, (1 << 4)); -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): charging now ENABLED.\r\n", gAddress >> 1); - } -#endif - gpI2c->unlock(); - } - - return success; -} - -// Disable charging. -bool BatteryChargerBq24295::disableCharging (void) -{ - bool success = false; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Charge enable is bit 4 of the power-on configuration register - success = clearRegisterBits(0x01, (1 << 4)); -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): charging now DISABLED.\r\n", gAddress >> 1); - } -#endif - gpI2c->unlock(); - } - - return success; -} - -// Get the whether charging is enabled or disabled. -bool BatteryChargerBq24295::isChargingEnabled (void) -{ - bool isEnabled = false; - char reg; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the power-on configuration register - if (getRegister(0x01, ®)) { - // Charge enable is bit 4 of the power-on configuration register - if ((reg & (1 << 4)) != 0) { - isEnabled = true; - } -#ifdef DEBUG_BQ24295 - if (isEnabled) { - printf("BatteryChargerBq24295 (I2C 0x%02x): charging is ENABLED.\r\n", gAddress >> 1); - } else { - printf("BatteryChargerBq24295 (I2C 0x%02x): charging is DISABLED.\r\n", gAddress >> 1); - } -#endif - } - gpI2c->unlock(); - } - - return isEnabled; -} - -// Enable OTG charging. -bool BatteryChargerBq24295::enableOtg (void) -{ - bool success = false; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // OTG enable is bit 5 of the power-on configuration register - success = setRegisterBits(0x01, (1 << 5)); -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): OTG charging now ENABLED.\r\n", gAddress >> 1); - } -#endif - gpI2c->unlock(); - } - - return success; -} - -// Disable OTG charging. -bool BatteryChargerBq24295::disableOtg (void) -{ - bool success = false; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // OTG enable is bit 5 of the power-on configuration register - success = clearRegisterBits(0x01, (1 << 5)); -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): OTG charging now DISABLED.\r\n", gAddress >> 1); - } -#endif - gpI2c->unlock(); - } - - return success; -} - -// Get whether OTG charging is enabled or not. -bool BatteryChargerBq24295::isOtgEnabled (void) -{ - bool isEnabled = false; - char reg; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the power-on configuration register - if (getRegister(0x01, ®)) { - // OTG enable is bit 5 of the power-on configuration register - if ((reg & (1 << 5)) != 0) { - isEnabled = true; - } -#ifdef DEBUG_BQ24295 - if (isEnabled) { - printf("BatteryChargerBq24295 (I2C 0x%02x): OTG charging is ENABLED.\r\n", gAddress >> 1); - } else { - printf("BatteryChargerBq24295 (I2C 0x%02x): OTG charging is DISABLED.\r\n", gAddress >> 1); - } -#endif - } - gpI2c->unlock(); - } - - return isEnabled; -} - -// Set the system voltage. -bool BatteryChargerBq24295::setSystemVoltage (int32_t voltageMV) -{ - bool success = false; - char reg; - int32_t codedValue; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the power-on configuration register - if (getRegister(0x01, ®)) { - // System voltage is in bits 1 to 3, - // coded to base "100 mV" with - // an offset of 3000 mV. - if ((voltageMV >= 3000) && (voltageMV <= 3700)) { - codedValue = voltageMV; - codedValue = (codedValue - 3000) / 100; - // If the voltage minus the base is not an exact multiple of 100, - // add one to the coded value to make sure we don't - // go under the requested system voltage - if ((voltageMV - 3000) % 100 != 0) { - codedValue++; - } - - reg &= ~(0x07 << 1); - reg |= (char) ((codedValue & 0x07) << 1); - - // Write to the power-on configuration register - success = setRegister (0x01, reg); -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): system voltage now set to %.3f V.\r\n", gAddress >> 1, (float) voltageMV / 1000); - } -#endif - } - } - gpI2c->unlock(); - } - - return success; -} - -// Get the system voltage. -bool BatteryChargerBq24295::getSystemVoltage (int32_t *pVoltageMV) -{ - bool success = false; - char reg; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the power-on configuration register - if (getRegister(0x01, ®)) { - success = true; - if (pVoltageMV != NULL) { - // Input voltage limit is in bits 1 to 3 - // Base voltage - *pVoltageMV = 3000; - // Shift reg down and add the number of multiples - // of 100 mV - reg = (reg >> 1) & 0x07; - *pVoltageMV += ((int32_t) reg) * 100; -#ifdef DEBUG_BQ24295 - printf("BatteryChargerBq24295 (I2C 0x%02x): system voltage is %.3f V.\r\n", gAddress >> 1, (float) *pVoltageMV / 1000); -#endif - } - } - gpI2c->unlock(); - } - - return success; -} - -// Set the fast charging current limit. -bool BatteryChargerBq24295::setFastChargingCurrentLimit (int32_t currentMA) -{ - bool success = false; - char reg; - int32_t codedValue; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the charge current control register - if (getRegister(0x02, ®)) { - // Fast charging current limit is in - // bits 2 to 7, coded to base "64 mA" with - // an offset of 512 mA. - if ((currentMA >= 512) && (currentMA <= 3008)) { - codedValue = currentMA; - codedValue = (codedValue - 512) / 64; - - reg &= ~(0x3f << 2); - reg |= (char) ((codedValue & 0x3f) << 2); - - // Write to the charge current control register - success = setRegister (0x02, reg); -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): fast charging current limit now set to %.3f A.\r\n", gAddress >> 1, (float) currentMA / 1000); - } -#endif - } - } - gpI2c->unlock(); - } - - return success; -} - -// Get the fast charging current limit. -bool BatteryChargerBq24295::getFastChargingCurrentLimit (int32_t *pCurrentMA) -{ - bool success = false; - char reg; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the charge current control register - if (getRegister(0x02, ®)) { - success = true; - if (pCurrentMA != NULL) { - // Fast charging current limit is in bits 2 to 7 - // Base current - *pCurrentMA = 512; - // Shift reg down and add the number of multiples - // of 64 mA - reg = (reg >> 2) & 0x3f; - *pCurrentMA += ((int32_t) reg) * 64; -#ifdef DEBUG_BQ24295 - printf("BatteryChargerBq24295 (I2C 0x%02x): fast charge current limit is %.3f A.\r\n", gAddress >> 1, (float) *pCurrentMA / 1000); -#endif - } - } - gpI2c->unlock(); - } - - return success; -} - -// Enable the ICHG/IPRECH margin. -bool BatteryChargerBq24295::enableIcghIprechMargin (void) -{ - bool success = false; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // FORCE_20PCT is bit 0 of the charge current control register - success = setRegisterBits(0x02, (1 << 0)); -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): ICGH/IPRECH margin now ENABLED.\r\n", gAddress >> 1); - } -#endif - gpI2c->unlock(); - } - - return success; -} - -// Disable the ICHG/IPRECH margin. -bool BatteryChargerBq24295::disableIcghIprechMargin (void) -{ - bool success = false; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // FORCE_20PCT is bit 0 of the charge current control register - success = clearRegisterBits(0x02, (1 << 0)); -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): ICGH/IPRECH margin now DISABLED.\r\n", gAddress >> 1); - } -#endif - gpI2c->unlock(); - } - - return success; -} - -// Check if the ICHG/IPRECH margin is set. -bool BatteryChargerBq24295::isIcghIprechMarginEnabled (void) -{ - bool isEnabled = false; - char reg; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the charge current control register - if (getRegister(0x02, ®)) { - // FORCE_20PCT is bit 0 - if ((reg & (1 << 0)) != 0) { - isEnabled = true; - } -#ifdef DEBUG_BQ24295 - if (isEnabled) { - printf("BatteryChargerBq24295 (I2C 0x%02x): ICGH/IPRECH margin is ENABLED.\r\n", gAddress >> 1); - } else { - printf("BatteryChargerBq24295 (I2C 0x%02x): ICGH/IPRECH margin is DISABLED.\r\n", gAddress >> 1); - } -#endif - } - gpI2c->unlock(); - } - - return isEnabled; -} - -// Set the fast charging safety timer. -bool BatteryChargerBq24295::setFastChargingSafetyTimer (int32_t timerHours) -{ - bool success = false; - char reg; - int32_t codedValue; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Mustn't be silly - if (timerHours >= 0) { - // Read the charge termination/timer control register - if (getRegister(0x05, ®)) { - // Timer setting is in bits 1 & 2, enable is in bit 3 - if (timerHours == 0) { - reg &= ~(1 << 3); - } else { - reg |= (1 << 3); - if (timerHours < 8) { - codedValue = 0; - } else if (timerHours < 12) { - codedValue = 1; - } else if (timerHours < 20) { - codedValue = 2; - } else { - codedValue = 3; - } - reg &= ~(0x03 << 1); - reg |= (char) (codedValue << 1); - } - // Write to the charge termination/timer control register - success = setRegister (0x05, reg); -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): fast charging safety timer now set to %d hours.\r\n", gAddress >> 1, (int) timerHours); - } -#endif - } - } - gpI2c->unlock(); - } - - return success; -} - -// Get the value of the fast charging safety timer. -bool BatteryChargerBq24295::getFastChargingSafetyTimer (int32_t *pTimerHours) -{ - bool success = false; - char reg; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the charge termination/timer control register - if (getRegister(0x05, ®)) { - success = true; - if (pTimerHours != NULL) { - *pTimerHours = 0; - // Timer enable is in bit 3 - if ((reg & (1 << 3)) != 0) { - // Timer value is in bits 1 & 2 - switch ((reg >> 1) & 0x03) { - case 0: - *pTimerHours = 5; - break; - case 1: - *pTimerHours = 8; - break; - case 2: - *pTimerHours = 12; - break; - case 3: - *pTimerHours = 20; - break; - default: - MBED_ASSERT(false); - break; - } - } -#ifdef DEBUG_BQ24295 - printf("BatteryChargerBq24295 (I2C 0x%02x): fast charging safety timer is %d hours.\r\n", gAddress >> 1, (int) *pTimerHours); -#endif - } - } - gpI2c->unlock(); - } - - return success; -} - -// Set the charging termination current. -bool BatteryChargerBq24295::setChargingTerminationCurrent (int32_t currentMA) -{ - bool success = false; - char reg; - int32_t codedValue; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the pre-charge/termination current control register - if (getRegister(0x03, ®)) { - // Charging termination current limit is in - // bits 0 to 3, coded to base "128 mA" with - // an offset of 128 mA. - if ((currentMA >= 128) && (currentMA <= 2048)) { - codedValue = currentMA; - codedValue = (codedValue - 128) / 128; - - reg &= ~0x0f; - reg |= (char) (codedValue & 0x0f); - - // Write to the pre-charge/termination current control register - success = setRegister (0x03, reg); -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): charging termination current now set to %.3f A.\r\n", gAddress >> 1, (float) currentMA / 1000); - } -#endif - } - } - gpI2c->unlock(); - } - - return success; -} - -// Get the charging termination current. -bool BatteryChargerBq24295::getChargingTerminationCurrent (int32_t *pCurrentMA) -{ - bool success = false; - char reg; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the pre-charge/termination current control register - if (getRegister(0x03, ®)) { - success = true; - if (pCurrentMA != NULL) { - // Pre-charging current limit is in bits 0 to 3 - // Base current - *pCurrentMA = 128; - // Add the number of multiples of 128 mA - reg = reg & 0x0f; - *pCurrentMA += ((int32_t) reg) * 128; -#ifdef DEBUG_BQ24295 - printf("BatteryChargerBq24295 (I2C 0x%02x): charging termination current is %.3f A.\r\n", gAddress >> 1, (float) *pCurrentMA / 1000); -#endif - } - } - gpI2c->unlock(); - } - - return success; -} - -// Enable charging termination. -bool BatteryChargerBq24295::enableChargingTermination (void) -{ - bool success = false; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // EN_TERM is bit 7 of the charge termination/timer control register - success = setRegisterBits(0x05, (1 << 7)); -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): charging termination now ENABLED.\r\n", gAddress >> 1); - } -#endif - gpI2c->unlock(); - } - - return success; -} - -// Disable charging termination. -bool BatteryChargerBq24295::disableChargingTermination (void) -{ - bool success = false; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // EN_TERM is bit 7 of the charge termination/timer control register - success = clearRegisterBits(0x05, (1 << 7)); -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): charging termination now DISABLED.\r\n", gAddress >> 1); - } -#endif - gpI2c->unlock(); - } - - return success; -} - -// Get the state of charging termination (enabled or disabled) -bool BatteryChargerBq24295::isChargingTerminationEnabled (void) -{ - bool isEnabled = false; - char reg; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the charge termination/timer control register - if (getRegister(0x05, ®)) { - // EN_TERM is bit 7 - if ((reg & (1 << 7)) != 0) { - isEnabled = true; - } -#ifdef DEBUG_BQ24295 - if (isEnabled) { - printf("BatteryChargerBq24295 (I2C 0x%02x): charging termination is ENABLED.\r\n", gAddress >> 1); - } else { - printf("BatteryChargerBq24295 (I2C 0x%02x): charging termination is DISABLED.\r\n", gAddress >> 1); - } -#endif - } - gpI2c->unlock(); - } - - return isEnabled; -} - -// Set the pre-charging current limit. -bool BatteryChargerBq24295::setPrechargingCurrentLimit (int32_t currentMA) -{ - bool success = false; - char reg; - int32_t codedValue; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the pre-charge/termination current control register - if (getRegister(0x03, ®)) { - // Pre-charging current limit is in - // bits 4 to 7, coded to base "128 mA" with - // an offset of 128 mA. - if ((currentMA >= 128) && (currentMA <= 2048)) { - codedValue = currentMA; - codedValue = (codedValue - 128) / 128; - - reg &= ~(0x0f << 4); - reg |= (char) ((codedValue & 0x0f) << 4); - - // Write to the pre-charge/termination current control register - success = setRegister (0x03, reg); -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): pre-charging current limit now set to %.3f A.\r\n", gAddress >> 1, (float) currentMA / 1000); - } -#endif - } - } - gpI2c->unlock(); - } - - return success; -} - -// Get the pre-charging current limit. -bool BatteryChargerBq24295::getPrechargingCurrentLimit (int32_t *pCurrentMA) -{ - bool success = false; - char reg; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the pre-charge/termination current control register - if (getRegister(0x03, ®)) { - success = true; - if (pCurrentMA != NULL) { - // Pre-charging current limit is in bits 4 to 7 - // Base current - *pCurrentMA = 128; - // Shift reg down and add the number of multiples - // of 128 mA - reg = (reg >> 4) & 0x0f; - *pCurrentMA += ((int32_t) reg) * 128; -#ifdef DEBUG_BQ24295 - printf("BatteryChargerBq24295 (I2C 0x%02x): pre-charging current limit is %.3f A.\r\n", gAddress >> 1, (float) *pCurrentMA / 1000); -#endif - } - } - gpI2c->unlock(); - } - - return success; -} - -// Set the charging voltage limit. -bool BatteryChargerBq24295::setChargingVoltageLimit (int32_t voltageMV) -{ - bool success = false; - char reg; - int32_t codedLimit; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the charging voltage control register - if (getRegister(0x04, ®)) { - // Charging voltage limit is in bits 2 to 7 - // but it is coded to base "16 mV" with - // an offset of 3504 mV. - if ((voltageMV >= 3504) && (voltageMV <= 4400)) { - codedLimit = voltageMV; - codedLimit = (codedLimit - 3504) / 16; - - reg &= ~(0x3f << 2); - reg |= (char) ((codedLimit & 0x3f) << 2); - - // Write to the charging voltage control register - success = setRegister (0x04, reg); -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): charging voltage limit now set to %.3f V.\r\n", gAddress >> 1, (float) voltageMV / 1000); - } -#endif - } - } - gpI2c->unlock(); - } - - return success; -} - -// Get the charging voltage limit. -bool BatteryChargerBq24295::getChargingVoltageLimit (int32_t *pVoltageMV) -{ - bool success = false; - char reg; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the charging voltage control register - if (getRegister(0x04, ®)) { - success = true; - if (pVoltageMV != NULL) { - // Charging voltage limit is in bits 2 to 7 - // Base voltage - *pVoltageMV = 3504; - // Shift reg down and add the number of multiples - // of 16 mV - reg = (reg >> 2) & 0x3f; - *pVoltageMV += ((int32_t) reg) * 16; -#ifdef DEBUG_BQ24295 - printf("BatteryChargerBq24295 (I2C 0x%02x): charging voltage limit is %.3f V.\r\n", gAddress >> 1, (float) *pVoltageMV / 1000); -#endif - } - } - gpI2c->unlock(); - } - - return success; -} - -// Set the pre-charge to fast-charge voltage threshold. -bool BatteryChargerBq24295::setFastChargingVoltageThreshold (int32_t voltageMV) -{ - bool success = false; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // There are only two possible values, 2.8 V and 3.0 V. - // BATLOWV is bit 1 of the charging voltage control register - if (voltageMV > 2800) { - success = setRegisterBits(0x04, (1 << 1)); -#ifdef DEBUG_BQ24295 - printf("BatteryChargerBq24295 (I2C 0x%02x): recharge voltage threshold now set to 3.000 V.\r\n", gAddress >> 1); -#endif - } else { - success = clearRegisterBits(0x04, (1 << 1)); -#ifdef DEBUG_BQ24295 - printf("BatteryChargerBq24295 (I2C 0x%02x): recharge voltage threshold now set to 2.800 V.\r\n", gAddress >> 1); -#endif - } - gpI2c->unlock(); - } - - return success; -} - -// Get the pre-charge to fast-charge voltage threshold. -bool BatteryChargerBq24295::getFastChargingVoltageThreshold (int32_t *pVoltageMV) -{ - bool success = false; - char reg; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // BATLOWV is bit 1 of the charging voltage control register - if (getRegister(0x04, ®)) { - success = true; - if (pVoltageMV != NULL) { - *pVoltageMV = 2800; - if ((reg & (1 << 1)) != 0) { - *pVoltageMV = 3000; - } - } - } - -#ifdef DEBUG_BQ24295 - if (reg & (1 << 1)) { - printf("BatteryChargerBq24295 (I2C 0x%02x): recharge voltage threshold is 3.000 V.\r\n", gAddress >> 1); - } else { - printf("BatteryChargerBq24295 (I2C 0x%02x): recharge voltage threshold is 2.800 V.\r\n", gAddress >> 1); - } -#endif - gpI2c->unlock(); - } - - return success; -} - -// Set the recharging voltage threshold. -bool BatteryChargerBq24295::setRechargingVoltageThreshold (int32_t voltageMV) -{ - bool success = false; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // There are only two possible values, 100 mV and 300 mV. - // VRECHG is bit 0 of the charging voltage control register - if (voltageMV > 100) { - success = setRegisterBits(0x04, (1 << 0)); -#ifdef DEBUG_BQ24295 - printf("BatteryChargerBq24295 (I2C 0x%02x): recharge voltage threshold now set to 0.300 V.\r\n", gAddress >> 1); -#endif - } else { - success = clearRegisterBits(0x04, (1 << 0)); -#ifdef DEBUG_BQ24295 - printf("BatteryChargerBq24295 (I2C 0x%02x): recharge voltage threshold now set to 0.100 V.\r\n", gAddress >> 1); -#endif - } - gpI2c->unlock(); - } - - return success; -} - -// Get the recharging voltage threshold. -bool BatteryChargerBq24295::getRechargingVoltageThreshold (int32_t *pVoltageMV) -{ - bool success = false; - char reg; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // VRECHG is bit 0 of the charging voltage control register - if (getRegister(0x04, ®)) { - success = true; - if (pVoltageMV != NULL) { - *pVoltageMV = 100; - if ((reg & (1 << 0)) != 0) { - *pVoltageMV = 300; - } - } - } - -#ifdef DEBUG_BQ24295 - if (reg & (1 << 1)) { - printf("BatteryChargerBq24295 (I2C 0x%02x): recharge voltage threshold is 0.300 V.\r\n", gAddress >> 1); - } else { - printf("BatteryChargerBq24295 (I2C 0x%02x): recharge voltage threshold is 0.100 V.\r\n", gAddress >> 1); - } -#endif - gpI2c->unlock(); - } - - return success; -} - -// Set the boost voltage. -bool BatteryChargerBq24295::setBoostVoltage (int32_t voltageMV) -{ - bool success = false; - char reg; - int32_t codedValue; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the boost voltage/thermal regulation control register - if (getRegister(0x06, ®)) { - // Boost voltage is in bits 4 to 7, coded to base "64 mV" - // with an offset of 4550 mV. - if ((voltageMV >= 4550) && (voltageMV <= 5510)) { - codedValue = voltageMV; - codedValue = (codedValue - 4550) / 64; - // If the voltage minus the base is not an exact multiple of 64, - // add one to the coded value to make sure we don't - // go under the requested boost voltage - if ((voltageMV - 4550) % 64 != 0) { - codedValue++; - } - - reg &= ~(0x0f << 4); - reg |= (char) ((codedValue & 0x0f) << 4); - - // Write to the boost voltage/thermal regulation control register - success = setRegister (0x06, reg); -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): boost voltage now set to %.3f V.\r\n", gAddress >> 1, (float) voltageMV / 1000); - } -#endif - } - } - gpI2c->unlock(); - } - - return success; -} - -// Get the boost voltage. -bool BatteryChargerBq24295::getBoostVoltage (int32_t *pVoltageMV) -{ - bool success = false; - char reg; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the boost voltage/thermal regulation control register - if (getRegister(0x06, ®)) { - success = true; - if (pVoltageMV != NULL) { - // Boost voltage is in bits 4 to 7 - // Base voltage - *pVoltageMV = 4550; - // Shift reg down and add the number of multiples - // of 64 mV - reg = (reg >> 4) & 0x0f; - *pVoltageMV += ((int32_t) reg) * 64; -#ifdef DEBUG_BQ24295 - printf("BatteryChargerBq24295 (I2C 0x%02x): boost voltage is %.3f V.\r\n", gAddress >> 1, (float) *pVoltageMV / 1000); -#endif - } - } - gpI2c->unlock(); - } - - return success; -} - -// Set the boost mode low temperature limit. -bool BatteryChargerBq24295::setBoostUpperTemperatureLimit (int32_t temperatureC) -{ - bool success = false; - char reg; - char codedValue; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the boost voltage/thermal regulation control register - if (getRegister(0x06, ®)) { - // BHOT is in bits 2 & 3 - if (temperatureC < 60) { - codedValue = 0; - } else if (temperatureC < 65) { - codedValue = 1; - } else { - codedValue = 2; - } - reg &= ~(0x03 << 2); - reg |= (char) (codedValue << 2); - // Write to boost voltage/thermal regulation control register - success = setRegister (0x06, reg); -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): boost mode lower temperature limit now set to %d C.\r\n", gAddress >> 1, (int) temperatureC); - } -#endif - } - gpI2c->unlock(); - } - - return success; -} - -// Get the boost mode upper temperature limit. -bool BatteryChargerBq24295::getBoostUpperTemperatureLimit (int32_t *pTemperatureC) -{ - bool success = false; - char reg; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // BHOT is in bits 2 & 3 of the boost voltage/thermal regulation control register - if (getRegister(0x06, ®)) { - // Only proceed (and return true) if the limit is enabled - if (((reg >> 2) & 0x03) != 0x03) { - success = true; - if (pTemperatureC != NULL) { - switch ((reg >> 2) & 0x03) { - case 0: - *pTemperatureC = 55; - break; - case 1: - *pTemperatureC = 60; - break; - case 2: - *pTemperatureC = 65; - break; - default: - MBED_ASSERT(false); - break; - } -#ifdef DEBUG_BQ24295 - printf("BatteryChargerBq24295 (I2C 0x%02x): boost mode upper temperature limit is %d C.\r\n", gAddress >> 1, (int) *pTemperatureC); -#endif - } - } - } - gpI2c->unlock(); - } - - return success; -} - -// Check whether the boost mode upper temperature limit is enabled. -bool BatteryChargerBq24295::isBoostUpperTemperatureLimitEnabled (void) -{ - bool isEnabled = false; - char reg; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // BHOT is in bits 2 & 3 of the boost voltage/thermal regulation control register - // and it is enabled if any bit is 0 - if (getRegister(0x06, ®)) { - if (((reg >> 2) & 3) != 3) { - isEnabled = true; - } -#ifdef DEBUG_BQ24295 - if (isEnabled) { - printf("BatteryChargerBq24295 (I2C 0x%02x): boost mode upper temperature limit is ENABLED.\r\n", gAddress >> 1); - } else { - printf("BatteryChargerBq24295 (I2C 0x%02x): boost mode upper temperature limit is DISABLED.\r\n", gAddress >> 1); - } -#endif - } - gpI2c->unlock(); - } - - return isEnabled; -} - -// Disable the boost mode upper temperature limit. -bool BatteryChargerBq24295::disableBoostUpperTemperatureLimit (void) -{ - bool success = false; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // BHOT is in bits 2 & 3 of the boost voltage/thermal regulation control register - // and setting all the bits indicates disabled - success = setRegisterBits(0x06, (3 << 2)); -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): boost mode upper temperature limit now DISABLED.\r\n", gAddress >> 1); - } -#endif - gpI2c->unlock(); - } - - return success; -} - -// Set the boost mode upper temperature limit. -bool BatteryChargerBq24295::setBoostLowerTemperatureLimit (int32_t temperatureC) -{ - bool success = false; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // There are only two possible values, -10 C and -20 C. - // BCOLD is bit 1 of the charge current control register - if (temperatureC < -10) { - success = setRegisterBits(0x02, (1 << 1)); -#ifdef DEBUG_BQ24295 - printf("BatteryChargerBq24295 (I2C 0x%02x): boost mode lower temperature limit now set to -20 C.\r\n", gAddress >> 1); -#endif - } else { - success = clearRegisterBits(0x02, (1 << 1)); -#ifdef DEBUG_BQ24295 - printf("BatteryChargerBq24295 (I2C 0x%02x): boost mode lower temperature limit now set to -10 C.\r\n", gAddress >> 1); -#endif - } - gpI2c->unlock(); - } - - return success; -} - -// Get the boost mode low temperature limit. -bool BatteryChargerBq24295::getBoostLowerTemperatureLimit (int32_t *pTemperatureC) -{ - bool success = false; - char reg; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // BCOLD is bit 1 of the charge current control register - if (getRegister(0x02, ®)) { - success = true; - if (pTemperatureC != NULL) { - *pTemperatureC = -10; - if ((reg & (1 << 1)) != 0) { - *pTemperatureC = -20; - } - } - } - -#ifdef DEBUG_BQ24295 - if (reg & (1 << 1)) { - printf("BatteryChargerBq24295 (I2C 0x%02x): boost mode lower temperature limit is -20 C.\r\n", gAddress >> 1); - } else { - printf("BatteryChargerBq24295 (I2C 0x%02x): boost mode lower temperature limit is -10 C.\r\n", gAddress >> 1); - } -#endif - gpI2c->unlock(); - } - - return success; -} - -// Set the input voltage limit. -bool BatteryChargerBq24295::setInputVoltageLimit (int32_t voltageMV) -{ - bool success = false; - char reg; - int32_t codedValue; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the input source control register - if (getRegister(0x00, ®)) { - // Input voltage limit is in bits 3 to 6 - // but it is coded to base "80 mV" with - // an offset of 3880 mV. - if ((voltageMV >= 3880) && (voltageMV <= 5080)) { - codedValue = voltageMV; - codedValue = (codedValue - 3880) / 80; - - reg &= ~(0x0f << 3); - reg |= (char) ((codedValue & 0x0f) << 3); - - // Write to the input source control register - success = setRegister (0x00, reg); -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): input voltage limit now set to %.3f V.\r\n", gAddress >> 1, (float) voltageMV / 1000); - } -#endif - } - } - gpI2c->unlock(); - } - - return success; -} - -// Get the input voltage limit. -bool BatteryChargerBq24295::getInputVoltageLimit (int32_t *pVoltageMV) -{ - bool success = false; - char reg; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the input source control register - if (getRegister(0x00, ®)) { - success = true; - if (pVoltageMV != NULL) { - // Input voltage limit is in bits 3 to 6 - // Base voltage - *pVoltageMV = 3880; - // Shift reg down and add the number of multiples - // of 80 mV - reg = (reg >> 3) & 0x0f; - *pVoltageMV += ((int32_t) reg) * 80; -#ifdef DEBUG_BQ24295 - printf("BatteryChargerBq24295 (I2C 0x%02x): input voltage limit is %.3f V.\r\n", gAddress >> 1, (float) *pVoltageMV / 1000); -#endif - } - } - gpI2c->unlock(); - } - - return success; -} - -// Set the input current limit. -bool BatteryChargerBq24295::setInputCurrentLimit (int32_t currentMA) -{ - bool success = false; - char reg; - char codedValue; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the input source control register - if (getRegister(0x00, ®)) { - // Input current limit is in bits 0 to 2, coded - // such that the smallest limit is applied for - // a range (e.g. 120 mA ends up as 100 mA rather - // than 150 mA) - if ((currentMA >= 100) && (currentMA <= 3000)) { - if (currentMA < 150) { - codedValue = 0; - } else if (currentMA < 500) { - codedValue = 1; - } else if (currentMA < 900) { - codedValue = 2; - } else if (currentMA < 1000) { - codedValue = 3; - } else if (currentMA < 1500) { - codedValue = 4; - } else if (currentMA < 2000) { - codedValue = 5; - } else if (currentMA < 3000) { - codedValue = 6; - } else { - codedValue = 7; - } - reg &= ~(0x07 << 0); - reg |= codedValue; - - // Write to the input source control register - success = setRegister (0x00, reg); -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): input current limit now set to %.3f A.\r\n", gAddress >> 1, (float) currentMA / 1000); - } -#endif - } - } - gpI2c->unlock(); - } - - return success; -} - -// Get the input current limit. -bool BatteryChargerBq24295::getInputCurrentLimit (int32_t *pCurrentMA) -{ - bool success = false; - char reg; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the input source control register - if (getRegister(0x00, ®)) { - success = true; - if (pCurrentMA != NULL) { - *pCurrentMA = 0; - // Input current limit is in bits 0 to 2 - switch (reg & 0x07) { - case 0: - *pCurrentMA = 100; - break; - case 1: - *pCurrentMA = 150; - break; - case 2: - *pCurrentMA = 500; - break; - case 3: - *pCurrentMA = 900; - break; - case 4: - *pCurrentMA = 1000; - break; - case 5: - *pCurrentMA = 1500; - break; - case 6: - *pCurrentMA = 2000; - break; - case 7: - *pCurrentMA = 3000; - break; - default: - MBED_ASSERT(false); - break; - } -#ifdef DEBUG_BQ24295 - printf("BatteryChargerBq24295 (I2C 0x%02x): input current limit is %.3f A.\r\n", gAddress >> 1, (float) *pCurrentMA / 1000); -#endif - } - } - gpI2c->unlock(); - } - - return success; -} - -// Enable input voltage or current limits. -bool BatteryChargerBq24295::enableInputLimits (void) -{ - bool success = false; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Input limit enable is bit 7 of the source control register - success = setRegisterBits(0x00, (1 << 7)); -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): input limits now ENABLED.\r\n", gAddress >> 1); - } -#endif - gpI2c->unlock(); - } - - return success; -} - -// Remove any input voltage or current limits. -bool BatteryChargerBq24295::disableInputLimits (void) -{ - bool success = false; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Input limit enable is bit 7 of the source control register - success = clearRegisterBits(0x00, (1 << 7)); -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): input limits now DISABLED.\r\n", gAddress >> 1); - } -#endif - gpI2c->unlock(); - } - - return success; -} - -// Check if input limits are enabled. -bool BatteryChargerBq24295::areInputLimitsEnabled (void) -{ - bool areEnabled = false; - char reg; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the input source control register - if (getRegister(0x00, ®)) { - // Input limit enable is bit 7 of the source control register - if ((reg & (1 << 7)) != 0) { - areEnabled = true; - } -#ifdef DEBUG_BQ24295 - if (areEnabled) { - printf("BatteryChargerBq24295 (I2C 0x%02x): input limits are ENABLED.\r\n", gAddress >> 1); - } else { - printf("BatteryChargerBq24295 (I2C 0x%02x): input limits are DISABLED.\r\n", gAddress >> 1); - } -#endif - } - gpI2c->unlock(); - } - - return areEnabled; -} - -// Set the thermal regulation threshold for the chip. -bool BatteryChargerBq24295::setChipThermalRegulationThreshold (int32_t temperatureC) -{ - bool success = false; - char reg; - char codedValue; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the boost voltage/thermal regulation control register - if (getRegister(0x06, ®)) { - // TREG is in bits 0 & 1 - if (temperatureC < 80) { - codedValue = 0; - } else if (temperatureC < 100) { - codedValue = 1; - } else if (temperatureC < 120) { - codedValue = 2; - } else { - codedValue = 3; - } - reg &= ~0x03; - reg |= (char) codedValue; - // Write to boost voltage/thermal regulation control register - success = setRegister (0x06, reg); -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): chip thermal regulation threshold now set to %d C.\r\n", gAddress >> 1, (int) temperatureC); - } -#endif - } - gpI2c->unlock(); - } - - return success; -} - -// Get the thermal regulation threshold for the chip. -bool BatteryChargerBq24295::getChipThermalRegulationThreshold (int32_t *pTemperatureC) -{ - bool success = false; - char reg; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - /// TREG is in bits 0 & 1 of the boost voltage/thermal regulation control register - if (getRegister(0x06, ®)) { - success = true; - if (pTemperatureC != NULL) { - switch (reg & 0x03) { - case 0: - *pTemperatureC = 60; - break; - case 1: - *pTemperatureC = 80; - break; - case 2: - *pTemperatureC = 100; - break; - case 3: - *pTemperatureC = 120; - break; - default: - MBED_ASSERT(false); - break; - } -#ifdef DEBUG_BQ24295 - printf("BatteryChargerBq24295 (I2C 0x%02x): chip thermal regulation threshold is %d C.\r\n", gAddress >> 1, (int) *pTemperatureC); -#endif - } - } - gpI2c->unlock(); - } - - return success; -} - -// Get the charger fault status as a bitmap. -char BatteryChargerBq24295::getChargerFaults(void) -{ - char bitmap = (char) CHARGER_FAULT_NONE; - char reg; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the fault register - if (getRegister(0x09, ®)) { - bitmap = reg; -#ifdef DEBUG_BQ24295 - printf("BatteryChargerBq24295 (I2C 0x%02x): charge fault register 0x%02x.\r\n", gAddress >> 1, bitmap); -#endif - } - gpI2c->unlock(); - } - - return bitmap; -} - -// Feed the watchdog timer of the BQ24295 chip. -bool BatteryChargerBq24295::feedWatchdog (void) -{ - bool success = false; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Watchdog reset is done by setting bit 6 of the power on control register - // to 1 - success = setRegisterBits(0x01, (1 << 6)); -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): watchdog fed.\r\n", gAddress >> 1); - } -#endif - gpI2c->unlock(); - } - - return success; -} - -// Get the watchdog timer of the BQ24295 chip. -bool BatteryChargerBq24295::getWatchdog (int32_t *pWatchdogS) -{ - bool success = false; - char reg; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Watchdog timer is in bits 4 and 5 of the charger termination/timer control - // register, where 0 is disabled 01 is 40 secs, 10 is 80 secs and 11 is 160 secs - success = getRegister(0x05, ®); - if (pWatchdogS != NULL) { - switch ((reg >> 4) & 0x03) { - case 0x00: - *pWatchdogS = 0; - break; - case 0x01: - *pWatchdogS = 40; - break; - case 0x02: - *pWatchdogS = 80; - break; - case 0x03: - *pWatchdogS = 160; - break; - default: - MBED_ASSERT(false); - break; - } -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): watchdog is %d seconds.\r\n", gAddress >> 1, (int) *pWatchdogS); - } -#endif - } - gpI2c->unlock(); - } - - return success; -} - -// Set the watchdog timer of the BQ24295 chip. -bool BatteryChargerBq24295::setWatchdog (int32_t watchdogS) -{ - bool success = false; - char reg; - char regValue = 0; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Watchdog timer is in bits 4 and 5 of the charger termination/timer control - // register, where 0 is disabled 01 is 40 secs, 10 is 80 secs and 11 is 160 secs - if (watchdogS > 80) { - regValue = 0x03; - } else if (watchdogS > 40) { - regValue = 0x02; - } else if (watchdogS > 0) { - regValue = 0x01; - } - if (getRegister(0x05, ®)) { - // Clear the bits then set them - reg &= ~(0x03 << 4); - reg |= regValue << 4; - success = setRegister(0x05, reg); -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): watchdog set to %d seconds.\r\n", gAddress >> 1, (int) watchdogS); - } -#endif - } - gpI2c->unlock(); - } - - return success; -} - -// Enable shipping mode. -bool BatteryChargerBq24295::enableShippingMode (void) -{ - bool success = false; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Have to disable the watchdog (bits 4 & 5 of charge termination/timer control register) - if (clearRegisterBits(0x05, (1 << 4) || (1 << 5))) { - // Now disable the BATFET, bit 5 of the misc operation control register - success = setRegisterBits(0x07, (1 << 5)); -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): shipping mode is now ENABLED.\r\n", gAddress >> 1); - } -#endif - } - gpI2c->unlock(); - } - - return success; -} - -// Disable shipping mode. -bool BatteryChargerBq24295::disableShippingMode (void) -{ - bool success = false; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Set the watchdog timer back to default (bit 4 of charge termination/timer control register) - if (setRegisterBits(0x05, (1 << 4))) { - // Now enable the BATFET, bit 5 of the misc operation control register - success = clearRegisterBits(0x07, (1 << 5)); -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): shipping mode is now DISABLED.\r\n", gAddress >> 1); - } -#endif - } - gpI2c->unlock(); - } - - return success; -} - -// Check if shipping mode is enabled. -bool BatteryChargerBq24295::isShippingModeEnabled (void) -{ - bool isEnabled = false; - char reg; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // To be in shipping mode the watchdog has to be disabled - if (getRegister(0x05, ®)) { - // Check bits 4 & 5 of the charge termination/timer control register for zero - if ((reg & ((1 << 4) || (1 << 5))) == 0) { - // Now see if the BATFET is disabled (bit 5 of the misc operation control register) - if (getRegister(0x07, ®) && ((reg & (1 << 5)) != 0)) { - isEnabled = true; - } - } -#ifdef DEBUG_BQ24295 - if (isEnabled) { - printf("BatteryChargerBq24295 (I2C 0x%02x): shipping mode is ENABLED.\r\n", gAddress >> 1); - } else { - printf("BatteryChargerBq24295 (I2C 0x%02x): shipping mode is DISABLED.\r\n", gAddress >> 1); - } -#endif - } - gpI2c->unlock(); - } - - return isEnabled; -} - -// Read a register on the chip. -bool BatteryChargerBq24295::advancedGet(char address, char *pValue) -{ - bool success = false; - char value; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Read the register - if (getRegister(address, &value)) { - success = true; -#ifdef DEBUG_BQ24295 - printf("BatteryChargerBq24295 (I2C 0x%02x): read 0x%02x from address 0x%02x.\n", gAddress >> 1, value, address); -#endif - if (pValue != NULL) { - *pValue = value; - } - } - gpI2c->unlock(); - } - - return success; -} - -// Set a register on the chip. -// TODO test -bool BatteryChargerBq24295::advancedSet(char address, char value) -{ - bool success = false; - - if (gReady && (gpI2c != NULL)) { - gpI2c->lock(); - // Set the register - success = setRegister (address, value); -#ifdef DEBUG_BQ24295 - if (success) { - printf("BatteryChargerBq24295 (I2C 0x%02x): wrote 0x%02x to address 0x%02x.\n", gAddress >> 1, value, address); - } -#endif - gpI2c->unlock(); - } - - return success; -} - -/* End Of File */
Binary file battery-charger-bq24295/doc/lipo_charger_bq24295.pdf has changed