10static constexpr uint8_t PD_MAX_NUM_DATA_OBJECTS = 7;
12static constexpr uint8_t MIN_REQUEST_VOLTAGE = 5;
13static constexpr uint8_t MAX_REQUEST_VOLTAGE = 20;
109 this->
request_voltage_ = std::clamp(voltage, MIN_REQUEST_VOLTAGE, MAX_REQUEST_VOLTAGE);
165 void handle_data_message_(
const PdMsg &msg);
166 void handle_control_message_(
const PdMsg &msg);
167 void handle_soft_reset_(
const PdMsg &msg);
168 void respond_to_source_caps_(
const PdMsg &msg);
169 bool send_request_();
170 void request_failed_();
173 PdContract accepted_contract_{};
175 bool active_ams_{
false};
177 bool ps_rdy_expected_{
false};
178 uint8_t last_received_msg_id_{255};
179 uint8_t msg_counter_{0};
180 uint8_t wait_retries_{0};
USB-PD sink protocol logic, independent of the PHY chip.
PdMsg make_control_msg_(PdControlMsgType type) const
virtual void on_source_caps_received()=0
Called when source capabilities arrive, before the request for them is sent.
void set_contract_(const PdContract &contract)
bool is_connected() const
uint32_t wait_received_ms_
bool retry_request_pending_
std::atomic< uint8_t > request_voltage_
PdMsg make_data_msg_(PdDataMsgType type, const uint32_t *objects, uint8_t len) const
uint8_t get_request_voltage() const
void set_ams_(bool active)
PdState get_state() const
std::atomic< uint32_t > contract_
void set_state_(PdState state)
std::atomic< PdState > state_
void set_request_voltage(uint8_t voltage)
void handle_message_(const PdMsg &msg)
void check_request_retry_()
Sends the last request again once tSinkRequest has passed after the source answered Wait.
virtual void on_state_changed()=0
Called after state_ or contract_ changed, to let the main loop publish it.
virtual bool send_message(const PdMsg &msg)=0
Returns false when the message could not be handed to the chip; the PHY logs the failure.
void handle_transmit_failed_()
Called by the PHY when the chip gave up on a message after its retries without receiving a GoodCRC.
virtual void on_soft_reset_received()=0
Called after the source soft reset the protocol; it sends new source capabilities next.
virtual void on_request_failed()=0
Called when a request ended without a new contract: it was rejected, the source kept answering Wait o...
bool check_ams_()
Returns true while an atomic message sequence is in progress.
void set_state_and_contract_(PdState state, const PdContract &contract)
Stores both before calling on_state_changed() once, so the main loop never sees the new state with th...
void check_transition_timeout_()
Falls back to default power when the source accepted a request but did not report the new supply read...
virtual void on_transition_timeout()=0
Called after an accepted request was not followed by PS_RDY in time and the state fell back to defaul...
@ PD_STATE_EXPLICIT_CONTRACT
@ PD_STATE_DEFAULT_CONTRACT
@ PD_STATE_PD_TIMEOUT
Negotiation is over without a PD contract (no capabilities, or the request was not accepted); the sou...
@ PD_CNTRL_GET_SOURCE_CAP
bool is_connected_state(PdState state)
Returns true for the states in which a source is attached and supplying power.
@ PD_PDO_TYPE_VARIABLE_SUPPLY
@ PD_PDO_TYPE_FIXED_SUPPLY
A contract packed into one word so it can be shared between tasks as an atomic.
static PackedContract from(const PdContract &contract)
bool operator==(const PdContract &other) const
bool operator!=(const PdContract &other) const
void set_header(uint16_t header)
uint16_t get_coded_header() const
uint32_t data_objects[PD_MAX_NUM_DATA_OBJECTS]