30#include "../../../SensorBuildOpt.h"
31#if !SENSORLIB_EXCLUDE_PMIC_BQ25896
43 _userDisableCharge =
false;
44 return _core.
updateBits(REG_CHG_CTRL, MASK_CHG_CONFIG, MASK_CHG_CONFIG) >= 0;
46 _userDisableCharge =
true;
47 return _core.
updateBits(REG_CHG_CTRL, MASK_CHG_CONFIG, 0) >= 0;
58 uint16_t closest = PRE_CHG_CUR_BASE + ((mA - PRE_CHG_CUR_BASE + PRE_CHG_CUR_STEP / 2) / PRE_CHG_CUR_STEP) * PRE_CHG_CUR_STEP;
59 if (closest < PRE_CHG_CURRENT_MIN) {
60 closest = PRE_CHG_CURRENT_MIN;
62 if (closest > PRE_CHG_CURRENT_MAX) {
63 closest = PRE_CHG_CURRENT_MAX;
65 uint8_t val = ((closest - PRE_CHG_CUR_BASE) / PRE_CHG_CUR_STEP) << SHIFT_IPRECHG;
66 return _core.
updateBits(REG_CHG_PRE_CURR, MASK_IPRECHG, val) >= 0;
71 int val = _core.
readReg(REG_CHG_PRE_CURR);
72 if (val < 0)
return 0;
73 val = (val >> SHIFT_IPRECHG) & 0x0F;
74 return PRE_CHG_CUR_BASE + (val * PRE_CHG_CUR_STEP);
80 if (mA <= FAST_CHG_CUR_STEP / 2) {
83 closest = ((mA + FAST_CHG_CUR_STEP / 2) / FAST_CHG_CUR_STEP) * FAST_CHG_CUR_STEP;
85 if (closest > FAST_CHG_CURRENT_MAX) {
86 closest = FAST_CHG_CURRENT_MAX;
88 uint8_t val = (closest / FAST_CHG_CUR_STEP) & MASK_ICHG;
89 return _core.
updateBits(REG_CHG_CURRENT, MASK_ICHG, val) >= 0;
94 int val = _core.
readReg(REG_CHG_CURRENT);
95 if (val < 0)
return 0;
96 return (val & MASK_ICHG) * FAST_CHG_CUR_STEP;
101 uint16_t closest = TERM_CHG_CUR_BASE + ((mA - TERM_CHG_CUR_BASE + TERM_CHG_CUR_STEP / 2) / TERM_CHG_CUR_STEP) * TERM_CHG_CUR_STEP;
102 if (closest < TERM_CHG_CURRENT_MIN) {
103 closest = TERM_CHG_CURRENT_MIN;
105 if (closest > TERM_CHG_CURRENT_MAX) {
106 closest = TERM_CHG_CURRENT_MAX;
108 uint8_t val = ((closest - TERM_CHG_CUR_BASE) / TERM_CHG_CUR_STEP) & MASK_ITERM;
109 return _core.
updateBits(REG_CHG_PRE_CURR, MASK_ITERM, val) >= 0;
114 int val = _core.
readReg(REG_CHG_PRE_CURR);
115 if (val < 0)
return 0;
116 val = val & MASK_ITERM;
117 return TERM_CHG_CUR_BASE + (val * TERM_CHG_CUR_STEP);
122 uint16_t closest = CHG_VOL_BASE + ((mV - CHG_VOL_BASE + CHG_VOL_STEP / 2) / CHG_VOL_STEP) * CHG_VOL_STEP;
123 if (closest < FAST_CHG_VOL_MIN) {
124 closest = FAST_CHG_VOL_MIN;
126 if (closest > FAST_CHG_VOL_MAX) {
127 closest = FAST_CHG_VOL_MAX;
129 uint8_t val = (((closest - CHG_VOL_BASE) / CHG_VOL_STEP) << SHIFT_VREG) & MASK_VREG;
130 return _core.
updateBits(REG_CHG_VOLT, MASK_VREG, val) >= 0;
135 int val = _core.
readReg(REG_CHG_VOLT);
136 if (val < 0)
return 0;
137 val = (val >> SHIFT_VREG) & 0x3F;
139 return FAST_CHG_VOL_MAX;
141 return CHG_VOL_BASE + (val * CHG_VOL_STEP);
171 status.
fault = (fault != 0);
bool setFastChargeCurrent(uint16_t mA) override
Set fast charge (constant-current) current.
uint16_t getPreChargeCurrent() override
Get pre-charge current.
uint16_t getFastChargeCurrent() override
Get fast charge current.
bool setTerminationCurrent(uint16_t mA) override
Set termination current.
bool enableCharging(bool enable) override
Enable or disable charging.
bool setChargeVoltage(uint16_t mV) override
Set charge voltage (constant voltage phase)
uint16_t getChargeVoltage() override
Get charge voltage.
BQ25896Charger(BQ25896Core &core)
Construct charger interface.
bool isCharging() override
Check if charging is in progress.
bool setPreChargeCurrent(uint16_t mA) override
Set pre-charge current.
uint16_t getTerminationCurrent() override
Get termination current.
Status getStatus() override
Get charger status.
uint8_t getChargeStatus()
Get charging status.
bool getFaultStatus(uint8_t &fault)
Get fault status.
bool isValid() const
Check if device is valid/present.
bool isVbusPresent()
Check if VBUS (USB power) is present.
bool isCharging()
Check if actively charging.
bool isChargeDone()
Check if charging is complete.
@ NO_CHARGING
Not charging, battery idle or disconnected.
@ PRE_CHARGE
Pre-charge phase (low current for weak battery)
@ UNKNOWN
Unable to determine status.
@ TERMINATION
Charging terminated (battery full)
@ FAST_CHARGE
Fast charge phase (constant current)
int readReg(uint8_t reg) const
int updateBits(uint8_t reg, uint8_t mask, uint8_t value_shifted)
Charger status structure.
bool vbusPresent
VBUS detected / good (USB power present)
bool chargeDone
Charge termination/done (battery full)
uint32_t faultCode
Optional raw fault code for diagnostics.
bool fault
Any fault latched/active.
bool charging
Charging state (coarse: true if in precharge/fastcharge)
ChargingStatus chargingStatus
Charging status enumeration.
bool online
PMIC reachable / initialized.