Tyler Sisk / battery-gauge-bq27441

Dependents:   rcCar

Fork of battery-gauge-bq27441 by u-blox

Embed: (wiki syntax)

« Back to documentation index

BatteryGaugeBq27441 Class Reference

BQ27441 battery gauge driver. More...

#include <battery_gauge_bq27441.h>

Public Member Functions

 BatteryGaugeBq27441 (void)
 Constructor.
 ~BatteryGaugeBq27441 (void)
 Destructor.
bool init (I2C *pI2c, uint8_t address=BATTERY_GAUGE_BQ27441_ADDRESS, uint16_t sealCode=SEAL_CODE_DEFAULT)
 Initialise the BQ27441 chip.
bool enableGauge (bool isSlow=false)
 Switch on the battery gauge.
bool disableGauge (void)
 Switch off the battery gauge.
bool isGaugeEnabled (void)
 Check whether battery gauging is enabled or not.
bool enableBatteryDetect ()
 Enable the battery detect pin of the chip.
bool disableBatteryDetect (void)
 Disable the battery detect pin of the chip and assume that the battery is ALWAYS connected.
bool isBatteryDetected (void)
 Determine whether a battery has been detected or not.
bool getTemperature (int32_t *pTemperatureC)
 Read the temperature of the BQ27441 chip.
bool getVoltage (int32_t *pVoltageMV)
 Read the voltage of the battery.
bool getPower (int32_t *pPowerMW)
 Read the power output the battery.
bool getCurrent (int32_t *pCurrentMA)
 Read the current flowing from the battery.
bool getRemainingCapacity (int32_t *pCapacityMAh)
 Read the remaining available battery energy.
bool getRemainingPercentage (int32_t *pBatteryPercent)
 Read the state of charge of the battery as a percentage.
bool advancedGetConfig (uint8_t subClassId, int32_t offset, int32_t length, char *pData)
 An advanced function to read configuration data from the BQ27441 chip memory.
bool advancedSetConfig (uint8_t subClassId, int32_t offset, int32_t length, const char *pData)
 An advanced function to write configuration data to the BQ27441 chip memory.
bool advancedSendControlWord (uint16_t controlWord, uint16_t *pDataReturned)
 Send a control word (see section 4.1 of the BQ27441 technical reference manual).
bool advancedGet (uint8_t address, uint16_t *pDataReturned)
 Read two bytes starting at a given address on the chip.
bool advancedIsSealed (void)
 Check if the chip is SEALED or UNSEALED.
bool advancedSeal (void)
 Put the chip into SEALED mode.
bool advancedUnseal (uint16_t sealCode=SEAL_CODE_DEFAULT)
 Send the seal code to the chip to unseal it.
bool advancedReset (void)
 Do a hard reset of the chip, reinitialising RAM data to defaults from ROM.

Protected Member Functions

bool getTwoBytes (uint8_t registerAddress, uint16_t *pBytes)
 Read two bytes starting at a given address.
bool getTwoBytesSigned (uint8_t registerAddress, int16_t *pBytes)
 Read two bytes starting at a given address (signed int16).
uint8_t computeChecksum (const char *pData)
 Compute the checksum of a block of memory in the chip.
bool readExtendedData (uint8_t subClassId, int32_t offset, int32_t length, char *pData)
 Read data of a given length and class ID.
bool writeExtendedData (uint8_t subClassId, int32_t offset, int32_t length, const char *pData)
 Write an extended data block.
bool isSealed (void)
 Check if the chip is SEALED or UNSEALED.
bool seal (void)
 Put the chip into SEALED mode.
bool unseal (uint16_t sealCode)
 Unseal the chip.
bool makeAdcReading (void)
 Make sure that the chip is awake and has taken a reading.
bool setHibernate (void)
 Set Hibernate mode.

Protected Attributes

I2C * gpI2c
 Pointer to the I2C interface.
uint8_t gAddress
 The address of the device.
uint16_t gSealCode
 The seal code for the device.
bool gReady
 Flag to indicate device is ready.
bool gGaugeOn
 Flag to indicate that monitor mode is active.

Detailed Description

BQ27441 battery gauge driver.

Definition at line 40 of file battery_gauge_bq27441.h.


Constructor & Destructor Documentation

BatteryGaugeBq27441 ( void   )

Constructor.

Definition at line 596 of file bq27441.cpp.

~BatteryGaugeBq27441 ( void   )

Destructor.

Definition at line 605 of file bq27441.cpp.


Member Function Documentation

bool advancedGet ( uint8_t  address,
uint16_t *  pDataReturned 
)

Read two bytes starting at a given address on the chip.

See sections 4.2 to 4.20 of the BQ27441 technical reference manual for the list of addresses.

Parameters:
addressthe start address to read from. For instance, for temperature this is 0x02.
pDataReturneda place to put the word of data returned.
Returns:
true if successful, otherwise false.

Definition at line 1196 of file bq27441.cpp.

bool advancedGetConfig ( uint8_t  subClassId,
int32_t  offset,
int32_t  length,
char *  pData 
)

An advanced function to read configuration data from the BQ27441 chip memory.

Please refer to the TI BQ27441 technical reference manual for details of classId, offset, the meanings of the data structures and their lengths. PLEASE READ THIS HEADER FILE DIRECTLY, DOXYGEN MANGLES THE NEXT BIT Note: the chip handles the data for each sub-class in 32 byte blocks and the offset/ length combination used must respect this. For instance:

Sub-class N (length 87 bytes) bytes 0 to 31 bytes 32 to 63 bytes 64 to 86 -------------------------------- -------------------------------- ----------------------- | Data Block 0 || xx Data Block 1 yy ||zz Data Block 2 | -------------------------------- -------------------------------- -----------------------

To read item xx, 2 bytes long at offset 36, one would specify an offset of 36 and a length of 2. To read both xx and yy at the same time (yy being 2 bytes long at offset 56), one could specify an offset of 36 and a length of 21. However, one could not read xx, yy and zz at the same time, or yy and zz at the same time, since they fall into different blocks; two separate reads are required.

Parameters:
subClassIdthe sub-class ID of the block.
offsetthe offset of the data within the class.
lengththe amount of data to read.
pDataa place to put the read data.
Returns:
true if successful, otherwise false.

Definition at line 1110 of file bq27441.cpp.

bool advancedIsSealed ( void   )

Check if the chip is SEALED or UNSEALED.

Returns:
true if it is SEALED, otherwise false.

Definition at line 1224 of file bq27441.cpp.

bool advancedReset ( void   )

Do a hard reset of the chip, reinitialising RAM data to defaults from ROM.

Note: the SEALED/UNSEALED status of the chip is unaffected.

Returns:
true if successful, otherwise false.

Definition at line 1298 of file bq27441.cpp.

bool advancedSeal ( void   )

Put the chip into SEALED mode.

SEALED mode is used to prevent accidental writes to the chip when it is in a production device. All of the functions in this class are able to work with a SEALED chip, provided the correct seal code is provided to the init() function.

Returns:
true if successful, otherwise false.

Definition at line 1245 of file bq27441.cpp.

bool advancedSendControlWord ( uint16_t  controlWord,
uint16_t *  pDataReturned 
)

Send a control word (see section 4.1 of the BQ27441 technical reference manual).

Parameters:
controlWordthe control word to send.
pDataReturneda place to put the word of data that could be returned, depending on which control word is used (may be NULL).
Returns:
true if successful, otherwise false.

Definition at line 1162 of file bq27441.cpp.

bool advancedSetConfig ( uint8_t  subClassId,
int32_t  offset,
int32_t  length,
const char *  pData 
)

An advanced function to write configuration data to the BQ27441 chip memory.

Please refer to the TI BQ27441 technical reference manual for details of classId, offset, the meanings of the data structures and their lengths. See also the note above advancedGetConfig() about how to use offset and length. If this function is used to change the seal code for the chip then init() should be called once more to update the seal code used by this driver.

Parameters:
subClassIdthe sub-class ID of the block.
offsetthe offset of the data within the class.
lengththe length of the data to be written.
pDataa pointer to the data to be written.
Returns:
true if successful, otherwise false.

Definition at line 1136 of file bq27441.cpp.

bool advancedUnseal ( uint16_t  sealCode = SEAL_CODE_DEFAULT )

Send the seal code to the chip to unseal it.

Parameters:
sealCodethe 16 bit seal code that will unseal the chip if it is sealed.
Returns:
true if successful, otherwise false.

Definition at line 1266 of file bq27441.cpp.

uint8_t computeChecksum ( const char *  pData ) [protected]

Compute the checksum of a block of memory in the chip.

Parameters:
pDataa pointer to the 32 byte data block.
Returns:
the checksum value.

Definition at line 109 of file bq27441.cpp.

bool disableBatteryDetect ( void   )

Disable the battery detect pin of the chip and assume that the battery is ALWAYS connected.

Default is that battery detect is enabled.

Returns:
true if successful, otherwise false.

Definition at line 763 of file bq27441.cpp.

bool disableGauge ( void   )

Switch off the battery gauge.

Returns:
true if successful, otherwise false.

Definition at line 738 of file bq27441.cpp.

bool enableBatteryDetect ( void   )

Enable the battery detect pin of the chip.

Default is enabled.

Returns:
true if successful, otherwise false.

Definition at line 811 of file bq27441.cpp.

bool enableGauge ( bool  isSlow = false )

Switch on the battery gauge.

Battery gauging must be switched on for the battery capacity and percentage readings to be valid. The chip will consume more when battery gauging is switched on.

Parameters:
isSlowset this to true to save power if the battery current is not fluctuating very much.
Returns:
true if successful, otherwise false.

Definition at line 684 of file bq27441.cpp.

bool getCurrent ( int32_t *  pCurrentMA )

Read the current flowing from the battery.

Negative value means battery is being drained Positive value means battery is being charged If battery gauging is off this function will take ~1 second to return while the ADCs are activated and the reading is taken. If battery gauging is on, the last current reading taken will be returned without delay.

Parameters:
pCurrentMAplace to put the current reading.
Returns:
true if successful, otherwise false.

Definition at line 1001 of file bq27441.cpp.

bool getPower ( int32_t *  pPowerMW )

Read the power output the battery.

If battery gauging is off this function will take ~1 second to return while the ADCs are activated and the reading is taken. If battery gauging is on, the last voltage reading taken will be returned without delay.

Parameters:
pPowerMWplace to put the power reading.
Returns:
true if successful, otherwise false.

Definition at line 968 of file bq27441.cpp.

bool getRemainingCapacity ( int32_t *  pCapacityMAh )

Read the remaining available battery energy.

The battery capacity reading will only be valid if battery gauging is switched on.

Parameters:
pCapacityMAhplace to put the capacity reading.
Returns:
true if successful, otherwise false.

Definition at line 1032 of file bq27441.cpp.

bool getRemainingPercentage ( int32_t *  pBatteryPercent )

Read the state of charge of the battery as a percentage.

The remaining percentage will only be valid if battery gauging is switched on.

Parameters:
pBatteryPercentplace to put the reading.
Returns:
true if successful, otherwise false.

Definition at line 1069 of file bq27441.cpp.

bool getTemperature ( int32_t *  pTemperatureC )

Read the temperature of the BQ27441 chip.

If battery gauging is off this function will take ~1 second to return while the ADCs are activated and the reading is taken. If battery gauging is on, the last temperature reading taken will be returned without delay.

Parameters:
pTemperatureCplace to put the temperature reading.
Returns:
true if successful, otherwise false.

Definition at line 893 of file bq27441.cpp.

bool getTwoBytes ( uint8_t  registerAddress,
uint16_t *  pBytes 
) [protected]

Read two bytes starting at a given address.

Note: gpI2c should be locked before this is called.

Parameters:
registerAddressthe register address to start reading from.
pBytesplace to put the two bytes.
Returns:
true if successful, otherwise false.

Definition at line 62 of file bq27441.cpp.

bool getTwoBytesSigned ( uint8_t  registerAddress,
int16_t *  pBytes 
) [protected]

Read two bytes starting at a given address (signed int16).

Note: gpI2c should be locked before this is called.

Parameters:
registerAddressthe register address to start reading from.
pBytesplace to put the two bytes.
Returns:
true if successful, otherwise false.

Definition at line 85 of file bq27441.cpp.

bool getVoltage ( int32_t *  pVoltageMV )

Read the voltage of the battery.

If battery gauging is off this function will take ~1 second to return while the ADCs are activated and the reading is taken. If battery gauging is on, the last voltage reading taken will be returned without delay.

Parameters:
pVoltageMVplace to put the voltage reading.
Returns:
true if successful, otherwise false.

Definition at line 932 of file bq27441.cpp.

bool init ( I2C *  pI2c,
uint8_t  address = BATTERY_GAUGE_BQ27441_ADDRESS,
uint16_t  sealCode = SEAL_CODE_DEFAULT 
)

Initialise the BQ27441 chip.

Once initialised the chip is put into its lowest power state. Any API call will awaken the chip from this state and then return it once more to the lowest possible power state.

Parameters:
pI2ca pointer to the I2C instance to use.
address7-bit I2C address of the battery gauge chip.
sealCodethe 16 bit seal code that will unseal the device if it is sealed.
Returns:
true if successful, otherwise false.

Definition at line 610 of file bq27441.cpp.

bool isBatteryDetected ( void   )

Determine whether a battery has been detected or not.

If battery detection is disabled, this function will always return true.

Returns:
true if a battery has been detected, otherwise false.

Definition at line 853 of file bq27441.cpp.

bool isGaugeEnabled ( void   )

Check whether battery gauging is enabled or not.

Returns:
true if battery gauging is enabled, otherwise false.

Definition at line 757 of file bq27441.cpp.

bool isSealed ( void   ) [protected]

Check if the chip is SEALED or UNSEALED.

Note: gpI2c should be locked before this is called.

Returns:
true if it is SEALED, otherwise false.

Definition at line 452 of file bq27441.cpp.

bool makeAdcReading ( void   ) [protected]

Make sure that the chip is awake and has taken a reading.

Note: the function does its own locking of gpI2C so that it isn't held for the entire time we wait for ADC readings to complete.

Returns:
true if successful, otherwise false.

Definition at line 559 of file bq27441.cpp.

bool readExtendedData ( uint8_t  subClassId,
int32_t  offset,
int32_t  length,
char *  pData 
) [protected]

Read data of a given length and class ID.

Note: gpI2c should be locked before this is called.

Parameters:
subClassIdthe sub-class ID of the block.
offsetthe offset of the data within the class.
pDataa place to put the read data.
lengththe size of the place to put the data block.
Returns:
true if successful, otherwise false.

Definition at line 150 of file bq27441.cpp.

bool seal ( void   ) [protected]

Put the chip into SEALED mode.

Note: gpI2c should be locked before this is called.

Returns:
true if successful, otherwise false.

Definition at line 482 of file bq27441.cpp.

bool setHibernate ( void   ) [protected]

Set Hibernate mode.

Note: gpI2c should be locked before this is called.

Returns:
true if successful, otherwise false.

Definition at line 579 of file bq27441.cpp.

bool unseal ( uint16_t  sealCode ) [protected]

Unseal the chip.

Note: gpI2c should be locked before this is called.

Parameters:
sealCodethe 16 bit seal code that will unseal the chip if it is sealed.
Returns:
true if successful, otherwise false.

Definition at line 519 of file bq27441.cpp.

bool writeExtendedData ( uint8_t  subClassId,
int32_t  offset,
int32_t  length,
const char *  pData 
) [protected]

Write an extended data block.

Note: gpI2c should be locked before this is called.

Parameters:
subClassIdthe sub-class ID of the block.
offsetthe offset of the data within the class.
pDataa pointer to the data to be written.
lengththe size of the data to be written.
Returns:
true if successful, otherwise false.

Definition at line 262 of file bq27441.cpp.


Field Documentation

uint8_t gAddress [protected]

The address of the device.

Definition at line 242 of file battery_gauge_bq27441.h.

bool gGaugeOn [protected]

Flag to indicate that monitor mode is active.

Definition at line 248 of file battery_gauge_bq27441.h.

I2C* gpI2c [protected]

Pointer to the I2C interface.

Definition at line 240 of file battery_gauge_bq27441.h.

bool gReady [protected]

Flag to indicate device is ready.

Definition at line 246 of file battery_gauge_bq27441.h.

uint16_t gSealCode [protected]

The seal code for the device.

Definition at line 244 of file battery_gauge_bq27441.h.