Skip to content

Commit 8838050

Browse files
committed
update usb core to use tud_descriptor_string_cb()
1 parent d19ae67 commit 8838050

File tree

8 files changed

+100
-83
lines changed

8 files changed

+100
-83
lines changed

boards.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ feather52840.build.f_cpu=64000000
8989
feather52840.build.board=NRF52840_FEATHER
9090
feather52840.build.core=nRF5
9191
feather52840.build.variant=feather_nrf52840_express
92+
feather52840.build.usb_manufacturer="Adafruit LLC"
93+
feather52840.build.usb_product="Feather nRF52840 Express"
9294
feather52840.build.extra_flags=-DNRF52840_XXAA -DARDUINO_NRF52_FEATHER {build.flags.usb}
9395
feather52840.build.ldscript=nrf52840_s140_v6.ld
9496
feather52840.build.vid=0x239A
@@ -140,6 +142,8 @@ metro52840.build.f_cpu=64000000
140142
metro52840.build.board=NRF52840_METRO
141143
metro52840.build.core=nRF5
142144
metro52840.build.variant=metro_nrf52840_express
145+
metro52840.build.usb_manufacturer="Adafruit LLC"
146+
metro52840.build.usb_product="Metro nRF52840 Express"
143147
metro52840.build.extra_flags=-DNRF52840_XXAA -DARDUINO_NRF52_FEATHER {build.flags.usb}
144148
metro52840.build.ldscript=nrf52840_s140_v6.ld
145149
metro52840.build.vid=0x239A
@@ -181,6 +185,8 @@ pca10056.build.f_cpu=64000000
181185
pca10056.build.board=NRF52840_PCA10056
182186
pca10056.build.core=nRF5
183187
pca10056.build.variant=pca10056
188+
pca10056.build.usb_manufacturer="Nordic"
189+
pca10056.build.usb_product="nRF52840 DK"
184190
pca10056.build.extra_flags=-DNRF52840_XXAA {build.flags.usb}
185191
pca10056.build.ldscript=nrf52840_s140_v6.ld
186192
pca10056.build.vid=0x239A

cores/nRF5/Adafruit_TinyUSB_Core/Adafruit_TinyUSB_Core.cpp

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@
3636

3737
#define USBD_STACK_SZ (150)
3838

39-
// Serial is 64-bit DeviceID -> 16 chars len
40-
uint16_t usb_desc_str_serial[1+16] = { TUD_DESC_STR_HEADER(16) };
41-
4239
// tinyusb function that handles power event (detected, ready, removed)
4340
// We must call it within SD's SOC event handler, or set it as power event handler if SD is not enabled.
4441
extern "C" void tusb_hal_nrf_power_event(uint32_t event);
@@ -82,23 +79,8 @@ static void usb_hardware_init(void)
8279
if ( usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk ) tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY);
8380
}
8481

85-
static void load_serial_number(void)
86-
{
87-
char tmp_serial[17];
88-
sprintf(tmp_serial, "%08lX%08lX", NRF_FICR->DEVICEID[1], NRF_FICR->DEVICEID[0]);
89-
90-
for(uint8_t i=0; i<16; i++)
91-
{
92-
usb_desc_str_serial[1+i] = tmp_serial[i];
93-
}
94-
95-
}
96-
9782
void Adafruit_TinyUSB_Core_init(void)
9883
{
99-
// Create Serial string descriptor
100-
load_serial_number();
101-
10284
USBDevice.addInterface( (Adafruit_USBD_Interface&) Serial);
10385
USBDevice.setID(USB_VID, USB_PID);
10486
USBDevice.begin();
@@ -112,4 +94,14 @@ void Adafruit_TinyUSB_Core_init(void)
11294
xTaskCreate( usb_device_task, "usbd", USBD_STACK_SZ, NULL, TASK_PRIO_HIGH, NULL);
11395
}
11496

97+
uint8_t load_serial_number(uint16_t* serial_str)
98+
{
99+
// Serial is 64-bit DeviceID -> 16 chars len
100+
char tmp_serial[17];
101+
sprintf(tmp_serial, "%08lX%08lX", NRF_FICR->DEVICEID[1], NRF_FICR->DEVICEID[0]);
102+
103+
for(uint8_t i=0; i<16; i++) serial_str[i] = tmp_serial[i];
104+
return 16;
105+
}
106+
115107
#endif // USE_TINYUSB

cores/nRF5/Adafruit_TinyUSB_Core/Adafruit_USBD_Device.cpp

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,42 +26,64 @@
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 =
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)
3554
{
36-
// 0: is supported language = English
37-
TUD_DESC_STRCONV(0x0409),
55+
switch (index)
56+
{
57+
case 0:
58+
// language = English
59+
desc[0] = 0x0409;
60+
return 1;
3861

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'),
62+
case 1: // Manufacturer
63+
case 2: // Product
64+
{
65+
char const * str = (index == 1) ? USB_MANUFACTURER : USB_PRODUCT;
4166

42-
// 2: Product
43-
TUD_DESC_STRCONV('B','l','u','e','f','r','u','i','t',' ','n','R','F','5','2','8','4','0'),
67+
// cap at max char
68+
uint8_t count = strlen(str);
69+
if ( count > max_char ) count = max_char;
4470

45-
// 3: Serials
46-
usb_desc_str_serial,
71+
for(uint8_t i=0; i<count; i++)
72+
{
73+
*desc++ = str[i];
74+
}
75+
return count;
76+
}
77+
break;
4778

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-
};
79+
case 3:
80+
return load_serial_number(desc);
5481

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]),
82+
default: return 0;
83+
}
6284

63-
.hid_report = NULL // update later
64-
};
85+
return 0;
86+
}
6587

6688
} // extern C
6789

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

platform.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ nordic.path={build.core.path}/nordic
6262
build.flags.nrf= -DSOFTDEVICE_PRESENT -DARDUINO_FEATHER52 -DARDUINO_NRF52_ADAFRUIT -DNRF52_SERIES -Os {build.debug_flags} "-I{build.core.path}/cmsis/include" "-I{nordic.path}" "-I{nordic.path}/nrfx" "-I{nordic.path}/nrfx/hal" "-I{nordic.path}/nrfx/mdk" "-I{nordic.path}/nrfx/soc" "-I{nordic.path}/nrfx/drivers/include" "-I{nordic.path}/nrfx/drivers/src" "-I{nordic.path}/softdevice/{build.sd_name}_nrf52_{build.sd_version}_API/include" "-I{rtos.path}/Source/include" "-I{rtos.path}/config" "-I{rtos.path}/portable/GCC/nrf52" "-I{rtos.path}/portable/CMSIS/nrf52" "-I{build.core.path}/sysview/SEGGER" "-I{build.core.path}/sysview/Config" "-I{build.core.path}/Adafruit_TinyUSB_Core" "-I{build.core.path}/Adafruit_TinyUSB_Core/tinyusb/src"
6363

6464
# usb flags
65-
build.flags.usb= -DUSBCON -DUSE_TINYUSB -DUSB_VID={build.vid} -DUSB_PID={build.pid}
65+
build.flags.usb= -DUSBCON -DUSE_TINYUSB -DUSB_VID={build.vid} -DUSB_PID={build.pid} '-DUSB_MANUFACTURER={build.usb_manufacturer}' '-DUSB_PRODUCT={build.usb_product}'
6666

6767
# These can be overridden in platform.local.txt
6868
compiler.c.extra_flags=

0 commit comments

Comments
 (0)