SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
AXP517PdNegotiator Class Reference

Minimal interrupt-driven USB-PD sink negotiation state machine. More...

#include <AXP517PdNegotiator.hpp>

Classes

struct  FixedOffer
 One fixed supply PDO offered by the source. More...
 
struct  RequestParams
 Requested sink operating point. More...
 
struct  SourceCaps
 Parsed Source_Capabilities message. More...
 

Public Types

enum class  State : uint8_t {
  Idle , WaitSourceCap , WaitAccept , WaitPsRdy ,
  Success , Failed
}
 Negotiation state. More...
 
enum class  Event : uint8_t {
  None , SourceCapabilities , RequestSent , TxSuccess ,
  TxFailed , TxDiscarded , Accept , Reject ,
  PsRdy , HardReset , CcDetached , CcChanged
}
 Event reported by service() and handleIrq(). More...
 

Public Member Functions

 AXP517PdNegotiator (AXP517Tcpc &tcpc, AXP517Pd &pd)
 Construct a negotiator from the low-level TCPC and PD helpers.
 
bool setIrqPin (int irqPin)
 Configure the PMIC/TCPC IRQ pin used for PD negotiation.
 
bool hasIrqPin () const
 Check whether a valid IRQ pin is configured.
 
bool initSink (int irqPin)
 Initialize the TCPC sink and configure the PD receive path.
 
bool negotiate (const RequestParams &req, SourceCaps *outCaps=nullptr, uint32_t timeoutMs=6000)
 Run a blocking fixed PDO negotiation.
 
bool service (const RequestParams &req, Event &outEvent, SourceCaps *outCaps=nullptr)
 Service pending IRQ-driven PD events.
 
bool handleIrq (const RequestParams &req, Event &outEvent, SourceCaps *outCaps=nullptr)
 Process one TCPC IRQ status sample.
 
bool requestFromCaps (const SourceCaps &caps, const RequestParams &req)
 Send a Request message selected from parsed source capabilities.
 
bool parseSourceCaps (const AXP517Tcpc::RxFifoMsg &rx, SourceCaps &outCaps)
 Parse fixed PDOs from a Source_Capabilities RX message.
 
bool selectFixedOffer (const SourceCaps &caps, const RequestParams &req, FixedOffer &outOffer)
 Select one fixed PDO offer matching the request.
 
State state () const
 Return the current negotiation state.
 
bool isSuccess () const
 Check whether negotiation completed successfully.
 
bool isDone () const
 Check whether negotiation reached a terminal state.
 
void reset ()
 Reset message IDs and return to the initial wait state.
 

Detailed Description

Minimal interrupt-driven USB-PD sink negotiation state machine.

The AXP517 TCPC delivers USB-PD messages through an IRQ-driven RX FIFO. A valid interrupt pin is therefore required before requesting a PD voltage. This class requests a fixed PDO by voltage or PDO index, waits for Accept and PS_RDY, and exposes parsed source capabilities for diagnostics.

Definition at line 45 of file AXP517PdNegotiator.hpp.

Member Enumeration Documentation

◆ Event

enum class AXP517PdNegotiator::Event : uint8_t
strong

Event reported by service() and handleIrq().

Enumerator
None 

No meaningful state change.

SourceCapabilities 

Source_Capabilities received.

RequestSent 

Request message transmitted.

TxSuccess 

TCPC reported TX success.

TxFailed 

TCPC reported TX failure.

TxDiscarded 

TCPC discarded TX due to RX activity.

Accept 

Accept control message received.

Reject 

Reject control message received.

PsRdy 

PS_RDY received; contract is active.

HardReset 

Hard Reset received.

CcDetached 

CC or VBUS detach detected.

CcChanged 

CC state changed while attached.

Definition at line 103 of file AXP517PdNegotiator.hpp.

◆ State

enum class AXP517PdNegotiator::State : uint8_t
strong

Negotiation state.

Enumerator
Idle 

Not initialized or inactive.

WaitSourceCap 

Waiting for Source_Capabilities.

WaitAccept 

Request sent; waiting for Accept.

WaitPsRdy 

Accept received; waiting for PS_RDY.

Success 

Requested contract is ready.

Failed 

Negotiation failed.

Definition at line 91 of file AXP517PdNegotiator.hpp.

Constructor & Destructor Documentation

◆ AXP517PdNegotiator()

AXP517PdNegotiator::AXP517PdNegotiator ( AXP517Tcpc tcpc,
AXP517Pd pd 
)
explicit

Construct a negotiator from the low-level TCPC and PD helpers.

Parameters
tcpcTCPC register/FIFO access object.
pdUSB-PD message helper.

Definition at line 73 of file AXP517PdNegotiator.cpp.

Member Function Documentation

◆ handleIrq()

◆ hasIrqPin()

bool AXP517PdNegotiator::hasIrqPin ( ) const
inline

Check whether a valid IRQ pin is configured.

Return values
trueif an IRQ pin has been configured.

Definition at line 129 of file AXP517PdNegotiator.hpp.

Referenced by handleIrq(), negotiate(), and service().

◆ initSink()

bool AXP517PdNegotiator::initSink ( int  irqPin)

Initialize the TCPC sink and configure the PD receive path.

Parameters
irqPinPlatform GPIO number connected to PMIC_IRQ.
Return values
trueon success, false on invalid IRQ pin or TCPC setup failure.

Definition at line 90 of file AXP517PdNegotiator.cpp.

References AXP517Tcpc::checkVendorId(), AXP517Tcpc::clearPmicIrqStatus(), AXP517Tcpc::init(), AXP517Pd::resetMsgId(), SENSORLIB_LOG_E, setIrqPin(), and WaitSourceCap.

Referenced by PmicAXP517::initPdSink(), and negotiate().

◆ isDone()

bool AXP517PdNegotiator::isDone ( ) const
inline

Check whether negotiation reached a terminal state.

Definition at line 215 of file AXP517PdNegotiator.hpp.

References Failed, and Success.

Referenced by negotiate(), and service().

◆ isSuccess()

bool AXP517PdNegotiator::isSuccess ( ) const
inline

Check whether negotiation completed successfully.

Definition at line 207 of file AXP517PdNegotiator.hpp.

References Success.

Referenced by negotiate().

◆ negotiate()

bool AXP517PdNegotiator::negotiate ( const RequestParams req,
SourceCaps outCaps = nullptr,
uint32_t  timeoutMs = 6000 
)

Run a blocking fixed PDO negotiation.

Parameters
reqRequested operating point.
outCapsOptional destination for parsed source capabilities.
timeoutMsTimeout in milliseconds.
Return values
truewhen a PD contract reaches PS_RDY, false otherwise.

Definition at line 129 of file AXP517PdNegotiator.cpp.

References AXP517Tcpc::clearPmicIrqStatus(), AXP517Tcpc::delayMs(), AXP517Tcpc::digitalRead(), handleIrq(), hasIrqPin(), AXP517Tcpc::init(), initSink(), isDone(), isSuccess(), AXP517Tcpc::millis(), None, AXP517Tcpc::readAlert(), reset(), SENSORLIB_LOG_E, SENSORLIB_LOG_W, and service().

Referenced by PmicAXP517::requestPd().

◆ parseSourceCaps()

◆ requestFromCaps()

bool AXP517PdNegotiator::requestFromCaps ( const SourceCaps caps,
const RequestParams req 
)

Send a Request message selected from parsed source capabilities.

Parameters
capsParsed source capabilities.
reqRequested operating point.
Return values
trueon success, false when no matching offer exists or TX fails.

Definition at line 385 of file AXP517PdNegotiator.cpp.

References AXP517Tcpc::clearAllAlerts(), AXP517Tcpc::clearTx(), and selectFixedOffer().

Referenced by handleIrq().

◆ reset()

void AXP517PdNegotiator::reset ( )

Reset message IDs and return to the initial wait state.

Definition at line 121 of file AXP517PdNegotiator.cpp.

References Idle, AXP517Pd::resetMsgId(), and WaitSourceCap.

Referenced by handleIrq(), negotiate(), and resetPdState().

◆ selectFixedOffer()

bool AXP517PdNegotiator::selectFixedOffer ( const SourceCaps caps,
const RequestParams req,
FixedOffer outOffer 
)

Select one fixed PDO offer matching the request.

Parameters
capsParsed source capabilities.
reqRequested operating point.
outOfferReceives the selected offer.
Return values
truewhen a matching fixed offer was found.

Definition at line 356 of file AXP517PdNegotiator.cpp.

References AXP517PdNegotiator::SourceCaps::fixed, AXP517PdNegotiator::SourceCaps::fixedCount, i, AXP517PdNegotiator::FixedOffer::mv, AXP517PdNegotiator::FixedOffer::pdoIndex1Based, AXP517PdNegotiator::RequestParams::pdoIndex1Based, and AXP517PdNegotiator::RequestParams::targetMv.

Referenced by requestFromCaps().

◆ service()

bool AXP517PdNegotiator::service ( const RequestParams req,
Event outEvent,
SourceCaps outCaps = nullptr 
)

Service pending IRQ-driven PD events.

Parameters
reqRequested operating point.
outEventReceives the most recent negotiator event.
outCapsOptional destination for parsed source capabilities.
Return values
trueon success, false on register/FIFO access failure.

Definition at line 174 of file AXP517PdNegotiator.cpp.

References AXP517Tcpc::digitalRead(), handleIrq(), hasIrqPin(), isDone(), None, AXP517Tcpc::readAlert(), and SENSORLIB_LOG_E.

Referenced by negotiate().

◆ setIrqPin()

bool AXP517PdNegotiator::setIrqPin ( int  irqPin)

Configure the PMIC/TCPC IRQ pin used for PD negotiation.

Parameters
irqPinPlatform GPIO number connected to PMIC_IRQ.
Return values
trueon success, false when irqPin is invalid.

Definition at line 78 of file AXP517PdNegotiator.cpp.

References AXP517Tcpc::pinMode().

Referenced by initSink(), and PmicAXP517::setPdIrqPin().

◆ state()

State AXP517PdNegotiator::state ( ) const
inline

Return the current negotiation state.

Definition at line 199 of file AXP517PdNegotiator.hpp.


The documentation for this class was generated from the following files: