@@ -102,17 +102,42 @@ bool nicla::enterShipMode()
102
102
103
103
bool nicla::enableCharge (uint8_t mA , bool disableNtc)
104
104
{
105
+ /*
106
+ The ICHRG is calculated using the following equation:
107
+ - If ICHRG_RANGE (Bit 7) is 0, then ICHRG = 5 mA + ICHRGCODE x 1 mA.
108
+ - If ICHRG_RANGE (Bit 7) is 1, then ICHRG = 40 mA + ICHRGCODE x 10 mA.
109
+ - If a value greater than 35 mA (ICHRG_RANGE = 0) or 300 mA (ICHRG_RANGE = 1) is written,
110
+ the setting goes to 35 mA or 300 mA respectively, except if the ICHRG bits are all 1 (that is, 11111),
111
+ then the externally programmed value is used. See section 9.6.4 in the datasheet.
112
+ */
113
+
114
+ if (mA > 300 ) {
115
+ mA = 300 ;
116
+ }
105
117
106
118
if (mA < 5 ) {
107
- _fastChargeRegisterData = 0x3 ;
108
- } else if (mA < 35 ) {
109
- _fastChargeRegisterData = ((mA -5 ) << 2 );
119
+ mA = 5 ;
120
+ }
121
+
122
+ if (mA > 35 && mA < 40 ) {
123
+ mA = 35 ;
124
+ }
125
+
126
+ if (mA <= 35 ) {
127
+ // Values 5 mA to 35 mA
128
+ _fastChargeRegisterData = ((mA -5 ) << 2 ); // e.g. 20mA - 5mA = 15mA << 2 -> 0b00111100
110
129
} else {
130
+ // Values 40 mA to 300 mA
131
+ // e.g. (200mA - 40mA) / 10 = 16mA << 2 -> 0b01000000 | 0x80 -> 0b11000000
111
132
_fastChargeRegisterData = (((mA -40 )/10 ) << 2 ) | 0x80 ;
112
133
}
134
+
113
135
_pmic.writeByte (BQ25120A_ADDRESS, BQ25120A_FAST_CHG, _fastChargeRegisterData);
114
136
115
- // For very depleted batteries, set ULVO at the very minimum (2.2V) to re-enable charging
137
+ // For very depleted batteries, set BULVO to the very minimum to re-enable charging.
138
+ // 2.2V or 2.0V are the minimum values for BULVO. The latter is not mentioned in the datasheet
139
+ // but it looks like a typo since 2.2V is mentioned twice. See: Table 22 in the datasheet.
140
+ // Also sets the input current limit to 350mA.
116
141
_pmic.writeByte (BQ25120A_ADDRESS, BQ25120A_ILIM_UVLO_CTRL, 0x3F );
117
142
118
143
_ntcEnabled = !disableNtc;
@@ -122,9 +147,6 @@ bool nicla::enableCharge(uint8_t mA, bool disableNtc)
122
147
_pmic.writeByte (BQ25120A_ADDRESS, BQ25120A_TS_CONTROL, 0 );
123
148
}
124
149
125
- // also set max battery voltage to 4.2V (VBREG)
126
- // _pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_BATTERY_CTRL, (4.2f - 3.6f)*100);
127
-
128
150
return _pmic.getFastChargeControlRegister () == _fastChargeRegisterData;
129
151
}
130
152
0 commit comments