SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
PmicAXP202.hpp
Go to the documentation of this file.
1
51#pragma once
52
53#include "../../../SensorBuildOpt.h"
54#if !SENSORLIB_EXCLUDE_PMIC_AXP202
55
56#include "../../PmicBase.hpp"
57#include "../../PmicAdcBase.hpp"
58#include "../../PmicIrqBase.hpp"
59
60#include "AXP202Regs.hpp"
61#include "AXP202Core.hpp"
62#include "AXP202Adc.hpp"
63#include "AXP202PowerEx.hpp"
64#include "AXP202ChargerEx.hpp"
65#include "AXP202Coulomb.hpp"
66#include "../axp1xx/AXP1xxIrq.hpp"
67#include "../axp1xx/AXP1xxCharger.hpp"
68#include "AXP202Gpio.hpp"
69#include "AXP202Channel.hpp"
70
71#include "../axp1xx/AXP1xxPower.hpp"
72#include "../axp1xx/AXP1xxPwron.hpp"
73#include "../axp1xx/AXP1xxTimer.hpp"
74#include "../axp1xx/AXP1xxLed.hpp"
75
76static constexpr uint8_t AXP202_SLAVE_ADDRESS = 0x35;
77
84class PmicAXP202 : public PmicBase
85{
86public:
90 PmicAXP202() : _core(), _adc(_core)
91 , _irq(_core), _charger(_core)
92 , _power(_core)
93 , _pwron(_core)
94 , _timer(_core)
95 , _led(_core)
96 , _gpio(_core), _channel(_core)
97 , _powerEx(_core), _chargerEx(_core), _coulomb(_core)
98 {}
99
100#if defined(ARDUINO)
109 bool begin(TwoWire &wire, uint8_t addr, int sda = -1, int scl = -1)
110 {
111 return _core.begin(wire, addr, sda, scl);
112 }
113#elif defined(ESP_PLATFORM)
114#if defined(SENSORLIB_USE_I2C_LEGACY)
123 bool begin(i2c_port_t port_num, uint8_t addr, int sda = -1, int scl = -1)
124 {
125 return _core.begin(port_num, addr, sda, scl);
126 }
127#else
134 bool begin(i2c_master_bus_handle_t handle, uint8_t addr)
135 {
136 return _core.begin(handle, addr);
137 }
138#endif
139#endif
140
150 uint8_t addr)
151 {
152 return _core.begin(callback, hal_cb, addr);
153 }
154
158 void end()
159 {
160 }
161
177
183 const PmicConfig &getConfig() const override
184 {
185 static const PmicConfig config = {
186 .chipName = "AXP202",
187 .i2cAddress = 0x35,
188 .chipIdReg = 0x03,
189 .chipIdValue = 0x41,
190 .channelCount = 7,
199 // REG33H[3:0]: Icharge = [300 + N*100] mA, N=0..15
200 .chargeCurrentMin = 300,
201 .chargeCurrentMax = 1800,
202 .chargeCurrentStep = 100,
203 .chargeCurrentSteps = 16,
204 };
205 return config;
206 }
207
208 // ---- Interface accessors (PmicBase contract) ----
209
215 {
216 return _power;
217 }
218
224 {
225 return &_charger;
226 }
227
232 PmicAdcBase &getAdc() override
233 {
234 return _adc;
235 }
236
238 {
239 return &_gpio;
240 }
241
246 PmicLedBase &getLed() override
247 {
248 return _led;
249 }
250
256 {
257 return &_channel;
258 }
259
260 PmicIrqBase *getIrq() override
261 {
262 return &_irq;
263 }
264
269 const char *getChipName() const override
270 {
271 return "AXP202";
272 }
273
274 // ---- Chip-specific module accessors ----
275
281 {
282 return _irq;
283 }
284
290 {
291 return _timer;
292 }
293
299 {
300 return _charger;
301 }
302
308 {
309 return _gpio;
310 }
311
317 {
318 return _adc;
319 }
320
326 {
327 return _core;
328 }
329
335 {
336 return _powerEx;
337 }
338
344 {
345 return _chargerEx;
346 }
347
353 {
354 return _coulomb;
355 }
356
358 {
359 return _led;
360 }
361
367 {
368 return _pwron;
369 }
370
371 // ---- Status & basic info ----
372
377 uint16_t getStatus()
378 {
379 int s0 = _core.readReg(axp202_regs::bmu::STATUS);
381 if (s0 < 0 || s1 < 0) return 0;
382 return (static_cast<uint16_t>(s0) << 8) | static_cast<uint16_t>(s1);
383 }
384
390 {
391 return _core.getRegBit(axp202_regs::bmu::STATUS, 5);
392 }
393
399 {
401 }
402
408 {
409 return _charger.isCharging();
410 }
411
416 uint8_t getChipID()
417 {
418 int id = _core.readReg(axp202_regs::bmu::IC_TYPE);
419 if (id < 0) return 0;
420 return static_cast<uint8_t>(id);
421 }
422
423 // ---- System control ----
424
432 void shutdown()
433 {
435 }
436
444 bool enableSleep(bool en)
445 {
446 return en ? _core.setRegBit(axp202_regs::pmu::VOFF_SET, 3)
448 }
449
450private:
454 const AXP202Core &core() const
455 {
456 return _core;
457 }
458
459 AXP202Core _core;
460 AXP202Adc _adc;
467 AXP202Gpio _gpio;
468 AXP202Channel _channel;
469 AXP202PowerEx _powerEx;
470 AXP202ChargerEx _chargerEx;
471 AXP202Coulomb _coulomb;
472};
473
476
479
480#endif
@license MIT License
@license MIT License
@license MIT License
@license MIT License
AXP202 Coulomb counter for battery charge/discharge measurement.
@license MIT License
AXP202-specific power management extensions.
@license MIT License
bool isCharging() override
Check whether the battery is currently charging.
AXP202 ADC channel control and data readout (template wrapper)
Definition AXP202Adc.hpp:53
AXP202 power channel enable and voltage control.
AXP202 GPIO pin control.
bool begin(SensorCommCustom::CustomCallback cb, SensorCommCustomHal::CustomHalCallback hal_cb, uint8_t addr)
Initialize the sensor using custom callback interface.
Unified AXP202 PMIC driver.
bool isBatteryConnect()
Check if a battery is connected.
AXP1xxPwron< axp202_regs > & pwron()
Access the power-button timing/control interface.
AXP202Gpio & gpio()
Access the GPIO interface.
PmicGpioBase * getGpio() override
Get GPIO interface.
AXP202Adc & adc()
Access the ADC interface (chip-specific)
void end()
De-initialise the PMIC (no-op)
uint8_t getChipID()
Read the AXP202 chip ID register.
bool isVbusGood()
Check if VBUS is present and valid.
AXP1xxIrq< axp202_regs > & irq()
Access the IRQ (interrupt) interface.
AXP1xxCharger< axp202_regs > & charger()
Access the charger interface (chip-specific)
void shutdown()
Power off the PMIC (shutdown)
AXP1xxTimer< axp202_regs > & timer()
Access the countdown timer subsystem.
PmicChannelBase * getChannel() override
Access the power channel control interface.
PmicAXP202()
Construct the AXP202 driver with all sub-modules.
PmicChargerBase * getCharger() override
Access the charger interface.
AXP202Coulomb & coulomb()
Access coulomb counter for charge/discharge energy measurement.
PmicIrqBase * getIrq() override
Get interrupt interface.
const PmicConfig & getConfig() const override
Get the PMIC configuration descriptor.
uint16_t getStatus()
Read combined STATUS and MODE_CHGSTATUS registers.
bool enableSleep(bool en)
Enable or disable sleep mode.
AXP202ChargerEx & chargerEx()
Access AXP202-specific charger extensions (thermal, backup, timeout)
const char * getChipName() const override
Get the chip name string.
bool isCharging()
Check if the battery is currently charging.
PmicAdcBase & getAdc() override
Access the ADC interface.
AXP1xxLed< axp202_regs > & led()
PmicCapability::Capability getCapabilities() const override
Get the PMIC capability flags.
bool begin(SensorCommCustom::CustomCallback callback, SensorCommCustomHal::CustomHalCallback hal_cb, uint8_t addr)
Initialise the PMIC using custom communication callbacks.
AXP202PowerEx & power()
Access AXP202-specific power extensions (DCDC mode, APS warning)
AXP202Core & core()
Access the core communication interface.
PmicLedBase & getLed() override
Access the LED control interface.
PmicPowerBase & getPower() override
Access the power management interface.
PMIC Base Abstract Class.
Definition PmicBase.hpp:130
PMIC power output channel base interface.
PMIC charger capability interface.
uint32_t(*)(Operation op, void *param1, void *param2) CustomHalCallback
bool(*)(uint8_t addr, uint8_t reg, uint8_t *buf, size_t len, bool writeReg, bool isWrite) CustomCallback
bool clrRegBit(uint8_t reg, uint8_t bit)
bool getRegBit(uint8_t reg, uint8_t bit)
int readReg(uint8_t reg) const
bool setRegBit(uint8_t reg, uint8_t bit)
@ PmicSupportCharger
Battery charger support.
@ PmicSupportGpio
General purpose I/O support.
@ PmicSupportIrq
Interrupt handling support.
@ PmicSupportPower
Power management (input limits, boost, etc.)
@ PmicSupportAdc
Analog-to-Digital converter for monitoring.
@ PmicSupportChannel
Power output channel control (DCDC/LDO)
@ PmicSupportLed
LED driver support.
@ PmicSupportPowerBtn
Power-on/Power-off control support.
PMIC static configuration data.
Definition PmicBase.hpp:108
const char * chipName
e.g. "AXP2101"
Definition PmicBase.hpp:109
static constexpr uint8_t STATUS
static constexpr uint8_t IC_TYPE
static constexpr uint8_t MODE_CHGSTATUS
static constexpr uint8_t VOFF_SET
static constexpr uint8_t OFF_CTL