Skip to content

Commit caf36be

Browse files
committed
update tinyusb for new string/hid/device/config descriptor callback
1 parent c727f2b commit caf36be

File tree

8 files changed

+101
-93
lines changed

8 files changed

+101
-93
lines changed

cores/arduino/Adafruit_TinyUSB_Core/Adafruit_USBD_Device.cpp

Lines changed: 66 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -36,59 +36,6 @@
3636

3737
extern uint8_t load_serial_number(uint16_t* serial_str);
3838

39-
extern "C"
40-
{
41-
42-
// tud_desc_set is required by tinyusb stack
43-
tud_desc_set_t tud_desc_set =
44-
{
45-
.device = NULL, // update later
46-
.config = NULL, // update later
47-
.hid_report = NULL // update later
48-
};
49-
50-
// Invoked when received GET_STRING_DESC request
51-
// max_char is CFG_TUD_ENDOINT0_SIZE/2 -1, typically max_char = 31 if Endpoint0 size is 64
52-
// Return number of characters. Note usb string is in 16-bits unicode format
53-
uint8_t tud_descriptor_string_cb(uint8_t index, uint16_t* desc, uint8_t max_char)
54-
{
55-
switch (index)
56-
{
57-
case 0:
58-
// language = English
59-
desc[0] = 0x0409;
60-
return 1;
61-
62-
case 1: // Manufacturer
63-
case 2: // Product
64-
{
65-
char const * str = (index == 1) ? USB_MANUFACTURER : USB_PRODUCT;
66-
67-
// cap at max char
68-
uint8_t count = strlen(str);
69-
if ( count > max_char ) count = max_char;
70-
71-
for(uint8_t i=0; i<count; i++)
72-
{
73-
*desc++ = str[i];
74-
}
75-
return count;
76-
}
77-
break;
78-
79-
case 3:
80-
// serial Number
81-
return load_serial_number(desc);
82-
83-
default: return 0;
84-
}
85-
86-
return 0;
87-
}
88-
89-
90-
} // extern C
91-
9239
Adafruit_USBD_Device USBDevice;
9340

9441
Adafruit_USBD_Device::Adafruit_USBD_Device(void)
@@ -99,17 +46,11 @@ Adafruit_USBD_Device::Adafruit_USBD_Device(void)
9946
.bDescriptorType = TUSB_DESC_DEVICE,
10047
.bcdUSB = 0x0200,
10148

102-
#if CFG_TUD_CDC
10349
// Use Interface Association Descriptor (IAD) for CDC
10450
// As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1)
10551
.bDeviceClass = TUSB_CLASS_MISC,
10652
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
10753
.bDeviceProtocol = MISC_PROTOCOL_IAD,
108-
#else
109-
.bDeviceClass = 0x00,
110-
.bDeviceSubClass = 0x00,
111-
.bDeviceProtocol = 0x00,
112-
#endif
11354

11455
.bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE,
11556

@@ -146,9 +87,6 @@ Adafruit_USBD_Device::Adafruit_USBD_Device(void)
14687
_desc_cfglen = sizeof(tusb_desc_configuration_t);
14788
_itf_count = 0;
14889
_epin_count = _epout_count = 1;
149-
150-
tud_desc_set.config = _desc_cfg;
151-
tud_desc_set.device = &_desc_device;
15290
}
15391

15492
// Add interface descriptor
@@ -208,4 +146,70 @@ bool Adafruit_USBD_Device::begin(void)
208146
return true;
209147
}
210148

149+
extern "C"
150+
{
151+
152+
// Invoked when received GET DEVICE DESCRIPTOR
153+
// Application return pointer to descriptor
154+
uint8_t const * tud_descriptor_device_cb(void)
155+
{
156+
return (uint8_t const *) &USBDevice._desc_device;
157+
}
158+
159+
// Invoked when received GET CONFIGURATION DESCRIPTOR
160+
// Application return pointer to descriptor
161+
// Descriptor contents must exist long enough for transfer to complete
162+
uint8_t const * tud_descriptor_configuration_cb(void)
163+
{
164+
return USBDevice._desc_cfg;
165+
}
166+
167+
static uint16_t _desc_str[32];
168+
169+
// Invoked when received GET STRING DESCRIPTOR request
170+
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
171+
uint16_t const* tud_descriptor_string_cb(uint8_t index)
172+
{
173+
uint8_t chr_count;
174+
175+
switch (index)
176+
{
177+
case 0:
178+
// language = English
179+
_desc_str[1] = 0x0409;
180+
chr_count = 1;
181+
break;
182+
183+
case 1: // Manufacturer
184+
case 2: // Product
185+
{
186+
char const * str = (index == 1) ? USB_MANUFACTURER : USB_PRODUCT;
187+
188+
// cap at max char
189+
chr_count = strlen(str);
190+
if ( chr_count > 31 ) chr_count = 31;
191+
192+
for(uint8_t i=0; i<chr_count; i++)
193+
{
194+
_desc_str[1+i] = str[i];
195+
}
196+
}
197+
break;
198+
199+
case 3:
200+
// serial Number
201+
chr_count = load_serial_number(_desc_str+1);
202+
break;
203+
204+
default: return NULL;
205+
}
206+
207+
// first byte is len, second byte is string type
208+
_desc_str[0] = TUD_DESC_STR_HEADER(chr_count);
209+
210+
return _desc_str;
211+
}
212+
213+
} // extern C
214+
211215
#endif // USE_TINYUSB

cores/arduino/Adafruit_TinyUSB_Core/Adafruit_USBD_Device.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ class Adafruit_USBD_Device
5858
bool suspended(void) { return tud_suspended(); }
5959
bool ready(void) { return tud_ready(); }
6060
bool remoteWakeup(void) { return tud_remote_wakeup(); }
61+
62+
friend uint8_t const * tud_descriptor_device_cb(void);
63+
friend uint8_t const * tud_descriptor_configuration_cb(void);
6164
};
6265

6366
extern Adafruit_USBD_Device USBDevice;

cores/arduino/Adafruit_TinyUSB_Core/tinyusb/src/class/hid/hid_device.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ bool hidd_control_request(uint8_t rhport, tusb_control_request_t const * p_reque
201201

202202
if (p_request->bRequest == TUSB_REQ_GET_DESCRIPTOR && desc_type == HID_DESC_TYPE_REPORT)
203203
{
204-
usbd_control_xfer(rhport, p_request, (void*) tud_desc_set.hid_report, p_hid->reprot_desc_len);
204+
uint8_t const * desc_report = tud_hid_descriptor_report_cb();
205+
usbd_control_xfer(rhport, p_request, (void*) desc_report, p_hid->reprot_desc_len);
205206
}else
206207
{
207208
return false; // stall unsupported request

cores/arduino/Adafruit_TinyUSB_Core/tinyusb/src/class/hid/hid_device.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y
6868
// Callbacks (Weak is optional)
6969
//--------------------------------------------------------------------+
7070

71+
// Invoked when received GET HID REPORT DESCRIPTOR request
72+
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
73+
ATTR_WEAK uint8_t const * tud_hid_descriptor_report_cb(void);
74+
7175
// Invoked when received GET_REPORT control request
7276
// Application must fill buffer report's content and return its length.
7377
// Return zero will cause the stack to STALL request

cores/arduino/Adafruit_TinyUSB_Core/tinyusb/src/common/tusb_types.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,10 @@ static inline uint8_t tu_desc_len(void const* desc)
403403
}
404404

405405
// Length of the string descriptors in bytes with slen characters
406-
#define TUD_DESC_STRLEN(_slen) (2*(_slen) + 2)
406+
#define TUD_DESC_STRLEN(_chr_count) (2*(_chr_count) + 2)
407407

408408
// Header of string descriptors with len + string type
409-
#define TUD_DESC_STR_HEADER(_slen) ( (uint16_t) ( (TUSB_DESC_STRING << 8 ) | TUD_DESC_STRLEN(_slen)) )
409+
#define TUD_DESC_STR_HEADER(_chr_count) ( (uint16_t) ( (TUSB_DESC_STRING << 8 ) | TUD_DESC_STRLEN(_chr_count)) )
410410

411411
// Convert comma-separated string to descriptor unicode format
412412
#define TUD_DESC_STRCONV( ... ) (const uint16_t[]) { TUD_DESC_STR_HEADER(VA_ARGS_NUM_(__VA_ARGS__)), __VA_ARGS__ }

cores/arduino/Adafruit_TinyUSB_Core/tinyusb/src/device/usbd.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const
479479
// This function parse configuration descriptor & open drivers accordingly
480480
static bool process_set_config(uint8_t rhport)
481481
{
482-
tusb_desc_configuration_t const * desc_cfg = (tusb_desc_configuration_t const *) tud_desc_set.config;
482+
tusb_desc_configuration_t const * desc_cfg = (tusb_desc_configuration_t const *) tud_descriptor_configuration_cb();
483483
TU_ASSERT(desc_cfg != NULL && desc_cfg->bDescriptorType == TUSB_DESC_CONFIGURATION);
484484

485485
// Parse configuration descriptor
@@ -558,11 +558,14 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const
558558
switch(desc_type)
559559
{
560560
case TUSB_DESC_DEVICE:
561-
return usbd_control_xfer(rhport, p_request, (void*) tud_desc_set.device, sizeof(tusb_desc_device_t));
561+
return usbd_control_xfer(rhport, p_request, (void*) tud_descriptor_device_cb(), sizeof(tusb_desc_device_t));
562562
break;
563563

564564
case TUSB_DESC_CONFIGURATION:
565-
return usbd_control_xfer(rhport, p_request, (void*) tud_desc_set.config, ((tusb_desc_configuration_t const*) tud_desc_set.config)->wTotalLength);
565+
{
566+
tusb_desc_configuration_t const* desc_config = (tusb_desc_configuration_t const*) tud_descriptor_configuration_cb();
567+
return usbd_control_xfer(rhport, p_request, (void*) desc_config, desc_config->wTotalLength);
568+
}
566569
break;
567570

568571
case TUSB_DESC_STRING:
@@ -574,16 +577,11 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const
574577
return false;
575578
}else
576579
{
577-
uint16_t desc_str[CFG_TUD_ENDOINT0_SIZE/2]; // up to endpoint0 size only
578-
uint8_t len = 2*tud_descriptor_string_cb(desc_index, desc_str+1, CFG_TUD_ENDOINT0_SIZE/2-1);
579-
580-
TU_ASSERT(len > 0);
581-
582-
// first byte of descriptor is size, second byte is string type
583-
len += 2; // header len
584-
desc_str[0] = tu_u16_from_u8(TUSB_DESC_STRING, len);
580+
uint8_t const* desc_str = (uint8_t const*) tud_descriptor_string_cb(desc_index);
581+
TU_ASSERT(desc_str);
585582

586-
return usbd_control_xfer(rhport, p_request, desc_str, len);
583+
// first byte of descriptor is its size
584+
return usbd_control_xfer(rhport, p_request, (void*) desc_str, desc_str[0]);
587585
}
588586
break;
589587

cores/arduino/Adafruit_TinyUSB_Core/tinyusb/src/device/usbd.h

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,6 @@
3737
#include "common/tusb_common.h"
3838
#include "device/dcd.h"
3939

40-
/// \brief Descriptor pointer collector to all the needed.
41-
typedef struct {
42-
void const * device; ///< pointer to device descriptor \ref tusb_desc_device_t
43-
void const * config; ///< pointer to the whole configuration descriptor, starting by \ref tusb_desc_configuration_t
44-
uint8_t const* hid_report;
45-
46-
}tud_desc_set_t;
47-
48-
// Descriptor collection set, must be defined by application
49-
extern tud_desc_set_t tud_desc_set;
50-
5140
//--------------------------------------------------------------------+
5241
// Application API
5342
//--------------------------------------------------------------------+
@@ -74,10 +63,17 @@ bool tud_remote_wakeup(void);
7463
// Application Callbacks (WEAK is optional)
7564
//--------------------------------------------------------------------+
7665

77-
// Invoked when received GET_STRING_DESC request
78-
// max_char is CFG_TUD_ENDOINT0_SIZE/2 -1, typically max_char = 31 if Endpoint0 size is 64
79-
// Return number of characters. Note usb string is in 16-bits unicode format
80-
uint8_t tud_descriptor_string_cb(uint8_t index, uint16_t* desc, uint8_t max_char);
66+
// Invoked when received GET DEVICE DESCRIPTOR request
67+
// Application return pointer to descriptor
68+
uint8_t const * tud_descriptor_device_cb(void);
69+
70+
// Invoked when received GET CONFIGURATION DESCRIPTOR request
71+
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
72+
uint8_t const * tud_descriptor_configuration_cb(void);
73+
74+
// Invoked when received GET STRING DESCRIPTOR request
75+
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
76+
uint16_t const* tud_descriptor_string_cb(uint8_t index);
8177

8278
// Invoked when device is mounted (configured)
8379
ATTR_WEAK void tud_mount_cb(void);

cores/arduino/Adafruit_TinyUSB_Core/tinyusb/src/device/usbd_control.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,10 @@ bool usbd_control_xfer(uint8_t rhport, tusb_control_request_t const * request, v
9393
_control_state.total_len = tu_min16(len, request->wLength);
9494
_control_state.total_transferred = 0;
9595

96-
if ( (buffer != NULL) && len )
96+
if ( len )
9797
{
98+
TU_ASSERT(buffer);
99+
98100
// Data stage
99101
TU_ASSERT( start_control_data_xact(rhport) );
100102
}else

0 commit comments

Comments
 (0)