update usb core to use tud_descriptor_string_cb()

This commit is contained in:
hathach committed 2019-05-11 21:25:10 +07:00
1 parent d19ae67e01
commit 8838050aff
8 files changed
+103 -86

No files matched your search

+6
View File
@@ -89,6 +89,8 @@ feather52840.build.f_cpu=64000000
feather52840.build.board=NRF52840_FEATHER
feather52840.build.core=nRF5
feather52840.build.variant=feather_nrf52840_express
feather52840.build.usb_manufacturer="Adafruit LLC"
feather52840.build.usb_product="Feather nRF52840 Express"
feather52840.build.extra_flags=-DNRF52840_XXAA -DARDUINO_NRF52_FEATHER {build.flags.usb}
feather52840.build.ldscript=nrf52840_s140_v6.ld
feather52840.build.vid=0x239A
@@ -140,6 +142,8 @@ metro52840.build.f_cpu=64000000
metro52840.build.board=NRF52840_METRO
metro52840.build.core=nRF5
metro52840.build.variant=metro_nrf52840_express
metro52840.build.usb_manufacturer="Adafruit LLC"
metro52840.build.usb_product="Metro nRF52840 Express"
metro52840.build.extra_flags=-DNRF52840_XXAA -DARDUINO_NRF52_FEATHER {build.flags.usb}
metro52840.build.ldscript=nrf52840_s140_v6.ld
metro52840.build.vid=0x239A
@@ -181,6 +185,8 @@ pca10056.build.f_cpu=64000000
pca10056.build.board=NRF52840_PCA10056
pca10056.build.core=nRF5
pca10056.build.variant=pca10056
pca10056.build.usb_manufacturer="Nordic"
pca10056.build.usb_product="nRF52840 DK"
pca10056.build.extra_flags=-DNRF52840_XXAA {build.flags.usb}
pca10056.build.ldscript=nrf52840_s140_v6.ld
pca10056.build.vid=0x239A
@@ -36,9 +36,6 @@
#define USBD_STACK_SZ (150)
// Serial is 64-bit DeviceID -> 16 chars len
uint16_t usb_desc_str_serial[1+16] = { TUD_DESC_STR_HEADER(16) };
// tinyusb function that handles power event (detected, ready, removed)
// We must call it within SD's SOC event handler, or set it as power event handler if SD is not enabled.
extern "C" void tusb_hal_nrf_power_event(uint32_t event);
@@ -82,23 +79,8 @@ static void usb_hardware_init(void)
if ( usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk ) tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY);
}
static void load_serial_number(void)
{
char tmp_serial[17];
sprintf(tmp_serial, "%08lX%08lX", NRF_FICR->DEVICEID[1], NRF_FICR->DEVICEID[0]);
for(uint8_t i=0; i<16; i++)
{
usb_desc_str_serial[1+i] = tmp_serial[i];
}
}
void Adafruit_TinyUSB_Core_init(void)
{
// Create Serial string descriptor
load_serial_number();
USBDevice.addInterface( (Adafruit_USBD_Interface&) Serial);
USBDevice.setID(USB_VID, USB_PID);
USBDevice.begin();
@@ -112,4 +94,14 @@ void Adafruit_TinyUSB_Core_init(void)
xTaskCreate( usb_device_task, "usbd", USBD_STACK_SZ, NULL, TASK_PRIO_HIGH, NULL);
}
uint8_t load_serial_number(uint16_t* serial_str)
{
// Serial is 64-bit DeviceID -> 16 chars len
char tmp_serial[17];
sprintf(tmp_serial, "%08lX%08lX", NRF_FICR->DEVICEID[1], NRF_FICR->DEVICEID[0]);
for(uint8_t i=0; i<16; i++) serial_str[i] = tmp_serial[i];
return 16;
}
#endif // USE_TINYUSB
@@ -26,43 +26,65 @@
#include "Adafruit_USBD_Device.h"
#ifndef USB_MANUFACTURER
#define USB_MANUFACTURER "Unknown"
#endif
#ifndef USB_PRODUCT
#define USB_PRODUCT "Unknown"
#endif
extern uint8_t load_serial_number(uint16_t* serial_str);
extern "C"
{
extern uint16_t usb_desc_str_serial[1+16];
// array of pointer to string descriptors
uint16_t const * const string_desc_arr [] =
{
// 0: is supported language = English
TUD_DESC_STRCONV(0x0409),
// 1: Manufacturer
TUD_DESC_STRCONV('A','d','a','f','r','u','i','t',' ','I','n','d','u','s','t','r','i','e','s'),
// 2: Product
TUD_DESC_STRCONV('B','l','u','e','f','r','u','i','t',' ','n','R','F','5','2','8','4','0'),
// 3: Serials
usb_desc_str_serial,
// // 4: CDC Interface
// TUD_DESC_STRCONV('B','l','u','e','f','r','u','i','t',' ','S','e','r','i','a','l'),
//
// // 5: MSC Interface
// TUD_DESC_STRCONV('B','l','u','e','f','r','u','i','t',' ','U','F','2'),
};
// tud_desc_set is required by tinyusb stack
tud_desc_set_t tud_desc_set =
{
.device = NULL, // update later
.config = NULL, // update later
.string_arr = (uint8_t const **) string_desc_arr,
.string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]),
.hid_report = NULL // update later
.device = NULL, // update later
.config = NULL, // update later
.hid_report = NULL // update later
};
// Invoked when received GET_STRING_DESC request
// max_char is CFG_TUD_ENDOINT0_SIZE/2 -1, typically max_char = 31 if Endpoint0 size is 64
// Return number of characters. Note usb string is in 16-bits unicode format
uint8_t tud_descriptor_string_cb(uint8_t index, uint16_t* desc, uint8_t max_char)
{
switch (index)
{
case 0:
// language = English
desc[0] = 0x0409;
return 1;
case 1: // Manufacturer
case 2: // Product
{
char const * str = (index == 1) ? USB_MANUFACTURER : USB_PRODUCT;
// cap at max char
uint8_t count = strlen(str);
if ( count > max_char ) count = max_char;
for(uint8_t i=0; i<count; i++)
{
*desc++ = str[i];
}
return count;
}
break;
case 3:
return load_serial_number(desc);
default: return 0;
}
return 0;
}
} // extern C
Adafruit_USBD_Device USBDevice;
@@ -142,6 +142,11 @@ static inline uint32_t tu_u32_from_u8(uint8_t b1, uint8_t b2, uint8_t b3, uint8_
return ( ((uint32_t) b1) << 24) + ( ((uint32_t) b2) << 16) + ( ((uint32_t) b3) << 8) + b4;
}
static inline uint16_t tu_u16_from_u8(uint8_t high, uint8_t low)
{
return (((uint16_t) high) << 8) + low;
}
static inline uint8_t tu_u16_high(uint16_t u16)
{
return (uint8_t) ( ((uint16_t) (u16 >> 8)) & 0x00ff);
@@ -160,7 +160,7 @@ static osal_queue_t _usbd_q;
static void mark_interface_endpoint(uint8_t ep2drv[8][2], uint8_t const* p_desc, uint16_t desc_len, uint8_t driver_id);
static bool process_control_request(uint8_t rhport, tusb_control_request_t const * p_request);
static bool process_set_config(uint8_t rhport);
static void const* get_descriptor(tusb_control_request_t const * p_request, uint16_t* desc_len);
static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const * p_request);
void usbd_control_reset (uint8_t rhport);
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
break;
case TUSB_REQ_GET_DESCRIPTOR:
{
uint16_t len = 0;
void* buf = (void*) get_descriptor(p_request, &len);
if ( buf == NULL || len == 0 ) return false;
usbd_control_xfer(rhport, p_request, buf, len);
}
TU_ASSERT( process_get_descriptor(rhport, p_request) );
break;
case TUSB_REQ_SET_FEATURE:
@@ -556,56 +550,53 @@ static void mark_interface_endpoint(uint8_t ep2drv[8][2], uint8_t const* p_desc,
}
// return descriptor's buffer and update desc_len
static void const* get_descriptor(tusb_control_request_t const * p_request, uint16_t* desc_len)
static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const * p_request)
{
tusb_desc_type_t const desc_type = (tusb_desc_type_t) tu_u16_high(p_request->wValue);
uint8_t const desc_index = tu_u16_low( p_request->wValue );
uint8_t const * desc_data = NULL;
uint16_t len = 0;
*desc_len = 0;
switch(desc_type)
{
case TUSB_DESC_DEVICE:
desc_data = (uint8_t const *) tud_desc_set.device;
len = sizeof(tusb_desc_device_t);
return usbd_control_xfer(rhport, p_request, (void*) tud_desc_set.device, sizeof(tusb_desc_device_t));
break;
case TUSB_DESC_CONFIGURATION:
desc_data = (uint8_t const *) tud_desc_set.config;
len = ((tusb_desc_configuration_t const*) desc_data)->wTotalLength;
return usbd_control_xfer(rhport, p_request, (void*) tud_desc_set.config, ((tusb_desc_configuration_t const*) tud_desc_set.config)->wTotalLength);
break;
case TUSB_DESC_STRING:
// String Descriptor always uses the desc set from user
if ( desc_index < tud_desc_set.string_count )
if ( desc_index == 0xEE )
{
desc_data = tud_desc_set.string_arr[desc_index];
TU_VERIFY( desc_data != NULL, NULL );
len = desc_data[0]; // first byte of descriptor is its size
}else
{
// out of range
// The 0xEE index string is a Microsoft USB extension.
// It can be used to tell Windows what driver it should use for the device !!!
return NULL;
return false;
}else
{
uint16_t desc_str[CFG_TUD_ENDOINT0_SIZE/2]; // up to endpoint0 size only
uint8_t len = 2*tud_descriptor_string_cb(desc_index, desc_str+1, CFG_TUD_ENDOINT0_SIZE/2-1);
TU_ASSERT(len > 0);
// first byte of descriptor is size, second byte is string type
len += 2; // header len
desc_str[0] = tu_u16_from_u8(TUSB_DESC_STRING, len);
return usbd_control_xfer(rhport, p_request, desc_str, len);
}
break;
case TUSB_DESC_DEVICE_QUALIFIER:
// TODO If not highspeed capable stall this request otherwise
// return the descriptor that could work in highspeed
return NULL;
return false;
break;
default: return NULL;
default: return false;
}
*desc_len = len;
return desc_data;
return true;
}
//--------------------------------------------------------------------+
@@ -39,12 +39,8 @@
/// \brief Descriptor pointer collector to all the needed.
typedef struct {
void const * device; ///< pointer to device descriptor \ref tusb_desc_device_t
void const * config; ///< pointer to the whole configuration descriptor, starting by \ref tusb_desc_configuration_t
uint8_t const** string_arr; ///< a array of pointers to string descriptors
uint16_t string_count;
void const * device; ///< pointer to device descriptor \ref tusb_desc_device_t
void const * config; ///< pointer to the whole configuration descriptor, starting by \ref tusb_desc_configuration_t
uint8_t const* hid_report;
}tud_desc_set_t;
@@ -78,6 +74,11 @@ bool tud_remote_wakeup(void);
// Application Callbacks (WEAK is optional)
//--------------------------------------------------------------------+
// Invoked when received GET_STRING_DESC request
// max_char is CFG_TUD_ENDOINT0_SIZE/2 -1, typically max_char = 31 if Endpoint0 size is 64
// Return number of characters. Note usb string is in 16-bits unicode format
uint8_t tud_descriptor_string_cb(uint8_t index, uint16_t* desc, uint8_t max_char);
// Invoked when device is mounted (configured)
ATTR_WEAK void tud_mount_cb(void);
@@ -93,7 +93,7 @@ bool usbd_control_xfer(uint8_t rhport, tusb_control_request_t const * request, v
_control_state.total_len = tu_min16(len, request->wLength);
_control_state.total_transferred = 0;
if ( buffer != NULL && len )
if ( (buffer != NULL) && len )
{
// Data stage
TU_ASSERT( start_control_data_xact(rhport) );
+1 -1
View File
@@ -62,7 +62,7 @@ nordic.path={build.core.path}/nordic
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"
# usb flags
build.flags.usb= -DUSBCON -DUSE_TINYUSB -DUSB_VID={build.vid} -DUSB_PID={build.pid}
build.flags.usb= -DUSBCON -DUSE_TINYUSB -DUSB_VID={build.vid} -DUSB_PID={build.pid} '-DUSB_MANUFACTURER={build.usb_manufacturer}' '-DUSB_PRODUCT={build.usb_product}'
# These can be overridden in platform.local.txt
compiler.c.extra_flags=