30#include "../../../SensorBuildOpt.h"
31#if !SENSORLIB_EXCLUDE_PMIC_AXP517
43bool readRegisterPair(
AXP517Core &core, uint8_t highReg, uint8_t lowReg,
44 uint8_t &high, uint8_t &low)
49 for (uint8_t attempt = 0; attempt < 3; ++attempt) {
50 int highBefore = core.
readReg(highReg);
51 int lowValue = core.
readReg(lowReg);
52 int highAfter = core.
readReg(highReg);
53 if (highBefore < 0 || lowValue < 0 || highAfter < 0)
return false;
54 if (highBefore == highAfter) {
55 high =
static_cast<uint8_t
>(highBefore);
56 low =
static_cast<uint8_t
>(lowValue);
63bool selectAndReadAdcData(
AXP517Core &core, uint8_t selector,
64 uint8_t &high, uint8_t &low)
67 if (dataSelect < 0)
return false;
69 if ((dataSelect & 0x0F) != selector) {
70 const uint8_t next =
static_cast<uint8_t
>((dataSelect & 0xF0) | selector);
101 auto ch =
static_cast<Channel>(bit);
102 hwMask |= axp517_channelToMask(ch);
104 if (hwMask == 0)
return false;
106 if (regVal < 0)
return false;
115 auto ch =
static_cast<Channel>(bit);
116 hwMask |= axp517_channelToMask(ch);
118 if (hwMask == 0)
return false;
120 if (regVal < 0)
return false;
128 uint8_t buffer[2] = {0, 0};
138 bool vbusPresent = (status0 >> 5) & 0x01;
139 bool batPresent = (status0 >> 3) & 0x01;
169 adcVal = (
static_cast<int16_t
>((buffer[0] & 0x3F) << 8 | buffer[1]));
182 adcVal = (
static_cast<int16_t
>(buffer[0]) << 8) | buffer[1];
186 if (!selectAndReadAdcData(_core, 0x01, buffer[0], buffer[1]))
return false;
187 adcVal = ((buffer[0] & 0x3F) << 8) | buffer[1];
191 if (!selectAndReadAdcData(_core, 0x00, buffer[0], buffer[1]))
return false;
192 adcVal = ((buffer[0] & 0x3F) << 8) | buffer[1];
193 out = (3552.0f - adcVal) / 1.79f + 25.0f;
199 out =
static_cast<float>(buffer[0]);
205 out =
static_cast<float>(buffer[0]);
AXP517Adc(AXP517Core &core)
Construct an AXP517 ADC driver.
bool enableChannels(uint32_t mask) override
Enable one or more ADC channels.
bool read(Channel ch, float &out) override
Read an ADC channel.
bool disableChannels(uint32_t mask) override
Disable one or more ADC channels.
AXP517 low-level core driver.
void delayMs(uint32_t ms)
Delay using the active platform HAL.
static void forEachChannel(uint32_t mask, Func fn)
Helper to iterate over set bits in a bitmask.
Channel
ADC channel bitmask.
@ BAT_PERCENTAGE
Battery percentage.
@ DIE_TEMPERATURE
Die temperature.
@ BAT_VOLTAGE
Battery voltage.
@ BAT_TEMPERATURE
Battery temperature.
@ BAT_CURRENT
Battery current.
@ VBUS_CURRENT
VBUS current.
@ VBUS_VOLTAGE
VBUS voltage.
@ VSYS_VOLTAGE
VSYS voltage.
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
static constexpr uint8_t BAT_VOLT_H
BAT voltage ADC high.
static constexpr uint8_t ENABLE
ADC channel enable control.
static constexpr uint8_t VBUS_CURR_H
VBUS current ADC high.
static constexpr uint8_t BAT_CURR_H
BAT current ADC high.
static constexpr uint8_t DATA_H
ADC data high.
static constexpr uint8_t VBUS_CURR_L
VBUS current ADC low.
static constexpr uint8_t BAT_CURR_L
BAT current ADC low.
static constexpr uint8_t VBUS_VOLT_H
VBUS voltage ADC high.
static constexpr uint8_t VBUS_VOLT_L
VBUS voltage ADC low.
static constexpr uint8_t BAT_VOLT_L
BAT voltage ADC low.
static constexpr uint8_t DATA_SEL
ADC data select.
static constexpr uint8_t DATA_L
ADC data low.
static constexpr uint8_t STATUS0
BMU status0.
static constexpr float FACTORY_BAT_CURRENT
16-Bit(including sign bit)
static constexpr float FACTORY_VBUS_CURRENT
14-Bit(including sign bit)
static constexpr float FACTORY_VBAT_VOLTAGE
14-Bit(including sign bit)
static constexpr float FACTORY_VSYS_VOLTAGE
14-Bit(including sign bit)
static constexpr float FACTORY_VBUS_VOLTAGE
14-Bit(including sign bit)
static constexpr uint8_t BAT_PERCENT
Battery percentage (RO)
static constexpr uint8_t BAT_TEMP
Battery temperature (RO)