Skip to content

Improvements to code #14

Description

@wystewart

recommend the following be incorporated into the code to improve it.

In BQ27441::i2cReadBytes and BQ27441::i2cWriteBytes, add delayMicroseconds(66); before return so that the chip has enough time to be ready for the next transaction. This will improve communication reliability

add function to read extended data
uint16_t BQ27441::readExtendedData16(uint8_t classID, uint8_t offset)

{
	uint16_t retData = 0;
    uint8_t *data = (uint8_t *)&retData;

	if (!_userConfigControl) enterConfig(false);
		
	if (!blockDataControl()) // // enable block data memory control
		return false; // Return false if enable fails
	if (!blockDataClass(classID)) // Write class ID using DataBlockClass()
		return false;
	
	blockDataOffset(offset / 32); // Write 32-bit block offset (usually 0)
	
	//computeBlockChecksum(); // Compute checksum going in
	uint8_t oldCsum = blockDataChecksum();
	/*for (int i=0; i<32; i++)
		Serial.print(String(readBlockData(i)) + " ");*/
	data[1] = readBlockData(offset % 32); // Read from offset (limit to 0-31)
	data[0] = readBlockData((offset % 32) + 1); // Read from offset (limit to 0-31)

	if (!_userConfigControl) exitConfig();
	
	return retData;
}

add hard reset function

// Issue a soft-reset to the BQ27441-G1A
bool BQ27441::hardReset(void)
{
	bool ret;

	if (sealed())
	{
		_sealFlag = true;
		unseal(); // Must be unsealed before making changes
	}

	ret = executeControlWord(BQ27441_CONTROL_RESET);

	if (_sealFlag) seal(); // Seal back up if we IC was sealed coming in

	return ret;
}

add function to read capacity

uint16_t BQ27441::getCapacity(void)
{
	return readExtendedData16(BQ27441_ID_STATE, 10);	
}

improve unseal by presenting the read between the second write in ReadcontrolWord. Not sure this is really necessary but I changed it in my version of the code.

// Unseal the BQ27441-G1A
bool BQ27441::unseal(void)
{
	// To unseal the BQ27441, write the key to the control
	// command. Then immediately write the same key to control again.
	//if (readControlWord(BQ27441_UNSEAL_KEY))
	//{
	//	return readControlWord(BQ27441_UNSEAL_KEY);
	//}
	//return false;
    uint16_t function = BQ27441_UNSEAL_KEY;
	uint8_t subCommandMSB = (function >> 8);
	uint8_t subCommandLSB = (function & 0x00FF);
	uint8_t command[2] = {subCommandLSB, subCommandMSB};
	uint8_t data[2] = {0, 0};

	i2cWriteBytes((uint8_t) 0, command, 2);
	i2cWriteBytes((uint8_t) 0, command, 2);
	return true;
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions