Skip to content

Commit d9732f9

Browse files
committed
nicla: also disable NTC stopping charge via API
1 parent fc7b392 commit d9732f9

File tree

2 files changed

+59
-12
lines changed

2 files changed

+59
-12
lines changed

libraries/Nicla_System/src/Nicla_System.cpp

+45-9
Original file line numberDiff line numberDiff line change
@@ -104,34 +104,70 @@ uint8_t nicla::readLDOreg()
104104
return _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_LDO_CTRL);
105105
}
106106

107-
bool nicla::enableCharge(uint8_t mA)
107+
bool nicla::ntc_disabled;
108+
109+
bool nicla::enableCharge(uint8_t mA, bool disable_ntc)
108110
{
109-
if (mA < 35) {
111+
if (mA < 5) {
112+
_chg_reg = 0x3;
113+
} else if (mA < 35) {
110114
_chg_reg = ((mA-5) << 2);
111115
} else {
112116
_chg_reg = (((mA-40)/10) << 2) | 0x80;
113117
}
114118
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_FAST_CHG, _chg_reg);
115119

116-
// For very depleted batteries, set ULVO at the vary minimum to re-enable charging
120+
// For very depleted batteries, set ULVO at the very minimum to re-enable charging
117121
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_ILIM_UVLO_CTRL, 0x3F);
118122

123+
// Disable TS and interrupt on charge
124+
ntc_disabled = disable_ntc;
125+
if (ntc_disabled) {
126+
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_TS_CONTROL, 1 << 3);
127+
}
128+
119129
// also set max battery voltage to 4.2V (VBREG)
120130
// _pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_BATTERY_CTRL, (4.2f - 3.6f)*100);
121131

122132
return _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_FAST_CHG) == _chg_reg;
123133
}
124134

125-
uint8_t nicla::getFault() {
126-
return _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_FAULTS);
135+
uint16_t nicla::getFault() {
136+
uint16_t tmp = _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_FAULTS) << 8;
137+
tmp |= (_pmic.readByte(BQ25120A_ADDRESS, BQ25120A_TS_CONTROL) & 0x60);
138+
return tmp;
127139
}
128140

129-
130-
float nicla::getBetteryStatus() {
141+
int nicla::getBatteryStatus() {
131142
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_BATT_MON, 1);
132-
delay(2);
143+
delay(3);
133144
uint8_t data = _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_BATT_MON);
134-
float res = 0.6f + (data >> 5) * 0.1f + (data >> 2) * 0.02f - 0.01f;
145+
float percent = 0.6f + (data >> 5) * 0.1f + ((data >> 2) & 0x7) * 0.02f;
146+
147+
int res = 0;
148+
if (percent >= 0.98) {
149+
res |= BATTERY_FULL;
150+
} else if (percent >= 0.94){
151+
res |= BATTERY_ALMOST_FULL;
152+
} else if (percent >= 0.90){
153+
res |= BATTERY_HALF;
154+
} else if (percent >= 0.86){
155+
res |= BATTERY_ALMOST_EMPTY;
156+
} else {
157+
res |= BATTERY_EMPTY;
158+
}
159+
160+
if (!ntc_disabled) {
161+
auto ts = ((_pmic.readByte(BQ25120A_ADDRESS, BQ25120A_TS_CONTROL) >> 5) & 0x3);
162+
if (ts == 1) {
163+
res |= BATTERY_COLD;
164+
} else if (ts == 2) {
165+
res |= BATTERY_COOL;
166+
} else if (ts == 3) {
167+
res |= BATTERY_HOT;
168+
}
169+
}
170+
135171
return res;
136172
}
137173

libraries/Nicla_System/src/Nicla_System.h

+14-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@
1010

1111
#define USE_FASTCHG_TO_KICK_WATCHDOG 1
1212

13+
#define BATTERY_FULL 5
14+
#define BATTERY_ALMOST_FULL 4
15+
#define BATTERY_HALF 3
16+
#define BATTERY_ALMOST_EMPTY 2
17+
#define BATTERY_EMPTY 1
18+
#define BATTERY_COLD (1 << 4)
19+
#define BATTERY_COOL (2 << 4)
20+
#define BATTERY_HOT (3 << 4)
21+
#define BATTERY_CHARGING (1 << 7)
22+
1323
class nicla {
1424

1525
public:
@@ -19,9 +29,10 @@ class nicla {
1929
static bool disableLDO();
2030
static bool enterShipMode();
2131
static uint8_t readLDOreg();
22-
static bool enableCharge(uint8_t mA = 20);
23-
static float getBetteryStatus();
24-
static uint8_t getFault();
32+
static bool enableCharge(uint8_t mA = 20, bool disable_ntc = true);
33+
static int getBatteryStatus();
34+
static uint16_t getFault();
35+
static bool ntc_disabled;
2536

2637
static RGBled leds;
2738
static BQ25120A _pmic;

0 commit comments

Comments
 (0)