Skip to content

Commit 188fb24

Browse files
committed
update tinyusb with new string descriptor callback
1 parent 28a1a48 commit 188fb24

File tree

5 files changed

+35
-36
lines changed

5 files changed

+35
-36
lines changed

cores/nRF5/Adafruit_TinyUSB_Core/Adafruit_USBD_Device.cpp

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -164,43 +164,50 @@ uint8_t const * tud_descriptor_configuration_cb(void)
164164
return USBDevice._desc_cfg;
165165
}
166166

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)
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)
171172
{
173+
uint8_t chr_count;
174+
172175
switch (index)
173176
{
174177
case 0:
175178
// language = English
176-
desc[0] = 0x0409;
177-
return 1;
179+
_desc_str[1] = 0x0409;
180+
chr_count = 1;
181+
break;
178182

179183
case 1: // Manufacturer
180184
case 2: // Product
181185
{
182186
char const * str = (index == 1) ? USB_MANUFACTURER : USB_PRODUCT;
183187

184188
// cap at max char
185-
uint8_t count = strlen(str);
186-
if ( count > max_char ) count = max_char;
189+
chr_count = strlen(str);
190+
if ( chr_count > 31 ) chr_count = 31;
187191

188-
for(uint8_t i=0; i<count; i++)
192+
for(uint8_t i=0; i<chr_count; i++)
189193
{
190-
*desc++ = str[i];
194+
_desc_str[1+i] = str[i];
191195
}
192-
return count;
193196
}
194197
break;
195198

196199
case 3:
197200
// serial Number
198-
return load_serial_number(desc);
201+
chr_count = load_serial_number(_desc_str+1);
202+
break;
199203

200-
default: return 0;
204+
default: return NULL;
201205
}
202206

203-
return 0;
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;
204211
}
205212

206213
} // extern C

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,8 @@ 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
72-
// Application return pointer to descriptor
73-
// Descriptor contents must exist long enough for transfer to complete
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
7473
ATTR_WEAK uint8_t const * tud_hid_descriptor_report_cb(void);
7574

7675
// Invoked when received GET_REPORT control request

cores/nRF5/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/nRF5/Adafruit_TinyUSB_Core/tinyusb/src/device/usbd.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -577,16 +577,11 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const
577577
return false;
578578
}else
579579
{
580-
uint16_t desc_str[CFG_TUD_ENDOINT0_SIZE/2]; // up to endpoint0 size only
581-
uint8_t len = 2*tud_descriptor_string_cb(desc_index, desc_str+1, CFG_TUD_ENDOINT0_SIZE/2-1);
580+
uint8_t const* desc_str = (uint8_t const*) tud_descriptor_string_cb(desc_index);
581+
TU_ASSERT(desc_str);
582582

583-
TU_ASSERT(len > 0);
584-
585-
// first byte of descriptor is size, second byte is string type
586-
len += 2; // header len
587-
desc_str[0] = tu_u16_from_u8(TUSB_DESC_STRING, len);
588-
589-
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]);
590585
}
591586
break;
592587

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,17 @@ bool tud_remote_wakeup(void);
6363
// Application Callbacks (WEAK is optional)
6464
//--------------------------------------------------------------------+
6565

66-
// Invoked when received GET DEVICE DESCRIPTOR
66+
// Invoked when received GET DEVICE DESCRIPTOR request
6767
// Application return pointer to descriptor
6868
uint8_t const * tud_descriptor_device_cb(void);
6969

70-
// Invoked when received GET CONFIGURATION DESCRIPTOR
71-
// Application return pointer to descriptor
72-
// Descriptor contents must exist long enough for transfer to complete
70+
// Invoked when received GET CONFIGURATION DESCRIPTOR request
71+
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
7372
uint8_t const * tud_descriptor_configuration_cb(void);
7473

75-
// Invoked when received GET STRING DESC request
76-
// 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 UTF-16 format
78-
uint8_t tud_descriptor_string_cb(uint8_t index, uint16_t* desc, uint8_t max_char);
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);
7977

8078
// Invoked when device is mounted (configured)
8179
ATTR_WEAK void tud_mount_cb(void);

0 commit comments

Comments
 (0)