Lawmate / BQ25703A

Basic library for interfacing with BQ25703A battery management chip from TI
GNU General Public License v3.0
12 stars 5 forks source link

Wrong register read/writes #5

Open BaptisteHudyma opened 3 weeks ago

BaptisteHudyma commented 3 weeks ago

Hello ! Thanks for your work.

I noticed that almost all your registers read and writes are wrong, compared to the documentation (refer to the datasheet), and it breaks the charger logic & working operation.

EXEMPLE: the charge current register :

In the doc the charge current register is written in 2 bytes:

byte 0 bit 0: unused
byte 0 bit 1: unused
byte 0 bit 2: unused
byte 0 bit 3: unused
byte 0 bit 4: unused
byte 0 bit 5: unused
byte 0 bit 6: bit 0 of the current register
byte 0 bit 7: bit 1 of the current register

byte 1 bit 0: bit 2 of the current register
byte 1 bit 1: bit 3 of the current register
byte 1 bit 2: bit 4 of the current register
byte 1 bit 3: bit 5 of the current register
byte 1 bit 4: bit 6 of the current register
byte 1 bit 5: unused
byte 1 bit 6: unused
byte 1 bit 7: unused

You notice that the data is 7 bit, splitted into 2 bytes, offseted by 6 bits. So the steps to write a register are :

So for the current register, if I want to write a 1 Amp charge current, I do the following (naive pseudo code):

// clamp the current value between min and max
uint16 chargeCurrentClamped_mA = clamp(chargeCurrent_mA, 0, 8120);
// divide by the resolution (here 64 mA)
uint chargeData = chargeCurrentClamped / 64;
// apply the 7 bits mask 
chargeData &= 0b01111111;
// offset the result byt the first unused bits
chargeData <<= 6;

// first 8 bits
byte val0 = chargeData;
// last 8 bits
byte val1 = chargeData >> 8;

// send val0 and val1 like you do already ! 

Sorry of the long issue, but it took me several hours to debug the entire library, with a few burned components !

For anyone interested in a fix, I'll publish one when I'll make it cleaner.

Lawmate commented 5 days ago

Hi Baptiste, sorry you couldn't get it working. I haven't actually played with the library or device in over 4 years so can't really remember that specific functionality. I do remember that my device worked as I wanted and that involved doing some things that were opposed to the documentation. Anyway, glad you got yours working. Thanks