Skip to content

Commit f36d409

Browse files
authored
Merge pull request arduino#112 from hathach/adafruit-tinyusb
update tinyusb
2 parents d81bda4 + 3e415ba commit f36d409

File tree

12 files changed

+139
-110
lines changed

12 files changed

+139
-110
lines changed

boards.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,11 @@ adafruit_metro_m4.menu.usbstack.tinyusb.build.flags.usbstack=-DUSE_TINYUSB
380380
# ------------------------------
381381
adafruit_grandcentral_m4.name=Adafruit Grand Central M4 (SAMD51)
382382
adafruit_grandcentral_m4.vid.0=0x239A
383-
adafruit_grandcentral_m4.pid.0=0x8020
383+
adafruit_grandcentral_m4.pid.0=0x8031
384384
adafruit_grandcentral_m4.vid.1=0x239A
385-
adafruit_grandcentral_m4.pid.1=0x0020
385+
adafruit_grandcentral_m4.pid.1=0x0031
386+
adafruit_grandcentral_m4.vid.1=0x239A
387+
adafruit_grandcentral_m4.pid.1=0x0032
386388
adafruit_grandcentral_m4.upload.tool=bossac18
387389
adafruit_grandcentral_m4.upload.protocol=sam-ba
388390
adafruit_grandcentral_m4.upload.maximum_size=1032192
@@ -402,7 +404,7 @@ adafruit_grandcentral_m4.build.openocdscript=openocd_scripts/arduino_zero.cfg
402404
adafruit_grandcentral_m4.build.variant=grand_central_m4
403405
adafruit_grandcentral_m4.build.variant_system_lib=
404406
adafruit_grandcentral_m4.build.vid=0x239A
405-
adafruit_grandcentral_m4.build.pid=0x8020
407+
adafruit_grandcentral_m4.build.pid=0x8031
406408
adafruit_grandcentral_m4.bootloader.tool=openocd
407409
adafruit_grandcentral_m4.bootloader.file=grand_central_m4/bootloader-grandcentralM4-v2.0.0-adafruit.5.bin
408410
adafruit_grandcentral_m4.compiler.arm.cmsis.ldflags="-L{build.variant.path}" -larm_cortexM4lf_math -mfloat-abi=hard -mfpu=fpv4-sp-d16

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: 73 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -26,44 +26,15 @@
2626

2727
#include "Adafruit_USBD_Device.h"
2828

29-
extern "C"
30-
{
31-
extern uint16_t usb_desc_str_serial[1+16];
32-
33-
// array of pointer to string descriptors
34-
uint16_t const * const string_desc_arr [] =
35-
{
36-
// 0: is supported language = English
37-
TUD_DESC_STRCONV(0x0409),
38-
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'),
41-
42-
// 2: Product
43-
TUD_DESC_STRCONV('B','l','u','e','f','r','u','i','t',' ','n','R','F','5','2','8','4','0'),
44-
45-
// 3: Serials
46-
usb_desc_str_serial,
47-
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-
};
54-
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]),
29+
#ifndef USB_MANUFACTURER
30+
#define USB_MANUFACTURER "Unknown"
31+
#endif
6232

63-
.hid_report = NULL // update later
64-
};
33+
#ifndef USB_PRODUCT
34+
#define USB_PRODUCT "Unknown"
35+
#endif
6536

66-
} // extern C
37+
extern uint8_t load_serial_number(uint16_t* serial_str);
6738

6839
Adafruit_USBD_Device USBDevice;
6940

@@ -75,17 +46,11 @@ Adafruit_USBD_Device::Adafruit_USBD_Device(void)
7546
.bDescriptorType = TUSB_DESC_DEVICE,
7647
.bcdUSB = 0x0200,
7748

78-
#if CFG_TUD_CDC
7949
// Use Interface Association Descriptor (IAD) for CDC
8050
// As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1)
8151
.bDeviceClass = TUSB_CLASS_MISC,
8252
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
8353
.bDeviceProtocol = MISC_PROTOCOL_IAD,
84-
#else
85-
.bDeviceClass = 0x00,
86-
.bDeviceSubClass = 0x00,
87-
.bDeviceProtocol = 0x00,
88-
#endif
8954

9055
.bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE,
9156

@@ -122,9 +87,6 @@ Adafruit_USBD_Device::Adafruit_USBD_Device(void)
12287
_desc_cfglen = sizeof(tusb_desc_configuration_t);
12388
_itf_count = 0;
12489
_epin_count = _epout_count = 1;
125-
126-
tud_desc_set.config = _desc_cfg;
127-
tud_desc_set.device = &_desc_device;
12890
}
12991

13092
// Add interface descriptor
@@ -184,4 +146,70 @@ bool Adafruit_USBD_Device::begin(void)
184146
return true;
185147
}
186148

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, whose contents must exist long enough for transfer to complete
161+
uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
162+
{
163+
(void) index; // for multiple configurations
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+
187215
#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(uint8_t index);
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_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/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: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ static osal_queue_t _usbd_q;
159159
//--------------------------------------------------------------------+
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);
162-
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);
162+
static bool process_set_config(uint8_t rhport, uint8_t cfg_num);
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);
@@ -376,19 +376,13 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const
376376
dcd_set_config(rhport, cfg_num);
377377
_usbd_dev.configured = cfg_num ? 1 : 0;
378378

379-
TU_ASSERT( process_set_config(rhport) );
379+
if ( cfg_num ) TU_ASSERT( process_set_config(rhport, cfg_num) );
380380
usbd_control_status(rhport, p_request);
381381
}
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:
@@ -483,9 +477,9 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const
483477

484478
// Process Set Configure Request
485479
// This function parse configuration descriptor & open drivers accordingly
486-
static bool process_set_config(uint8_t rhport)
480+
static bool process_set_config(uint8_t rhport, uint8_t cfg_num)
487481
{
488-
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(cfg_num-1); // index is cfg_num-1
489483
TU_ASSERT(desc_cfg != NULL && desc_cfg->bDescriptorType == TUSB_DESC_CONFIGURATION);
490484

491485
// Parse configuration descriptor
@@ -556,56 +550,51 @@ 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_descriptor_device_cb(), 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+
{
566+
tusb_desc_configuration_t const* desc_config = (tusb_desc_configuration_t const*) tud_descriptor_configuration_cb(desc_index);
567+
return usbd_control_xfer(rhport, p_request, (void*) desc_config, desc_config->wTotalLength);
568+
}
579569
break;
580570

581571
case TUSB_DESC_STRING:
582572
// String Descriptor always uses the desc set from user
583-
if ( desc_index < tud_desc_set.string_count )
573+
if ( desc_index == 0xEE )
584574
{
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
590-
{
591-
// out of range
592575
// The 0xEE index string is a Microsoft USB extension.
593576
// It can be used to tell Windows what driver it should use for the device !!!
594-
return NULL;
577+
return false;
578+
}else
579+
{
580+
uint8_t const* desc_str = (uint8_t const*) tud_descriptor_string_cb(desc_index);
581+
TU_ASSERT(desc_str);
582+
583+
// first byte of descriptor is its size
584+
return usbd_control_xfer(rhport, p_request, (void*) desc_str, desc_str[0]);
595585
}
596586
break;
597587

598588
case TUSB_DESC_DEVICE_QUALIFIER:
599589
// TODO If not highspeed capable stall this request otherwise
600590
// return the descriptor that could work in highspeed
601-
return NULL;
591+
return false;
602592
break;
603593

604-
default: return NULL;
594+
default: return false;
605595
}
606596

607-
*desc_len = len;
608-
return desc_data;
597+
return true;
609598
}
610599

611600
//--------------------------------------------------------------------+

0 commit comments

Comments
 (0)