@@ -30,10 +30,21 @@ uint8_t BQ25120A::getLDOControlRegister()
30
30
return readByte (BQ25120A_ADDRESS, BQ25120A_LDO_CTRL);
31
31
}
32
32
33
+ bool BQ25120A::runsOnBattery (uint8_t address){
34
+ uint8_t faults = readByteUnprotected (address, BQ25120A_FAULTS);
35
+ // Read VIN under voltage fault (VIN_UV on Bit 6) from the faults register.
36
+ bool runsOnBattery = (faults & 0b01000000 ) != 0 ;
37
+ return runsOnBattery;
38
+ }
39
+
33
40
void BQ25120A::writeByte (uint8_t address, uint8_t subAddress, uint8_t data)
34
41
{
35
- setHighImpedanceModeEnabled (false );
36
42
nicla::_i2c_mutex.lock ();
43
+ // Only enter active mode when runnning on battery.
44
+ // When powered from VIN, driving CD HIGH would disable charging.
45
+ if (runsOnBattery (address)){
46
+ setHighImpedanceModeEnabled (false );
47
+ }
37
48
Wire1.beginTransmission (address);
38
49
Wire1.write (subAddress);
39
50
Wire1.write (data);
@@ -42,19 +53,27 @@ void BQ25120A::writeByte(uint8_t address, uint8_t subAddress, uint8_t data)
42
53
setHighImpedanceModeEnabled (true );
43
54
}
44
55
45
- uint8_t BQ25120A::readByte (uint8_t address, uint8_t subAddress)
46
- {
47
- setHighImpedanceModeEnabled (false );
48
- nicla::_i2c_mutex.lock ();
56
+ uint8_t BQ25120A::readByteUnprotected (uint8_t address, uint8_t subAddress){
49
57
Wire1.beginTransmission (address);
50
58
Wire1.write (subAddress);
51
59
Wire1.endTransmission (false );
52
60
Wire1.requestFrom (address, 1 );
53
61
uint32_t timeout = 100 ;
54
62
uint32_t start_time = millis ();
55
63
while (!Wire1.available () && (millis () - start_time) < timeout) {}
56
- uint8_t ret = Wire1.read ();
57
- nicla::_i2c_mutex.unlock ();
64
+ return Wire1.read ();
65
+ }
66
+
67
+ uint8_t BQ25120A::readByte (uint8_t address, uint8_t subAddress)
68
+ {
69
+ nicla::_i2c_mutex.lock ();
70
+ // Only enter active mode when runnning on battery.
71
+ // When powered from VIN, driving CD HIGH would disable charging.
72
+ if (runsOnBattery (address)){
73
+ setHighImpedanceModeEnabled (false );
74
+ }
75
+ uint8_t ret = readByteUnprotected (address, subAddress);
76
+ nicla::_i2c_mutex.unlock ();
58
77
setHighImpedanceModeEnabled (true );
59
78
return ret;
60
79
}
0 commit comments