Skip to content

Commit 52fca11

Browse files
committed
rework configuration builder with new allocInterface(), allocEndpoint()
1 parent 7a926e8 commit 52fca11

13 files changed

+187
-68
lines changed

examples/Vendor/i2c_tiny_usb_adapter/Adafruit_USBD_I2C.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,28 @@ Adafruit_USBD_I2C::Adafruit_USBD_I2C(TwoWire* wire) {
6464
setStringDescriptor("I2C Interface");
6565
}
6666

67-
uint16_t Adafruit_USBD_I2C::getInterfaceDescriptor(uint8_t itfnum, uint8_t* buf, uint16_t bufsize) {
68-
uint8_t desc[] = { TUD_VENDOR_DESCRIPTOR(itfnum, 0, 0x00, 0x80, 64) };
67+
uint16_t Adafruit_USBD_I2C::getInterfaceDescriptor(uint8_t itfnum_deprecated, uint8_t* buf, uint16_t bufsize) {
68+
uint8_t itfnum = 0;
69+
uint8_t ep_in = 0;
70+
uint8_t ep_out = 0;
71+
72+
// null buffer is used to get the length of descriptor only
73+
if (buf) {
74+
itfnum = TinyUSBDevice.allocInterface(1);
75+
ep_in = TinyUSBDevice.allocEndpoint(TUSB_DIR_IN);
76+
ep_out = TinyUSBDevice.allocEndpoint(TUSB_DIR_OUT);
77+
}
78+
79+
uint8_t const desc[] = { TUD_VENDOR_DESCRIPTOR(itfnum, _strid, ep_out, ep_in, 64) };
6980
uint16_t const len = sizeof(desc);
81+
7082
if (buf) {
7183
if (bufsize < len) {
7284
return 0;
7385
}
7486
memcpy(buf, desc, len);
7587
}
88+
7689
return len;
7790
}
7891

examples/Vendor/i2c_tiny_usb_adapter/Adafruit_USBD_I2C.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,13 @@
8888
class Adafruit_USBD_I2C : public Adafruit_USBD_Interface {
8989
public:
9090
Adafruit_USBD_I2C(TwoWire* wire);
91-
uint16_t getInterfaceDescriptor(uint8_t itfnum, uint8_t* buf, uint16_t bufsize);
9291
bool begin(uint8_t* buffer, size_t bufsize);
9392

9493
bool handleControlTransfer(uint8_t rhport, uint8_t stage, tusb_control_request_t const* request);
9594

95+
// from Adafruit_USBD_Interface
96+
virtual uint16_t getInterfaceDescriptor(uint8_t itfnum, uint8_t* buf, uint16_t bufsize);
97+
9698
private:
9799
TwoWire* _wire;
98100
uint8_t _state;

src/arduino/Adafruit_USBD_CDC.cpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,34 @@ Adafruit_USBD_CDC::Adafruit_USBD_CDC(void) { _instance = INVALID_INSTANCE; }
4646

4747
#if CFG_TUD_ENABLED
4848

49-
#define EPOUT 0x00
50-
#define EPIN 0x80
51-
52-
uint16_t Adafruit_USBD_CDC::getInterfaceDescriptor(uint8_t itfnum, uint8_t *buf,
49+
uint16_t Adafruit_USBD_CDC::getInterfaceDescriptor(uint8_t itfnum_deprecated,
50+
uint8_t *buf,
5351
uint16_t bufsize) {
5452
// CDC is mostly always existed for DFU
55-
// usb core will automatically update endpoint number
56-
uint8_t desc[] = {TUD_CDC_DESCRIPTOR(itfnum, 0, EPIN, 8, EPOUT, EPIN, 64)};
53+
uint8_t itfnum = 0;
54+
uint8_t ep_notif = 0;
55+
uint8_t ep_in = 0;
56+
uint8_t ep_out = 0;
57+
58+
if (buf) {
59+
itfnum = TinyUSBDevice.allocInterface(2);
60+
ep_notif = TinyUSBDevice.allocEndpoint(TUSB_DIR_IN);
61+
ep_in = TinyUSBDevice.allocEndpoint(TUSB_DIR_IN);
62+
ep_out = TinyUSBDevice.allocEndpoint(TUSB_DIR_OUT);
63+
}
64+
65+
uint8_t const desc[] = {
66+
TUD_CDC_DESCRIPTOR(itfnum, _strid, ep_notif, 8, ep_out, ep_in, 64)};
5767
uint16_t const len = sizeof(desc);
5868

59-
if (bufsize < len) {
60-
return 0;
69+
// null buffer is used to get the length of descriptor only
70+
if (buf) {
71+
if (bufsize < len) {
72+
return 0;
73+
}
74+
memcpy(buf, desc, len);
6175
}
6276

63-
memcpy(buf, desc, len);
6477
return len;
6578
}
6679

src/arduino/Adafruit_USBD_CDC.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ class Adafruit_USBD_CDC : public Stream, public Adafruit_USBD_Interface {
4545

4646
static uint8_t getInstanceCount(void) { return _instance_count; }
4747

48-
// from Adafruit_USBD_Interface
49-
virtual uint16_t getInterfaceDescriptor(uint8_t itfnum, uint8_t *buf,
50-
uint16_t bufsize);
51-
5248
void setPins(uint8_t pin_rx, uint8_t pin_tx) {
5349
(void)pin_rx;
5450
(void)pin_tx;
@@ -83,6 +79,10 @@ class Adafruit_USBD_CDC : public Stream, public Adafruit_USBD_Interface {
8379
using Print::write; // pull in write(str) from Print
8480
operator bool();
8581

82+
// from Adafruit_USBD_Interface
83+
virtual uint16_t getInterfaceDescriptor(uint8_t itfnum_deprecated,
84+
uint8_t *buf, uint16_t bufsize);
85+
8686
private:
8787
enum { INVALID_INSTANCE = 0xffu };
8888
static uint8_t _instance_count;

src/arduino/Adafruit_USBD_Device.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,15 @@ bool Adafruit_USBD_Device::addInterface(Adafruit_USBD_Interface &itf) {
235235
uint8_t *desc = _desc_cfg + _desc_cfg_len;
236236
uint16_t const len = itf.getInterfaceDescriptor(
237237
_itf_count, desc, _desc_cfg_maxlen - _desc_cfg_len);
238-
uint8_t *desc_end = desc + len;
239-
240-
const char *desc_str = itf.getStringDescriptor();
241238

242239
if (!len) {
243240
return false;
244241
}
245242

243+
#if 0
244+
uint8_t *desc_end = desc + len;
245+
const char *desc_str = itf.getStringDescriptor();
246+
246247
// Parse interface descriptor to update
247248
// - IAD: interface number
248249
// - Interface: number & string descriptor
@@ -321,6 +322,7 @@ bool Adafruit_USBD_Device::addInterface(Adafruit_USBD_Interface &itf) {
321322

322323
desc += tu_desc_len(desc);
323324
}
325+
#endif
324326

325327
_desc_cfg_len += len;
326328

src/arduino/Adafruit_USBD_Device.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,26 @@ class Adafruit_USBD_Device {
7171

7272
//------------- Configuration descriptor -------------//
7373

74-
// Add an new interface
74+
// Add a new interface
7575
bool addInterface(Adafruit_USBD_Interface &itf);
7676

7777
// Clear/Reset configuration descriptor
7878
void clearConfiguration(void);
7979

80-
// Provide user buffer for configuration descriptor, needed if total length >
81-
// 256
80+
// Provide user buffer for configuration descriptor, if total length > 256
8281
void setConfigurationBuffer(uint8_t *buf, uint32_t buflen);
8382

83+
// Allocate a new interface number
84+
uint8_t allocInterface(uint8_t count = 1) {
85+
uint8_t ret = _itf_count;
86+
_itf_count += count;
87+
return ret;
88+
}
89+
90+
uint8_t allocEndpoint(uint8_t in) {
91+
return in ? (0x80 | _epin_count++) : _epout_count++;
92+
}
93+
8494
//------------- String descriptor -------------//
8595
void setLanguageDescriptor(uint16_t language_id);
8696
void setManufacturerDescriptor(const char *s);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2021 Ha Thach (tinyusb.org) for Adafruit Industries
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
#include "tusb_option.h"
26+
27+
#if CFG_TUD_ENABLED
28+
29+
#include "Adafruit_USBD_Device.h"
30+
31+
void Adafruit_USBD_Interface::setStringDescriptor(const char *str) {
32+
_strid = TinyUSBDevice.addStringDescriptor(str);
33+
}
34+
35+
#endif

src/arduino/Adafruit_USBD_Interface.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,21 @@
3030

3131
class Adafruit_USBD_Interface {
3232
protected:
33-
const char *_desc_str;
33+
uint8_t _strid;
3434

3535
public:
36-
Adafruit_USBD_Interface(void) { _desc_str = NULL; }
36+
Adafruit_USBD_Interface(void) { _strid = 0; }
3737

3838
// Get Interface Descriptor
3939
// Fill the descriptor (if buf is not NULL) and return its length
4040
virtual uint16_t getInterfaceDescriptor(uint8_t itfnum, uint8_t *buf,
4141
uint16_t bufsize) = 0;
42+
// Get Interface Descriptor Length
43+
uint16_t getInterfaceDescriptorLen() {
44+
return getInterfaceDescriptor(0, NULL, 0);
45+
}
4246

43-
void setStringDescriptor(const char *str) { _desc_str = str; }
44-
const char *getStringDescriptor(void) { return _desc_str; }
47+
void setStringDescriptor(const char *str);
4548
};
4649

4750
#endif

src/arduino/hid/Adafruit_USBD_HID.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828

2929
#include "Adafruit_USBD_HID.h"
3030

31-
#define EPOUT 0x00
32-
#define EPIN 0x80
33-
3431
uint8_t const _ascii2keycode[128][2] = {HID_ASCII_TO_KEYCODE};
3532
static Adafruit_USBD_HID *_hid_instances[CFG_TUD_HID] = {0};
3633

@@ -94,7 +91,7 @@ Adafruit_USBD_HID::Adafruit_USBD_HID(uint8_t const *desc_report, uint16_t len,
9491
_instance = _instance_count++;
9592
_hid_instances[_instance] = this;
9693

97-
uint16_t const desc_len = getInterfaceDescriptor(0, NULL, 0);
94+
uint16_t const desc_len = getInterfaceDescriptorLen();
9895
tinyusb_enable_interface(USB_INTERFACE_HID, desc_len, hid_load_descriptor);
9996
#endif
10097
}
@@ -132,11 +129,11 @@ uint16_t Adafruit_USBD_HID::makeItfDesc(uint8_t itfnum, uint8_t *buf,
132129
return 0;
133130
}
134131

135-
uint8_t const desc_inout[] = {
136-
TUD_HID_INOUT_DESCRIPTOR(itfnum, 0, _protocol, _desc_report_len, ep_in,
137-
ep_out, CFG_TUD_HID_EP_BUFSIZE, _interval_ms)};
132+
uint8_t const desc_inout[] = {TUD_HID_INOUT_DESCRIPTOR(
133+
itfnum, _strid, _protocol, _desc_report_len, ep_in, ep_out,
134+
CFG_TUD_HID_EP_BUFSIZE, _interval_ms)};
138135
uint8_t const desc_in_only[] = {
139-
TUD_HID_DESCRIPTOR(itfnum, 0, _protocol, _desc_report_len, ep_in,
136+
TUD_HID_DESCRIPTOR(itfnum, _strid, _protocol, _desc_report_len, ep_in,
140137
CFG_TUD_HID_EP_BUFSIZE, _interval_ms)};
141138

142139
uint8_t const *desc;
@@ -155,17 +152,30 @@ uint16_t Adafruit_USBD_HID::makeItfDesc(uint8_t itfnum, uint8_t *buf,
155152
if (bufsize < len) {
156153
return 0;
157154
}
158-
159155
memcpy(buf, desc, len);
160156
}
161157

162158
return len;
163159
}
164160

165-
uint16_t Adafruit_USBD_HID::getInterfaceDescriptor(uint8_t itfnum, uint8_t *buf,
161+
uint16_t Adafruit_USBD_HID::getInterfaceDescriptor(uint8_t itfnum_deprecated,
162+
uint8_t *buf,
166163
uint16_t bufsize) {
167-
// usb core will automatically update endpoint number
168-
return makeItfDesc(itfnum, buf, bufsize, EPIN, EPOUT);
164+
uint8_t itfnum = 0;
165+
uint8_t ep_in = 0;
166+
uint8_t ep_out = 0;
167+
168+
// null buffer is used to get the length of descriptor only
169+
if (buf) {
170+
itfnum = TinyUSBDevice.allocInterface(1);
171+
ep_in = TinyUSBDevice.allocEndpoint(TUSB_DIR_IN);
172+
173+
if (_out_endpoint) {
174+
TinyUSBDevice.allocEndpoint(TUSB_DIR_OUT);
175+
}
176+
}
177+
178+
return makeItfDesc(itfnum, buf, bufsize, ep_in, ep_out);
169179
}
170180

171181
bool Adafruit_USBD_HID::begin(void) {

src/arduino/midi/Adafruit_USBD_MIDI.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
//--------------------------------------------------------------------+
3232
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
3333
//--------------------------------------------------------------------+
34-
#define EPOUT 0x00
35-
#define EPIN 0x80
3634
#define EPSIZE 64
3735

3836
// TODO multiple instances
@@ -47,8 +45,7 @@ static uint16_t midi_load_descriptor(uint8_t *dst, uint8_t *itf) {
4745
TU_VERIFY(ep_in && ep_out);
4846
ep_in |= 0x80;
4947

50-
uint16_t desc_len = _midi_dev->getInterfaceDescriptor(0, NULL, 0);
51-
48+
uint16_t desc_len = _midi_dev->getInterfaceDescriptorLen();
5249
desc_len = _midi_dev->makeItfDesc(*itf, dst, desc_len, ep_in, ep_out);
5350

5451
*itf += 2;
@@ -63,7 +60,7 @@ Adafruit_USBD_MIDI::Adafruit_USBD_MIDI(uint8_t n_cables) {
6360
#ifdef ARDUINO_ARCH_ESP32
6461
// ESP32 requires setup configuration descriptor within constructor
6562
_midi_dev = this;
66-
uint16_t const desc_len = getInterfaceDescriptor(0, NULL, 0);
63+
uint16_t const desc_len = getInterfaceDescriptorLen();
6764
tinyusb_enable_interface(USB_INTERFACE_MIDI, desc_len, midi_load_descriptor);
6865
#endif
6966
}
@@ -110,7 +107,7 @@ uint16_t Adafruit_USBD_MIDI::makeItfDesc(uint8_t itfnum, uint8_t *buf,
110107

111108
// Header
112109
{
113-
uint8_t desc[] = {TUD_MIDI_DESC_HEAD(itfnum, 0, _n_cables)};
110+
uint8_t desc[] = {TUD_MIDI_DESC_HEAD(itfnum, _strid, _n_cables)};
114111
memcpy(buf + len, desc, sizeof(desc));
115112
len += sizeof(desc);
116113
}
@@ -156,10 +153,21 @@ uint16_t Adafruit_USBD_MIDI::makeItfDesc(uint8_t itfnum, uint8_t *buf,
156153
return desc_len;
157154
}
158155

159-
uint16_t Adafruit_USBD_MIDI::getInterfaceDescriptor(uint8_t itfnum,
156+
uint16_t Adafruit_USBD_MIDI::getInterfaceDescriptor(uint8_t itfnum_deprecated,
160157
uint8_t *buf,
161158
uint16_t bufsize) {
162-
return makeItfDesc(itfnum, buf, bufsize, EPIN, EPOUT);
159+
uint8_t itfnum = 0;
160+
uint8_t ep_in = 0;
161+
uint8_t ep_out = 0;
162+
163+
// null buffer is used to get the length of descriptor only
164+
if (buf) {
165+
itfnum = TinyUSBDevice.allocInterface(2);
166+
ep_in = TinyUSBDevice.allocEndpoint(TUSB_DIR_IN);
167+
ep_out = TinyUSBDevice.allocEndpoint(TUSB_DIR_OUT);
168+
}
169+
170+
return makeItfDesc(itfnum, buf, bufsize, ep_in, ep_out);
163171
}
164172

165173
int Adafruit_USBD_MIDI::read(void) {

0 commit comments

Comments
 (0)