@@ -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
0 commit comments