Skip to content

Commit 28a1a48

Browse files
committed
add tud_descriptor_device_cb()/tud_descriptor_configuration_cb()
remove the use of tud_desc_set_t tud_desc_set
1 parent 6f639b0 commit 28a1a48

File tree

6 files changed

+85
-79
lines changed

6 files changed

+85
-79
lines changed

cores/nRF5/Adafruit_TinyUSB_Core/Adafruit_USBD_Device.cpp

Lines changed: 59 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -36,58 +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-
};
48-
49-
// Invoked when received GET_STRING_DESC request
50-
// max_char is CFG_TUD_ENDOINT0_SIZE/2 -1, typically max_char = 31 if Endpoint0 size is 64
51-
// Return number of characters. Note usb string is in 16-bits unicode format
52-
uint8_t tud_descriptor_string_cb(uint8_t index, uint16_t* desc, uint8_t max_char)
53-
{
54-
switch (index)
55-
{
56-
case 0:
57-
// language = English
58-
desc[0] = 0x0409;
59-
return 1;
60-
61-
case 1: // Manufacturer
62-
case 2: // Product
63-
{
64-
char const * str = (index == 1) ? USB_MANUFACTURER : USB_PRODUCT;
65-
66-
// cap at max char
67-
uint8_t count = strlen(str);
68-
if ( count > max_char ) count = max_char;
69-
70-
for(uint8_t i=0; i<count; i++)
71-
{
72-
*desc++ = str[i];
73-
}
74-
return count;
75-
}
76-
break;
77-
78-
case 3:
79-
// serial Number
80-
return load_serial_number(desc);
81-
82-
default: return 0;
83-
}
84-
85-
return 0;
86-
}
87-
88-
89-
} // extern C
90-
9139
Adafruit_USBD_Device USBDevice;
9240

9341
Adafruit_USBD_Device::Adafruit_USBD_Device(void)
@@ -98,17 +46,11 @@ Adafruit_USBD_Device::Adafruit_USBD_Device(void)
9846
.bDescriptorType = TUSB_DESC_DEVICE,
9947
.bcdUSB = 0x0200,
10048

101-
#if CFG_TUD_CDC
10249
// Use Interface Association Descriptor (IAD) for CDC
10350
// As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1)
10451
.bDeviceClass = TUSB_CLASS_MISC,
10552
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
10653
.bDeviceProtocol = MISC_PROTOCOL_IAD,
107-
#else
108-
.bDeviceClass = 0x00,
109-
.bDeviceSubClass = 0x00,
110-
.bDeviceProtocol = 0x00,
111-
#endif
11254

11355
.bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE,
11456

@@ -145,9 +87,6 @@ Adafruit_USBD_Device::Adafruit_USBD_Device(void)
14587
_desc_cfglen = sizeof(tusb_desc_configuration_t);
14688
_itf_count = 0;
14789
_epin_count = _epout_count = 1;
148-
149-
tud_desc_set.config = _desc_cfg;
150-
tud_desc_set.device = &_desc_device;
15190
}
15291

15392
// Add interface descriptor
@@ -207,4 +146,63 @@ bool Adafruit_USBD_Device::begin(void)
207146
return true;
208147
}
209148

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+
// Invoked when received GET_STRING_DESC request
168+
// max_char is CFG_TUD_ENDOINT0_SIZE/2 -1, typically max_char = 31 if Endpoint0 size is 64
169+
// Return number of characters. Note usb string is in UTF-16 format
170+
uint8_t tud_descriptor_string_cb(uint8_t index, uint16_t* desc, uint8_t max_char)
171+
{
172+
switch (index)
173+
{
174+
case 0:
175+
// language = English
176+
desc[0] = 0x0409;
177+
return 1;
178+
179+
case 1: // Manufacturer
180+
case 2: // Product
181+
{
182+
char const * str = (index == 1) ? USB_MANUFACTURER : USB_PRODUCT;
183+
184+
// cap at max char
185+
uint8_t count = strlen(str);
186+
if ( count > max_char ) count = max_char;
187+
188+
for(uint8_t i=0; i<count; i++)
189+
{
190+
*desc++ = str[i];
191+
}
192+
return count;
193+
}
194+
break;
195+
196+
case 3:
197+
// serial Number
198+
return load_serial_number(desc);
199+
200+
default: return 0;
201+
}
202+
203+
return 0;
204+
}
205+
206+
} // extern C
207+
210208
#endif // USE_TINYUSB

cores/nRF5/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/nRF5/Adafruit_TinyUSB_Core/tinyusb/src/class/hid/hid_device.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ 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 REPORT DESCRIPTOR
72-
// Application return pointer to report descriptor.
73-
// descriptor contents must exist long enough for transfer to complete
71+
// Invoked when received GET HID REPORT DESCRIPTOR
72+
// Application return pointer to descriptor
73+
// Descriptor contents must exist long enough for transfer to complete
7474
ATTR_WEAK uint8_t const * tud_hid_descriptor_report_cb(void);
7575

7676
// Invoked when received GET_REPORT control request

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

Lines changed: 6 additions & 3 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:

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +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-
}tud_desc_set_t;
45-
46-
// Descriptor collection set, must be defined by application
47-
extern tud_desc_set_t tud_desc_set;
48-
4940
//--------------------------------------------------------------------+
5041
// Application API
5142
//--------------------------------------------------------------------+
@@ -72,9 +63,18 @@ bool tud_remote_wakeup(void);
7263
// Application Callbacks (WEAK is optional)
7364
//--------------------------------------------------------------------+
7465

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

8080
// Invoked when device is mounted (configured)

cores/nRF5/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)