Skip to content

Commit 1723633

Browse files
committed
improve device begin
1 parent 9e1402e commit 1723633

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

src/Adafruit_USBD_Interface.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ class Adafruit_USBD_Interface
3535
public:
3636
Adafruit_USBD_Interface(void) { _desc_str = NULL; }
3737

38+
// Get Interface Descriptor
39+
// Device fill descriptor and return its length
3840
virtual uint16_t getInterfaceDescriptor(uint8_t itfnum, uint8_t* buf, uint16_t bufsize) = 0;
41+
3942
void setStringDescriptor(const char* str) { _desc_str = str; }
4043
const char* getStringDescriptor(void) { return _desc_str; }
4144
};

src/arduino/Adafruit_USBD_Device.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,10 @@ void Adafruit_USBD_Device::clearConfiguration(void)
6666
.bLength = sizeof(tusb_desc_device_t),
6767
.bDescriptorType = TUSB_DESC_DEVICE,
6868
.bcdUSB = 0x0200,
69-
70-
// Use Interface Association Descriptor (IAD) for CDC
71-
// As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1)
72-
.bDeviceClass = TUSB_CLASS_MISC,
73-
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
74-
.bDeviceProtocol = MISC_PROTOCOL_IAD,
75-
69+
.bDeviceClass = 0,
70+
.bDeviceSubClass = 0,
71+
.bDeviceProtocol = 0,
7672
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
77-
7873
.idVendor = 0,
7974
.idProduct = 0,
8075
.bcdDevice = 0x0100,
@@ -84,7 +79,7 @@ void Adafruit_USBD_Device::clearConfiguration(void)
8479
.bNumConfigurations = 0x01
8580
};
8681

87-
memcpy(_desc_device, &desc_dev, sizeof(tusb_desc_device_t));
82+
_desc_device = desc_dev;
8883

8984
uint8_t const dev_cfg [sizeof(tusb_desc_configuration_t)] =
9085
{
@@ -124,7 +119,7 @@ bool Adafruit_USBD_Device::addInterface(Adafruit_USBD_Interface& itf)
124119
if ( !len ) return false;
125120

126121
// Parse interface descriptor to update
127-
// - Interface Number & string descrioptor
122+
// - Interface Number & string descriptor
128123
// - Endpoint address
129124
while (desc < desc_end)
130125
{
@@ -175,18 +170,18 @@ void Adafruit_USBD_Device::setConfigurationBuffer(uint8_t* buf, uint32_t buflen)
175170

176171
void Adafruit_USBD_Device::setID(uint16_t vid, uint16_t pid)
177172
{
178-
((tusb_desc_device_t*)_desc_device)->idVendor = vid;
179-
((tusb_desc_device_t*)_desc_device)->idProduct = pid;
173+
_desc_device.idVendor = vid;
174+
_desc_device.idProduct = pid;
180175
}
181176

182177
void Adafruit_USBD_Device::setVersion(uint16_t bcd)
183178
{
184-
((tusb_desc_device_t*)_desc_device)->bcdUSB = bcd;
179+
_desc_device.bcdUSB = bcd;
185180
}
186181

187182
void Adafruit_USBD_Device::setDeviceVersion(uint16_t bcd)
188183
{
189-
((tusb_desc_device_t*)_desc_device)->bcdDevice = bcd;
184+
_desc_device.bcdDevice = bcd;
190185
}
191186

192187
void Adafruit_USBD_Device::setLanguageDescriptor (uint16_t language_id)
@@ -233,6 +228,12 @@ bool Adafruit_USBD_Device::begin(uint8_t rhport)
233228
setID(USB_VID, USB_PID);
234229

235230
// Serial is always added by default
231+
// Use Interface Association Descriptor (IAD) for CDC
232+
// As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1)
233+
_desc_device.bDeviceClass = TUSB_CLASS_MISC;
234+
_desc_device.bDeviceSubClass = MISC_SUBCLASS_COMMON;
235+
_desc_device.bDeviceProtocol = MISC_PROTOCOL_IAD;
236+
236237
Serial.begin(115200);
237238

238239
TinyUSB_Port_InitDeviceController(rhport);
@@ -317,7 +318,7 @@ extern "C"
317318
// Application return pointer to descriptor
318319
uint8_t const * tud_descriptor_device_cb(void)
319320
{
320-
return USBDevice._desc_device;
321+
return (uint8_t const * ) &USBDevice._desc_device;
321322
}
322323

323324
// Invoked when received GET CONFIGURATION DESCRIPTOR

src/arduino/Adafruit_USBD_Device.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Adafruit_USBD_Device
3434
enum { STRING_DESCRIPTOR_MAX = 8 };
3535

3636
// Device descriptor
37-
uint8_t _desc_device[18] __attribute__ ((aligned(4)));
37+
tusb_desc_device_t _desc_device __attribute__ ((aligned(4)));
3838

3939
// Configuration descriptor
4040
uint8_t* _desc_cfg;

0 commit comments

Comments
 (0)