SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
axp517_pd_voltage.ino
Go to the documentation of this file.
1
30#include <Arduino.h>
31#include <Wire.h>
32#include <PmicDrv.hpp>
33
34#ifndef PMIC_SDA
35#define PMIC_SDA 1
36#endif
37
38#ifndef PMIC_SCL
39#define PMIC_SCL 2
40#endif
41
42#ifndef PMIC_IRQ
43#define PMIC_IRQ 44
44#endif
45
46#ifndef PD_TARGET_MV
47#define PD_TARGET_MV 9000
48#endif
49
50#ifndef PD_TIMEOUT_MS
51#define PD_TIMEOUT_MS 6000
52#endif
53
54#if PMIC_IRQ < 0
55#error "AXP517 PD negotiation requires a valid PMIC_IRQ pin."
56#endif
57
60bool pdReady = false;
61
63{
64 auto &gpio = pmic.gpio();
65
67 return false;
68 }
69
70 if (!PmicGpioBase::isOk(gpio.setDirection(0, PmicGpioBase::Direction::Output))) {
71 return false;
72 }
73
75}
76
78{
79 Serial.println("\n--- Source Capabilities ---");
80 Serial.print("Total PDO count: ");
81 Serial.println(caps.totalPdoCount);
82
83 if (caps.fixedCount == 0) {
84 Serial.println("No fixed PDO found.");
85 return;
86 }
87
88 for (uint8_t i = 0; i < caps.fixedCount; ++i) {
89 const auto &pdo = caps.fixed[i];
90
91 Serial.print("PDO");
92 Serial.print(pdo.pdoIndex1Based);
93 Serial.print(": ");
94 Serial.print(pdo.mv);
95 Serial.print(" mV / ");
96 Serial.print(pdo.maMax);
97 Serial.print(" mA, raw=0x");
98 Serial.println(pdo.rawPdo, HEX);
99 }
100}
101
103{
104 uint16_t vbusMv = 0;
105
106 if (pmic.tcpc().readVbusMv(vbusMv)) {
107 Serial.print("TCPC VBUS: ");
108 Serial.print(vbusMv);
109 Serial.println(" mV");
110 }
111}
112
113void setup()
114{
115 Serial.begin(115200);
116 delay(500);
117
118 Serial.println("\n=== AXP517 PD Voltage Example ===\n");
119 Serial.print("Target voltage: ");
120 Serial.print(PD_TARGET_MV);
121 Serial.println(" mV");
122
123 if (!pmic.begin(Wire, AXP517_SLAVE_ADDRESS, PMIC_SDA, PMIC_SCL, PMIC_IRQ)) {
124 Serial.println("Failed to init AXP517 or PMIC_IRQ pin!");
125 while (1) delay(1000);
126 }
127
128 if (!configurePdIrqOutput()) {
129 Serial.println("Failed to route AXP517 GPIO0 to PD_IRQ!");
130 while (1) delay(1000);
131 }
132
133 Serial.print("PMIC_IRQ pin: ");
134 Serial.println(PMIC_IRQ);
135 Serial.println("Connect a USB-C PD source now.");
136
139
140 Serial.println("\n--- PD Result ---");
141 Serial.println(pdReady ? "Negotiation success." : "Negotiation failed.");
142 printVbus();
143
144 if (!pdReady) {
145 Serial.println("Check PD source capability, CC wiring, and PMIC_IRQ wiring.");
146 }
147}
148
149void loop()
150{
151 static uint32_t lastPrint = 0;
152
153 if (millis() - lastPrint > 2000) {
154 lastPrint = millis();
155
156 Serial.println("\n--- Status ---");
157 Serial.print("PD: ");
158 Serial.println(pdReady ? "Ready" : "Not ready");
159 printVbus();
160 }
161
162 delay(100);
163}
@license MIT License
bool pdReady
PmicAXP517 pmic
void printVbus()
bool configurePdIrqOutput()
#define PMIC_SDA
#define PMIC_SCL
void setup()
#define PMIC_IRQ
#define PD_TARGET_MV
#define PD_TIMEOUT_MS
void printSourceCaps(const AXP517PdNegotiator::SourceCaps &caps)
AXP517PdNegotiator::SourceCaps sourceCaps
void loop()
bool setOutputSource(uint8_t pin, OutputSource src)
Select GPIO output source.
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.
AXP517Gpio & gpio()
Access the AXP517 GPIO driver.
bool requestPd(uint16_t targetMv, uint32_t timeoutMs=6000, AXP517PdNegotiator::SourceCaps *outCaps=nullptr)
Request a fixed USB-PD voltage from the attached source.
bool begin(SensorCommCustom::CustomCallback callback, SensorCommCustomHal::CustomHalCallback hal_cb, uint8_t addr)
Initialization using a custom communication interface.
@ OpenDrain
Open-drain output: only pulls low, high-impedance for high.
static constexpr bool isOk(Status s)
@ Output
Configure pin as output (drive external circuits)
uint8_t i
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[].