47#define PD_TARGET_MV 9000
51#define PD_TIMEOUT_MS 6000
55#error "AXP517 PD negotiation requires a valid PMIC_IRQ pin."
64static constexpr uint16_t VBUS_PRESENT_MV = 4000;
65static constexpr uint32_t VBUS_SETTLE_MS = 300;
66static constexpr uint32_t IRQ_PENDING_PRINT_MS = 1000;
75 Serial.println(
"\n--- Source Capabilities ---");
76 Serial.print(
"Total PDO count: ");
80 Serial.println(
"No fixed PDO found.");
85 const auto &pdo = caps.
fixed[
i];
88 Serial.print(pdo.pdoIndex1Based);
91 Serial.print(
" mV / ");
92 Serial.print(pdo.maMax);
93 Serial.print(
" mA, raw=0x");
94 Serial.println(pdo.rawPdo, HEX);
100 Serial.print(
"IRQ Status: high=0x");
101 Serial.print(
static_cast<uint32_t
>(status >> 32), HEX);
102 Serial.print(
" low=0x");
103 Serial.println(
static_cast<uint32_t
>(status), HEX);
111 Serial.print(
"TCPC VBUS: ");
112 Serial.print(vbusMv);
113 Serial.println(
" mV");
133 uint32_t start = millis();
140 }
while (millis() - start < timeoutMs);
147 uint16_t pdAlert =
static_cast<uint16_t
>(irqStatus >> 32);
170 uint16_t pdAlert =
static_cast<uint16_t
>(irqStatus >> 32);
177 uint16_t pdAlert =
static_cast<uint16_t
>(irqStatus >> 32);
183 uint16_t pdAlert =
static_cast<uint16_t
>(irqStatus >> 32);
184 uint32_t pmicIrq =
static_cast<uint32_t
>(irqStatus);
185 constexpr uint16_t actionable =
195 return pmicIrq == 0 && pdAlert != 0 && (pdAlert & actionable) == 0;
200 Serial.println(reason);
207 Serial.println(
"Failed to clear IRQ status after PD reset.");
215 Serial.print(
"Requesting PD voltage: ");
217 Serial.println(
" mV");
222 Serial.println(
"\n--- PD Result ---");
223 Serial.println(
pdReady ?
"Negotiation success." :
"Negotiation failed.");
229 Serial.println(
"Failed to clear IRQ status after PD request.");
236 if (irqStatus == 0) {
246 Serial.println(
"Power On Short Press Detected");
251 Serial.println(
"Battery Inserted");
255 Serial.println(
"Battery Removed");
262 if (!
pdReady && !vbusPresent && (vbusInserted || tcpcSourceActivity)) {
268 if (
pdReady && (vbusRemoved || tcpcDetached || (!vbusPresent && (tcpcFault || stickyTcpc)))) {
270 Serial.println(
"VBUS Removed");
273 Serial.println(
"TCPC detached");
279 if (!
pdReady && (vbusInserted || tcpcSourceActivity)) {
281 Serial.println(
"VBUS Inserted");
283 Serial.println(
"TCPC VBUS activity detected");
289 Serial.println(
"VBUS is not present, skip PD request.");
295 static uint32_t lastFaultPrint = 0;
296 if (millis() - lastFaultPrint > 1000) {
297 lastFaultPrint = millis();
299 Serial.println(tcpcFault ?
"TCPC fault pending" :
"TCPC alert pending");
309 uint32_t deadline = millis() + 250;
311 while (millis() < deadline) {
325 Serial.println(
"PMIC_IRQ is still LOW after drain.");
331 Serial.begin(115200);
334 Serial.println(
"\n=== AXP517 PD Auto Request Example ===\n");
335 Serial.print(
"Target voltage: ");
337 Serial.println(
" mV");
340 Serial.println(
"Failed to init AXP517 or PMIC_IRQ pin!");
341 while (1) delay(1000);
354 Serial.print(
"PMIC_IRQ pin: ");
356 Serial.println(
"Waiting for VBUS insert interrupt...");
359 Serial.println(
"VBUS already present");
366 static uint32_t lastPrint = 0;
367 static uint32_t lastPendingPrint = 0;
371 Serial.println(
"IRQ Triggered");
372 }
else if (millis() - lastPendingPrint > IRQ_PENDING_PRINT_MS) {
373 lastPendingPrint = millis();
374 Serial.println(
"IRQ Pending");
380 if (millis() - lastPrint > 2000) {
381 lastPrint = millis();
383 Serial.println(
"\n--- Status ---");
384 Serial.print(
"PD: ");
385 Serial.println(
pdReady ?
"Ready" :
"Waiting");
bool waitForVbusPresent(uint32_t timeoutMs)
bool isTcpcDetachAlert(uint64_t irqStatus, bool vbusPresent)
bool isStickyTcpcAlert(uint64_t irqStatus)
bool isTcpcSourceActivityAlert(uint64_t irqStatus)
volatile bool irqTriggered
void resetPdState(const char *reason)
void printSourceCaps(const AXP517PdNegotiator::SourceCaps &caps)
void printIrqStatus(uint64_t status)
AXP517PdNegotiator::SourceCaps sourceCaps
bool isTcpcFaultAlert(uint64_t irqStatus)
Status getStatus() override
Get current charger status (best-effort).
bool enable(uint64_t mask) override
Enable one or more PMIC IRQ sources.
static bool isPwrOnShortPress(uint64_t mask)
static bool isBatteryRemove(uint64_t mask)
static constexpr uint64_t IRQ_VBUS_REMOVE
VBUS Remove.
static constexpr uint64_t IRQ_VBUS_INSERT
VBUS Insert.
static constexpr uint64_t IRQ_BATTERY_INSERT
Battery Insert.
static bool isBatteryInsert(uint64_t mask)
static constexpr uint64_t IRQ_BATTERY_REMOVE
Battery Remove.
static constexpr uint64_t IRQ_PWR_ON_SHORT_PRESS
PWR_ON pin Short PRESS.
static bool isVbusInsert(uint64_t mask)
uint64_t readStatus(bool clear=true) override
Read the current IRQ status.
bool clearStatus() override
Clear all pending PMIC IRQ and TCPC alert status bits.
static bool isVbusRemove(uint64_t mask)
void reset()
Reset message IDs and return to the initial wait state.
bool clearTx()
Reset the TX byte count and transmit command register.
bool isAttached()
Check whether either CC pin indicates attachment.
@ AlertTxFailed
Message transmission failed.
@ AlertPowerStatus
POWER_STATUS register changed.
@ AlertTxSuccess
Message transmission completed.
@ AlertRxHardReset
Hard Reset received.
@ AlertRxStatus
RX buffer contains a SOP message.
@ AlertTxDiscarded
Transmission discarded due to RX activity.
@ AlertFault
TCPC fault status changed.
@ AlertVbusDisconnect
VBUS sink disconnect threshold crossed.
@ AlertCcStatus
CC_STATUS register changed.
bool readVbusMv(uint16_t &mv)
Read TCPC VBUS ADC value.
bool isVbusPresent()
Check whether TCPC VBUS present status is set.
High-level driver facade for the X-Powers AXP517 PMIC.
AXP517Tcpc & tcpc()
Access the low-level AXP517 TCPC helper.
bool requestPd(uint16_t targetMv, uint32_t timeoutMs=6000, AXP517PdNegotiator::SourceCaps *outCaps=nullptr)
Request a fixed USB-PD voltage from the attached source.
AXP517PdNegotiator & pd()
Access the USB-PD sink negotiator.
AXP517Charger & charger()
Access the AXP517 charger driver.
bool begin(SensorCommCustom::CustomCallback callback, SensorCommCustomHal::CustomHalCallback hal_cb, uint8_t addr)
Initialization using a custom communication interface.
AXP517Irq & irq()
Access the AXP517 IRQ driver.
Parsed Source_Capabilities message.
uint8_t totalPdoCount
Total PDO count advertised in the message.
FixedOffer fixed[7]
Parsed fixed PDO offers.
uint8_t fixedCount
Number of valid entries in fixed[].
bool vbusPresent
VBUS detected / good (USB power present)