SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
AXP517Charger.cpp
Go to the documentation of this file.
1
30#include "../../../SensorBuildOpt.h"
31#if !SENSORLIB_EXCLUDE_PMIC_AXP517
32
33#include "AXP517Charger.hpp"
34#include "AXP517Regs.hpp"
35
36
38{
39
40}
41
43{
44 return _core.updateBits(axp517_regs::ctrl::MODULE_EN1, 0x02, enable ? 0x02 : 0x00) >= 0;
45}
46
48{
49 return getStatus().charging;
50}
51
53{
54 return setCurrentWithStep(axp517_regs::chg::IPRECHG_ITRICHG, mA, 960, 64, 0x0F);
55}
56
58{
59 return getCurrentWithStep(axp517_regs::chg::IPRECHG_ITRICHG, 64, 0x0F);
60}
61
63{
64 return setCurrentWithStep(axp517_regs::chg::ICC_SETTING, mA, 5120, 64, 0x7F);
65}
66
68{
69 return getCurrentWithStep(axp517_regs::chg::ICC_SETTING, 64, 0x7F);
70}
71
73{
74 return setCurrentWithStep(axp517_regs::chg::ITERM_CTRL, mA, 960, 64, 0x0F);
75}
76
78{
79 return getCurrentWithStep(axp517_regs::chg::ITERM_CTRL, 64, 0x0F);
80}
81
83{
84 uint8_t voltCode = 0;
85 int regValue = 0;
86 switch (mV) {
87 case 4000: voltCode = 0x00; break;
88 case 4100: voltCode = 0x01; break;
89 case 4200: voltCode = 0x02; break;
90 case 4350: voltCode = 0x03; break;
91 case 4400: voltCode = 0x04; break;
92 case 3800: voltCode = 0x05; break;
93 case 3600: voltCode = 0x06; break;
94 case 5000: voltCode = 0x07; break;
95 default: return false;
96 }
98 if (regValue < 0) {
99 return false;
100 }
101 regValue = (regValue & 0xF8) | voltCode;
102 if (_core.writeReg(axp517_regs::chg::CV_CHG_VOLTAGE, regValue) < 0) {
103 return false;
104 }
105 return true;
106}
107
109{
110 int regValue = _core.readReg(axp517_regs::chg::CV_CHG_VOLTAGE);
111 if (regValue < 0) {
112 return 0;
113 }
114 uint8_t voltCode = regValue & 0x07;
115 switch (voltCode) {
116 case 0x00: return 4000;
117 case 0x01: return 4100;
118 case 0x02: return 4200;
119 case 0x03: return 4350;
120 case 0x04: return 4400;
121 case 0x05: return 3800;
122 case 0x06: return 3600;
123 case 0x07: return 5000;
124 default: break;
125 }
126 return 0;
127}
128
130{
131 Status status {};
132 uint8_t buffer[2] {};
133 if (_core.readRegBuff(axp517_regs::bmu::STATUS0, buffer, sizeof(buffer)) < 0) {
134 return status;
135 }
136 status.online = true;
137 status.vbusPresent = (buffer[0] >> 5) & 0x01;
138 status.batteryPresent = (buffer[0] >> 3) & 0x01;
146 if (status.batteryPresent && status.vbusPresent) {
147 uint8_t charging_status = buffer[1] & 0x03;
148 status.charging = charging_status < 3;
149 status.chargeDone = charging_status == 4;
150 } else {
151 status.charging = false;
152 status.chargeDone = false;
153 }
154 status.fault = false; //TODO: Implement fault detection
155 return status;
156}
157
158bool AXP517Charger::setCurrentWithStep(uint8_t reg, uint16_t milliamps,
159 uint16_t max, uint16_t step, uint8_t mask)
160{
161 if (milliamps > max) milliamps = max;
162 uint8_t code = milliamps / step;
163 milliamps = code * step;
164 int regValue = _core.readReg(reg);
165 if (regValue < 0) {
166 return false;
167 }
168 regValue = (regValue & ~mask) | code;
169 if (_core.writeReg(reg, regValue) < 0) {
170 return false;
171 }
172 return true;
173}
174
175uint16_t AXP517Charger::getCurrentWithStep(uint8_t reg, uint16_t step, uint8_t mask)
176{
177 int regValue = _core.readReg(reg);
178 if (regValue < 0) {
179 return 0;
180 }
181 return (regValue & mask) * step;
182}
183
184#endif
@license MIT License
@license MIT License
uint16_t getChargeVoltage() override
Get charge voltage (CV).
uint16_t getPreChargeCurrent() override
Get pre-charge current.
bool setChargeVoltage(uint16_t mV) override
Set charge voltage (CV).
uint16_t getFastChargeCurrent() override
Get fast charge (constant-current) current.
bool enableCharging(bool enable) override
Enable or disable charging.
Status getStatus() override
Get current charger status (best-effort).
bool setFastChargeCurrent(uint16_t mA) override
Set fast charge (constant-current) current.
bool isCharging() override
Check whether charger is currently charging (best-effort).
bool setTerminationCurrent(uint16_t mA) override
Set termination current.
uint16_t getTerminationCurrent() override
Get termination current.
bool setPreChargeCurrent(uint16_t mA) override
Set pre-charge current.
AXP517Charger(AXP517Core &core)
Construct an AXP517 charger driver.
AXP517 low-level core driver.
int readReg(uint8_t reg) const
int writeReg(uint8_t reg, uint8_t val)
int readRegBuff(uint8_t reg, uint8_t *buf, size_t len) const
int updateBits(uint8_t reg, uint8_t mask, uint8_t value_shifted)
Charger status structure.
bool charging
Charging state (coarse: true if in precharge/fastcharge)
bool online
PMIC reachable / initialized.
static constexpr uint8_t STATUS0
BMU status0.
static constexpr uint8_t ICC_SETTING
ICC setting.
static constexpr uint8_t IPRECHG_ITRICHG
Iprechg/Itrichg setting.
static constexpr uint8_t ITERM_CTRL
Iterm setting and control.
static constexpr uint8_t CV_CHG_VOLTAGE
CV charger voltage setting.
static constexpr uint8_t MODULE_EN1
Module enable control1.