Skip to content

Commit 702aae9

Browse files
committed
nicla-system: Add documentation to charging function.
1 parent 1ce192d commit 702aae9

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
@@ -105,17 +105,42 @@ bool nicla::enterShipMode()
105105

106106
bool nicla::enableCharge(uint8_t mA, bool disableNtc)
107107
{
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+
}
108120

109121
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
113132
} else {
133+
// Values 40 mA to 300 mA
134+
// e.g. (200mA - 40mA) / 10 = 16mA << 2 -> 0b01000000 | 0x80 -> 0b11000000
114135
_fastChargeRegisterData = (((mA-40)/10) << 2) | 0x80;
115136
}
137+
116138
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_FAST_CHG, _fastChargeRegisterData);
117139

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

121146
_ntcEnabled = !disableNtc;
@@ -125,9 +150,6 @@ bool nicla::enableCharge(uint8_t mA, bool disableNtc)
125150
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_TS_CONTROL, 0);
126151
}
127152

128-
// also set max battery voltage to 4.2V (VBREG)
129-
// _pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_BATTERY_CTRL, (4.2f - 3.6f)*100);
130-
131153
return _pmic.getFastChargeControlRegister() == _fastChargeRegisterData;
132154
}
133155

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)