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