SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
AXP2101Power.cpp
Go to the documentation of this file.
1
31#include "../../../SensorBuildOpt.h"
32#if !SENSORLIB_EXCLUDE_PMIC_AXP2101
33
34#include "AXP2101Power.hpp"
35#include "AXP2101Regs.hpp"
36#include <string.h>
37
38
40{
41}
42
43// ---- PmicPowerBase overrides ----
44
46{
47 if (mv < 4100 || mv > 4800) return false;
48 uint8_t code = (mv - 4100) / 100;
49 return _core.updateBits(axp2101_regs::ctrl::MIN_SYS_VOLTAGE, 0x70, code << 4) >= 0;
50}
51
53{
55 if (val < 0) return 0;
56 return 4100 + (((val >> 4) & 0x07) * 100);
57}
58
60{
61 if (mv < 3880) mv = 3880;
62 if (mv > 5080) mv = 5080;
63 uint8_t code = (mv - 3880) / 80;
64 return _core.updateBits(axp2101_regs::ctrl::INPUT_VOLT_LIMIT, 0x0F, code) >= 0;
65}
66
68{
70 if (val < 0) return 0;
71 return 3880 + ((val & 0x0F) * 80);
72}
73
75{
76 static const uint16_t values[] = {100, 500, 900, 1000, 1500, 2000};
77 static const uint8_t codes[] = {0, 1, 2, 3, 4, 5};
78 uint8_t code = 0;
79 uint32_t best = 0;
80 for (int i = 0; i < 6; ++i) {
81 uint32_t diff = (mA > values[i]) ? (mA - values[i]) : (values[i] - mA);
82 if (i == 0 || diff < best) {
83 best = diff;
84 code = codes[i];
85 }
86 }
87 return _core.updateBits(axp2101_regs::ctrl::INPUT_CURR_LIMIT, 0x07, code) >= 0;
88}
89
91{
92 static const uint16_t values[] = {100, 500, 900, 1000, 1500, 2000};
94 if (val < 0) return 0;
95 uint8_t code = val & 0x07;
96 return (code <= 5) ? values[code] : values[5];
97}
98
99// Ship mode closes BATFET
101{
102 if (enable) {
104 }
106}
107
112
113// ---- VSYS Power Down Voltage (REG 24) ----
114
116{
117 if (millivolt < 2600 || millivolt > 3300 || (millivolt % 100))
118 return false;
119 int val = _core.readReg(axp2101_regs::pmu::VOFF_SET);
120 if (val < 0) return false;
121 val &= 0xF8;
122 val |= (millivolt - 2600) / 100;
123 return _core.writeReg(axp2101_regs::pmu::VOFF_SET, val) >= 0;
124}
125
127{
128 int val = _core.readReg(axp2101_regs::pmu::VOFF_SET);
129 if (val < 0) return 0;
130 return ((val & 0x07) * 100) + 2600;
131}
132
133// ---- Internal Discharge (REG 10 bit 5) ----
134
139
144
145// ---- System Reset & Shutdown (REG 10) ----
146
151
156
157// ---- Sleep / Wakeup (REG 26) ----
158
163
168
173
178
179// ---- PWROK & Sequence (REG 25) ----
180
185
190
195
200
205
210
212{
213 if (opt > 3) return false;
215 if (val < 0) return false;
216 val &= 0xFC;
217 val |= opt;
218 return _core.writeReg(axp2101_regs::pmu::PWROK_SEQU_CTRL, val) >= 0;
219}
220
222{
224 if (val < 0) return 0;
225 return val & 0x03;
226}
227
228// ---- Power Off Configuration (REG 22) ----
229
234
239
244
249
251{
252 if (restart) {
254 } else {
256 }
257}
258
259// ---- DCDC Protection (REG 23-24) ----
260
262{
263 if (enable) {
265 } else {
267 }
268}
269
274
275void AXP2101Power::setDCLowVoltageProtection(uint8_t dc_id, bool enable)
276{
277 if (dc_id > 4) return;
278 if (enable) {
280 } else {
282 }
283}
284
286{
287 if (dc_id > 4) return false;
289}
290
291// ---- Power On/Off Source Status (REG 20-21) ----
292
294{
296 return (val < 0) ? 0 : static_cast<uint8_t>(val);
297}
298
300{
302 return (val < 0) ? 0 : static_cast<uint8_t>(val);
303}
304
305// ---- Fuel Gauge ----
306
307bool AXP2101Power::writeGaugeData(uint8_t *data, uint8_t len)
308{
309 if (len != 128 || !data) return false;
314 for (int i = 0; i < 128; ++i) {
316 }
319 return compareGaugeData(data, len);
320}
321
322bool AXP2101Power::compareGaugeData(uint8_t *data, uint8_t len)
323{
324 if (len != 128 || !data) return false;
325 uint8_t buffer[128];
326 memset(buffer, 0, sizeof(buffer));
329 for (int i = 0; i < 128; ++i) {
331 }
336 return memcmp(data, buffer, 128) == 0;
337}
338
339// ---- Low Battery Thresholds (REG 1A) ----
340
342{
343 if (percentage < 5 || percentage > 20) return;
345 if (val < 0) return;
346 val &= 0x0F;
347 val |= ((percentage - 5) << 4);
349}
350
352{
354 if (val < 0) return 0;
355 return ((val >> 4) & 0x0F) + 5;
356}
357
359{
360 if (percentage > 15) percentage = 15;
362 if (val < 0) return;
363 val &= 0xF0;
364 val |= percentage;
366}
367
369{
371 if (val < 0) return 0;
372 return val & 0x0F;
373}
374
375// ---- Die Temperature Protection (REG 13) ----
376
382
387
389{
390 if (opt > 2) return false;
392 if (val < 0) return false;
393 val &= 0xF9;
394 val |= (opt << 1);
395 return _core.writeReg(axp2101_regs::ctrl::DIE_TEMP_CFG, static_cast<uint8_t>(val)) >= 0;
396}
397
399{
401 if (val < 0) return 0;
402 return (val >> 1) & 0x03;
403}
404
405// ---- DCDC PWM Mode Control (REG 81) ----
406
407bool AXP2101Power::setDCDCForcePWM(uint8_t dc_id, bool force)
408{
409 // DCDC1-4 map to bits 2-5 of REG 81. DCDC5 has no PWM control.
410 if (dc_id == 0 || dc_id > 4) return false;
411 return force ? _core.setRegBit(axp2101_regs::dcdc::FORCE_PWM_CTRL, dc_id + 1)
413}
414
416{
417 if (dc_id == 0 || dc_id > 4) return false;
418 return _core.getRegBit(axp2101_regs::dcdc::FORCE_PWM_CTRL, dc_id + 1);
419}
420
422{
423 return enable ? _core.setRegBit(axp2101_regs::dcdc::ONOFF_DVM_CTRL, 6)
425}
426
431
437
442
448
453
459
464
466{
467 if (opt > 3) return false;
468 return _core.updateBits(axp2101_regs::dcdc::FORCE_PWM_CTRL, 0x03, opt) >= 0;
469}
470
472{
474 if (val < 0) return 0;
475 return val & 0x03;
476}
477
478// ---- Fast Power-On Sequencing (REG 28-2B) ----
479
485
490
492{
493 return enable ? _core.setRegBit(axp2101_regs::pmu::FAST_PWRON_CTRL, 6)
495}
496
501
502bool AXP2101Power::setFastPowerOnSequence(uint8_t channel, uint8_t seq)
503{
504 if (channel > 13 || seq > 3) return false;
505
506 // Register and bit mapping for each channel:
507 // REG 0x28: DCDC1[1:0], DCDC2[3:2], DCDC3[5:4], DCDC4[7:6]
508 // REG 0x29: DCDC5[1:0], ALDO1[3:2], ALDO2[5:4], ALDO3[7:6]
509 // REG 0x2A: ALDO4[1:0], BLDO1[3:2], BLDO2[5:4], CPUSLDO[7:6]
510 // REG 0x2B: DLDO1[1:0], DLDO2[3:2]
511 static const uint8_t regs[] = {
526 };
527 static const uint8_t bits[] = {
528 0, 2, 4, 6, // DCDC1-4 in SET0
529 0, 2, 4, 6, // DCDC5, ALDO1-3 in SET1
530 0, 2, 4, 6, // ALDO4, BLDO1-2, CPUSLDO in SET2
531 0, 2, // DLDO1-2 in CTRL
532 };
533
534 return _core.updateBits(regs[channel], 0x03 << bits[channel], seq << bits[channel]) >= 0;
535}
536
538{
539 if (channel > 13) return 3;
540
541 static const uint8_t regs[] = {
549 };
550 static const uint8_t bits[] = {
551 0, 2, 4, 6, 0, 2, 4, 6, 0, 2, 4, 6, 0, 2,
552 };
553
554 int val = _core.readReg(regs[channel]);
555 if (val < 0) return 3;
556 return (val >> bits[channel]) & 0x03;
557}
558
559#endif
@license MIT License
Core I2C communication layer for the AXP2101 PMIC.
void enablePowerSequence()
Enable power-on sequencing (REG 0x25 bit 2).
void softShutdown()
Initiate a soft shutdown (REG 0x10 bit 0).
void setLowBatShutdownThreshold(uint8_t percentage)
Set the low-battery shutdown threshold (REG 0x1A bits 3:0).
void disableLongPressShutdown()
Disable long-press power-off (REG 0x22 bit 1).
bool setDCDCUVPDebounce(uint8_t opt)
Set DCDC UVP debounce time (REG 81 bits 1:0).
void enablePwrOk()
Enable PWROK output (REG 0x25 bit 4).
bool isFastPowerOnEnabled()
Check if fast power-on sequence is enabled (REG 2B bit 7).
void disablePwrOk()
Disable PWROK output (REG 0x25 bit 4).
void disableInternalDischarge()
Disable internal battery discharge resistor (REG 0x10 bit 5).
uint8_t getDCDCUVPDebounce()
Get DCDC UVP debounce time (REG 81 bits 1:0).
uint32_t getInputCurrentLimit() const override
Get the VBUS input current limit (REG 0x16 bits 2:0).
bool getFrequencySpreadRange()
Get the frequency spread range (REG 81 bit 6).
bool isFastWakeupEnabled()
Check if fast wakeup is enabled (REG 2B bit 6).
bool enableFastWakeup(bool enable)
Enable fast wakeup (REG 2B bit 6).
bool enableWakeup()
Enable wakeup function (REG 0x26 bit 1).
bool enableSleep()
Enable sleep mode (REG 0x26 bit 0).
bool isDCDCForcePWM(uint8_t dc_id)
Check if a DCDC converter is in forced-PWM mode (REG 81 bits 2-5).
uint16_t getSysPowerDownVoltage()
Get the VSYS power-off voltage threshold (REG 0x24 bits 2:0).
void setDCOverVoltageProtection(bool enable)
Enable or disable DC over-voltage protection (REG 0x23 bit 5).
bool compareGaugeData(uint8_t *data, uint8_t len)
Compare data against the fuel-gauge backup registers.
void setDCLowVoltageProtection(uint8_t dc_id, bool enable)
Enable or disable under-voltage protection for a DCDC (REG 0x23 bits 0-4).
void disablePowerSequence()
Disable power-on sequencing (REG 0x25 bit 2).
void enableOverTempShutdown()
Enable automatic shutdown on over-temperature (REG 0x22 bit 2).
bool isShipModeEnabled() const override
Check whether ship mode is currently active (REG 0x12 bit 3).
uint32_t getMinimumSystemVoltage() const override
Get the minimum VSYS system voltage (REG 0x14 bits 6:4).
bool setFrequencySpreadRange(bool wide)
Set frequency spread range (REG 81 bit 6).
bool enableDieTempDetection(bool enable)
Enable or disable die temperature detection.
bool setDieTempLevel1Threshold(uint8_t opt)
Set die over-temperature Level1 threshold (REG 13 bits 2:1).
void resetSoC()
Trigger a system-on-chip reset (REG 0x10 bit 1).
uint8_t getPwrOkDelay()
Get the PWROK delay time (REG 0x25 bits 1:0).
bool setInputCurrentLimit(uint32_t mA) override
Set the VBUS input current limit (REG 0x16 bits 2:0).
uint8_t getPowerOnStatus()
Read the power-on source status register (REG 0x20, read-only).
void setLowBatWarnThreshold(uint8_t percentage)
Set the low-battery warning threshold (REG 0x1A bits 7:4).
bool setDCDCForcePWM(uint8_t dc_id, bool force)
Force a DCDC converter to always-PWM mode (REG 81 bits 2-5).
void disableOverTempShutdown()
Disable automatic shutdown on over-temperature (REG 0x22 bit 2).
bool setSysPowerDownVoltage(uint16_t millivolt)
Set the VSYS power-off voltage threshold (REG 0x24 bits 2:0).
AXP2101Power(AXP2101Core &core)
Construct an AXP2101Power instance.
bool isFrequencySpreadEnabled()
Check if frequency spread spectrum is enabled (REG 81 bit 7).
bool writeGaugeData(uint8_t *data, uint8_t len)
Write user data to the fuel-gauge backup registers.
uint8_t getFastPowerOnSequence(uint8_t channel)
Get fast power-on start sequence for a channel (REG 28-2B).
uint32_t getInputVoltageLimit() const override
Get the VBUS input voltage limit (REG 0x15 bits 3:0).
bool disableWakeup()
Disable wakeup function (REG 0x26 bit 1).
bool isDCDCForceCCM()
Check if all DCDC1-4 are in forced CCM mode (REG 80 bit 6).
bool setDVMRampSlow(bool slow)
Set DVM voltage ramp rate (REG 80 bit 5).
bool isDVMRampSlow()
Check if DVM ramp is set to slow (REG 80 bit 5).
bool setInputVoltageLimit(uint32_t mv) override
Set the VBUS input voltage limit (REG 0x15 bits 3:0).
bool enableFastPowerOn(bool enable)
Enable fast power-on sequence (REG 2B bit 7).
bool isDieTempDetectionEnabled()
Check if die temperature detection is enabled.
bool setDCDCForceCCM(bool enable)
Force all DCDC1-4 to CCM (continuous conduction mode) (REG 80 bit 6).
uint8_t getLowBatShutdownThreshold()
Get the low-battery shutdown threshold (REG 0x1A bits 3:0).
bool disableSleep()
Disable sleep mode (REG 0x26 bit 0).
bool enableShipMode(bool enable) override
Enable or disable ship mode (REG 0x12 bit 3).
bool isDCLowVoltageProtectionEnabled(uint8_t dc_id)
Check if under-voltage protection is enabled for a DCDC (REG 0x23 bits 0-4).
bool setMinimumSystemVoltage(uint32_t mv) override
Set the minimum VSYS system voltage (REG 0x14 bits 6:4).
bool setFastPowerOnSequence(uint8_t channel, uint8_t seq)
Set fast power-on start sequence for a channel (REG 28-2B).
void enablePowerOffDelay()
Enable power-off delay (REG 0x25 bit 3).
void disablePowerOffDelay()
Disable power-off delay (REG 0x25 bit 3).
bool isDCOverVoltageProtectionEnabled()
Check if DC over-voltage protection is enabled (REG 0x23 bit 5).
uint8_t getDieTempLevel1Threshold()
Get die over-temperature Level1 threshold (REG 13 bits 2:1).
void enableLongPressShutdown()
Enable long-press power-off (REG 0x22 bit 1).
bool setFrequencySpreadEnable(bool enable)
Enable DCDC frequency spread spectrum (REG 81 bit 7).
uint8_t getLowBatWarnThreshold()
Get the low-battery warning threshold (REG 0x1A bits 7:4).
uint8_t getPowerOffStatus()
Read the power-off source status register (REG 0x21, read-only).
bool setPwrOkDelay(uint8_t opt)
Set the PWROK delay time (REG 0x25 bits 1:0).
void enableInternalDischarge()
Enable internal battery discharge resistor (REG 0x10 bit 5).
void setLongPressAction(bool restart)
Set the long-press power key action (REG 0x22 bit 0).
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)
int updateBits(uint8_t reg, uint8_t mask, uint8_t value_shifted)
bool setRegBit(uint8_t reg, uint8_t bit)
uint8_t i
static constexpr uint8_t LOW_BAT_WARN_SET
static constexpr uint8_t RESET_FUEL_GAUGE
static constexpr uint8_t COMMON_CFG
static constexpr uint8_t DIE_TEMP_CFG
static constexpr uint8_t MIN_SYS_VOLTAGE
static constexpr uint8_t INPUT_CURR_LIMIT
static constexpr uint8_t INPUT_VOLT_LIMIT
static constexpr uint8_t BATFET_CTRL
static constexpr uint8_t ONOFF_DVM_CTRL
static constexpr uint8_t FORCE_PWM_CTRL
static constexpr uint8_t BAT_PARAMS
static constexpr uint8_t FUEL_GAUGE_CTRL
static constexpr uint8_t FAST_PWRON_CTRL
static constexpr uint8_t FAST_PWRON_SET2
static constexpr uint8_t PWROK_SEQU_CTRL
static constexpr uint8_t DC_OVP_UVP_CTRL
static constexpr uint8_t FAST_PWRON_SET0
static constexpr uint8_t PWROFF_EN
static constexpr uint8_t FAST_PWRON_SET1
static constexpr uint8_t PWRON_STATUS
static constexpr uint8_t SLEEP_WAKEUP_CTRL
static constexpr uint8_t PWROFF_STATUS
static constexpr uint8_t VOFF_SET