Skip to content

Fix Nicla Sense battery charge #588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 58 additions & 3 deletions libraries/Nicla_System/src/Nicla_System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,73 @@ uint8_t nicla::readLDOreg()
return _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_LDO_CTRL);
}

bool nicla::enableCharge(uint8_t mA)
bool nicla::ntc_disabled;

bool nicla::enableCharge(uint8_t mA, bool disable_ntc)
{
digitalWrite(p25, LOW);
if (mA < 35) {
if (mA < 5) {
_chg_reg = 0x3;
} else if (mA < 35) {
_chg_reg = ((mA-5) << 2);
} else {
_chg_reg = (((mA-40)/10) << 2) | 0x80;
}
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_FAST_CHG, _chg_reg);

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

// Disable TS and interrupt on charge
ntc_disabled = disable_ntc;
if (ntc_disabled) {
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_TS_CONTROL, 1 << 3);
}

// also set max battery voltage to 4.2V (VBREG)
// _pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_BATTERY_CTRL, (4.2f - 3.6f)*100);

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

uint16_t nicla::getFault() {
uint16_t tmp = _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_FAULTS) << 8;
tmp |= (_pmic.readByte(BQ25120A_ADDRESS, BQ25120A_TS_CONTROL) & 0x60);
return tmp;
}

int nicla::getBatteryStatus() {
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_BATT_MON, 1);
delay(3);
uint8_t data = _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_BATT_MON);
float percent = 0.6f + (data >> 5) * 0.1f + ((data >> 2) & 0x7) * 0.02f;

int res = 0;
if (percent >= 0.98) {
res |= BATTERY_FULL;
} else if (percent >= 0.94){
res |= BATTERY_ALMOST_FULL;
} else if (percent >= 0.90){
res |= BATTERY_HALF;
} else if (percent >= 0.86){
res |= BATTERY_ALMOST_EMPTY;
} else {
res |= BATTERY_EMPTY;
}

if (!ntc_disabled) {
auto ts = ((_pmic.readByte(BQ25120A_ADDRESS, BQ25120A_TS_CONTROL) >> 5) & 0x3);
if (ts == 1) {
res |= BATTERY_COLD;
} else if (ts == 2) {
res |= BATTERY_COOL;
} else if (ts == 3) {
res |= BATTERY_HOT;
}
}

return res;
}

void nicla::checkChgReg()
{
if (_chg_reg != _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_FAST_CHG)) {
Expand Down
15 changes: 14 additions & 1 deletion libraries/Nicla_System/src/Nicla_System.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@

#define USE_FASTCHG_TO_KICK_WATCHDOG 1

#define BATTERY_FULL 5
#define BATTERY_ALMOST_FULL 4
#define BATTERY_HALF 3
#define BATTERY_ALMOST_EMPTY 2
#define BATTERY_EMPTY 1
#define BATTERY_COLD (1 << 4)
#define BATTERY_COOL (2 << 4)
#define BATTERY_HOT (3 << 4)
#define BATTERY_CHARGING (1 << 7)

class nicla {

public:
Expand All @@ -19,7 +29,10 @@ class nicla {
static bool disableLDO();
static bool enterShipMode();
static uint8_t readLDOreg();
static bool enableCharge(uint8_t mA = 20);
static bool enableCharge(uint8_t mA = 20, bool disable_ntc = true);
static int getBatteryStatus();
static uint16_t getFault();
static bool ntc_disabled;

static RGBled leds;
static BQ25120A _pmic;
Expand Down