Example program to demonstrate the use of the BatteryChargerBQ24295 class.

Dependencies:   battery-charger-bq24295

Committer:
rob.meades@u-blox.com
Date:
Wed Aug 02 13:35:34 2017 +0100
Revision:
9:1d409db9e6e0
Parent:
1:18ffb01b6380
Update battery-charger-bq24295 library reference.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RobMeades 0:3594f16acb1e 1 /* mbed Microcontroller Library
RobMeades 0:3594f16acb1e 2 * Copyright (c) 2017 u-blox
RobMeades 0:3594f16acb1e 3 *
RobMeades 0:3594f16acb1e 4 * Licensed under the Apache License, Version 2.0 (the "License");
RobMeades 0:3594f16acb1e 5 * you may not use this file except in compliance with the License.
RobMeades 0:3594f16acb1e 6 * You may obtain a copy of the License at
RobMeades 0:3594f16acb1e 7 *
RobMeades 0:3594f16acb1e 8 * http://www.apache.org/licenses/LICENSE-2.0
RobMeades 0:3594f16acb1e 9 *
RobMeades 0:3594f16acb1e 10 * Unless required by applicable law or agreed to in writing, software
RobMeades 0:3594f16acb1e 11 * distributed under the License is distributed on an "AS IS" BASIS,
RobMeades 0:3594f16acb1e 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
RobMeades 0:3594f16acb1e 13 * See the License for the specific language governing permissions and
RobMeades 0:3594f16acb1e 14 * limitations under the License.
RobMeades 0:3594f16acb1e 15 */
RobMeades 0:3594f16acb1e 16
RobMeades 0:3594f16acb1e 17 #include "mbed.h"
RobMeades 0:3594f16acb1e 18 #include "battery_charger_bq24295.h"
RobMeades 0:3594f16acb1e 19
RobMeades 0:3594f16acb1e 20 // LEDs
RobMeades 0:3594f16acb1e 21 DigitalOut ledRed(LED1, 1);
RobMeades 0:3594f16acb1e 22 DigitalOut ledGreen(LED2, 1);
RobMeades 0:3594f16acb1e 23
RobMeades 0:3594f16acb1e 24 /* This example program for the u-blox C030 board instantiates
RobMeades 0:3594f16acb1e 25 * the BQ24295 battery charger interface and performs a few
rob.meades@u-blox.com 1:18ffb01b6380 26 * example calls to the battery charger API, displaying information
rob.meades@u-blox.com 1:18ffb01b6380 27 * about the state of the charger chip. Progress may be
RobMeades 0:3594f16acb1e 28 * monitored with a serial terminal running at 9600 baud. The
RobMeades 0:3594f16acb1e 29 * LED on the C030 board will turn green when this program is
RobMeades 0:3594f16acb1e 30 * operating correctly and will turn red if there is a failure.
rob.meades@u-blox.com 1:18ffb01b6380 31 *
rob.meades@u-blox.com 1:18ffb01b6380 32 * To understand the meaning of the API calls it is necessary to
rob.meades@u-blox.com 1:18ffb01b6380 33 * refer to the data sheet for the TI BQ24295 chip.
rob.meades@u-blox.com 1:18ffb01b6380 34 * IMPORTANT it is NOT necessary to use the BQ24295 class in order
rob.meades@u-blox.com 1:18ffb01b6380 35 * to charge a battery connected to the C030 board, that will happen
rob.meades@u-blox.com 1:18ffb01b6380 36 * automatically. The BQ24295 class is only required if the battery
rob.meades@u-blox.com 1:18ffb01b6380 37 * charger is to be configured or monitored.
RobMeades 0:3594f16acb1e 38 */
RobMeades 0:3594f16acb1e 39
RobMeades 0:3594f16acb1e 40 int main()
RobMeades 0:3594f16acb1e 41 {
rob.meades@u-blox.com 1:18ffb01b6380 42 I2C i2C(I2C_SDA_B, I2C_SCL_B);
RobMeades 0:3594f16acb1e 43 BatteryChargerBq24295 charger;
rob.meades@u-blox.com 1:18ffb01b6380 44 BatteryChargerBq24295::ChargerState state = BatteryChargerBq24295::CHARGER_STATE_UNKNOWN;
rob.meades@u-blox.com 1:18ffb01b6380 45 char fault = 0;
RobMeades 0:3594f16acb1e 46
rob.meades@u-blox.com 1:18ffb01b6380 47 printf ("Starting up...\n");
rob.meades@u-blox.com 1:18ffb01b6380 48 if (charger.init(&i2C)) {
rob.meades@u-blox.com 1:18ffb01b6380 49
rob.meades@u-blox.com 1:18ffb01b6380 50 printf ("BQ24295 battery charger chip is initialised.\n");
rob.meades@u-blox.com 1:18ffb01b6380 51 if (charger.isExternalPowerPresent()) {
rob.meades@u-blox.com 1:18ffb01b6380 52 printf ("External power is present.\n");
rob.meades@u-blox.com 1:18ffb01b6380 53 } else {
rob.meades@u-blox.com 1:18ffb01b6380 54 printf ("External power is NOT present.\n");
rob.meades@u-blox.com 1:18ffb01b6380 55 }
rob.meades@u-blox.com 1:18ffb01b6380 56 if (charger.areInputLimitsEnabled()) {
rob.meades@u-blox.com 1:18ffb01b6380 57 printf ("Input limits are enabled.\n");
rob.meades@u-blox.com 1:18ffb01b6380 58 } else {
rob.meades@u-blox.com 1:18ffb01b6380 59 printf ("Input limits are disabled.\n");
rob.meades@u-blox.com 1:18ffb01b6380 60 }
rob.meades@u-blox.com 1:18ffb01b6380 61 if (charger.isChargingEnabled()) {
rob.meades@u-blox.com 1:18ffb01b6380 62 printf ("Charging is already enabled.\n");
rob.meades@u-blox.com 1:18ffb01b6380 63 } else {
rob.meades@u-blox.com 1:18ffb01b6380 64 printf ("Charging is disabled.\n");
rob.meades@u-blox.com 1:18ffb01b6380 65 }
rob.meades@u-blox.com 1:18ffb01b6380 66 if (charger.isOtgEnabled()) {
rob.meades@u-blox.com 1:18ffb01b6380 67 printf ("OTG is enabled.\n");
rob.meades@u-blox.com 1:18ffb01b6380 68 } else {
rob.meades@u-blox.com 1:18ffb01b6380 69 printf ("OTG is disabled.\n");
rob.meades@u-blox.com 1:18ffb01b6380 70 }
rob.meades@u-blox.com 1:18ffb01b6380 71 if (charger.setWatchdog(160)) {
rob.meades@u-blox.com 1:18ffb01b6380 72 printf ("Watchdog set to 160 seconds.\n");
rob.meades@u-blox.com 1:18ffb01b6380 73 }
rob.meades@u-blox.com 1:18ffb01b6380 74
rob.meades@u-blox.com 1:18ffb01b6380 75 while (state < BatteryChargerBq24295::MAX_NUM_CHARGER_STATES) {
rob.meades@u-blox.com 1:18ffb01b6380 76 // Feed the watchdog timer to keep the charger chip in
rob.meades@u-blox.com 1:18ffb01b6380 77 // Host mode while we are monitoring it. If we do not
rob.meades@u-blox.com 1:18ffb01b6380 78 // feed the watchdog it will expire, which does no
rob.meades@u-blox.com 1:18ffb01b6380 79 // harm at all (the chip is now simply in default mode again
rob.meades@u-blox.com 1:18ffb01b6380 80 // and will return to host mode if we write to it), but
rob.meades@u-blox.com 1:18ffb01b6380 81 // it will appear as a fault state.
rob.meades@u-blox.com 1:18ffb01b6380 82 charger.feedWatchdog();
rob.meades@u-blox.com 1:18ffb01b6380 83 fault = charger.getChargerFaults();
rob.meades@u-blox.com 1:18ffb01b6380 84 if (fault == BatteryChargerBq24295::CHARGER_FAULT_NONE) {
rob.meades@u-blox.com 1:18ffb01b6380 85 ledGreen = 0;
rob.meades@u-blox.com 1:18ffb01b6380 86 ledRed = 1;
rob.meades@u-blox.com 1:18ffb01b6380 87 state = charger.getChargerState();
rob.meades@u-blox.com 1:18ffb01b6380 88 switch (state) {
rob.meades@u-blox.com 1:18ffb01b6380 89 case BatteryChargerBq24295::CHARGER_STATE_UNKNOWN:
rob.meades@u-blox.com 1:18ffb01b6380 90 printf("Charger state is not (yet) known.\n");
rob.meades@u-blox.com 1:18ffb01b6380 91 break;
rob.meades@u-blox.com 1:18ffb01b6380 92 case BatteryChargerBq24295::CHARGER_STATE_DISABLED:
rob.meades@u-blox.com 1:18ffb01b6380 93 printf("Charger state: disabled.\n");
rob.meades@u-blox.com 1:18ffb01b6380 94 break;
rob.meades@u-blox.com 1:18ffb01b6380 95 case BatteryChargerBq24295::CHARGER_STATE_NO_EXTERNAL_POWER:
rob.meades@u-blox.com 1:18ffb01b6380 96 printf("Charger state: no external power.\n");
rob.meades@u-blox.com 1:18ffb01b6380 97 break;
rob.meades@u-blox.com 1:18ffb01b6380 98 case BatteryChargerBq24295::CHARGER_STATE_NOT_CHARGING:
rob.meades@u-blox.com 1:18ffb01b6380 99 printf("Charger state: not charging.\n");
rob.meades@u-blox.com 1:18ffb01b6380 100 break;
rob.meades@u-blox.com 1:18ffb01b6380 101 case BatteryChargerBq24295::CHARGER_STATE_PRECHARGE:
rob.meades@u-blox.com 1:18ffb01b6380 102 printf("Charger state: pre-charge.\n");
rob.meades@u-blox.com 1:18ffb01b6380 103 break;
rob.meades@u-blox.com 1:18ffb01b6380 104 case BatteryChargerBq24295::CHARGER_STATE_FAST_CHARGE:
rob.meades@u-blox.com 1:18ffb01b6380 105 printf("Charger state: fast charge.\n");
rob.meades@u-blox.com 1:18ffb01b6380 106 break;
rob.meades@u-blox.com 1:18ffb01b6380 107 case BatteryChargerBq24295::CHARGER_STATE_COMPLETE:
rob.meades@u-blox.com 1:18ffb01b6380 108 printf("Charger state: charging complete.\n");
rob.meades@u-blox.com 1:18ffb01b6380 109 break;
rob.meades@u-blox.com 1:18ffb01b6380 110 default:
rob.meades@u-blox.com 1:18ffb01b6380 111 printf("Unknown charger state (%d).\n", state);
rob.meades@u-blox.com 1:18ffb01b6380 112 break;
rob.meades@u-blox.com 1:18ffb01b6380 113 }
rob.meades@u-blox.com 1:18ffb01b6380 114 } else {
rob.meades@u-blox.com 1:18ffb01b6380 115 ledGreen = 1;
rob.meades@u-blox.com 1:18ffb01b6380 116 ledRed = 0;
rob.meades@u-blox.com 1:18ffb01b6380 117 if (fault & BatteryChargerBq24295::CHARGER_FAULT_THERMISTOR_TOO_HOT) {
rob.meades@u-blox.com 1:18ffb01b6380 118 printf("Charger fault: thermistor is too hot.\n");
rob.meades@u-blox.com 1:18ffb01b6380 119 }
rob.meades@u-blox.com 1:18ffb01b6380 120 if (fault & BatteryChargerBq24295::CHARGER_FAULT_THERMISTOR_TOO_COLD) {
rob.meades@u-blox.com 1:18ffb01b6380 121 printf("Charger fault: thermistor is too cold.\n");
rob.meades@u-blox.com 1:18ffb01b6380 122 }
rob.meades@u-blox.com 1:18ffb01b6380 123 if (fault & BatteryChargerBq24295::CHARGER_FAULT_BATTERY_OVER_VOLTAGE) {
rob.meades@u-blox.com 1:18ffb01b6380 124 printf("Charger fault: battery over-voltage.\n");
rob.meades@u-blox.com 1:18ffb01b6380 125 }
rob.meades@u-blox.com 1:18ffb01b6380 126 // The testing of the fault bit map looks slightly strange here.
rob.meades@u-blox.com 1:18ffb01b6380 127 // This is because the definition of the fault bits in the
rob.meades@u-blox.com 1:18ffb01b6380 128 // BQ24295 chip means that Charger Timer Expired overlaps
rob.meades@u-blox.com 1:18ffb01b6380 129 // input fault and thermal shutdown. See the definition of the
rob.meades@u-blox.com 1:18ffb01b6380 130 // meaning of REG09 in the BQ24295 data sheet for more information.
rob.meades@u-blox.com 1:18ffb01b6380 131 if ((fault & BatteryChargerBq24295::CHARGER_FAULT_INPUT_FAULT) &&
rob.meades@u-blox.com 1:18ffb01b6380 132 !(fault & BatteryChargerBq24295::CHARGER_FAULT_THERMAL_SHUTDOWN)) {
rob.meades@u-blox.com 1:18ffb01b6380 133 printf("Charger fault: input fault.\n");
rob.meades@u-blox.com 1:18ffb01b6380 134 }
rob.meades@u-blox.com 1:18ffb01b6380 135 if ((fault & BatteryChargerBq24295::CHARGER_FAULT_THERMAL_SHUTDOWN) &&
rob.meades@u-blox.com 1:18ffb01b6380 136 !(fault & BatteryChargerBq24295::CHARGER_FAULT_INPUT_FAULT)) {
rob.meades@u-blox.com 1:18ffb01b6380 137 printf("Charger fault: thermal shutdown.\n");
rob.meades@u-blox.com 1:18ffb01b6380 138 }
rob.meades@u-blox.com 1:18ffb01b6380 139 if ((fault & BatteryChargerBq24295::CHARGER_FAULT_CHARGE_TIMER_EXPIRED) ==
rob.meades@u-blox.com 1:18ffb01b6380 140 BatteryChargerBq24295::CHARGER_FAULT_CHARGE_TIMER_EXPIRED) {
rob.meades@u-blox.com 1:18ffb01b6380 141 printf("Charger fault: charge timer expired.\n");
rob.meades@u-blox.com 1:18ffb01b6380 142 }
rob.meades@u-blox.com 1:18ffb01b6380 143 if (fault & BatteryChargerBq24295::CHARGER_FAULT_OTG) {
rob.meades@u-blox.com 1:18ffb01b6380 144 printf("Charger fault: OTG charging fault.\n");
rob.meades@u-blox.com 1:18ffb01b6380 145 }
rob.meades@u-blox.com 1:18ffb01b6380 146 if (fault & BatteryChargerBq24295::CHARGER_FAULT_WATCHDOG_EXPIRED) {
rob.meades@u-blox.com 1:18ffb01b6380 147 printf("Charger fault: charger watchdog expired.\n");
rob.meades@u-blox.com 1:18ffb01b6380 148 }
rob.meades@u-blox.com 1:18ffb01b6380 149 }
rob.meades@u-blox.com 1:18ffb01b6380 150 wait_ms(5000);
rob.meades@u-blox.com 1:18ffb01b6380 151 }
RobMeades 0:3594f16acb1e 152 }
rob.meades@u-blox.com 1:18ffb01b6380 153
rob.meades@u-blox.com 1:18ffb01b6380 154 ledGreen = 1;
rob.meades@u-blox.com 1:18ffb01b6380 155 ledRed = 0;
rob.meades@u-blox.com 1:18ffb01b6380 156 printf("Should never get here.\n");
rob.meades@u-blox.com 1:18ffb01b6380 157 MBED_ASSERT(false);
RobMeades 0:3594f16acb1e 158 }
RobMeades 0:3594f16acb1e 159
RobMeades 0:3594f16acb1e 160 // End Of File