Hi There,
I Have used this fuelgague library ->> https://github.com/sparkfun/SparkFun_BQ27441_Arduino_Library
and done some modification related to battery detection using software instead of bat_detect pin.
I use ITPOR flag to detect ehether gauge is already configured or not if configured then i do not configure it again.
problem A)
1 -> Setup is like, battery is not connected to my device, i plug in usb power to power up my device, device starts runing and first checks for ITPOR Flag in fuelgauge and in this case it shows fuelgague was power on reset which is expected then i configure design capacity, design energy, terminate voltage, tapper rate.
2 -> Configured opconf register to disable Battery Detection over BIN PIN Status, instead i read current value if it's zero battery is not connected and then i write BAT_REMOVE to make BAT_DETECT flag = 0 command and if current is not zero i write BAT_INSERT command to make BT_DETECT = 1.
3-> i now connect battery to device.
4-> now software read battery soc, before reading battery soc software checks for battery insertion logic. in this case battery is connected and software writes BAT_INSERT command. and it shows 100% which is in correct, below are the logs.
STEP1
PAL: Initialization Success
ADF: Rf_Ver = 7, Rf_Freq = 673000000
GPS: $PMTK220,100*2F
GPS: $PMTK314,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28
GPIO: GPIO INTR/TLMM Registered
GPIO\LED: LED Registered
ADC: ADC Registered
PWM: PWM Registered
HSU: HSUART Registered
HSU\SMODEM: Ubiik module Registered
I2C: I2C Registered
I2C\TEMP1075: Temperature Sensor Registered
I2C\GPIO_EXP: GPIO Expander Registered
I2C\USB_PD: USB PD Registered
I2C\BCG: Battery Charger Registered
I2C\IMU: IMU Registered
I2C\FUEL_GAUGE: fuelgauge Registered
SPI: SPI Registered
SPI\RF_SYNTH: RF Synthesizer Module Registered
WLAN: WLAN Registered
Net: Net Registered
FwUp: Firmware Upgrade Registered
GPS: GPS Registered
BLE: BLE Registered
FS: FS Registered
I2C\FUEL_GAUGE: FG: OP Config Data=25f8
I2C\FUEL_GAUGE: FG: Control Status=c
I2C\FUEL_GAUGE: FG: flag = 127
I2C\FUEL_GAUGE: FG: Flags Status=127
I2C\FUEL_GAUGE: FG: Init FG For First Time : ITPOR Is Set
I2C\FUEL_GAUGE: FG: Init FG For First Time : Battery Capacity Is Set To 1340
I2C\FUEL_GAUGE: FG: Set Capacity = 10500(2904), msb = 29, lsb =4
I2C\FUEL_GAUGE: FG: Set Energy = 38850(97c2), msb = 97, lsb =C2
I2C\FUEL_GAUGE: FG: Set Term Volt = 3300(ce4), msb = C, lsb =E4
I2C\FUEL_GAUGE: FG: Set Tap Rate = 913(391), msb = 3, lsb =91
I2C\FUEL_GAUGE: FG: Sealed Status = 0
I2C\FUEL_GAUGE: FG: #2 Step To Send SET_CFGUPFDATE
I2C\FUEL_GAUGE: FG: #3 Step To confirm CFGUPDATE mode by Flags Register :
FUEL_GAUGE: FG: flag = 137
I2C\FUEL_GAUGE: CFGUPMODE Flag IS up.
I2C\FUEL_GAUGE: FG: #4 Write 0x00 Using BlockDataControl() Command (0x61) To Enable Block Data Memory Control.
I2C\FUEL_GAUGE: FG: #5 Write 0x52 using the DataBlockClass() command (0x3E) to access the State subclass (82 decimal, 0x52 hex) containing the Design Capacity parameter.
I2C\FUEL_GAUGE: FG: #6 Write the block offset location using DataBlock() command (0x3F).
I2C\FUEL_GAUGE: FG: #7b Read Block And Calculate Checksum, FG: Old Caclulated Checkcsum : 172(AC).
I2C\FUEL_GAUGE: FG: #7a Read the 1-byte checksum using the BlockDataChecksum() command (0x60), FG: Read Old CheckSum :172(AC).
I2C\FUEL_GAUGE: FG: #8 Read both Design Capacity bytes starting at 0x4A (offset = 10):
I2C\FUEL_GAUGE: FG: Current Design Capacity = 1340(53C)
I2C\FUEL_GAUGE: FG: Current Design Energy = 4960(1360)
I2C\FUEL_GAUGE: FG: Current Design Term Vol = 1340(53C)
I2C\FUEL_GAUGE: FG: Current Design Tap Rate = 3200(C80)
I2C\FUEL_GAUGE: FG: #9 Write both Design Capacity bytes starting at 0x4A (offset = 10).
I2C\FUEL_GAUGE: FG: New Caclulated Checkcsum : 136(88).
I2C\FUEL_GAUGE: FG: New Caclulated Checkcsum : 192(C0).
I2C\FUEL_GAUGE: FG: New Caclulated Checkcsum : 60(3C).
I2C\FUEL_GAUGE: FG: New Caclulated Checkcsum : 218(DA).
I2C\FUEL_GAUGE: FG: New Caclulated Checkcsum : 211(D3).
I2C\FUEL_GAUGE: FG: New Caclulated Checkcsum : 43(2B).
I2C\FUEL_GAUGE: FG: New Caclulated Checkcsum : 52(34).
I2C\FUEL_GAUGE: FG: New Caclulated Checkcsum : 35(23).
I2C\FUEL_GAUGE: FG: #10 Read Block And Calculate Checksum, New Caclulated Checkcsum : 35(23).
I2C\FUEL_GAUGE: FG: #11 Write Newly Calculated Checksum.
STEP 2 - > opconf updated to disable BIE and( manually detect battery using current consuption).
I2C\FUEL_GAUGE: FG: OP Config Read Only Data=25f8
I2C\FUEL_GAUGE: FG: Read Old CheckSum :106(6A).
I2C\FUEL_GAUGE: FG: Old Caclulated Checkcsum : 106(6A).
I2C\FUEL_GAUGE: FG: Current OpconfigB Settings =0xf
I2C\FUEL_GAUGE: FG: Current Opconfig Settings =0x25f8
I2C\FUEL_GAUGE: FG: Set Opconfig Settings =0x5d8
I2C\FUEL_GAUGE: After Set Opconfig Setting =0x5d8
I2C\FUEL_GAUGE: FG: #10 Read Block And Calculate Checksum, New Caclulated Checkcsum : 170(AA).
I2C\FUEL_GAUGE: FG: #11 Write Newly Calculated Checksum.
I2C\FUEL_GAUGE: FG: #12 Exit CFGUPDATE mode by sending SOFT_RESET subcommand, Control(0x0042).
I2C\FUEL_GAUGE: FG: #13 Step To confirm CFGUPDATE mode by Flags Register :
FUEL_GAUGE: FG: flag = 117
I2C\FUEL_GAUGE: FG: flag = 0
I2C\FUEL_GAUGE: FG: Timeout = 1
I2C\FUEL_GAUGE: FG:#14 Step If the device was previously SEALED, return to SEALED mode by sending the Control(0x0020) subcommand.
I2C\FUEL_GAUGE: FG: FG Config Is Done
>>>>> "Battery Detection Logic Based On Current Consuption at this moment battery is not inserted, as it can be infered from log"
I2C\FUEL_GAUGE: FG: Battery is not Charging and not Discharging
I2C\FUEL_GAUGE: FG: Battery Removed
Command List:
Commands:
0. Ver
1. Help
2. Exit
3. Heap
4. CPU
5. Stack
6. Reset
Subgroups:
7. GPIO
8. ADC
9. PWM
10. HSU
11. I2C
12. SPI
13. WLAN
14. Net
15. FwUp
16. GPS
17. BLE
18. FS
> 11
I2C> 19
I2C\FUEL_GAUGE> 8 5
I2C\FUEL_GAUGE: I2c FuelGauge Control status Reg 0x06 = 200c
I2C\FUEL_GAUGE: I2c FuelGauge Control VOK = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control RUP_DIS = 1
I2C\FUEL_GAUGE: I2c FuelGauge Control LDMD = 1
I2C\FUEL_GAUGE: I2c FuelGauge Control Sleep = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control Hibernate = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control INITCOMP = 0 ->> due to BAT_REMOVE command.
I2C\FUEL_GAUGE: I2c FuelGauge Control RES_UP = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control QMAX_UP = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control BCA = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control CCA = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control CALMODE = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control Sealed State = 1
I2C\FUEL_GAUGE: I2c FuelGauge Control WDRESET = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control SHUTDOWN = 0
I2C\FUEL_GAUGE> 8 4
I2C\FUEL_GAUGE: FG: flag = 0 -> means no battery detected
I2C\FUEL_GAUGE> 8 3
I2C\FUEL_GAUGE: I2c FuelGauge opConfig Reg 0x3A = 5d8
I2C\FUEL_GAUGE: I2c FuelGauge opConfig Temperature Source = 0
I2C\FUEL_GAUGE: I2c FuelGauge opConfig Battery Low status = 0
I2C\FUEL_GAUGE: I2c FuelGauge opConfig RM set status= 1
I2C\FUEL_GAUGE: I2c FuelGauge opConfig Sleep enable/disable = 0
I2C\FUEL_GAUGE: I2c FuelGauge opConfig GPOUT active high/low = 0
I2C\FUEL_GAUGE: I2c FuelGauge opConfig Pullup Resistor internal/external = 0
I2C\FUEL_GAUGE: I2c FuelGauge opConfig Battery Insertion enalbe/disable = 0
Battery Is Connected Now
I2C\FUEL_GAUGE> 8 2
I2C\FUEL_GAUGE: FG: Battery is Charging
I2C\FUEL_GAUGE: FG: Battery Inserted
I2C\FUEL_GAUGE: FG: Bat % - 100%
I2C\FUEL_GAUGE: FG: Bat Temp Kel - 302.20K
I2C\FUEL_GAUGE: FG: Bat Temp Cel - 29.05C
I2C\FUEL_GAUGE: FG: Bat Current - 182mA
I2C\FUEL_GAUGE: FG: Bat Voltage - 4187mV
I2C\FUEL_GAUGE: FG: Bat Avail Cap - 9126mAh
I2C\FUEL_GAUGE: FG: Bat Rem Cap - 10500mAh
I2C\FUEL_GAUGE: FG: Bat Full Cap - 10500mAh
I2C\FUEL_GAUGE: FG: Bat Design Cap - 10500mAh
I2C\FUEL_GAUGE: FG: Battery is Charging
I2C\FUEL_GAUGE: FG: Battery Inserted
I2C\FUEL_GAUGE: FG: Bat % - 100%
I2C\FUEL_GAUGE> 8 5
I2C\FUEL_GAUGE: I2c FuelGauge Control status Reg 0x06 = 208e
I2C\FUEL_GAUGE: I2c FuelGauge Control VOK = 1
I2C\FUEL_GAUGE: I2c FuelGauge Control RUP_DIS = 1
I2C\FUEL_GAUGE: I2c FuelGauge Control LDMD = 1
I2C\FUEL_GAUGE: I2c FuelGauge Control Sleep = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control Hibernate = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control INITCOMP = 1
I2C\FUEL_GAUGE: I2c FuelGauge Control RES_UP = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control QMAX_UP = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control BCA = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control CCA = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control CALMODE = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control Sealed State = 1
I2C\FUEL_GAUGE: I2c FuelGauge Control WDRESET = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control SHUTDOWN = 0
I2C\FUEL_GAUGE> 8 4
I2C\FUEL_GAUGE: FG: flag = 88
I2C\FUEL_GAUGE: I2c FuelGauge Flags Reg 0x06 = 88
I2C\FUEL_GAUGE: I2c FuelGauge Flags Discharging Detected = 0
I2C\FUEL_GAUGE: I2c FuelGauge Flags SOCF = 0
I2C\FUEL_GAUGE: I2c FuelGauge Flags SOC1 = 0
I2C\FUEL_GAUGE: I2c FuelGauge Flags BAT_DET = 1
I2C\FUEL_GAUGE: I2c FuelGauge Flags ITPOR = 0
I2C\FUEL_GAUGE: I2c FuelGauge Flags OCVTAKEN = 1
I2C\FUEL_GAUGE: I2c FuelGauge Flags Fast Charging allowed = 0
I2C\FUEL_GAUGE: I2c FuelGauge Flags Full charge detected = 0
I2C\FUEL_GAUGE: I2c FuelGauge Flags Under Temp Detection = 0
I2C\FUEL_GAUGE: I2c FuelGauge Flags Over Temp detection = 0
Second observation
now i remove usb power and from below command current direction is changing means it is drawing power from battery. and tried to read battery soc multiple time but it shows 100%
I2C\FUEL_GAUGE> 8 2
I2C\FUEL_GAUGE: FG: Battery is Discharging
I2C\FUEL_GAUGE: FG: Battery Inserted
I2C\FUEL_GAUGE: FG: Bat % - 100%
I2C\FUEL_GAUGE: FG: Bat Temp Kel - 301.90K
I2C\FUEL_GAUGE: FG: Bat Temp Cel - 28.75C
I2C\FUEL_GAUGE: FG: Bat Current - -72mA
I2C\FUEL_GAUGE: FG: Bat Voltage - 4021mV
I2C\FUEL_GAUGE: FG: Bat Avail Cap - 9126mAh
I2C\FUEL_GAUGE: FG: Bat Rem Cap - 9154mAh
I2C\FUEL_GAUGE: FG: Bat Full Cap - 9155mAh
I2C\FUEL_GAUGE: FG: Bat Design Cap - 10500mAh
I2C\FUEL_GAUGE: FG: Battery is Discharging
I2C\FUEL_GAUGE: FG: Battery Inserted
I2C\FUEL_GAUGE: FG: Bat % - 100%
now even when battery is connected i fired BAT_REMOVE command and then BAT_INSERT Command Manually.
I2C\FUEL_GAUGE> 10
I2C\FUEL_GAUGE: FG: Battery Removed
I2C\FUEL_GAUGE: FG: flag = 80
FUEL_GAUGE: I2c FuelGauge Flags Reg 0x06 = 80
I2C\FUEL_GAUGE: I2c FuelGauge Flags Discharging Detected = 0
I2C\FUEL_GAUGE: I2c FuelGauge Flags [BAT_DET] = 0
I2C\FUEL_GAUGE: I2c FuelGauge Flags [ITPOR] = 0
FUEL_GAUGE: I2c FuelGauge Control status Reg 0x00 = 200c
I2C\FUEL_GAUGE: I2c FuelGauge Control Sleep = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control Hibernate = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control [INITCOMP] = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control RES_UP = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control QMAX_UP = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control Sealed State = 1
I2C\FUEL_GAUGE: I2c FuelGauge Control SHUTDOWN = 0
FUEL_GAUGE: I2c FuelGauge opConfig Reg 0x3A = 5d8
I2C\FUEL_GAUGE: I2c FuelGauge opConfig Temperature Source = 0
I2C\FUEL_GAUGE: I2c FuelGauge opConfig Battery Low status = 0
I2C\FUEL_GAUGE: I2c FuelGauge opConfig RM set status= 1
I2C\FUEL_GAUGE: I2c FuelGauge opConfig Sleep enable/disable = 0
I2C\FUEL_GAUGE: I2c FuelGauge opConfig GPOUT active high/low = 0
I2C\FUEL_GAUGE: I2c FuelGauge opConfig Pullup Resistor internal/external = 0
I2C\FUEL_GAUGE: I2c FuelGauge opConfig Battery Insertion enalbe/disable [BIE] = 0
I2C\FUEL_GAUGE> 9
I2C\FUEL_GAUGE: FG: Battery Inserted
I2C\FUEL_GAUGE: FG: flag = 188
FUEL_GAUGE: I2c FuelGauge Flags Reg 0x06 = 188
I2C\FUEL_GAUGE: I2c FuelGauge Flags Discharging Detected = 0
I2C\FUEL_GAUGE: I2c FuelGauge Flags [BAT_DET] = 1
I2C\FUEL_GAUGE: I2c FuelGauge Flags [ITPOR] = 0
FUEL_GAUGE: I2c FuelGauge Control status Reg 0x00 = 200e
I2C\FUEL_GAUGE: I2c FuelGauge Control Sleep = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control Hibernate = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control [INITCOMP] = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control RES_UP = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control QMAX_UP = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control Sealed State = 1
I2C\FUEL_GAUGE: I2c FuelGauge Control SHUTDOWN = 0
FUEL_GAUGE: I2c FuelGauge opConfig Reg 0x3A = 5d8
I2C\FUEL_GAUGE: I2c FuelGauge opConfig Temperature Source = 0
I2C\FUEL_GAUGE: I2c FuelGauge opConfig Battery Low status = 0
I2C\FUEL_GAUGE: I2c FuelGauge opConfig RM set status= 1
I2C\FUEL_GAUGE: I2c FuelGauge opConfig Sleep enable/disable = 0
I2C\FUEL_GAUGE: I2c FuelGauge opConfig GPOUT active high/low = 0
I2C\FUEL_GAUGE: I2c FuelGauge opConfig Pullup Resistor internal/external = 0
I2C\FUEL_GAUGE: I2c FuelGauge opConfig Battery Insertion enalbe/disable [BIE] = 0
Read Control Status Just to check INITCOMP =1 before reading soc
I2C\FUEL_GAUGE> 8 5
I2C\FUEL_GAUGE: I2c FuelGauge Control status Reg 0x06 = 208e
I2C\FUEL_GAUGE: I2c FuelGauge Control VOK = 1
I2C\FUEL_GAUGE: I2c FuelGauge Control RUP_DIS = 1
I2C\FUEL_GAUGE: I2c FuelGauge Control LDMD = 1
I2C\FUEL_GAUGE: I2c FuelGauge Control Sleep = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control Hibernate = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control INITCOMP = 1
I2C\FUEL_GAUGE: I2c FuelGauge Control RES_UP = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control QMAX_UP = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control BCA = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control CCA = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control CALMODE = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control Sealed State = 1
I2C\FUEL_GAUGE: I2c FuelGauge Control WDRESET = 0
I2C\FUEL_GAUGE: I2c FuelGauge Control SHUTDOWN = 0
Now I read Battery SOC and it show correct value
I2C\FUEL_GAUGE> 8 2
I2C\FUEL_GAUGE: FG: Battery is Discharging
I2C\FUEL_GAUGE: FG: Battery Inserted
I2C\FUEL_GAUGE: FG: Bat % - 93%
I2C\FUEL_GAUGE: FG: Bat Temp Kel - 300.40K
I2C\FUEL_GAUGE: FG: Bat Temp Cel - 27.25C
I2C\FUEL_GAUGE: FG: Bat Current - -69mA
I2C\FUEL_GAUGE: FG: Bat Voltage - 3981mV
I2C\FUEL_GAUGE: FG: Bat Avail Cap - 8437mAh
I2C\FUEL_GAUGE: FG: Bat Rem Cap - 8437mAh
I2C\FUEL_GAUGE: FG: Bat Full Cap - 9126mAh
I2C\FUEL_GAUGE: FG: Bat Design Cap - 10500mAh
I2C\FUEL_GAUGE: FG: Battery is Discharging
I2C\FUEL_GAUGE: FG: Battery Inserted
I2C\FUEL_GAUGE: FG: Bat % - 93%
Question 1: When To configure fuel gauge? based on ITPOR bit or based on Battery detection?
Question 2: In any case if i configure fuelgauge while USB power is on and BATTERY connected, soc always reads 100%, is this correct behaviour? if not how to avoid this?