SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
UsbPdDefs.hpp
Go to the documentation of this file.
1
31#pragma once
32
33#include <stdint.h>
34
38namespace usbpd
39{
40
46inline uint16_t le16(const uint8_t *p)
47{
48 return (uint16_t)p[0] | ((uint16_t)p[1] << 8);
49}
50
56inline uint32_t le32(const uint8_t *p)
57{
58 return (uint32_t)p[0] |
59 ((uint32_t)p[1] << 8) |
60 ((uint32_t)p[2] << 16) |
61 ((uint32_t)p[3] << 24);
62}
63
69inline void wr_le16(uint8_t *p, uint16_t v)
70{
71 p[0] = (uint8_t)(v & 0xFF);
72 p[1] = (uint8_t)((v >> 8) & 0xFF);
73}
74
80inline void wr_le32(uint8_t *p, uint32_t v)
81{
82 p[0] = (uint8_t)(v & 0xFF);
83 p[1] = (uint8_t)((v >> 8) & 0xFF);
84 p[2] = (uint8_t)((v >> 16) & 0xFF);
85 p[3] = (uint8_t)((v >> 24) & 0xFF);
86}
87
91struct Header {
93 uint16_t raw = 0;
94
96 Header() = default;
97
102 explicit Header(uint16_t v) : raw(v) {}
103
105 uint8_t msgType() const
106 {
107 return (uint8_t)(raw & 0x1F);
108 }
109
111 bool dataRoleDfp() const
112 {
113 return ((raw >> 5) & 0x1) != 0;
114 }
115
117 uint8_t specRev() const
118 {
119 return (uint8_t)((raw >> 6) & 0x3);
120 }
121
123 bool powerRoleSource() const
124 {
125 return ((raw >> 8) & 0x1) != 0;
126 }
127
129 uint8_t msgId() const
130 {
131 return (uint8_t)((raw >> 9) & 0x7);
132 }
133
135 uint8_t objCount() const
136 {
137 return (uint8_t)((raw >> 12) & 0x7);
138 }
139
141 bool extended() const
142 {
143 return ((raw >> 15) & 0x1) != 0;
144 }
145
147 void setMsgType(uint8_t v)
148 {
149 raw = (uint16_t)((raw & ~0x001F) | (v & 0x1F));
150 }
151
153 void setDataRoleDfp(bool dfp)
154 {
155 raw = (uint16_t)((raw & ~(1u << 5)) | ((uint16_t)dfp << 5));
156 }
157
159 void setSpecRev(uint8_t r)
160 {
161 raw = (uint16_t)((raw & ~(3u << 6)) | ((uint16_t)(r & 0x3) << 6));
162 }
163
165 void setPowerRoleSource(bool src)
166 {
167 raw = (uint16_t)((raw & ~(1u << 8)) | ((uint16_t)src << 8));
168 }
169
171 void setMsgId(uint8_t id)
172 {
173 raw = (uint16_t)((raw & ~(7u << 9)) | ((uint16_t)(id & 0x7) << 9));
174 }
175
177 void setObjCount(uint8_t n)
178 {
179 raw = (uint16_t)((raw & ~(7u << 12)) | ((uint16_t)(n & 0x7) << 12));
180 }
181
183 void setExtended(bool ex)
184 {
185 raw = (uint16_t)((raw & ~(1u << 15)) | ((uint16_t)ex << 15));
186 }
187};
188
192enum class SpecRev : uint8_t {
193 Rev10 = 0,
194 Rev20 = 1,
195 Rev30 = 2,
196};
197
201enum class ControlMsg : uint8_t {
202 GoodCRC = 0x01,
203 GotoMin = 0x02,
204 Accept = 0x03,
205 Reject = 0x04,
206 Ping = 0x05,
207 PS_RDY = 0x06,
208 GetSourceCap = 0x07,
209 GetSinkCap = 0x08,
210 DR_Swap = 0x09,
211 PR_Swap = 0x0A,
212 VCONN_Swap = 0x0B,
213 Wait = 0x0C,
214 SoftReset = 0x0D,
215};
216
220enum class DataMsg : uint8_t {
221 SourceCapabilities = 0x01,
222 Request = 0x02,
223 BIST = 0x03,
224 SinkCapabilities = 0x04,
225 VendorDefined = 0x0F,
226};
227
237inline uint16_t buildControlHeader(
238 ControlMsg type,
239 uint8_t msgId,
240 bool powerRoleSource,
241 bool dataRoleDfp,
242 SpecRev specRev
243)
244{
245 Header h{};
246 h.setExtended(false);
247 h.setObjCount(0);
248 h.setMsgId(msgId);
249 h.setPowerRoleSource(powerRoleSource);
250 h.setSpecRev((uint8_t)specRev);
251 h.setDataRoleDfp(dataRoleDfp);
252 h.setMsgType((uint8_t)type);
253 return h.raw;
254}
255
266inline uint16_t buildDataHeader(
267 DataMsg type,
268 uint8_t objCount,
269 uint8_t msgId,
270 bool powerRoleSource,
271 bool dataRoleDfp,
272 SpecRev specRev
273)
274{
275 Header h{};
276 h.setExtended(false);
277 h.setObjCount(objCount);
278 h.setMsgId(msgId);
279 h.setPowerRoleSource(powerRoleSource);
280 h.setSpecRev((uint8_t)specRev);
281 h.setDataRoleDfp(dataRoleDfp);
282 h.setMsgType((uint8_t)type);
283 return h.raw;
284}
285
289enum class PdoType : uint8_t {
290 Fixed = 0b00,
291 Battery = 0b01,
292 Variable = 0b10,
293 Augmented = 0b11,
294};
295
300 uint16_t mv = 0;
301 uint16_t ma = 0;
302 bool usbCommCapable = false;
303 bool dualRolePower = false;
304};
305
311inline PdoType pdoType(uint32_t pdo)
312{
313 return (PdoType)((pdo >> 30) & 0x03);
314}
315
322inline bool parseFixedSourcePdo(uint32_t pdo, FixedSourcePdo &out)
323{
324 if (pdoType(pdo) != PdoType::Fixed) return false;
325
326 uint16_t v50 = (uint16_t)((pdo >> 10) & 0x03FF);
327 uint16_t i10 = (uint16_t)(pdo & 0x03FF);
328
329 out.mv = (uint16_t)(v50 * 50);
330 out.ma = (uint16_t)(i10 * 10);
331
332 out.usbCommCapable = ((pdo >> 26) & 0x1) != 0;
333 out.dualRolePower = ((pdo >> 29) & 0x1) != 0;
334 return true;
335}
336
352inline uint32_t buildRdoFixedCurrent(
353 uint8_t objectPos1based,
354 uint16_t opMa,
355 uint16_t maxMa,
356 bool capMismatch = false,
357 bool usbCommCapable = true,
358 bool noUsbSuspend = true
359)
360{
361 if (objectPos1based < 1) objectPos1based = 1;
362 if (objectPos1based > 7) objectPos1based = 7;
363
364 uint16_t opI10 = (uint16_t)(opMa / 10);
365 uint16_t maxI10 = (uint16_t)(maxMa / 10);
366 if (opI10 > 0x3FF) opI10 = 0x3FF;
367 if (maxI10 > 0x3FF) maxI10 = 0x3FF;
368
369 uint32_t rdo = 0;
370 rdo |= ((uint32_t)(objectPos1based & 0x0F) << 28);
371 rdo |= ((uint32_t)(capMismatch ? 1u : 0u) << 26);
372 rdo |= ((uint32_t)(usbCommCapable ? 1u : 0u) << 25);
373 rdo |= ((uint32_t)(noUsbSuspend ? 1u : 0u) << 24);
374 rdo |= ((uint32_t)(opI10 & 0x3FF) << 10);
375 rdo |= ((uint32_t)(maxI10 & 0x3FF) << 0);
376 return rdo;
377}
378
379} // namespace usbpd
USB Power Delivery protocol helper definitions.
Definition UsbPdDefs.hpp:39
PdoType pdoType(uint32_t pdo)
Return the type field of a raw PDO.
bool parseFixedSourcePdo(uint32_t pdo, FixedSourcePdo &out)
Parse a fixed source PDO.
SpecRev
USB-PD specification revision values used in message headers.
@ Rev10
USB-PD 1.0.
@ Rev30
USB-PD 3.0.
@ Rev20
USB-PD 2.0.
ControlMsg
USB-PD control message type field values.
@ VCONN_Swap
VCONN Swap.
@ SoftReset
Soft Reset.
@ DR_Swap
Data Role Swap.
@ PS_RDY
Power Supply Ready.
@ PR_Swap
Power Role Swap.
@ GetSinkCap
Get Sink Capabilities.
@ GetSourceCap
Get Source Capabilities.
DataMsg
USB-PD data message type field values.
@ SinkCapabilities
Sink_Capabilities.
@ Request
Request.
@ BIST
Built-In Self Test.
@ SourceCapabilities
Source_Capabilities.
@ VendorDefined
Vendor Defined Message.
uint16_t buildControlHeader(ControlMsg type, uint8_t msgId, bool powerRoleSource, bool dataRoleDfp, SpecRev specRev)
Build a USB-PD control message header.
void wr_le32(uint8_t *p, uint32_t v)
Write a 32-bit value to a byte buffer in little-endian order.
Definition UsbPdDefs.hpp:80
void wr_le16(uint8_t *p, uint16_t v)
Write a 16-bit value to a byte buffer in little-endian order.
Definition UsbPdDefs.hpp:69
uint16_t buildDataHeader(DataMsg type, uint8_t objCount, uint8_t msgId, bool powerRoleSource, bool dataRoleDfp, SpecRev specRev)
Build a USB-PD data message header.
PdoType
USB-PD Power Data Object type.
@ Fixed
Fixed supply PDO.
@ Variable
Variable supply PDO.
@ Augmented
Augmented PDO.
@ Battery
Battery supply PDO.
uint32_t le32(const uint8_t *p)
Read a 32-bit little-endian integer from a byte buffer.
Definition UsbPdDefs.hpp:56
uint32_t buildRdoFixedCurrent(uint8_t objectPos1based, uint16_t opMa, uint16_t maxMa, bool capMismatch=false, bool usbCommCapable=true, bool noUsbSuspend=true)
Build a fixed-current Request Data Object.
uint16_t le16(const uint8_t *p)
Read a 16-bit little-endian integer from a byte buffer.
Definition UsbPdDefs.hpp:46
Parsed fixed source PDO fields.
uint16_t ma
Maximum current in milliamps.
bool dualRolePower
Dual-role power flag.
uint16_t mv
Voltage in millivolts.
bool usbCommCapable
USB communications capable flag.
USB-PD 16-bit message header wrapper.
Definition UsbPdDefs.hpp:91
void setMsgType(uint8_t v)
Set the USB-PD message type field.
void setObjCount(uint8_t n)
Set the number of 32-bit data objects.
uint8_t specRev() const
Return the USB-PD specification revision field.
bool powerRoleSource() const
Return true when the power role field is Source.
void setPowerRoleSource(bool src)
Set the power role field.
Header(uint16_t v)
Construct a header from a raw value.
void setExtended(bool ex)
Set or clear the extended message bit.
uint16_t raw
Raw 16-bit header value in little-endian host order.
Definition UsbPdDefs.hpp:93
uint8_t objCount() const
Return the number of 32-bit data objects in this message.
bool dataRoleDfp() const
Return true when the data role field is DFP.
Header()=default
Construct an empty header.
void setMsgId(uint8_t id)
Set the USB-PD message ID field.
void setDataRoleDfp(bool dfp)
Set the data role field.
uint8_t msgId() const
Return the USB-PD message ID field.
uint8_t msgType() const
Return the USB-PD message type field.
bool extended() const
Return true when the extended message bit is set.
void setSpecRev(uint8_t r)
Set the USB-PD specification revision field.