SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
AXP517Pd.cpp
Go to the documentation of this file.
1
31#include "../../../SensorBuildOpt.h"
32#if !SENSORLIB_EXCLUDE_PMIC_AXP517
33
34#include "AXP517Pd.hpp"
35
37{
38 _cfg = cfg;
39 // AXP517_TCPC_MSG_HDR_INFO:
40 // bit3 data role, bits[2:1] rev, bit0 pwr role
41 uint8_t v = 0;
42 v |= (uint8_t)(((uint8_t)cfg.spec & 0x03) << 1);
43 if (cfg.pr == PowerRole::Source) v |= 0x01;
44 if (cfg.dr == DataRole::DFP) v |= 0x08;
45 return _tcpc.writeMessageHeaderInfo(v);
46}
47
49{
50 return _tcpc.writeReceiveDetect(enable ? 0x01 : 0x00); // SOP only
51}
52
53uint16_t AXP517Pd::buildHeader(uint8_t msgType, uint8_t objCnt)
54{
55 // PD header:
56 // bit4:0 = msgType, bit5 = dataRole, bit6:7 = specRev, bit8 = powerRole
57 // bit9:11 = msgId, bit12:14 = numDataObjects
58 uint16_t h = 0;
59 h |= (uint16_t)(msgType & 0x1F);
60 h |= (uint16_t)(((uint8_t)_cfg.dr & 0x1) << 5);
61 h |= (uint16_t)(((uint8_t)_cfg.spec & 0x03) << 6);
62 h |= (uint16_t)(((uint8_t)_cfg.pr & 0x1) << 8);
63 h |= (uint16_t)((_msgId & 0x7) << 9);
64 h |= (uint16_t)((objCnt & 0x7) << 12);
65 return h;
66}
67
69{
70 uint16_t hdr = buildHeader((uint8_t)msgType, 0);
73 hdr, nullptr, 0);
74 if (ok) _msgId = (uint8_t)((_msgId + 1) & 0x7);
75 return ok;
76}
77
82
84{
85 // Hard Reset is a special ordered set - no TX buffer writes, retryCount=0.
86 // Matches Linux axp517_tcpci_pd_transmit(type=TCPC_TX_HARD_RESET, msg=NULL):
87 // reg = (0 << TRANSMIT_RETRY_SHIFT) | (5 << TRANSMIT_TYPE_SHIFT) => 0x05
89 AXP517Tcpc::RetryCount::R0, // retryCount=0 for Hard Reset
90 0, nullptr, 0);
91}
92
93bool AXP517Pd::sendRaw(uint8_t msgType, const uint8_t *payload, uint8_t payloadLen)
94{
95 uint8_t objCnt = (uint8_t)(payloadLen / 4);
96 uint16_t hdr = buildHeader(msgType, objCnt);
99 hdr, payload, payloadLen);
100 if (ok) _msgId = (uint8_t)((_msgId + 1) & 0x7);
101 return ok;
102}
103
105{
106 return _tcpc.rxReadMessage(out);
107}
108
109bool AXP517Pd::handleFaultOnce(uint8_t &faultStatus)
110{
111 faultStatus = 0;
112 uint16_t alert = 0;
113 if (!_tcpc.readAlert(alert)) {
114 return false;
115 }
116 if (!(alert & (1u << 9))) {
117 return true;
118 }
119
120 if (!_tcpc.readFaultStatus(faultStatus)) {
121 return false;
122 }
123 (void)_tcpc.clearFaultStatus(faultStatus);
124 (void)_tcpc.clearAlert(1u << 9);
125 return true;
126}
127
128#endif
@license MIT License
bool sendControl(ControlMsg msgType)
Send a USB-PD control message.
Definition AXP517Pd.cpp:68
@ Source
Source/DFP supplies VBUS.
bool sendRaw(uint8_t msgType, const uint8_t *payload, uint8_t payloadLen)
Send a raw USB-PD data message.
Definition AXP517Pd.cpp:93
bool tryReceive(AXP517Tcpc::RxFifoMsg &out)
Try to read one pending RX FIFO message.
Definition AXP517Pd.cpp:104
bool handleFaultOnce(uint8_t &faultStatus)
Read and clear one TCPC fault status sample.
Definition AXP517Pd.cpp:109
bool setReceiveDetectSop(bool enable)
Enable or disable SOP receive detection.
Definition AXP517Pd.cpp:48
bool configureHeaderInfo(const HeaderInfoCfg &cfg)
Configure local message-header defaults.
Definition AXP517Pd.cpp:36
@ DFP
Downstream Facing Port.
ControlMsg
Supported USB-PD control message type values.
Definition AXP517Pd.hpp:81
@ GetSourceCap
Get Source Capabilities control message.
bool sendHardReset()
Send a Hard Reset ordered set.
Definition AXP517Pd.cpp:83
bool sendGetSourceCap()
Send a Get_Source_Cap control message.
Definition AXP517Pd.cpp:78
bool writeMessageHeaderInfo(uint8_t v)
Write the TCPCI MESSAGE_HEADER_INFO register.
bool clearAlert(uint16_t maskRw1c)
Clear PD_ALERTL/H bits using write-one-to-clear semantics.
bool txSend(TransmitType type, RetryCount retry, uint16_t headerLE, const uint8_t *payload, uint8_t payloadLen)
Send a USB-PD packet through the TCPC TX FIFO.
bool clearFaultStatus(uint8_t maskRw1c)
Clear TCPC FAULT_STATUS bits using write-one-to-clear semantics.
@ R3
Three retries.
bool readFaultStatus(uint8_t &out)
Read the TCPC FAULT_STATUS register.
bool writeReceiveDetect(uint8_t v)
Write the TCPCI RECEIVE_DETECT register.
bool readAlert(uint16_t &out)
Read PD_ALERTL/H.
bool rxReadMessage(RxFifoMsg &out)
Read and decode one TCPC RX FIFO message.
@ HARD_RESET
Hard Reset ordered set.
Local USB-PD header defaults used for transmitted messages.
Definition AXP517Pd.hpp:91
PowerRole pr
Local power role.
Definition AXP517Pd.hpp:93
DataRole dr
Local data role.
Definition AXP517Pd.hpp:94
SpecRev spec
USB-PD revision to advertise.
Definition AXP517Pd.hpp:92
Decoded RX FIFO message.