SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
axp202_basic.ino
Go to the documentation of this file.
1
30#include <PmicXPowers.hpp>
31
32#ifndef PMIC_SDA
33#define PMIC_SDA 3
34#endif
35
36#ifndef PMIC_SCL
37#define PMIC_SCL 2
38#endif
39
40#ifndef PMIC_IRQ
41#define PMIC_IRQ 21
42#endif
43
45
46void setup()
47{
48 Serial.begin(115200);
49 delay(500);
50
51 Serial.println("\n=== AXP202 Basic Example ===\n");
52
53 if (!pmic.begin(Wire, AXP202_SLAVE_ADDRESS, PMIC_SDA, PMIC_SCL)) {
54 Serial.println("Failed to init AXP202!");
55 while (1) delay(1000);
56 }
57 Serial.println("AXP202 initialized");
58
59 Serial.println("\n--- Channel Test ---");
60
61 // Enable DCDC2 at 1.2V via unified channel API
64 Serial.print("DCDC2: ");
66 Serial.println(" mV");
67
68 // Enable LDO3 (wider range on AXP202: 700-3500mV)
71 Serial.print("LDO3: ");
73 Serial.println(" mV");
74
75 // Enable LDO4 (AXP202 only)
78 Serial.print("LDO4: ");
80 Serial.println(" mV");
81
82 Serial.print("Total channels: ");
83 Serial.println(pmic.getChannel()->count());
84
85 for (uint8_t i = 0; i < pmic.getChannel()->count(); i++) {
86 auto info = pmic.getChannel()->getInfo(i);
87 Serial.print(" Ch");
88 Serial.print(i);
89 Serial.print(": ");
90 Serial.print(info.name);
91 Serial.print(" ");
92 Serial.print(info.minMillivolt);
93 Serial.print("-");
94 Serial.print(info.maxMillivolt);
95 Serial.print(" mV");
96 Serial.print(" Step: ");
97 Serial.print(info.stepMillivolt);
98 Serial.println(" mV");
99 }
100
101 Serial.println("\n--- Charger Test ---");
102
103 auto *charger = pmic.getCharger();
104 if (charger) {
105 // The Charge input parameter is fuzzed and set to the value closest to the input.
106 charger->setChargeVoltage(4200);
107 charger->setFastChargeCurrent(500);
108 charger->enableCharging(true);
109 Serial.print("CV: ");
110 Serial.print(charger->getChargeVoltage());
111 Serial.println(" mV");
112 Serial.print("ICC: ");
113 Serial.print(charger->getFastChargeCurrent());
114 Serial.println(" mA");
115 }
116
117 Serial.println("\n--- ADC Test ---");
118
119 // Enable ADC channels
120 auto &adc = pmic.adc();
121 adc.enableChannels(
128 );
129
130
131 // Enable LED set blinking
134
135 Serial.println("\n=== Setup Complete ===\n");
136}
137
138void loop()
139{
140 static uint32_t lastPrint = 0;
141
142 if (millis() - lastPrint > 2000) {
143 lastPrint = millis();
144
145 Serial.println("\n--- Status ---");
146
147 auto *charger = pmic.getCharger();
148 if (charger) {
149 auto status = charger->getStatus();
150 Serial.print("Charging: ");
151 Serial.println(status.charging ? "Yes" : "No");
152 Serial.print("Vbus: ");
153 Serial.println(status.vbusPresent ? "Present" : "Absent");
154 }
155
156 auto &adc = pmic.adc();
157 float val = 0;
158 if (adc.read(PmicAdcBase::Channel::VBUS_VOLTAGE, val)) {
159 Serial.print("VBUS: ");
160 Serial.print(val);
161 Serial.println(" mV");
162 }
163 if (adc.read(PmicAdcBase::Channel::VBUS_CURRENT, val)) {
164 Serial.print("VBUS: ");
165 Serial.print(val);
166 Serial.println(" mA");
167 }
168 if (adc.read(PmicAdcBase::Channel::BAT_VOLTAGE, val)) {
169 Serial.print("VBAT: ");
170 Serial.print(val);
171 Serial.println(" mV");
172 }
173 if (adc.read(PmicAdcBase::Channel::BAT_CURRENT, val)) {
174 Serial.print("IBAT: ");
175 Serial.print(val);
176 Serial.println(" mA");
177 }
178 if (adc.read(PmicAdcBase::Channel::DIE_TEMPERATURE, val)) {
179 Serial.print("TEMP: ");
180 Serial.print(val);
181 Serial.println(" C");
182 }
183 if (adc.read(PmicAdcBase::Channel::VSYS_VOLTAGE, val)) {
184 Serial.print("VSYS: ");
185 Serial.print(val);
186 Serial.println(" mV");
187 }
188 }
189
190 delay(100);
191}
@license MIT License
#define PMIC_SDA
#define PMIC_SCL
void setup()
PmicAXP202 pmic
void loop()
bool setMode(Mode mode) override
Set LED operating mode.
Definition AXP1xxLed.hpp:74
bool setManualState(ManualState state) override
Set the LED state in manual mode.
static constexpr uint8_t CH_LDO3
static constexpr uint8_t CH_DCDC2
static constexpr uint8_t CH_LDO4
Unified AXP202 PMIC driver.
AXP202Adc & adc()
Access the ADC interface (chip-specific)
PmicChannelBase * getChannel() override
Access the power channel control interface.
PmicChargerBase * getCharger() override
Access the charger interface.
AXP1xxLed< axp202_regs > & led()
bool begin(SensorCommCustom::CustomCallback callback, SensorCommCustomHal::CustomHalCallback hal_cb, uint8_t addr)
Initialise the PMIC using custom communication callbacks.
@ 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.
virtual uint16_t getVoltage(uint8_t channel)=0
Get the current output voltage of a channel.
virtual bool setVoltage(uint8_t channel, uint16_t mV)=0
Set the output voltage of a channel.
virtual uint8_t count() const =0
Get the number of power output channels available on this PMIC.
virtual bool enable(uint8_t channel, bool enable)=0
Enable or disable a power output channel.
virtual Info getInfo(uint8_t channel) const =0
Get static metadata for a specific channel.
virtual bool setChargeVoltage(uint16_t mV)=0
Set charge voltage (CV mode voltage)
virtual Status getStatus()=0
Get current charger status.
@ MANUAL
Manual control through registers.
bool enableChannels(uint32_t mask) override
Enable one or more ADC channels.
uint8_t i