SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
BQ25896Charger.cpp
Go to the documentation of this file.
1
30#include "../../../SensorBuildOpt.h"
31#if !SENSORLIB_EXCLUDE_PMIC_BQ25896
32
33#include "BQ25896Charger.hpp"
34#include "BQ25896Regs.hpp"
35
36using namespace BQ25896Regs;
37
39
41{
42 if (enable) {
43 _userDisableCharge = false;
44 return _core.updateBits(REG_CHG_CTRL, MASK_CHG_CONFIG, MASK_CHG_CONFIG) >= 0;
45 } else {
46 _userDisableCharge = true;
47 return _core.updateBits(REG_CHG_CTRL, MASK_CHG_CONFIG, 0) >= 0;
48 }
49}
50
52{
53 return _core.isCharging();
54}
55
57{
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;
61 }
62 if (closest > PRE_CHG_CURRENT_MAX) {
63 closest = PRE_CHG_CURRENT_MAX;
64 }
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;
67}
68
70{
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);
75}
76
78{
79 uint16_t closest;
80 if (mA <= FAST_CHG_CUR_STEP / 2) {
81 closest = 0;
82 } else {
83 closest = ((mA + FAST_CHG_CUR_STEP / 2) / FAST_CHG_CUR_STEP) * FAST_CHG_CUR_STEP;
84 }
85 if (closest > FAST_CHG_CURRENT_MAX) {
86 closest = FAST_CHG_CURRENT_MAX;
87 }
88 uint8_t val = (closest / FAST_CHG_CUR_STEP) & MASK_ICHG;
89 return _core.updateBits(REG_CHG_CURRENT, MASK_ICHG, val) >= 0;
90}
91
93{
94 int val = _core.readReg(REG_CHG_CURRENT);
95 if (val < 0) return 0;
96 return (val & MASK_ICHG) * FAST_CHG_CUR_STEP;
97}
98
100{
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;
104 }
105 if (closest > TERM_CHG_CURRENT_MAX) {
106 closest = TERM_CHG_CURRENT_MAX;
107 }
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;
110}
111
113{
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);
118}
119
121{
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;
125 }
126 if (closest > FAST_CHG_VOL_MAX) {
127 closest = FAST_CHG_VOL_MAX;
128 }
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;
131}
132
134{
135 int val = _core.readReg(REG_CHG_VOLT);
136 if (val < 0) return 0;
137 val = (val >> SHIFT_VREG) & 0x3F;
138 if (val > 0x30) {
139 return FAST_CHG_VOL_MAX;
140 }
141 return CHG_VOL_BASE + (val * CHG_VOL_STEP);
142}
143
145{
146 Status status;
147 status.online = _core.isValid();
148 status.vbusPresent = _core.isVbusPresent();
149 status.charging = _core.isCharging();
150 status.chargeDone = _core.isChargeDone();
151 uint8_t tmp = _core.getChargeStatus();
152 switch (tmp) {
153 case 0x00:
155 break;
156 case 0x01:
158 break;
159 case 0x02:
161 break;
162 case 0x03:
164 break;
165 default:
167 break;
168 }
169 uint8_t fault;
170 if (_core.getFaultStatus(fault)) {
171 status.fault = (fault != 0);
172 status.faultCode = fault;
173 }
174
175 return status;
176}
177
178#endif
@license MIT License
@license MIT License
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.