SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
AXP2101Charger.cpp
Go to the documentation of this file.
1
31#include "../../../SensorBuildOpt.h"
32#if !SENSORLIB_EXCLUDE_PMIC_AXP2101
33
34#include "AXP2101Charger.hpp"
35#include "AXP2101Regs.hpp"
36
37
39{
40}
41
43{
45}
46
48{
49 return getStatus().charging;
50}
51
53{
54 if (mA > 200) mA = 200;
55 uint8_t code = mA / 25;
56 int regVal = _core.readReg(axp2101_regs::chg::IPRECHG_SET);
57 if (regVal < 0) return false;
58 regVal = (regVal & 0xF0) | code;
59 return _core.writeReg(axp2101_regs::chg::IPRECHG_SET, static_cast<uint8_t>(regVal)) >= 0;
60}
61
63{
65 if (val < 0) return 0;
66 return (val & 0x0F) * 25;
67}
68
70{
71 uint8_t code;
72 if (mA > 1000) mA = 1000;
73 if (mA <= 200) {
74 code = mA / 25;
75 } else {
76 code = (mA - 200) / 100 + 8;
77 }
78 int regVal = _core.readReg(axp2101_regs::chg::ICC_SETTING);
79 if (regVal < 0) return false;
80 regVal = (regVal & 0xE0) | code;
81 return _core.writeReg(axp2101_regs::chg::ICC_SETTING, static_cast<uint8_t>(regVal)) >= 0;
82}
83
85{
87 if (val < 0) return 0;
88 uint8_t code = val & 0x1F;
89 if (code <= 8) {
90 return code * 25;
91 }
92 return 200 + (code - 8) * 100;
93}
94
96{
97 if (mA > 200) mA = 200;
98 uint8_t code = mA / 25;
99 int regVal = _core.readReg(axp2101_regs::chg::ITERM_CTRL);
100 if (regVal < 0) return false;
101 regVal = (regVal & 0xF0) | code;
102 return _core.writeReg(axp2101_regs::chg::ITERM_CTRL, static_cast<uint8_t>(regVal)) >= 0;
103}
104
106{
107 int val = _core.readReg(axp2101_regs::chg::ITERM_CTRL);
108 if (val < 0) return 0;
109 return (val & 0x0F) * 25;
110}
111
113{
114 uint8_t code;
115 switch (mV) {
116 case 5000: code = 0; break;
117 case 4000: code = 1; break;
118 case 4100: code = 2; break;
119 case 4200: code = 3; break;
120 case 4350: code = 4; break;
121 case 4400: code = 5; break;
122 default: return false;
123 }
124 int regVal = _core.readReg(axp2101_regs::chg::CV_CHG_VOLTAGE);
125 if (regVal < 0) return false;
126 regVal = (regVal & 0xF8) | code;
127 return _core.writeReg(axp2101_regs::chg::CV_CHG_VOLTAGE, static_cast<uint8_t>(regVal)) >= 0;
128}
129
131{
133 if (val < 0) return 0;
134 switch (val & 0x07) {
135 case 0: return 5000;
136 case 1: return 4000;
137 case 2: return 4100;
138 case 3: return 4200;
139 case 4: return 4350;
140 case 5: return 4400;
141 default: return 0;
142 }
143}
144
146{
147 Status status{};
148 int status1 = _core.readReg(axp2101_regs::bmu::STATUS1);
149 int status2 = _core.readReg(axp2101_regs::bmu::STATUS2);
150 if (status1 < 0 || status2 < 0) {
151 return status;
152 }
153 status.online = true;
154 status.vbusPresent = (status1 >> 5) & 0x01;
155 status.batteryPresent = (status1 >> 3) & 0x01;
156 uint8_t chargePhase = status2 & 0x07;
157 switch (chargePhase) {
158 case 1:
159 status.chargingStatus = ChargingStatus::PRE_CHARGE;
160 break;
161 case 2:
162 case 3:
163 status.chargingStatus = ChargingStatus::FAST_CHARGE;
164 break;
165 case 4:
166 status.chargingStatus = ChargingStatus::TERMINATION;
167 break;
168 default:
169 status.chargingStatus = ChargingStatus::NO_CHARGING;
170 break;
171 }
172 status.chargeDone = (chargePhase == 4);
173 status.charging = (chargePhase >= 1 && chargePhase <= 3);
174 status.fault = (status1 & 0x01) != 0;
175 return status;
176}
177
178// ---- Termination Control ----
179
181{
182 int val = _core.readReg(axp2101_regs::chg::ITERM_CTRL);
183 if (val < 0) return;
184 _core.writeReg(axp2101_regs::chg::ITERM_CTRL, val | 0x10);
185}
186
188{
189 int val = _core.readReg(axp2101_regs::chg::ITERM_CTRL);
190 if (val < 0) return;
191 _core.writeReg(axp2101_regs::chg::ITERM_CTRL, val & 0xEF);
192}
193
195{
196 int val = _core.readReg(axp2101_regs::chg::ITERM_CTRL);
197 if (val < 0) return false;
198 return (val >> 4) & 0x01;
199}
200
201// ---- Button Battery Charge ----
202
207
212
217
219{
220 if (millivolt < 2600 || millivolt > 3300 || (millivolt % 100))
221 return false;
223 if (val < 0) return false;
224 val &= 0xF8;
225 val |= (millivolt - 2600) / 100;
226 return _core.writeReg(axp2101_regs::chg::BTN_BAT_CHG_VOL, val) >= 0;
227}
228
230{
232 if (val < 0) return 0;
233 return ((val & 0x07) * 100) + 2600;
234}
235
236// ---- Battery Detection ----
237
242
247
252
253// ---- Charger Safety Timer ----
254
256{
257 if (opt > 3) opt = 3;
259 if (val < 0) return;
260 val &= 0xFC;
261 val |= opt;
263}
264
266{
267 if (opt > 3) opt = 3;
269 if (val < 0) return;
270 val &= 0xCF;
271 val |= (opt << 4);
273}
274
275// ---- Thermal Regulation ----
276
278{
279 if (opt > 3) return;
281 if (val < 0) return;
282 val &= 0xFC;
283 val |= opt;
285}
286
288{
290 if (val < 0) return 0;
291 return val & 0x03;
292}
293
294// ---- JEITA Temperature Protection ----
295
297{
298 return enable ? _core.setRegBit(axp2101_regs::jeita::EN_CTRL, 0)
300}
301
306
307// -- Temperature Fault Thresholds --
308
310{
311 return _core.writeReg(axp2101_regs::jeita::VLTF_CHG, val) >= 0;
312}
313
315{
316 int val = _core.readReg(axp2101_regs::jeita::VLTF_CHG);
317 return (val < 0) ? 0 : static_cast<uint8_t>(val);
318}
319
321{
322 return _core.writeReg(axp2101_regs::jeita::VHTF_CHG, val) >= 0;
323}
324
326{
327 int val = _core.readReg(axp2101_regs::jeita::VHTF_CHG);
328 return (val < 0) ? 0 : static_cast<uint8_t>(val);
329}
330
332{
333 return _core.writeReg(axp2101_regs::jeita::VLTF_WORK, val) >= 0;
334}
335
337{
339 return (val < 0) ? 0 : static_cast<uint8_t>(val);
340}
341
343{
344 return _core.writeReg(axp2101_regs::jeita::VHTF_WORK, val) >= 0;
345}
346
348{
350 return (val < 0) ? 0 : static_cast<uint8_t>(val);
351}
352
353// -- Temperature Hysteresis --
354
356{
357 return _core.writeReg(axp2101_regs::ts::HYSL2H, val) >= 0;
358}
359
361{
362 int val = _core.readReg(axp2101_regs::ts::HYSL2H);
363 return (val < 0) ? 0 : static_cast<uint8_t>(val);
364}
365
367{
368 return _core.writeReg(axp2101_regs::ts::HYSH2L, val) >= 0;
369}
370
372{
373 int val = _core.readReg(axp2101_regs::ts::HYSH2L);
374 return (val < 0) ? 0 : static_cast<uint8_t>(val);
375}
376
377// -- JEITA CV Configuration (REG 59) --
378
380{
381 return reduce ? _core.setRegBit(axp2101_regs::jeita::SET0, 6)
383}
384
389
391{
392 return reduce ? _core.setRegBit(axp2101_regs::jeita::SET0, 4)
394}
395
400
402{
403 if (levels > 2) return false;
404 int val = _core.readReg(axp2101_regs::jeita::SET0);
405 if (val < 0) return false;
406 val &= 0xF3;
407 val |= (levels << 2);
408 return _core.writeReg(axp2101_regs::jeita::SET0, static_cast<uint8_t>(val)) >= 0;
409}
410
412{
413 int val = _core.readReg(axp2101_regs::jeita::SET0);
414 if (val < 0) return 0;
415 return (val >> 2) & 0x03;
416}
417
419{
420 if (levels > 2) return false;
421 int val = _core.readReg(axp2101_regs::jeita::SET0);
422 if (val < 0) return false;
423 val &= 0xFC;
424 val |= levels;
425 return _core.writeReg(axp2101_regs::jeita::SET0, static_cast<uint8_t>(val)) >= 0;
426}
427
429{
430 int val = _core.readReg(axp2101_regs::jeita::SET0);
431 if (val < 0) return 0;
432 return val & 0x03;
433}
434
435// -- JEITA Temperature Thresholds (REG 5B) --
436
438{
439 // T2: VHTF = val * 16mV, stored in lower nibble of REG 0x5B
440 int reg = _core.readReg(axp2101_regs::jeita::SET2);
441 if (reg < 0) return false;
442 reg &= 0xF0;
443 reg |= (val & 0x0F);
444 return _core.writeReg(axp2101_regs::jeita::SET2, static_cast<uint8_t>(reg)) >= 0;
445}
446
448{
449 int val = _core.readReg(axp2101_regs::jeita::SET2);
450 if (val < 0) return 0;
451 return val & 0x0F;
452}
453
455{
456 // T3: VHTF = val * 8mV, stored in upper nibble of REG 0x5B
457 int reg = _core.readReg(axp2101_regs::jeita::SET2);
458 if (reg < 0) return false;
459 reg &= 0x0F;
460 reg |= ((val & 0x0F) << 4);
461 return _core.writeReg(axp2101_regs::jeita::SET2, static_cast<uint8_t>(reg)) >= 0;
462}
463
465{
466 int val = _core.readReg(axp2101_regs::jeita::SET2);
467 if (val < 0) return 0;
468 return (val >> 4) & 0x0F;
469}
470
471#endif
@license MIT License
Status getStatus() override
Get the current charger status.
void disableButtonBatteryCharge()
Disable button/coin-cell battery charging (REG 0x18, bit 2).
bool isCharging() override
Check if the charger is actively charging.
uint8_t getChargeHighTempFaultThreshold()
Get charge high-temperature fault threshold (REG 55, VHTF_CHG).
bool isBatteryDetectionEnabled()
Check if battery detection is enabled (REG 0x68, bit 0).
void disableBatteryDetection()
Disable battery detection (REG 0x68, bit 0).
bool setLowTempHysteresis(uint8_t val)
Set low-temperature hysteresis (REG 52, HYSL2H).
void enableBatteryDetection()
Enable battery detection (REG 0x68, bit 0).
bool setChargeLowTempFaultThreshold(uint8_t val)
Set charge low-temperature fault threshold (REG 54, VLTF_CHG).
bool setJeitaCoolVoltageDrop(uint8_t levels)
Set cool-zone voltage drop levels in JEITA.
AXP2101Charger(AXP2101Core &core)
Construct AXP2101Charger with a reference to the AXP2101Core.
uint16_t getPreChargeCurrent() override
Get the configured precharge current (REG 0x61, bits 3:0).
bool setJeitaCoolThreshold(uint8_t val)
Set JEITA cool temperature threshold T2 (REG 5B low nibble).
bool setWorkLowTempFaultThreshold(uint8_t val)
Set work low-temperature fault threshold (REG 56, VLTF_WORK).
uint8_t getJeitaWarmVoltageDrop()
Get warm-zone voltage drop levels in JEITA (REG 0x59).
uint8_t getJeitaWarmThreshold()
Get JEITA warm temperature threshold T3 (REG 5B high nibble).
void enableButtonBatteryCharge()
Enable button/coin-cell battery charging (REG 0x18, bit 2).
uint8_t getThermalRegulationThreshold()
Get the thermal regulation threshold (REG 0x65, bits 1:0).
bool setJeitaWarmVoltageDrop(uint8_t levels)
Set warm-zone voltage drop levels in JEITA.
uint8_t getLowTempHysteresis()
Get low-temperature hysteresis (REG 52, HYSL2H).
bool setFastChargeCurrent(uint16_t mA) override
Set the fast-charge current limit (REG 0x62, bits 4:0).
bool setJeitaCoolCurrentReduction(bool reduce)
Set cool-zone current reduction in JEITA.
bool setPreChargeCurrent(uint16_t mA) override
Set the precharge current limit (REG 0x61, bits 3:0).
void setChargeDoneSafetyTimer(uint8_t opt)
Set the charge-done safety timer (REG 0x67, bits 6:4).
uint8_t getWorkLowTempFaultThreshold()
Get work low-temperature fault threshold (REG 56, VLTF_WORK).
bool enableJeita(bool enable)
Enable or disable JEITA temperature standard.
bool setChargeVoltage(uint16_t mV) override
Set the charge voltage target (REG 0x64, bits 2:0).
bool setHighTempHysteresis(uint8_t val)
Set high-temperature hysteresis (REG 53, HYSH2L).
uint8_t getHighTempHysteresis()
Get high-temperature hysteresis (REG 53, HYSH2L).
bool isJeitaEnabled()
Check if JEITA temperature standard is enabled.
void enableChargeTermination()
Enable charge termination detection (REG 0x63, bit 4).
bool getJeitaCoolCurrentReduction()
Get cool-zone current reduction setting in JEITA (REG 0x59).
uint16_t getButtonBatteryChargeVoltage()
Get the configured button battery charge voltage (REG 0x6A, bits 2:0).
uint8_t getJeitaCoolThreshold()
Get JEITA cool temperature threshold T2 (REG 5B low nibble).
uint8_t getJeitaCoolVoltageDrop()
Get cool-zone voltage drop levels in JEITA (REG 0x59).
bool enableCharging(bool enable) override
Enable or disable battery charging.
uint16_t getChargeVoltage() override
Get the configured charge voltage target (REG 0x64, bits 2:0).
void disableChargeTermination()
Disable charge termination detection (REG 0x63, bit 4).
uint16_t getFastChargeCurrent() override
Get the configured fast-charge current (REG 0x62, bits 4:0).
bool setChargeHighTempFaultThreshold(uint8_t val)
Set charge high-temperature fault threshold (REG 55, VHTF_CHG).
bool isButtonBatteryChargeEnabled()
Check if button battery charging is enabled (REG 0x18, bit 2).
bool setWorkHighTempFaultThreshold(uint8_t val)
Set work high-temperature fault threshold (REG 57, VHTF_WORK).
uint8_t getChargeLowTempFaultThreshold()
Get charge low-temperature fault threshold (REG 54, VLTF_CHG).
bool setJeitaWarmThreshold(uint8_t val)
Set JEITA warm temperature threshold T3 (REG 5B high nibble).
bool setButtonBatteryChargeVoltage(uint16_t millivolt)
Set the button battery charge voltage (REG 0x6A, bits 2:0).
bool getJeitaWarmCurrentReduction()
Get warm-zone current reduction setting in JEITA (REG 0x59).
void setPreChargeSafetyTimer(uint8_t opt)
Set the precharge safety timer (REG 0x67, bits 2:0).
uint8_t getWorkHighTempFaultThreshold()
Get work high-temperature fault threshold (REG 57, VHTF_WORK).
bool setJeitaWarmCurrentReduction(bool reduce)
Set warm-zone current reduction in JEITA.
uint16_t getTerminationCurrent() override
Get the configured termination current (REG 0x63, bits 3:0).
bool setTerminationCurrent(uint16_t mA) override
Set the charge termination current (REG 0x63, bits 3:0).
void setThermalRegulationThreshold(uint8_t opt)
Set the thermal regulation threshold (REG 0x65, bits 1:0).
bool isChargeTerminationEnabled()
Check if charge termination detection is enabled (REG 0x63, bit 4).
Core I2C communication layer for the AXP2101 PMIC.
bool enableModule(Module module, bool enable)
Enable or disable an internal functional module.
@ CELL_CHARGE
Cell battery charger (REG 0x18 bit 1)
@ NO_CHARGING
Not charging, battery idle or disconnected.
@ PRE_CHARGE
Pre-charge phase (low current for weak battery)
@ TERMINATION
Charging terminated (battery full)
@ FAST_CHARGE
Fast charge phase (constant current)
bool clrRegBit(uint8_t reg, uint8_t bit)
bool getRegBit(uint8_t reg, uint8_t bit)
int readReg(uint8_t reg) const
int writeReg(uint8_t reg, uint8_t val)
bool setRegBit(uint8_t reg, uint8_t bit)
Charger status structure.
bool charging
Charging state (coarse: true if in precharge/fastcharge)
static constexpr uint8_t STATUS2
static constexpr uint8_t STATUS1
static constexpr uint8_t BAT_DETECT_CTRL
static constexpr uint8_t CV_CHG_VOLTAGE
static constexpr uint8_t ITERM_CTRL
static constexpr uint8_t ICC_SETTING
static constexpr uint8_t BTN_BAT_CHG_VOL
static constexpr uint8_t CHG_TIMER_CFG
static constexpr uint8_t THERMAL_REG_THRESH
static constexpr uint8_t IPRECHG_SET
static constexpr uint8_t MODULE_EN
static constexpr uint8_t SET2
static constexpr uint8_t SET0
static constexpr uint8_t EN_CTRL
static constexpr uint8_t VLTF_CHG
static constexpr uint8_t VHTF_CHG
static constexpr uint8_t VLTF_WORK
static constexpr uint8_t VHTF_WORK
static constexpr uint8_t HYSH2L
static constexpr uint8_t HYSL2H