Skip to content

Commit 949c70c

Browse files
authored
Merge pull request arduino#588 from arduino/nicla_sense_battery
Fix Nicla Sense battery charge
2 parents 9890d87 + d9732f9 commit 949c70c

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

libraries/Nicla_System/src/Nicla_System.cpp

+58-3
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,73 @@ 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-
digitalWrite(p25, LOW);
110-
if (mA < 35) {
111+
if (mA < 5) {
112+
_chg_reg = 0x3;
113+
} else if (mA < 35) {
111114
_chg_reg = ((mA-5) << 2);
112115
} else {
113116
_chg_reg = (((mA-40)/10) << 2) | 0x80;
114117
}
115118
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_FAST_CHG, _chg_reg);
119+
120+
// For very depleted batteries, set ULVO at the very minimum to re-enable charging
121+
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_ILIM_UVLO_CTRL, 0x3F);
122+
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+
129+
// also set max battery voltage to 4.2V (VBREG)
130+
// _pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_BATTERY_CTRL, (4.2f - 3.6f)*100);
131+
116132
return _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_FAST_CHG) == _chg_reg;
117133
}
118134

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;
139+
}
140+
141+
int nicla::getBatteryStatus() {
142+
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_BATT_MON, 1);
143+
delay(3);
144+
uint8_t data = _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_BATT_MON);
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+
171+
return res;
172+
}
173+
119174
void nicla::checkChgReg()
120175
{
121176
if (_chg_reg != _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_FAST_CHG)) {

libraries/Nicla_System/src/Nicla_System.h

+14-1
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,7 +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);
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;
2336

2437
static RGBled leds;
2538
static BQ25120A _pmic;

0 commit comments

Comments
 (0)