@@ -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+
119174void nicla::checkChgReg ()
120175{
121176 if (_chg_reg != _pmic.readByte (BQ25120A_ADDRESS, BQ25120A_FAST_CHG)) {
0 commit comments