Skip to content

Commit c727f2b

Browse files
committed
update usb core to use tud_descriptor_string_cb()
1 parent 0552d34 commit c727f2b

File tree

6 files changed

+89
-72
lines changed

6 files changed

+89
-72
lines changed

cores/arduino/Adafruit_TinyUSB_Core/Adafruit_TinyUSB_Core.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@
3131
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
3232
//--------------------------------------------------------------------+
3333

34-
// Serial is 64-bit DeviceID -> 16 chars len
35-
uint16_t usb_desc_str_serial[1+16] = { TUD_DESC_STR_HEADER(16) };
36-
3734
// Init usb hardware when starting up. Softdevice is not enabled yet
3835
static void usb_hardware_init(void)
3936
{
@@ -84,7 +81,7 @@ static void usb_hardware_init(void)
8481
#endif
8582
}
8683

87-
static void load_serial_number(void)
84+
uint8_t load_serial_number(uint16_t* serial_str)
8885
{
8986
#ifdef __SAMD51__
9087
uint32_t* id_addresses[4] = {(uint32_t *) 0x008061FC, (uint32_t *) 0x00806010,
@@ -108,16 +105,15 @@ static void load_serial_number(void)
108105
for (int j = 0; j < 2; j++) {
109106
uint8_t nibble = (raw_id[i] >> (j * 4)) & 0xf;
110107
// Strings are UTF-16-LE encoded.
111-
usb_desc_str_serial[1 + i * 2 + j] = nibble_to_hex[nibble];
108+
serial_str[i * 2 + j] = nibble_to_hex[nibble];
112109
}
113110
}
111+
112+
return 16;
114113
}
115114

116115
void Adafruit_TinyUSB_Core_init(void)
117116
{
118-
// Create Serial string descriptor
119-
load_serial_number();
120-
121117
USBDevice.addInterface( (Adafruit_USBD_Interface&) Serial);
122118
USBDevice.setID(USB_VID, USB_PID);
123119
USBDevice.begin();

cores/arduino/Adafruit_TinyUSB_Core/Adafruit_USBD_Device.cpp

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,42 +26,66 @@
2626

2727
#include "Adafruit_USBD_Device.h"
2828

29+
#ifndef USB_MANUFACTURER
30+
#define USB_MANUFACTURER "Unknown"
31+
#endif
32+
33+
#ifndef USB_PRODUCT
34+
#define USB_PRODUCT "Unknown"
35+
#endif
36+
37+
extern uint8_t load_serial_number(uint16_t* serial_str);
38+
2939
extern "C"
3040
{
31-
extern uint16_t usb_desc_str_serial[1+16];
3241

33-
// array of pointer to string descriptors
34-
uint16_t const * const string_desc_arr [] =
42+
// tud_desc_set is required by tinyusb stack
43+
tud_desc_set_t tud_desc_set =
3544
{
36-
// 0: is supported language = English
37-
TUD_DESC_STRCONV(0x0409),
45+
.device = NULL, // update later
46+
.config = NULL, // update later
47+
.hid_report = NULL // update later
48+
};
3849

39-
// 1: Manufacturer
40-
TUD_DESC_STRCONV('A','d','a','f','r','u','i','t',' ','I','n','d','u','s','t','r','i','e','s'),
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;
4161

42-
// 2: Product
43-
TUD_DESC_STRCONV('B','l','u','e','f','r','u','i','t',' ','n','R','F','5','2','8','4','0'),
62+
case 1: // Manufacturer
63+
case 2: // Product
64+
{
65+
char const * str = (index == 1) ? USB_MANUFACTURER : USB_PRODUCT;
4466

45-
// 3: Serials
46-
usb_desc_str_serial,
67+
// cap at max char
68+
uint8_t count = strlen(str);
69+
if ( count > max_char ) count = max_char;
4770

48-
// // 4: CDC Interface
49-
// TUD_DESC_STRCONV('B','l','u','e','f','r','u','i','t',' ','S','e','r','i','a','l'),
50-
//
51-
// // 5: MSC Interface
52-
// TUD_DESC_STRCONV('B','l','u','e','f','r','u','i','t',' ','U','F','2'),
53-
};
71+
for(uint8_t i=0; i<count; i++)
72+
{
73+
*desc++ = str[i];
74+
}
75+
return count;
76+
}
77+
break;
5478

55-
// tud_desc_set is required by tinyusb stack
56-
tud_desc_set_t tud_desc_set =
57-
{
58-
.device = NULL, // update later
59-
.config = NULL, // update later
60-
.string_arr = (uint8_t const **) string_desc_arr,
61-
.string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]),
79+
case 3:
80+
// serial Number
81+
return load_serial_number(desc);
82+
83+
default: return 0;
84+
}
85+
86+
return 0;
87+
}
6288

63-
.hid_report = NULL // update later
64-
};
6589

6690
} // extern C
6791

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ static inline uint32_t tu_u32_from_u8(uint8_t b1, uint8_t b2, uint8_t b3, uint8_
142142
return ( ((uint32_t) b1) << 24) + ( ((uint32_t) b2) << 16) + ( ((uint32_t) b3) << 8) + b4;
143143
}
144144

145+
static inline uint16_t tu_u16_from_u8(uint8_t high, uint8_t low)
146+
{
147+
return (((uint16_t) high) << 8) + low;
148+
}
149+
145150
static inline uint8_t tu_u16_high(uint16_t u16)
146151
{
147152
return (uint8_t) ( ((uint16_t) (u16 >> 8)) & 0x00ff);

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

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ static osal_queue_t _usbd_q;
160160
static void mark_interface_endpoint(uint8_t ep2drv[8][2], uint8_t const* p_desc, uint16_t desc_len, uint8_t driver_id);
161161
static bool process_control_request(uint8_t rhport, tusb_control_request_t const * p_request);
162162
static bool process_set_config(uint8_t rhport);
163-
static void const* get_descriptor(tusb_control_request_t const * p_request, uint16_t* desc_len);
163+
static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const * p_request);
164164

165165
void usbd_control_reset (uint8_t rhport);
166166
bool usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
@@ -382,13 +382,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const
382382
break;
383383

384384
case TUSB_REQ_GET_DESCRIPTOR:
385-
{
386-
uint16_t len = 0;
387-
void* buf = (void*) get_descriptor(p_request, &len);
388-
if ( buf == NULL || len == 0 ) return false;
389-
390-
usbd_control_xfer(rhport, p_request, buf, len);
391-
}
385+
TU_ASSERT( process_get_descriptor(rhport, p_request) );
392386
break;
393387

394388
case TUSB_REQ_SET_FEATURE:
@@ -556,56 +550,53 @@ static void mark_interface_endpoint(uint8_t ep2drv[8][2], uint8_t const* p_desc,
556550
}
557551

558552
// return descriptor's buffer and update desc_len
559-
static void const* get_descriptor(tusb_control_request_t const * p_request, uint16_t* desc_len)
553+
static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const * p_request)
560554
{
561555
tusb_desc_type_t const desc_type = (tusb_desc_type_t) tu_u16_high(p_request->wValue);
562556
uint8_t const desc_index = tu_u16_low( p_request->wValue );
563557

564-
uint8_t const * desc_data = NULL;
565-
uint16_t len = 0;
566-
567-
*desc_len = 0;
568-
569558
switch(desc_type)
570559
{
571560
case TUSB_DESC_DEVICE:
572-
desc_data = (uint8_t const *) tud_desc_set.device;
573-
len = sizeof(tusb_desc_device_t);
561+
return usbd_control_xfer(rhport, p_request, (void*) tud_desc_set.device, sizeof(tusb_desc_device_t));
574562
break;
575563

576564
case TUSB_DESC_CONFIGURATION:
577-
desc_data = (uint8_t const *) tud_desc_set.config;
578-
len = ((tusb_desc_configuration_t const*) desc_data)->wTotalLength;
565+
return usbd_control_xfer(rhport, p_request, (void*) tud_desc_set.config, ((tusb_desc_configuration_t const*) tud_desc_set.config)->wTotalLength);
579566
break;
580567

581568
case TUSB_DESC_STRING:
582569
// String Descriptor always uses the desc set from user
583-
if ( desc_index < tud_desc_set.string_count )
584-
{
585-
desc_data = tud_desc_set.string_arr[desc_index];
586-
TU_VERIFY( desc_data != NULL, NULL );
587-
588-
len = desc_data[0]; // first byte of descriptor is its size
589-
}else
570+
if ( desc_index == 0xEE )
590571
{
591-
// out of range
592572
// The 0xEE index string is a Microsoft USB extension.
593573
// It can be used to tell Windows what driver it should use for the device !!!
594-
return NULL;
574+
return false;
575+
}else
576+
{
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);
585+
586+
return usbd_control_xfer(rhport, p_request, desc_str, len);
595587
}
596588
break;
597589

598590
case TUSB_DESC_DEVICE_QUALIFIER:
599591
// TODO If not highspeed capable stall this request otherwise
600592
// return the descriptor that could work in highspeed
601-
return NULL;
593+
return false;
602594
break;
603595

604-
default: return NULL;
596+
default: return false;
605597
}
606598

607-
*desc_len = len;
608-
return desc_data;
599+
return true;
609600
}
610601

611602
//--------------------------------------------------------------------+

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,8 @@
3939

4040
/// \brief Descriptor pointer collector to all the needed.
4141
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-
45-
uint8_t const** string_arr; ///< a array of pointers to string descriptors
46-
uint16_t string_count;
47-
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
4844
uint8_t const* hid_report;
4945

5046
}tud_desc_set_t;
@@ -78,6 +74,11 @@ bool tud_remote_wakeup(void);
7874
// Application Callbacks (WEAK is optional)
7975
//--------------------------------------------------------------------+
8076

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);
81+
8182
// Invoked when device is mounted (configured)
8283
ATTR_WEAK void tud_mount_cb(void);
8384

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ 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 ( (buffer != NULL) && len )
9797
{
9898
// Data stage
9999
TU_ASSERT( start_control_data_xact(rhport) );

0 commit comments

Comments
 (0)