This class provides APIs to all of the registers of the TI BQ35100 battery gauge, as used on the u-blox C030 primary battery shield.
Dependents: example-battery-gauge-bq35100
Diff: bq35100.cpp
- Revision:
- 0:cec745c014b7
- Child:
- 1:ee7cc8d75283
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bq35100.cpp Mon Jul 03 16:12:22 2017 +0000 @@ -0,0 +1,126 @@ +/* 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 bq35100.cpp + * This file defines the API to the TI BQ35100 battery gauge chip. + */ + +/** Define these to print debug information. */ +//#define DEBUG_BQ35100 + +#include <mbed.h> +#include <battery_gauge_bq35100.h> + +#ifdef DEBUG_BQ35100 +# include <stdio.h> +#endif + +// ---------------------------------------------------------------- +// COMPILE-TIME MACROS +// ---------------------------------------------------------------- + +// ---------------------------------------------------------------- +// PRIVATE VARIABLES +// ---------------------------------------------------------------- + +// ---------------------------------------------------------------- +// GENERIC PRIVATE FUNCTIONS +// ---------------------------------------------------------------- + +// Read two bytes from an address. +// Note: gpI2c should be locked before this is called. +bool BatteryGaugeBq35100::getTwoBytes (uint8_t registerAddress, uint16_t *pBytes) +{ + bool success = false; + char data[3]; + + if (gpI2c != NULL) { + // Send a command to read from registerAddress + data[0] = registerAddress; + data[1] = 0; + data[2] = 0; + + if ((gpI2c->write(gAddress, &(data[0]), 1) == 0) && + (gpI2c->read(gAddress, &(data[1]), 2) == 0)) { + success = true; + if (pBytes) { + *pBytes = (((uint16_t) data[2]) << 8) + data[1]; + } + } + } + + return success; +} + +//---------------------------------------------------------------- +// PUBLIC FUNCTIONS +// ---------------------------------------------------------------- + +// Constructor. +BatteryGaugeBq35100::BatteryGaugeBq35100(void) +{ + gpI2c = NULL; + gReady = false; + gGaugeOn = false; + gSealCode = 0; +} + +// Destructor. +BatteryGaugeBq35100::~BatteryGaugeBq35100(void) +{ +} + +// Initialise ourselves. +bool BatteryGaugeBq35100::init (I2C * pI2c, uint8_t address, uint16_t sealCode) +{ + uint16_t answer; + char data[4]; + + gpI2c = pI2c; + gAddress = address << 1; + gSealCode = sealCode; + + if (gpI2c != NULL) { + gpI2c->lock(); + + // Send a control command to read the firmware version + data[0] = 0x00; // Set address to first register for control + data[1] = 0x02; // First byte of FW_VERSION sub-command (0x02) + data[2] = 0x00; // Second byte of FW_VERSION sub-command (0x00) (register address will auto-increment) + + if (gpI2c->write(gAddress, &(data[0]), 3) == 0) { + if (getTwoBytes (0, &answer)) { +#ifdef DEBUG_BQ35100 + printf("BatteryGaugeBq35100 (I2C 0x%02x): read 0x%04x as FW_VERSION, expected 0x0109.\n", gAddress >> 1, answer); +#endif + } + } + gpI2c->unlock(); + } + +#ifdef DEBUG_BQ35100 + if (gReady) { + printf("BatteryGaugeBq35100 (I2C 0x%02x): handler initialised.\r\n", gAddress >> 1); + } else { + printf("BatteryGaugeBq35100 (I2C 0x%02x): init NOT successful.\r\n", gAddress >> 1); + } +#endif + + return gReady; +} + +/* End Of File */