Skip to content

tinyusb: Allow to replace the built-in descriptor buffer #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions cores/arduino/Adafruit_TinyUSB_Core/Adafruit_USBD_Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ Adafruit_USBD_Device::Adafruit_USBD_Device(void)
.bMaxPower = TUSB_DESC_CONFIG_POWER_MA(USB_CONFIG_POWER)
};

memcpy(_desc_cfg, &dev_cfg, sizeof(tusb_desc_configuration_t));

memcpy(_desc_cfg_buffer, &dev_cfg, sizeof(tusb_desc_configuration_t));
_desc_cfg = _desc_cfg_buffer;
_desc_cfg_size = sizeof(_desc_cfg_buffer);
_desc_cfglen = sizeof(tusb_desc_configuration_t);
_itf_count = 0;
_epin_count = _epout_count = 1;
Expand All @@ -96,7 +97,7 @@ Adafruit_USBD_Device::Adafruit_USBD_Device(void)
bool Adafruit_USBD_Device::addInterface(Adafruit_USBD_Interface& itf)
{
uint8_t* desc = _desc_cfg+_desc_cfglen;
uint16_t const len = itf.getDescriptor(_itf_count, desc, sizeof(_desc_cfg)-_desc_cfglen);
uint16_t const len = itf.getDescriptor(_itf_count, desc, _desc_cfg_size-_desc_cfglen);
uint8_t* desc_end = desc+len;

if ( !len ) return false;
Expand Down Expand Up @@ -127,6 +128,16 @@ bool Adafruit_USBD_Device::addInterface(Adafruit_USBD_Interface& itf)
return true;
}

void Adafruit_USBD_Device::setDescriptorBuffer(uint8_t* buf, uint32_t buflen)
{
if (buflen < _desc_cfg_size)
return;

memcpy(buf, _desc_cfg, _desc_cfglen);
_desc_cfg = buf;
_desc_cfg_size = buflen;
}

void Adafruit_USBD_Device::setID(uint16_t vid, uint16_t pid)
{
_desc_device.idVendor = vid;
Expand Down
5 changes: 4 additions & 1 deletion cores/arduino/Adafruit_TinyUSB_Core/Adafruit_USBD_Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ class Adafruit_USBD_Device
private:
tusb_desc_device_t _desc_device;

uint8_t _desc_cfg[256];
uint8_t *_desc_cfg;
uint16_t _desc_cfg_size;
uint16_t _desc_cfglen;
uint8_t _desc_cfg_buffer[256];

uint8_t _itf_count;

Expand All @@ -50,6 +52,7 @@ class Adafruit_USBD_Device
Adafruit_USBD_Device(void);

bool addInterface(Adafruit_USBD_Interface& itf);
void setDescriptorBuffer(uint8_t* buf, uint32_t buflen);

void setID(uint16_t vid, uint16_t pid);
void setVersion(uint16_t bcd);
Expand Down