SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
AXP517PdNegotiator.hpp
Go to the documentation of this file.
1
31#pragma once
32
33#include <stdint.h>
34#include "AXP517Pd.hpp"
35#include "AXP517Tcpc.hpp"
36
46{
47public:
53 explicit AXP517PdNegotiator(AXP517Tcpc &tcpc, AXP517Pd &pd);
54
58 struct FixedOffer {
59 uint16_t mv = 0;
60 uint16_t maMax = 0;
61 uint8_t pdoIndex1Based = 0;
62 uint32_t rawPdo = 0;
63 };
64
68 struct SourceCaps {
69 FixedOffer fixed[7] = {};
70 uint8_t fixedCount = 0;
71 uint8_t totalPdoCount = 0;
72 uint16_t rawHeaderLE = 0;
73 };
74
79 uint8_t pdoIndex1Based = 0;
80 uint16_t targetMv = 0;
81 uint16_t opMa = 0;
82 uint16_t maxMa = 0;
83 bool capMismatch = false;
84 bool usbCommCapable = false;
85 bool noUsbSuspend = false;
86 };
87
91 enum class State : uint8_t {
92 Idle,
95 WaitPsRdy,
96 Success,
97 Failed,
98 };
99
103 enum class Event : uint8_t {
104 None,
107 TxSuccess,
108 TxFailed,
110 Accept,
111 Reject,
112 PsRdy,
113 HardReset,
114 CcDetached,
115 CcChanged,
116 };
117
123 bool setIrqPin(int irqPin);
124
129 bool hasIrqPin() const
130 {
131 return _irqPin >= 0;
132 }
133
139 bool initSink(int irqPin);
140
148 bool negotiate(const RequestParams &req, SourceCaps *outCaps = nullptr,
149 uint32_t timeoutMs = 6000);
150
158 bool service(const RequestParams &req, Event &outEvent,
159 SourceCaps *outCaps = nullptr);
160
168 bool handleIrq(const RequestParams &req, Event &outEvent,
169 SourceCaps *outCaps = nullptr);
170
177 bool requestFromCaps(const SourceCaps &caps, const RequestParams &req);
178
185 bool parseSourceCaps(const AXP517Tcpc::RxFifoMsg &rx, SourceCaps &outCaps);
186
194 bool selectFixedOffer(const SourceCaps &caps, const RequestParams &req, FixedOffer &outOffer);
195
199 State state() const
200 {
201 return _state;
202 }
203
207 bool isSuccess() const
208 {
209 return _state == State::Success;
210 }
211
215 bool isDone() const
216 {
217 return _state == State::Success || _state == State::Failed;
218 }
219
223 void reset();
224
225private:
232 bool sendRequest(const FixedOffer &offer, const RequestParams &req);
233
234private:
235 AXP517Tcpc &_tcpc;
236 AXP517Pd &_pd;
237 State _state = State::Idle;
238 int _irqPin = -1;
239 bool _sinkReady = false;
240 bool _txPending = false;
241 uint8_t _txRetry = 0;
242};
@license MIT License
@license MIT License
Minimal interrupt-driven USB-PD sink negotiation state machine.
bool handleIrq(const RequestParams &req, Event &outEvent, SourceCaps *outCaps=nullptr)
Process one TCPC IRQ status sample.
State state() const
Return the current negotiation state.
State
Negotiation state.
@ WaitAccept
Request sent; waiting for Accept.
@ Success
Requested contract is ready.
@ WaitPsRdy
Accept received; waiting for PS_RDY.
@ WaitSourceCap
Waiting for Source_Capabilities.
@ Failed
Negotiation failed.
@ Idle
Not initialized or inactive.
bool selectFixedOffer(const SourceCaps &caps, const RequestParams &req, FixedOffer &outOffer)
Select one fixed PDO offer matching the request.
bool hasIrqPin() const
Check whether a valid IRQ pin is configured.
void reset()
Reset message IDs and return to the initial wait state.
bool negotiate(const RequestParams &req, SourceCaps *outCaps=nullptr, uint32_t timeoutMs=6000)
Run a blocking fixed PDO negotiation.
bool setIrqPin(int irqPin)
Configure the PMIC/TCPC IRQ pin used for PD negotiation.
bool requestFromCaps(const SourceCaps &caps, const RequestParams &req)
Send a Request message selected from parsed source capabilities.
bool service(const RequestParams &req, Event &outEvent, SourceCaps *outCaps=nullptr)
Service pending IRQ-driven PD events.
bool parseSourceCaps(const AXP517Tcpc::RxFifoMsg &rx, SourceCaps &outCaps)
Parse fixed PDOs from a Source_Capabilities RX message.
bool initSink(int irqPin)
Initialize the TCPC sink and configure the PD receive path.
bool isDone() const
Check whether negotiation reached a terminal state.
bool isSuccess() const
Check whether negotiation completed successfully.
Event
Event reported by service() and handleIrq().
@ PsRdy
PS_RDY received; contract is active.
@ TxDiscarded
TCPC discarded TX due to RX activity.
@ TxFailed
TCPC reported TX failure.
@ None
No meaningful state change.
@ HardReset
Hard Reset received.
@ RequestSent
Request message transmitted.
@ Accept
Accept control message received.
@ SourceCapabilities
Source_Capabilities received.
@ Reject
Reject control message received.
@ CcDetached
CC or VBUS detach detected.
@ TxSuccess
TCPC reported TX success.
@ CcChanged
CC state changed while attached.
USB-PD message builder and FIFO helper for AXP517.
Definition AXP517Pd.hpp:45
AXP517 Type-C Port Controller helper.
One fixed supply PDO offered by the source.
uint16_t mv
Fixed supply voltage in millivolts.
uint32_t rawPdo
Raw 32-bit PDO value.
uint8_t pdoIndex1Based
USB-PD object position, 1 through 7.
uint16_t maMax
Maximum current in milliamps.
Requested sink operating point.
bool noUsbSuspend
Set No USB Suspend in the RDO.
uint8_t pdoIndex1Based
Optional explicit source PDO index, 1 through 7.
uint16_t targetMv
Optional fixed PDO voltage to select.
bool usbCommCapable
Set USB Communications Capable in the RDO.
uint16_t maxMa
Optional maximum current; 0 uses opMa.
uint16_t opMa
Optional operating current; 0 uses source maximum.
bool capMismatch
Set Capability Mismatch in the RDO.
Parsed Source_Capabilities message.
uint16_t rawHeaderLE
Raw Source_Capabilities header.
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[].
Decoded RX FIFO message.