SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
AXP1xxCharger.hpp
Go to the documentation of this file.
1
31#pragma once
32#include "../../PmicChargerBase.hpp"
33#include "../../../platform/SensorCommWrapper.hpp"
34
35template<typename Regs>
37{
38public:
43 explicit AXP1xxCharger(SensorCommWrapper &core) : _core(core) {}
44
54 bool enableCharging(bool enable) override
55 {
56 return enable ? _core.setRegBit(Regs::chg::CHARGE1, 7)
57 : _core.clrRegBit(Regs::chg::CHARGE1, 7);
58 }
59
64 bool isCharging() override
65 {
66 return getStatus().charging;
67 }
68
74 bool setPreChargeCurrent(uint16_t mA) override
75 {
76 (void)mA;
77 return false;
78 }
79
84 uint16_t getPreChargeCurrent() override
85 {
86 return 0;
87 }
88
100 bool setFastChargeCurrent(uint16_t mA) override
101 {
102 if (mA < Regs::chg::CHG_CUR_MIN) mA = Regs::chg::CHG_CUR_MIN;
103 if (mA > Regs::chg::CHG_CUR_MAX) mA = Regs::chg::CHG_CUR_MAX;
104 uint8_t code = (mA - Regs::chg::CHG_CUR_MIN) / Regs::chg::CHG_CUR_STEP;
105 int regVal = _core.readReg(Regs::chg::CHARGE1);
106 if (regVal < 0) return false;
107 regVal = (regVal & 0xF0) | code;
108 return _core.writeReg(Regs::chg::CHARGE1, static_cast<uint8_t>(regVal)) >= 0;
109 }
110
115 uint16_t getFastChargeCurrent() override
116 {
117 int val = _core.readReg(Regs::chg::CHARGE1);
118 if (val < 0) return 0;
119 uint8_t code = val & 0x0F;
120 return code * Regs::chg::CHG_CUR_STEP + Regs::chg::CHG_CUR_MIN;
121 }
122
133 bool setTerminationCurrent(uint16_t mA) override
134 {
135 bool set = (mA >= 150);
136 return _core.updateBits(Regs::chg::CHARGE1, 0x10, set ? 0x10 : 0x00) >= 0;
137 }
138
143 uint16_t getTerminationCurrent() override
144 {
145 int val = _core.readReg(Regs::chg::CHARGE1);
146 if (val < 0) return 0;
147 return (val & 0x10) ? 150 : 0;
148 }
149
163 bool setChargeVoltage(uint16_t mV) override
164 {
165 uint8_t code;
166 if (mV <= 4100) {
167 code = 0;
168 } else if (mV <= 4150) {
169 code = 1;
170 } else if (mV <= 4200) {
171 code = 2;
172 } else {
173 code = 3;
174 }
175 return _core.updateBits(Regs::chg::CHARGE1, 0x60, code << 5) >= 0;
176 }
177
182 uint16_t getChargeVoltage() override
183 {
184 int val = _core.readReg(Regs::chg::CHARGE1);
185 if (val < 0) return 0;
186 uint8_t code = (val >> 5) & 0x03;
187 switch (code) {
188 case 0: return 4100;
189 case 1: return 4150;
190 case 2: return 4200;
191 case 3: return 4360;
192 default: return 0;
193 }
194 }
195
204 Status getStatus() override
205 {
206 Status status{};
207 int s0 = _core.readReg(Regs::bmu::STATUS);
208 int s1 = _core.readReg(Regs::bmu::MODE_CHGSTATUS);
209 if (s0 < 0 || s1 < 0) {
210 return status;
211 }
212 status.online = true;
213 status.vbusPresent = (s0 >> 5) & 0x01;
214 status.batteryPresent = (s1 >> 5) & 0x01;
215 status.charging = (s1 >> 6) & 0x01;
216 status.chargeDone = false;
217 status.fault = (s1 >> 7) & 0x01;
218 if (status.charging) {
219 status.chargingStatus = ChargingStatus::FAST_CHARGE;
220 } else {
221 status.chargingStatus = ChargingStatus::NO_CHARGING;
222 }
223 return status;
224 }
225
226protected:
228};
bool setFastChargeCurrent(uint16_t mA) override
Set fast charge (constant-current) current.
bool setChargeVoltage(uint16_t mV) override
Set the charge target voltage (CV).
uint16_t getChargeVoltage() override
Get the charge target voltage (CV).
SensorCommWrapper & _core
uint16_t getPreChargeCurrent() override
Get pre-charge current (not supported by AXP1xx).
bool setTerminationCurrent(uint16_t mA) override
Set the termination current threshold.
uint16_t getTerminationCurrent() override
Get the termination current threshold.
uint16_t getFastChargeCurrent() override
Get the fast charge (constant-current) current.
bool isCharging() override
Check whether the battery is currently charging.
bool setPreChargeCurrent(uint16_t mA) override
Set pre-charge current (not supported by AXP1xx).
bool enableCharging(bool enable) override
Enable or disable battery charging.
Status getStatus() override
Get the charger status.
AXP1xxCharger(SensorCommWrapper &core)
Construct charger control interface.
PMIC charger capability interface.
@ NO_CHARGING
Not charging, battery idle or disconnected.
@ FAST_CHARGE
Fast charge phase (constant current)
bool clrRegBit(uint8_t reg, uint8_t bit)
int readReg(uint8_t reg) const
int writeReg(uint8_t reg, uint8_t val)
int updateBits(uint8_t reg, uint8_t mask, uint8_t value_shifted)
bool setRegBit(uint8_t reg, uint8_t bit)
Charger status structure.
bool charging
Charging state (coarse: true if in precharge/fastcharge)