manuelbl / usb-pd-arduino

USB Power Delivery for Arduino
MIT License
56 stars 10 forks source link

SNK PDO generation #4

Open topherbuckley opened 1 year ago

topherbuckley commented 1 year ago

Hello,

You pointed me here from my question on the tinyusb repo. I had a quick skim through the code, but I'm not well versed in C++. Is there code in here somewhere that handles the generation of PDOs?

In particular I'm looking for some kind of function that takes in the binary values of the various Descriptions specified in Table 6-16 of the USB-PD Standard V1.8 and returns an array of bytes that I can pass via i2c. I'm normally doing everything in C, so I'll likely end up making my own, but just wanted to check if you had something to work off of, and if so, I could potentially add what I'm working on to this instead.

image

manuelbl commented 1 year ago

The message that you mentioned is used by power sinks to advertise their capabilities. That's a rare case and not implemented by this library. The very similar message of a power source advertising its capabilities is implemented. You'll find it here:

https://github.com/manuelbl/usb-pd-arduino/blob/61c293a49bd17d6cd7fba3bcdb8e7b7f88edfc5a/src/PDSourceCapability.h#L38-L39

The input is a PDMessage instance. If you have the raw USB PD message with 2 bytes of header and multiple bytes of payload, just copy them to the position where the instance variable header is, e.g.:

PDMessage msg;
const uint8_t* rawMessage = ...;
int rawMessageLength = ...;
memcpy((uint8_t*)msg.header, rawMessage, rawMessageLength);

Then create an array for the resulting capabilities and call xxx:

int numCapabilities = 0;
PDSourceCapability capabilities[8];
PDSourceCapability::parseCapability(&msg, numCapabilities, capabilities);