SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
AXP202Adc.cpp
Go to the documentation of this file.
1
38#include "../../../SensorBuildOpt.h"
39#if !SENSORLIB_EXCLUDE_PMIC_AXP202
40
41#include "AXP202Adc.hpp"
42#include "AXP202Regs.hpp"
43
44// ---- ADC Configuration ----
46{
47 if (rate > 3) return false;
48 return _core.updateBits(axp202_regs::adc::ADC_SPEED, 0xC0, rate << 6) >= 0;
49}
50
52{
54 if (val < 0) return 0;
55 static const uint16_t rates[] = {25, 50, 100, 200};
56 return rates[(val >> 6) & 0x03];
57}
58
60{
62}
63
65{
67 return (val < 0) ? 0 : static_cast<uint8_t>(val);
68}
69
74
76{
78 return (val < 0) ? 0 : static_cast<uint8_t>(val);
79}
80
85
87{
89 return (val < 0) ? 0 : static_cast<uint8_t>(val);
90}
91
92// ---- Additional ADC Channel Reads ----
94{
95 uint16_t raw = readRegisterH8L4(axp202_regs::adc::TS_IN_H, axp202_regs::adc::TS_IN_L);
96 if (raw == 0xFFFF) return 0.0f;
97 return static_cast<float>(raw) * axp202_regs::factory::FACTORY_TS_VOLTAGE;
98}
99
100// Datasheet REG 64H/65H (GPIO0 ADC, 12-bit H8L4, 0.5mV/step)
101
103{
104 uint16_t raw = readRegisterH8L4(axp202_regs::adc::GPIO0_ADC_H, axp202_regs::adc::GPIO0_ADC_L);
105 if (raw == 0xFFFF) return 0.0f;
106 return static_cast<float>(raw) * axp202_regs::factory::FACTORY_GPIO_VOLTAGE;
107}
108
109// Datasheet REG 66H/67H (GPIO1 ADC, 12-bit H8L4, 0.5mV/step)
110
112{
113 uint16_t raw = readRegisterH8L4(axp202_regs::adc::GPIO1_ADC_H, axp202_regs::adc::GPIO1_ADC_L);
114 if (raw == 0xFFFF) return 0.0f;
115 return static_cast<float>(raw) * axp202_regs::factory::FACTORY_GPIO_VOLTAGE;
116}
117
118// Datasheet REG 70H-72H (Battery power, 24-bit)
119
121{
122 uint8_t buf[3];
123 if (_core.readRegBuff(axp202_regs::adc::BAT_POWER_H, buf, 3) < 0) return 0.0f;
124 uint32_t raw = (static_cast<uint32_t>(buf[0]) << 16) |
125 (static_cast<uint32_t>(buf[1]) << 8) |
126 static_cast<uint32_t>(buf[2]);
127 return static_cast<float>(raw) * 2.0f * 1.1f * 0.5f / 1000.0f;
128}
129
130// ---- Helper ----
131
132uint16_t AXP202Adc::readRegisterH8L4(uint8_t regH, uint8_t regL)
133{
134 int h = _core.readReg(regH);
135 int l = _core.readReg(regL);
136 if (h < 0 || l < 0) {
137 return 0xFFFF;
138 }
139 return (static_cast<uint16_t>(h) << 4) | (static_cast<uint16_t>(l) & 0x0F);
140}
141
142#endif
@license MIT License
@license MIT License
bool setADCInputRange(uint8_t val)
Set ADC input range (REG 85H)
Definition AXP202Adc.cpp:59
bool setADCSampleRate(uint8_t rate)
Set ADC sample rate (REG 84H bits 7:6)
Definition AXP202Adc.cpp:45
float getBatteryPower()
Read battery instantaneous power (REG 70H-72H, 24-bit)
float getGpio0Voltage()
Read GPIO0 ADC voltage in mV (REG 64H/65H, 12-bit H8L4)
uint8_t getADCIrqRisingThreshold()
Definition AXP202Adc.cpp:75
uint8_t getADCIrqFallingThreshold()
Definition AXP202Adc.cpp:86
bool setADCIrqRisingThreshold(uint8_t val)
Set ADC IRQ rising-edge threshold (REG 86H)
Definition AXP202Adc.cpp:70
bool setADCIrqFallingThreshold(uint8_t val)
Set ADC IRQ falling-edge threshold (REG 87H)
Definition AXP202Adc.cpp:81
uint16_t getADCSampleRate()
Get ADC sample rate in Hz.
Definition AXP202Adc.cpp:51
uint8_t getADCInputRange()
Definition AXP202Adc.cpp:64
float getTsVoltage()
Read TS pin voltage in mV (REG 62H/63H, 12-bit H8L4)
Definition AXP202Adc.cpp:93
float getGpio1Voltage()
Read GPIO1 ADC voltage in mV (REG 66H/67H, 12-bit H8L4)
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)
AXP202Core & _core
Reference to I2C communication core.
char buf[64]
static constexpr uint8_t ADC_IRQ_RETFSET
static constexpr uint8_t GPIO0_ADC_H
static constexpr uint8_t GPIO1_ADC_H
static constexpr uint8_t BAT_POWER_H
static constexpr uint8_t GPIO0_ADC_L
static constexpr uint8_t ADC_INPUTRANGE
static constexpr uint8_t TS_IN_H
static constexpr uint8_t ADC_SPEED
static constexpr uint8_t ADC_IRQ_FETFSET
static constexpr uint8_t GPIO1_ADC_L
static constexpr uint8_t TS_IN_L
static constexpr float FACTORY_GPIO_VOLTAGE
static constexpr float FACTORY_TS_VOLTAGE