47#error "AXP517 interrupt example requires a valid PMIC_IRQ pin."
50static constexpr uint32_t IRQ_DRAIN_MS = 250;
51static constexpr uint32_t IRQ_PENDING_PRINT_MS = 1000;
90 default:
return "Unknown";
96 return present ?
"Present" :
"Absent";
101 return attached ?
"Attached" :
"Detached";
106 return value ?
"yes" :
"no";
111 if (snapshot.
charging)
return "Charging";
119 snprintf(buffer,
sizeof(buffer),
"0x%04X", value);
120 Serial.print(buffer);
126 snprintf(buffer,
sizeof(buffer),
"0x%08lX",
static_cast<unsigned long>(value));
127 Serial.print(buffer);
136 snapshot.
valid =
true;
139 snapshot.
charging = charger.charging;
162 Serial.print(
" TCPC ");
207 Serial.print(
"none");
214 Serial.print(
" PMIC ");
304 Serial.print(
"none");
309bool printBoolChange(
const char *label,
bool before,
bool after,
const char *(*name)(
bool),
bool &first)
311 if (before == after) {
320 Serial.print(name(before));
321 Serial.print(
" -> ");
322 Serial.print(name(after));
331 if (strcmp(beforeName, afterName) == 0) {
338 Serial.print(
"Charge: ");
339 Serial.print(beforeName);
340 Serial.print(
" -> ");
341 Serial.print(afterName);
351 Serial.println(
" no change");
359 Serial.print(
" CHG VBUS=");
361 Serial.print(
" Battery=");
363 Serial.print(
" Charge=");
365 Serial.print(
" Fault=");
368 Serial.print(
" code=");
373 Serial.print(
" TCPC CC=");
375 Serial.print(
" VBUS=");
384 Serial.print(
" BC1.2 Type=");
386 Serial.print(
" raw=");
388 Serial.print(
" detecting=");
399 Serial.print(
" CHG ");
406 if (!first) Serial.print(
" | ");
407 Serial.print(
"FaultCode: ");
409 Serial.print(
" -> ");
415 Serial.print(
" TYPEC ");
420 if (!first) Serial.print(
" | ");
421 Serial.print(
"VBUS: ");
428 Serial.print(
" -> ");
439 Serial.print(
" BC1.2 ");
442 Serial.print(
"Type: ");
444 Serial.print(
" -> ");
449 if (!first) Serial.print(
" | ");
450 Serial.print(
"Raw: ");
452 Serial.print(
" -> ");
471 Serial.print(millis());
472 Serial.print(
" ms] IRQ #");
475 Serial.print(
" RAW pd=");
476 printHex16(
static_cast<uint16_t
>(status >> 32));
477 Serial.print(
" pmic=");
479 Serial.print(
" pin=");
480 Serial.println(digitalRead(
PMIC_IRQ) == LOW ?
"LOW" :
"HIGH");
492 uint32_t start = millis();
505 }
while (millis() - start < IRQ_DRAIN_MS);
508 Serial.println(
"PMIC_IRQ is still LOW after drain.");
514 Serial.begin(115200);
517 Serial.println(
"\n=== AXP517 Interrupt Example ===\n");
520 Serial.println(
"Failed to init AXP517 or PMIC_IRQ pin!");
521 while (1) delay(1000);
560 Serial.println(
"Failed to enable AXP517 IRQ mask.");
563 Serial.println(
"Failed to clear AXP517 IRQ status.");
569 Serial.print(
"SDA: ");
571 Serial.print(
"SCL: ");
573 Serial.print(
"PMIC_IRQ: ");
576 Serial.println(
"\nInitial status:");
578 Serial.println(
"\nWaiting for AXP517 interrupts...");
583 static uint32_t lastPendingPrint = 0;
587 Serial.println(
"\nIRQ Triggered");
588 }
else if (millis() - lastPendingPrint > IRQ_PENDING_PRINT_MS) {
589 lastPendingPrint = millis();
590 Serial.println(
"\nIRQ Pending");
void printSnapshot(const StatusSnapshot &snapshot)
StatusSnapshot readStatusSnapshot()
const char * attachedName(bool attached)
bool printBoolChange(const char *label, bool before, bool after, const char *(*name)(bool), bool &first)
const char * bc12TypeName(PmicBc12Base::PortType type)
void printStatusLine(const char *label, bool anyChange)
void printHex16(uint16_t value)
bool printChargeChange(const StatusSnapshot &before, const StatusSnapshot &after, bool &first)
StatusSnapshot lastSnapshot
void printHex32(uint32_t value)
const char * presentName(bool present)
void printEventItem(bool &first, const char *text)
volatile bool irqTriggered
const char * chargeName(const StatusSnapshot &snapshot)
void printPmicIrqLine(uint64_t status)
const char * yesNoName(bool value)
void printPdAlertLine(uint16_t pdAlert)
void printSnapshotChanges(const StatusSnapshot &before, const StatusSnapshot &after)
Result readResult() override
Read the current BC1.2 detection result.
bool enableAutoDetect(bool enable) override
Enable or disable automatic BC1.2 detection.
Status getStatus() override
Get current charger status (best-effort).
static constexpr uint64_t IRQ_CHARGE_START
Battery charge start.
static bool isChargeToNormal(uint64_t mask)
static bool isBc12DetectFinished(uint64_t mask)
bool enable(uint64_t mask) override
Enable one or more PMIC IRQ sources.
static bool isPwrOnShortPress(uint64_t mask)
static constexpr uint64_t IRQ_VBUS_FAULT
VBUS Fault.
static constexpr uint64_t IRQ_BAT_UNDER_TEMP_CHG
Battery Under Temperature in Charge mode.
static constexpr uint64_t IRQ_CHG_SAFETY_TIMER
Charger Safety Timer1/2 expire.
static bool isBatteryRemove(uint64_t mask)
static bool isBatFetOcp(uint64_t mask)
static bool isBatOverTempWork(uint64_t mask)
static constexpr uint64_t IRQ_VBUS_REMOVE
VBUS Remove.
static constexpr uint64_t IRQ_DIE_OVER_TEMP_LEVEL1
DIE Over Temperature level1.
static bool isWatchdogExpire(uint64_t mask)
static bool isDieOverTempLevel1(uint64_t mask)
static constexpr uint64_t IRQ_VBUS_INSERT
VBUS Insert.
static constexpr uint64_t IRQ_BATTERY_INSERT
Battery Insert.
static constexpr uint64_t IRQ_BAT_FET_OCP
BAT FET Over Current Protection.
static constexpr uint64_t IRQ_GAUGE_NEW_SOC
Gauge New SOC.
static bool isSocShutdownLevel(uint64_t mask)
static bool isBatteryInsert(uint64_t mask)
static bool isBatOverTempQuitChg(uint64_t mask)
static constexpr uint64_t IRQ_CHARGE_DONE
Battery charge done.
static constexpr uint64_t IRQ_PWR_ON_NEGATIVE_EDGE
PWR_ON pin Negative Edge.
static bool isVbusFault(uint64_t mask)
static constexpr uint64_t IRQ_BC12_DETECT_FINISHED
BC1.2 detect finished.
static constexpr uint64_t IRQ_BATTERY_REMOVE
Battery Remove.
static bool isChgSafetyTimer(uint64_t mask)
static bool isBatUnderTempWork(uint64_t mask)
static constexpr uint64_t IRQ_CHARGE_TO_NORMAL
Charge to normal.
static constexpr uint64_t IRQ_WATCHDOG_EXPIRE
Watchdog Expire.
static constexpr uint64_t IRQ_PWR_ON_SHORT_PRESS
PWR_ON pin Short PRESS.
static bool isBatUnderTempChg(uint64_t mask)
static bool isChargeDone(uint64_t mask)
static bool isSocWarningLevel(uint64_t mask)
static bool isVbusInsert(uint64_t mask)
static constexpr uint64_t IRQ_BAT_OVER_TEMP_CHG
Battery Over Temperature in Charge mode.
uint64_t readStatus(bool clear=true) override
Read the current IRQ status.
static constexpr uint64_t IRQ_VBUS_OVER_VOLTAGE
VBUS Over Voltage.
static bool isVbusOverVoltage(uint64_t mask)
static bool isGaugeNewSoc(uint64_t mask)
static constexpr uint64_t IRQ_PWR_ON_LONG_PRESS
PWR_ON pin Long PRESS.
static constexpr uint64_t IRQ_BC12_DETECT_CHANGE
BC1.2 detect result change.
static constexpr uint64_t IRQ_BAT_OVER_VOLTAGE
Battery Over Voltage Protection.
static bool isPwrOnPositiveEdge(uint64_t mask)
static constexpr uint64_t IRQ_BAT_UNDER_TEMP_WORK
Battery Under Temperature in Work mode.
static bool isBc12DetectChange(uint64_t mask)
bool clearStatus() override
Clear all pending PMIC IRQ and TCPC alert status bits.
static bool isBatOverVoltage(uint64_t mask)
static bool isChargeStart(uint64_t mask)
static bool isVbusRemove(uint64_t mask)
static constexpr uint64_t IRQ_BAT_OVER_TEMP_QUIT_CHG
Battery Over Temperature Quit in Charge mode.
static constexpr uint64_t IRQ_SOC_SHUTDOWN_LEVEL
SOC drop to Shutdown Level.
static bool isPwrOnLongPress(uint64_t mask)
static bool isBatOverTempChg(uint64_t mask)
static bool isPwrOnNegativeEdge(uint64_t mask)
static constexpr uint64_t IRQ_SOC_WARNING_LEVEL
SOC drop to Warning Level.
static constexpr uint64_t IRQ_BAT_OVER_TEMP_WORK
Battery Over Temperature in Work mode.
static bool isBoostOverVoltage(uint64_t mask)
static constexpr uint64_t IRQ_PWR_ON_POSITIVE_EDGE
PWR_ON pin Positive Edge.
static constexpr uint64_t IRQ_BOOST_OVER_VOLTAGE
BOOST Over Voltage.
bool isAttached()
Check whether either CC pin indicates attachment.
@ AlertExtendedStatus
EXTENDED_STATUS register changed.
@ AlertTxFailed
Message transmission failed.
@ AlertPowerStatus
POWER_STATUS register changed.
@ AlertExtended
ALERT_EXTENDED register changed.
@ AlertTxSuccess
Message transmission completed.
@ AlertRxHardReset
Hard Reset received.
@ AlertVbusAlarmHi
VBUS high-voltage alarm.
@ AlertRxStatus
RX buffer contains a SOP message.
@ AlertTxDiscarded
Transmission discarded due to RX activity.
@ AlertVbusAlarmLo
VBUS low-voltage alarm.
@ AlertRxBufOverflow
RX buffer overflow.
@ 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.
High-level driver facade for the X-Powers AXP517 PMIC.
AXP517Tcpc & tcpc()
Access the low-level AXP517 TCPC helper.
bool enableModule(Module module, bool enable)
Enable or disable a functional module.
AXP517Bc12 & bc12()
Access the AXP517 BC1.2 driver.
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.
PortType
Common detected port/source type.
@ SDP
Standard Downstream Port (USB 500mA/900mA)
@ None
no valid detection / not connected
@ DCP
Dedicated Charging Port.
@ CDP
Charging Downstream Port.
uint32_t chargerFaultCode
PmicBc12Base::PortType bc12Type