Skip to content

Commit e6ff5c5

Browse files
committed
nicla-system: Add documentation to charging function.
1 parent 0708980 commit e6ff5c5

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

libraries/Nicla_System/src/Nicla_System.cpp

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,42 @@ bool nicla::enterShipMode()
102102

103103
bool nicla::enableCharge(uint8_t mA, bool disableNtc)
104104
{
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+
}
105117

106118
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
110129
} else {
130+
// Values 40 mA to 300 mA
131+
// e.g. (200mA - 40mA) / 10 = 16mA << 2 -> 0b01000000 | 0x80 -> 0b11000000
111132
_fastChargeRegisterData = (((mA-40)/10) << 2) | 0x80;
112133
}
134+
113135
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_FAST_CHG, _fastChargeRegisterData);
114136

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.
116141
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_ILIM_UVLO_CTRL, 0x3F);
117142

118143
_ntcEnabled = !disableNtc;
@@ -122,9 +147,6 @@ bool nicla::enableCharge(uint8_t mA, bool disableNtc)
122147
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_TS_CONTROL, 0);
123148
}
124149

125-
// also set max battery voltage to 4.2V (VBREG)
126-
// _pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_BATTERY_CTRL, (4.2f - 3.6f)*100);
127-
128150
return _pmic.getFastChargeControlRegister() == _fastChargeRegisterData;
129151
}
130152

libraries/Nicla_System/src/Nicla_System.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ class nicla {
5151
* @return true if the ship mode is entered successfully.
5252
*/
5353
static bool enterShipMode();
54+
55+
/**
56+
* @brief Enables fast charging of the battery.
57+
*
58+
* @param mA The desired milliampere (mA) charging current. The default is 20mA.
59+
* @param disableNtc Whether to disable Temperature Sense and interrupt on charge. The default is true.
60+
* @return true If the fast charging is enabled successfully. False, otherwise.
61+
*/
5462
static bool enableCharge(uint8_t mA = 20, bool disableNtc = true);
5563

5664
/**

0 commit comments

Comments
 (0)