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 1 commit
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
21 changes: 20 additions & 1 deletion libraries/Nicla_System/src/Nicla_System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,35 @@ uint8_t nicla::readLDOreg()

bool nicla::enableCharge(uint8_t mA)
{
digitalWrite(p25, LOW);
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 vary minimum to re-enable charging
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_ILIM_UVLO_CTRL, 0x3F);

// 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;
}

uint8_t nicla::getFault() {
return _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_FAULTS);
}


float nicla::getBetteryStatus() {
_pmic.writeByte(BQ25120A_ADDRESS, BQ25120A_BATT_MON, 1);
delay(2);
uint8_t data = _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_BATT_MON);
float res = 0.6f + (data >> 5) * 0.1f + (data >> 2) * 0.02f - 0.01f;
return res;
}

void nicla::checkChgReg()
{
if (_chg_reg != _pmic.readByte(BQ25120A_ADDRESS, BQ25120A_FAST_CHG)) {
Expand Down
2 changes: 2 additions & 0 deletions libraries/Nicla_System/src/Nicla_System.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class nicla {
static bool enterShipMode();
static uint8_t readLDOreg();
static bool enableCharge(uint8_t mA = 20);
static float getBetteryStatus();
static uint8_t getFault();

static RGBled leds;
static BQ25120A _pmic;
Expand Down