update all examples to use unique ID as serial if avaialble
This commit is contained in:
@@ -23,6 +23,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "bsp/board_api.h"
|
||||||
#include "tusb.h"
|
#include "tusb.h"
|
||||||
|
|
||||||
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
|
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
|
||||||
@@ -116,50 +117,63 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
|||||||
// String Descriptors
|
// String Descriptors
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
|
||||||
|
// String Descriptor Index
|
||||||
|
enum {
|
||||||
|
STRID_LANGID = 0,
|
||||||
|
STRID_MANUFACTURER,
|
||||||
|
STRID_PRODUCT,
|
||||||
|
STRID_SERIAL,
|
||||||
|
};
|
||||||
|
|
||||||
// array of pointer to string descriptors
|
// array of pointer to string descriptors
|
||||||
char const* string_desc_arr [] =
|
char const* string_desc_arr [] = {
|
||||||
{
|
|
||||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||||
"PaniRCorp", // 1: Manufacturer
|
"PaniRCorp", // 1: Manufacturer
|
||||||
"MicNode_4_Ch", // 2: Product
|
"MicNode_4_Ch", // 2: Product
|
||||||
"123458", // 3: Serials, should use chip ID
|
NULL, // 3: Serials will use unique ID if possible
|
||||||
"UAC2", // 4: Audio Interface
|
"UAC2", // 4: Audio Interface
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint16_t _desc_str[32];
|
static uint16_t _desc_str[32 + 1];
|
||||||
|
|
||||||
// Invoked when received GET STRING DESCRIPTOR request
|
// Invoked when received GET STRING DESCRIPTOR request
|
||||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||||
{
|
|
||||||
(void) langid;
|
(void) langid;
|
||||||
|
size_t chr_count;
|
||||||
|
|
||||||
uint8_t chr_count;
|
switch ( index ) {
|
||||||
|
case STRID_LANGID:
|
||||||
if ( index == 0)
|
|
||||||
{
|
|
||||||
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||||
chr_count = 1;
|
chr_count = 1;
|
||||||
}else
|
break;
|
||||||
{
|
|
||||||
// Convert ASCII string into UTF-16
|
|
||||||
|
|
||||||
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
|
case STRID_SERIAL:
|
||||||
|
chr_count = board_usb_get_serial(_desc_str + 1, 32);
|
||||||
|
break;
|
||||||
|
|
||||||
const char* str = string_desc_arr[index];
|
default:
|
||||||
|
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
||||||
|
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
||||||
|
|
||||||
|
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
|
||||||
|
|
||||||
|
const char *str = string_desc_arr[index];
|
||||||
|
|
||||||
// Cap at max char
|
// Cap at max char
|
||||||
chr_count = (uint8_t) strlen(str);
|
chr_count = strlen(str);
|
||||||
if ( chr_count > 31 ) chr_count = 31;
|
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
|
||||||
|
if ( chr_count > max_count ) chr_count = max_count;
|
||||||
|
|
||||||
for(uint8_t i=0; i<chr_count; i++)
|
// Convert ASCII string into UTF-16
|
||||||
{
|
for ( size_t i = 0; i < chr_count; i++ ) {
|
||||||
_desc_str[1+i] = str[i];
|
_desc_str[1 + i] = str[i];
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// first byte is length (including header), second byte is string type
|
// first byte is length (including header), second byte is string type
|
||||||
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
|
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
|
||||||
|
|
||||||
return _desc_str;
|
return _desc_str;
|
||||||
}
|
}
|
||||||
|
@@ -23,6 +23,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "bsp/board_api.h"
|
||||||
#include "tusb.h"
|
#include "tusb.h"
|
||||||
|
|
||||||
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
|
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
|
||||||
@@ -116,50 +117,65 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
|||||||
// String Descriptors
|
// String Descriptors
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
|
||||||
|
// String Descriptor Index
|
||||||
|
enum {
|
||||||
|
STRID_LANGID = 0,
|
||||||
|
STRID_MANUFACTURER,
|
||||||
|
STRID_PRODUCT,
|
||||||
|
STRID_SERIAL,
|
||||||
|
};
|
||||||
|
|
||||||
// array of pointer to string descriptors
|
// array of pointer to string descriptors
|
||||||
char const* string_desc_arr [] =
|
char const* string_desc_arr [] =
|
||||||
{
|
{
|
||||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||||
"PaniRCorp", // 1: Manufacturer
|
"PaniRCorp", // 1: Manufacturer
|
||||||
"MicNode", // 2: Product
|
"MicNode", // 2: Product
|
||||||
"123456", // 3: Serials, should use chip ID
|
NULL, // 3: Serials will use unique ID if possible
|
||||||
"UAC2", // 4: Audio Interface
|
"UAC2", // 4: Audio Interface
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint16_t _desc_str[32];
|
static uint16_t _desc_str[32 + 1];
|
||||||
|
|
||||||
// Invoked when received GET STRING DESCRIPTOR request
|
// Invoked when received GET STRING DESCRIPTOR request
|
||||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||||
{
|
|
||||||
(void) langid;
|
(void) langid;
|
||||||
|
size_t chr_count;
|
||||||
|
|
||||||
uint8_t chr_count;
|
switch ( index ) {
|
||||||
|
case STRID_LANGID:
|
||||||
if ( index == 0)
|
|
||||||
{
|
|
||||||
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||||
chr_count = 1;
|
chr_count = 1;
|
||||||
}else
|
break;
|
||||||
{
|
|
||||||
// Convert ASCII string into UTF-16
|
|
||||||
|
|
||||||
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
|
case STRID_SERIAL:
|
||||||
|
chr_count = board_usb_get_serial(_desc_str + 1, 32);
|
||||||
|
break;
|
||||||
|
|
||||||
const char* str = string_desc_arr[index];
|
default:
|
||||||
|
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
||||||
|
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
||||||
|
|
||||||
|
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
|
||||||
|
|
||||||
|
const char *str = string_desc_arr[index];
|
||||||
|
|
||||||
// Cap at max char
|
// Cap at max char
|
||||||
chr_count = (uint8_t) strlen(str);
|
chr_count = strlen(str);
|
||||||
if ( chr_count > 31 ) chr_count = 31;
|
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
|
||||||
|
if ( chr_count > max_count ) chr_count = max_count;
|
||||||
|
|
||||||
for(uint8_t i=0; i<chr_count; i++)
|
// Convert ASCII string into UTF-16
|
||||||
{
|
for ( size_t i = 0; i < chr_count; i++ ) {
|
||||||
_desc_str[1+i] = str[i];
|
_desc_str[1 + i] = str[i];
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// first byte is length (including header), second byte is string type
|
// first byte is length (including header), second byte is string type
|
||||||
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
|
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
|
||||||
|
|
||||||
return _desc_str;
|
return _desc_str;
|
||||||
}
|
}
|
||||||
|
@@ -24,6 +24,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "bsp/board_api.h"
|
||||||
#include "tusb.h"
|
#include "tusb.h"
|
||||||
#include "usb_descriptors.h"
|
#include "usb_descriptors.h"
|
||||||
|
|
||||||
@@ -120,50 +121,65 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
|||||||
// String Descriptors
|
// String Descriptors
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
|
||||||
|
// String Descriptor Index
|
||||||
|
enum {
|
||||||
|
STRID_LANGID = 0,
|
||||||
|
STRID_MANUFACTURER,
|
||||||
|
STRID_PRODUCT,
|
||||||
|
STRID_SERIAL,
|
||||||
|
};
|
||||||
|
|
||||||
// array of pointer to string descriptors
|
// array of pointer to string descriptors
|
||||||
char const* string_desc_arr [] =
|
char const* string_desc_arr [] =
|
||||||
{
|
{
|
||||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||||
"PaniRCorp", // 1: Manufacturer
|
"PaniRCorp", // 1: Manufacturer
|
||||||
"MicNode", // 2: Product
|
"MicNode", // 2: Product
|
||||||
"123456", // 3: Serials, should use chip ID
|
NULL, // 3: Serials will use unique ID if possible
|
||||||
"UAC2", // 4: Audio Interface
|
"UAC2", // 4: Audio Interface
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint16_t _desc_str[32];
|
static uint16_t _desc_str[32 + 1];
|
||||||
|
|
||||||
// Invoked when received GET STRING DESCRIPTOR request
|
// Invoked when received GET STRING DESCRIPTOR request
|
||||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||||
{
|
|
||||||
(void) langid;
|
(void) langid;
|
||||||
|
size_t chr_count;
|
||||||
|
|
||||||
uint8_t chr_count;
|
switch ( index ) {
|
||||||
|
case STRID_LANGID:
|
||||||
if ( index == 0)
|
|
||||||
{
|
|
||||||
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||||
chr_count = 1;
|
chr_count = 1;
|
||||||
}else
|
break;
|
||||||
{
|
|
||||||
// Convert ASCII string into UTF-16
|
|
||||||
|
|
||||||
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
|
case STRID_SERIAL:
|
||||||
|
chr_count = board_usb_get_serial(_desc_str + 1, 32);
|
||||||
|
break;
|
||||||
|
|
||||||
const char* str = string_desc_arr[index];
|
default:
|
||||||
|
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
||||||
|
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
||||||
|
|
||||||
|
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
|
||||||
|
|
||||||
|
const char *str = string_desc_arr[index];
|
||||||
|
|
||||||
// Cap at max char
|
// Cap at max char
|
||||||
chr_count = (uint8_t) strlen(str);
|
chr_count = strlen(str);
|
||||||
if ( chr_count > 31 ) chr_count = 31;
|
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
|
||||||
|
if ( chr_count > max_count ) chr_count = max_count;
|
||||||
|
|
||||||
for(uint8_t i=0; i<chr_count; i++)
|
// Convert ASCII string into UTF-16
|
||||||
{
|
for ( size_t i = 0; i < chr_count; i++ ) {
|
||||||
_desc_str[1+i] = str[i];
|
_desc_str[1 + i] = str[i];
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// first byte is length (including header), second byte is string type
|
// first byte is length (including header), second byte is string type
|
||||||
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
|
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
|
||||||
|
|
||||||
return _desc_str;
|
return _desc_str;
|
||||||
}
|
}
|
||||||
|
@@ -23,13 +23,14 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "bsp/board_api.h"
|
||||||
#include "tusb.h"
|
#include "tusb.h"
|
||||||
|
|
||||||
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
|
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
|
||||||
* Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC.
|
* Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC.
|
||||||
*
|
*
|
||||||
* Auto ProductID layout's Bitmap:
|
* Auto ProductID layout's Bitmap:
|
||||||
* [MSB] MIDI | HID | MSC | CDC [LSB]
|
* [MSB] HID | MSC | CDC [LSB]
|
||||||
*/
|
*/
|
||||||
#define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) )
|
#define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) )
|
||||||
#define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \
|
#define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \
|
||||||
@@ -213,52 +214,64 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
|||||||
// String Descriptors
|
// String Descriptors
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
|
||||||
|
// String Descriptor Index
|
||||||
|
enum {
|
||||||
|
STRID_LANGID = 0,
|
||||||
|
STRID_MANUFACTURER,
|
||||||
|
STRID_PRODUCT,
|
||||||
|
STRID_SERIAL,
|
||||||
|
};
|
||||||
|
|
||||||
// array of pointer to string descriptors
|
// array of pointer to string descriptors
|
||||||
char const* string_desc_arr [] =
|
char const *string_desc_arr[] =
|
||||||
{
|
{
|
||||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||||
"TinyUSB", // 1: Manufacturer
|
"TinyUSB", // 1: Manufacturer
|
||||||
"TinyUSB Device", // 2: Product
|
"TinyUSB Device", // 2: Product
|
||||||
"123456", // 3: Serials, should use chip ID
|
NULL, // 3: Serials will use unique ID if possible
|
||||||
"TinyUSB CDC", // 4: CDC Interface
|
"TinyUSB CDC", // 4: CDC Interface
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint16_t _desc_str[32];
|
static uint16_t _desc_str[32 + 1];
|
||||||
|
|
||||||
// Invoked when received GET STRING DESCRIPTOR request
|
// Invoked when received GET STRING DESCRIPTOR request
|
||||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||||
{
|
|
||||||
(void) langid;
|
(void) langid;
|
||||||
|
size_t chr_count;
|
||||||
|
|
||||||
uint8_t chr_count;
|
switch ( index ) {
|
||||||
|
case STRID_LANGID:
|
||||||
if ( index == 0)
|
|
||||||
{
|
|
||||||
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||||
chr_count = 1;
|
chr_count = 1;
|
||||||
}else
|
break;
|
||||||
{
|
|
||||||
|
case STRID_SERIAL:
|
||||||
|
chr_count = board_usb_get_serial(_desc_str + 1, 32);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
||||||
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
||||||
|
|
||||||
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
|
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
|
||||||
|
|
||||||
const char* str = string_desc_arr[index];
|
const char *str = string_desc_arr[index];
|
||||||
|
|
||||||
// Cap at max char
|
// Cap at max char
|
||||||
chr_count = (uint8_t) strlen(str);
|
chr_count = strlen(str);
|
||||||
if ( chr_count > 31 ) chr_count = 31;
|
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
|
||||||
|
if ( chr_count > max_count ) chr_count = max_count;
|
||||||
|
|
||||||
// Convert ASCII string into UTF-16
|
// Convert ASCII string into UTF-16
|
||||||
for(uint8_t i=0; i<chr_count; i++)
|
for ( size_t i = 0; i < chr_count; i++ ) {
|
||||||
{
|
_desc_str[1 + i] = str[i];
|
||||||
_desc_str[1+i] = str[i];
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// first byte is length (including header), second byte is string type
|
// first byte is length (including header), second byte is string type
|
||||||
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
|
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
|
||||||
|
|
||||||
return _desc_str;
|
return _desc_str;
|
||||||
}
|
}
|
||||||
|
@@ -42,8 +42,7 @@
|
|||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
// Device Descriptors
|
// Device Descriptors
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
tusb_desc_device_t const desc_device =
|
tusb_desc_device_t const desc_device = {
|
||||||
{
|
|
||||||
.bLength = sizeof(tusb_desc_device_t),
|
.bLength = sizeof(tusb_desc_device_t),
|
||||||
.bDescriptorType = TUSB_DESC_DEVICE,
|
.bDescriptorType = TUSB_DESC_DEVICE,
|
||||||
.bcdUSB = USB_BCD,
|
.bcdUSB = USB_BCD,
|
||||||
@@ -69,8 +68,7 @@ tusb_desc_device_t const desc_device =
|
|||||||
|
|
||||||
// Invoked when received GET DEVICE DESCRIPTOR
|
// Invoked when received GET DEVICE DESCRIPTOR
|
||||||
// Application return pointer to descriptor
|
// Application return pointer to descriptor
|
||||||
uint8_t const * tud_descriptor_device_cb(void)
|
uint8_t const *tud_descriptor_device_cb(void) {
|
||||||
{
|
|
||||||
return (uint8_t const *) &desc_device;
|
return (uint8_t const *) &desc_device;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,8 +76,7 @@ uint8_t const * tud_descriptor_device_cb(void)
|
|||||||
// Configuration Descriptor
|
// Configuration Descriptor
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
|
||||||
enum
|
enum {
|
||||||
{
|
|
||||||
ITF_NUM_CDC = 0,
|
ITF_NUM_CDC = 0,
|
||||||
ITF_NUM_CDC_DATA,
|
ITF_NUM_CDC_DATA,
|
||||||
ITF_NUM_MSC,
|
ITF_NUM_MSC,
|
||||||
@@ -141,8 +138,7 @@ enum
|
|||||||
#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_MSC_DESC_LEN)
|
#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_MSC_DESC_LEN)
|
||||||
|
|
||||||
// full speed configuration
|
// full speed configuration
|
||||||
uint8_t const desc_fs_configuration[] =
|
uint8_t const desc_fs_configuration[] = {
|
||||||
{
|
|
||||||
// Config number, interface count, string index, total length, attribute, power in mA
|
// Config number, interface count, string index, total length, attribute, power in mA
|
||||||
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100),
|
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100),
|
||||||
|
|
||||||
@@ -157,8 +153,7 @@ uint8_t const desc_fs_configuration[] =
|
|||||||
// Per USB specs: high speed capable device must report device_qualifier and other_speed_configuration
|
// Per USB specs: high speed capable device must report device_qualifier and other_speed_configuration
|
||||||
|
|
||||||
// high speed configuration
|
// high speed configuration
|
||||||
uint8_t const desc_hs_configuration[] =
|
uint8_t const desc_hs_configuration[] = {
|
||||||
{
|
|
||||||
// Config number, interface count, string index, total length, attribute, power in mA
|
// Config number, interface count, string index, total length, attribute, power in mA
|
||||||
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100),
|
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100),
|
||||||
|
|
||||||
@@ -173,8 +168,7 @@ uint8_t const desc_hs_configuration[] =
|
|||||||
uint8_t desc_other_speed_config[CONFIG_TOTAL_LEN];
|
uint8_t desc_other_speed_config[CONFIG_TOTAL_LEN];
|
||||||
|
|
||||||
// device qualifier is mostly similar to device descriptor since we don't change configuration based on speed
|
// device qualifier is mostly similar to device descriptor since we don't change configuration based on speed
|
||||||
tusb_desc_device_qualifier_t const desc_device_qualifier =
|
tusb_desc_device_qualifier_t const desc_device_qualifier = {
|
||||||
{
|
|
||||||
.bLength = sizeof(tusb_desc_device_qualifier_t),
|
.bLength = sizeof(tusb_desc_device_qualifier_t),
|
||||||
.bDescriptorType = TUSB_DESC_DEVICE_QUALIFIER,
|
.bDescriptorType = TUSB_DESC_DEVICE_QUALIFIER,
|
||||||
.bcdUSB = USB_BCD,
|
.bcdUSB = USB_BCD,
|
||||||
@@ -192,16 +186,14 @@ tusb_desc_device_qualifier_t const desc_device_qualifier =
|
|||||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete.
|
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete.
|
||||||
// device_qualifier descriptor describes information about a high-speed capable device that would
|
// device_qualifier descriptor describes information about a high-speed capable device that would
|
||||||
// change if the device were operating at the other speed. If not highspeed capable stall this request.
|
// change if the device were operating at the other speed. If not highspeed capable stall this request.
|
||||||
uint8_t const* tud_descriptor_device_qualifier_cb(void)
|
uint8_t const *tud_descriptor_device_qualifier_cb(void) {
|
||||||
{
|
return (uint8_t const *) &desc_device_qualifier;
|
||||||
return (uint8_t const*) &desc_device_qualifier;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Invoked when received GET OTHER SEED CONFIGURATION DESCRIPTOR request
|
// Invoked when received GET OTHER SEED CONFIGURATION DESCRIPTOR request
|
||||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||||
// Configuration descriptor in the other speed e.g if high speed then this is for full speed and vice versa
|
// Configuration descriptor in the other speed e.g if high speed then this is for full speed and vice versa
|
||||||
uint8_t const* tud_descriptor_other_speed_configuration_cb(uint8_t index)
|
uint8_t const *tud_descriptor_other_speed_configuration_cb(uint8_t index) {
|
||||||
{
|
|
||||||
(void) index; // for multiple configurations
|
(void) index; // for multiple configurations
|
||||||
|
|
||||||
// if link speed is high return fullspeed config, and vice versa
|
// if link speed is high return fullspeed config, and vice versa
|
||||||
@@ -221,8 +213,7 @@ uint8_t const* tud_descriptor_other_speed_configuration_cb(uint8_t index)
|
|||||||
// Invoked when received GET CONFIGURATION DESCRIPTOR
|
// Invoked when received GET CONFIGURATION DESCRIPTOR
|
||||||
// Application return pointer to descriptor
|
// Application return pointer to descriptor
|
||||||
// Descriptor contents must exist long enough for transfer to complete
|
// Descriptor contents must exist long enough for transfer to complete
|
||||||
uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
uint8_t const *tud_descriptor_configuration_cb(uint8_t index) {
|
||||||
{
|
|
||||||
(void) index; // for multiple configurations
|
(void) index; // for multiple configurations
|
||||||
|
|
||||||
#if TUD_OPT_HIGH_SPEED
|
#if TUD_OPT_HIGH_SPEED
|
||||||
@@ -246,57 +237,55 @@ enum {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// array of pointer to string descriptors
|
// array of pointer to string descriptors
|
||||||
char const* string_desc_arr [] =
|
char const *string_desc_arr[] = {
|
||||||
{
|
|
||||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||||
"TinyUSB", // 1: Manufacturer
|
"TinyUSB", // 1: Manufacturer
|
||||||
"TinyUSB Device", // 2: Product
|
"TinyUSB Device", // 2: Product
|
||||||
"123456789012", // 3: Serials, should use chip ID
|
NULL, // 3: Serials will use unique ID if possible
|
||||||
"TinyUSB CDC", // 4: CDC Interface
|
"TinyUSB CDC", // 4: CDC Interface
|
||||||
"TinyUSB MSC", // 5: MSC Interface
|
"TinyUSB MSC", // 5: MSC Interface
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint16_t _desc_str[32+1];
|
static uint16_t _desc_str[32 + 1];
|
||||||
|
|
||||||
// Invoked when received GET STRING DESCRIPTOR request
|
// Invoked when received GET STRING DESCRIPTOR request
|
||||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||||
{
|
|
||||||
(void) langid;
|
(void) langid;
|
||||||
|
|
||||||
size_t chr_count;
|
size_t chr_count;
|
||||||
|
|
||||||
switch(index) {
|
switch ( index ) {
|
||||||
case STRID_LANGID:
|
case STRID_LANGID:
|
||||||
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||||
chr_count = 1;
|
chr_count = 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case STRID_SERIAL:
|
case STRID_SERIAL:
|
||||||
chr_count = board_usb_get_serial(_desc_str+1, 32);
|
chr_count = board_usb_get_serial(_desc_str + 1, 32);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
||||||
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
||||||
|
|
||||||
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
|
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
|
||||||
|
|
||||||
const char* str = string_desc_arr[index];
|
const char *str = string_desc_arr[index];
|
||||||
|
|
||||||
// Cap at max char
|
// Cap at max char
|
||||||
chr_count = strlen(str);
|
chr_count = strlen(str);
|
||||||
if ( chr_count > 31 ) chr_count = 31;
|
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
|
||||||
|
if ( chr_count > max_count ) chr_count = max_count;
|
||||||
|
|
||||||
// Convert ASCII string into UTF-16
|
// Convert ASCII string into UTF-16
|
||||||
for(size_t i=0; i<chr_count; i++) {
|
for ( size_t i = 0; i < chr_count; i++ ) {
|
||||||
_desc_str[1+i] = str[i];
|
_desc_str[1 + i] = str[i];
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// first byte is length (including header), second byte is string type
|
// first byte is length (including header), second byte is string type
|
||||||
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
|
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
|
||||||
|
|
||||||
return _desc_str;
|
return _desc_str;
|
||||||
}
|
}
|
||||||
|
@@ -23,6 +23,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "bsp/board_api.h"
|
||||||
#include "tusb.h"
|
#include "tusb.h"
|
||||||
|
|
||||||
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
|
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
|
||||||
@@ -212,53 +213,65 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
|||||||
// String Descriptors
|
// String Descriptors
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
|
||||||
|
// String Descriptor Index
|
||||||
|
enum {
|
||||||
|
STRID_LANGID = 0,
|
||||||
|
STRID_MANUFACTURER,
|
||||||
|
STRID_PRODUCT,
|
||||||
|
STRID_SERIAL,
|
||||||
|
};
|
||||||
|
|
||||||
// array of pointer to string descriptors
|
// array of pointer to string descriptors
|
||||||
char const* string_desc_arr [] =
|
char const *string_desc_arr[] =
|
||||||
{
|
{
|
||||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||||
"TinyUSB", // 1: Manufacturer
|
"TinyUSB", // 1: Manufacturer
|
||||||
"TinyUSB Device", // 2: Product
|
"TinyUSB Device", // 2: Product
|
||||||
"123456789012", // 3: Serials, should use chip ID
|
NULL, // 3: Serials will use unique ID if possible
|
||||||
"TinyUSB CDC", // 4: CDC Interface
|
"TinyUSB CDC", // 4: CDC Interface
|
||||||
"TinyUSB MSC", // 5: MSC Interface
|
"TinyUSB MSC", // 5: MSC Interface
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint16_t _desc_str[32];
|
static uint16_t _desc_str[32 + 1];
|
||||||
|
|
||||||
// Invoked when received GET STRING DESCRIPTOR request
|
// Invoked when received GET STRING DESCRIPTOR request
|
||||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||||
{
|
|
||||||
(void) langid;
|
(void) langid;
|
||||||
|
size_t chr_count;
|
||||||
|
|
||||||
uint8_t chr_count;
|
switch ( index ) {
|
||||||
|
case STRID_LANGID:
|
||||||
if ( index == 0)
|
|
||||||
{
|
|
||||||
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||||
chr_count = 1;
|
chr_count = 1;
|
||||||
}else
|
break;
|
||||||
{
|
|
||||||
|
case STRID_SERIAL:
|
||||||
|
chr_count = board_usb_get_serial(_desc_str + 1, 32);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
||||||
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
||||||
|
|
||||||
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
|
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
|
||||||
|
|
||||||
const char* str = string_desc_arr[index];
|
const char *str = string_desc_arr[index];
|
||||||
|
|
||||||
// Cap at max char
|
// Cap at max char
|
||||||
chr_count = (uint8_t) strlen(str);
|
chr_count = strlen(str);
|
||||||
if ( chr_count > 31 ) chr_count = 31;
|
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
|
||||||
|
if ( chr_count > max_count ) chr_count = max_count;
|
||||||
|
|
||||||
// Convert ASCII string into UTF-16
|
// Convert ASCII string into UTF-16
|
||||||
for(uint8_t i=0; i<chr_count; i++)
|
for ( size_t i = 0; i < chr_count; i++ ) {
|
||||||
{
|
_desc_str[1 + i] = str[i];
|
||||||
_desc_str[1+i] = str[i];
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// first byte is length (including header), second byte is string type
|
// first byte is length (including header), second byte is string type
|
||||||
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
|
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
|
||||||
|
|
||||||
return _desc_str;
|
return _desc_str;
|
||||||
}
|
}
|
||||||
|
@@ -25,6 +25,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "bsp/board_api.h"
|
||||||
#include "tusb.h"
|
#include "tusb.h"
|
||||||
#include "usb_descriptors.h"
|
#include "usb_descriptors.h"
|
||||||
|
|
||||||
@@ -150,53 +151,66 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
|||||||
// String Descriptors
|
// String Descriptors
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
|
||||||
|
// String Descriptor Index
|
||||||
|
enum {
|
||||||
|
STRID_LANGID = 0,
|
||||||
|
STRID_MANUFACTURER,
|
||||||
|
STRID_PRODUCT,
|
||||||
|
STRID_SERIAL,
|
||||||
|
};
|
||||||
|
|
||||||
// array of pointer to string descriptors
|
// array of pointer to string descriptors
|
||||||
char const* string_desc_arr [] =
|
char const *string_desc_arr[] =
|
||||||
{
|
{
|
||||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||||
"TinyUSB", // 1: Manufacturer
|
"TinyUSB", // 1: Manufacturer
|
||||||
"TinyUSB headset", // 2: Product
|
"TinyUSB headset", // 2: Product
|
||||||
"000001", // 3: Serials, should use chip ID
|
NULL, // 3: Serials will use unique ID if possible
|
||||||
"TinyUSB Speakers", // 4: Audio Interface
|
"TinyUSB Speakers", // 4: Audio Interface
|
||||||
"TinyUSB Microphone", // 5: Audio Interface
|
"TinyUSB Microphone", // 5: Audio Interface
|
||||||
"TinyUSB CDC", // 6: Audio Interface
|
"TinyUSB CDC", // 6: Audio Interface
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint16_t _desc_str[32];
|
static uint16_t _desc_str[32 + 1];
|
||||||
|
|
||||||
// Invoked when received GET STRING DESCRIPTOR request
|
// Invoked when received GET STRING DESCRIPTOR request
|
||||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||||
{
|
(void) langid;
|
||||||
(void)langid;
|
size_t chr_count;
|
||||||
|
|
||||||
uint8_t chr_count;
|
switch ( index ) {
|
||||||
|
case STRID_LANGID:
|
||||||
if (index == 0)
|
|
||||||
{
|
|
||||||
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||||
chr_count = 1;
|
chr_count = 1;
|
||||||
}
|
break;
|
||||||
else
|
|
||||||
{
|
|
||||||
// Convert ASCII string into UTF-16
|
|
||||||
|
|
||||||
if (!(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0]))) return NULL;
|
case STRID_SERIAL:
|
||||||
|
chr_count = board_usb_get_serial(_desc_str + 1, 32);
|
||||||
|
break;
|
||||||
|
|
||||||
const char* str = string_desc_arr[index];
|
default:
|
||||||
|
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
||||||
|
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
||||||
|
|
||||||
|
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
|
||||||
|
|
||||||
|
const char *str = string_desc_arr[index];
|
||||||
|
|
||||||
// Cap at max char
|
// Cap at max char
|
||||||
chr_count = (uint8_t) strlen(str);
|
chr_count = strlen(str);
|
||||||
if (chr_count > 31) chr_count = 31;
|
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
|
||||||
|
if ( chr_count > max_count ) chr_count = max_count;
|
||||||
|
|
||||||
for (uint8_t i = 0; i < chr_count; i++)
|
// Convert ASCII string into UTF-16
|
||||||
{
|
for ( size_t i = 0; i < chr_count; i++ ) {
|
||||||
_desc_str[1 + i] = str[i];
|
_desc_str[1 + i] = str[i];
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// first byte is length (including header), second byte is string type
|
// first byte is length (including header), second byte is string type
|
||||||
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
|
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
|
||||||
|
|
||||||
return _desc_str;
|
return _desc_str;
|
||||||
}
|
}
|
||||||
|
@@ -23,6 +23,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "bsp/board_api.h"
|
||||||
#include "tusb.h"
|
#include "tusb.h"
|
||||||
#include "class/dfu/dfu_device.h"
|
#include "class/dfu/dfu_device.h"
|
||||||
|
|
||||||
@@ -116,56 +117,65 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
|||||||
// String Descriptors
|
// String Descriptors
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
|
||||||
|
// String Descriptor Index
|
||||||
|
enum {
|
||||||
|
STRID_LANGID = 0,
|
||||||
|
STRID_MANUFACTURER,
|
||||||
|
STRID_PRODUCT,
|
||||||
|
STRID_SERIAL,
|
||||||
|
};
|
||||||
|
|
||||||
// array of pointer to string descriptors
|
// array of pointer to string descriptors
|
||||||
char const* string_desc_arr [] =
|
char const *string_desc_arr[] =
|
||||||
{
|
{
|
||||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||||
"TinyUSB", // 1: Manufacturer
|
"TinyUSB", // 1: Manufacturer
|
||||||
"TinyUSB Device", // 2: Product
|
"TinyUSB Device", // 2: Product
|
||||||
"123456", // 3: Serials, should use chip ID
|
NULL, // 3: Serials will use unique ID if possible
|
||||||
"FLASH", // 4: DFU Partition 1
|
"FLASH", // 4: DFU Partition 1
|
||||||
"EEPROM", // 5: DFU Partition 2
|
"EEPROM", // 5: DFU Partition 2
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint16_t _desc_str[32];
|
static uint16_t _desc_str[32 + 1];
|
||||||
|
|
||||||
// Invoked when received GET STRING DESCRIPTOR request
|
// Invoked when received GET STRING DESCRIPTOR request
|
||||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||||
{
|
|
||||||
(void) langid;
|
(void) langid;
|
||||||
|
|
||||||
size_t chr_count;
|
size_t chr_count;
|
||||||
|
|
||||||
if ( index == 0)
|
switch ( index ) {
|
||||||
{
|
case STRID_LANGID:
|
||||||
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||||
chr_count = 1;
|
chr_count = 1;
|
||||||
}
|
break;
|
||||||
else
|
|
||||||
{
|
case STRID_SERIAL:
|
||||||
|
chr_count = board_usb_get_serial(_desc_str + 1, 32);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
||||||
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
||||||
|
|
||||||
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
|
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
|
||||||
|
|
||||||
const char* str = string_desc_arr[index];
|
const char *str = string_desc_arr[index];
|
||||||
|
|
||||||
// Cap at max char
|
// Cap at max char
|
||||||
chr_count = (uint8_t) strlen(str);
|
chr_count = strlen(str);
|
||||||
if ( chr_count > 31 ) {
|
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
|
||||||
chr_count = 31;
|
if ( chr_count > max_count ) chr_count = max_count;
|
||||||
}
|
|
||||||
|
|
||||||
// Convert ASCII string into UTF-16
|
// Convert ASCII string into UTF-16
|
||||||
for(uint8_t i=0; i<chr_count; i++)
|
for ( size_t i = 0; i < chr_count; i++ ) {
|
||||||
{
|
_desc_str[1 + i] = str[i];
|
||||||
_desc_str[1+i] = str[i];
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// first byte is length (including header), second byte is string type
|
// first byte is length (including header), second byte is string type
|
||||||
_desc_str[0] = (uint16_t)((((uint16_t)TUSB_DESC_STRING) << 8 ) | (2u*chr_count + 2u));
|
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
|
||||||
|
|
||||||
return _desc_str;
|
return _desc_str;
|
||||||
}
|
}
|
||||||
|
@@ -23,6 +23,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "bsp/board_api.h"
|
||||||
#include "tusb.h"
|
#include "tusb.h"
|
||||||
#include "class/dfu/dfu_rt_device.h"
|
#include "class/dfu/dfu_rt_device.h"
|
||||||
|
|
||||||
@@ -112,55 +113,64 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
|||||||
// String Descriptors
|
// String Descriptors
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
|
||||||
|
// String Descriptor Index
|
||||||
|
enum {
|
||||||
|
STRID_LANGID = 0,
|
||||||
|
STRID_MANUFACTURER,
|
||||||
|
STRID_PRODUCT,
|
||||||
|
STRID_SERIAL,
|
||||||
|
};
|
||||||
|
|
||||||
// array of pointer to string descriptors
|
// array of pointer to string descriptors
|
||||||
char const* string_desc_arr [] =
|
char const *string_desc_arr[] =
|
||||||
{
|
{
|
||||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||||
"TinyUSB", // 1: Manufacturer
|
"TinyUSB", // 1: Manufacturer
|
||||||
"TinyUSB Device", // 2: Product
|
"TinyUSB Device", // 2: Product
|
||||||
"123456", // 3: Serials, should use chip ID
|
NULL, // 3: Serials will use unique ID if possible
|
||||||
"TinyUSB DFU runtime", // 4: DFU runtime
|
"TinyUSB DFU runtime", // 4: DFU runtime
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint16_t _desc_str[32];
|
static uint16_t _desc_str[32 + 1];
|
||||||
|
|
||||||
// Invoked when received GET STRING DESCRIPTOR request
|
// Invoked when received GET STRING DESCRIPTOR request
|
||||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||||
{
|
|
||||||
(void) langid;
|
(void) langid;
|
||||||
|
|
||||||
size_t chr_count;
|
size_t chr_count;
|
||||||
|
|
||||||
if ( index == 0)
|
switch ( index ) {
|
||||||
{
|
case STRID_LANGID:
|
||||||
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||||
chr_count = 1;
|
chr_count = 1;
|
||||||
}
|
break;
|
||||||
else
|
|
||||||
{
|
case STRID_SERIAL:
|
||||||
|
chr_count = board_usb_get_serial(_desc_str + 1, 32);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
||||||
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
||||||
|
|
||||||
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
|
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
|
||||||
|
|
||||||
const char* str = string_desc_arr[index];
|
const char *str = string_desc_arr[index];
|
||||||
|
|
||||||
// Cap at max char
|
// Cap at max char
|
||||||
chr_count = (uint8_t) strlen(str);
|
chr_count = strlen(str);
|
||||||
if ( chr_count > 31 ) {
|
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
|
||||||
chr_count = 31;
|
if ( chr_count > max_count ) chr_count = max_count;
|
||||||
}
|
|
||||||
|
|
||||||
// Convert ASCII string into UTF-16
|
// Convert ASCII string into UTF-16
|
||||||
for(uint8_t i=0; i<chr_count; i++)
|
for ( size_t i = 0; i < chr_count; i++ ) {
|
||||||
{
|
_desc_str[1 + i] = str[i];
|
||||||
_desc_str[1+i] = str[i];
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// first byte is length (including header), second byte is string type
|
// first byte is length (including header), second byte is string type
|
||||||
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
|
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
|
||||||
|
|
||||||
return _desc_str;
|
return _desc_str;
|
||||||
}
|
}
|
||||||
|
@@ -23,8 +23,8 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "tusb.h"
|
|
||||||
#include "bsp/board_api.h"
|
#include "bsp/board_api.h"
|
||||||
|
#include "tusb.h"
|
||||||
|
|
||||||
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
|
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
|
||||||
* Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC.
|
* Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC.
|
||||||
@@ -206,51 +206,63 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
|||||||
// String Descriptors
|
// String Descriptors
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
|
||||||
|
// String Descriptor Index
|
||||||
|
enum {
|
||||||
|
STRID_LANGID = 0,
|
||||||
|
STRID_MANUFACTURER,
|
||||||
|
STRID_PRODUCT,
|
||||||
|
STRID_SERIAL,
|
||||||
|
};
|
||||||
|
|
||||||
// array of pointer to string descriptors
|
// array of pointer to string descriptors
|
||||||
char const* string_desc_arr [] =
|
char const *string_desc_arr[] =
|
||||||
{
|
{
|
||||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||||
"TinyUSB", // 1: Manufacturer
|
"TinyUSB", // 1: Manufacturer
|
||||||
"TinyUSB Device", // 2: Product
|
"TinyUSB Device", // 2: Product
|
||||||
"123456", // 3: Serials, should use chip ID
|
NULL, // 3: Serials will use unique ID if possible
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint16_t _desc_str[32];
|
static uint16_t _desc_str[32 + 1];
|
||||||
|
|
||||||
// Invoked when received GET STRING DESCRIPTOR request
|
// Invoked when received GET STRING DESCRIPTOR request
|
||||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||||
{
|
|
||||||
(void) langid;
|
(void) langid;
|
||||||
|
size_t chr_count;
|
||||||
|
|
||||||
uint8_t chr_count;
|
switch ( index ) {
|
||||||
|
case STRID_LANGID:
|
||||||
if ( index == 0)
|
|
||||||
{
|
|
||||||
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||||
chr_count = 1;
|
chr_count = 1;
|
||||||
}else
|
break;
|
||||||
{
|
|
||||||
|
case STRID_SERIAL:
|
||||||
|
chr_count = board_usb_get_serial(_desc_str + 1, 32);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
||||||
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
||||||
|
|
||||||
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
|
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
|
||||||
|
|
||||||
const char* str = string_desc_arr[index];
|
const char *str = string_desc_arr[index];
|
||||||
|
|
||||||
// Cap at max char
|
// Cap at max char
|
||||||
chr_count = (uint8_t) strlen(str);
|
chr_count = strlen(str);
|
||||||
if ( chr_count > 31 ) chr_count = 31;
|
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
|
||||||
|
if ( chr_count > max_count ) chr_count = max_count;
|
||||||
|
|
||||||
// Convert ASCII string into UTF-16
|
// Convert ASCII string into UTF-16
|
||||||
for(uint8_t i=0; i<chr_count; i++)
|
for ( size_t i = 0; i < chr_count; i++ ) {
|
||||||
{
|
_desc_str[1 + i] = str[i];
|
||||||
_desc_str[1+i] = str[i];
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// first byte is length (including header), second byte is string type
|
// first byte is length (including header), second byte is string type
|
||||||
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
|
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
|
||||||
|
|
||||||
return _desc_str;
|
return _desc_str;
|
||||||
}
|
}
|
||||||
|
@@ -23,6 +23,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "bsp/board_api.h"
|
||||||
#include "tusb.h"
|
#include "tusb.h"
|
||||||
#include "usb_descriptors.h"
|
#include "usb_descriptors.h"
|
||||||
|
|
||||||
@@ -130,51 +131,63 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
|||||||
// String Descriptors
|
// String Descriptors
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
|
||||||
|
// String Descriptor Index
|
||||||
|
enum {
|
||||||
|
STRID_LANGID = 0,
|
||||||
|
STRID_MANUFACTURER,
|
||||||
|
STRID_PRODUCT,
|
||||||
|
STRID_SERIAL,
|
||||||
|
};
|
||||||
|
|
||||||
// array of pointer to string descriptors
|
// array of pointer to string descriptors
|
||||||
char const* string_desc_arr [] =
|
char const *string_desc_arr[] =
|
||||||
{
|
{
|
||||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||||
"TinyUSB", // 1: Manufacturer
|
"TinyUSB", // 1: Manufacturer
|
||||||
"TinyUSB Device", // 2: Product
|
"TinyUSB Device", // 2: Product
|
||||||
"123456", // 3: Serials, should use chip ID
|
NULL, // 3: Serials will use unique ID if possible
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint16_t _desc_str[32];
|
static uint16_t _desc_str[32 + 1];
|
||||||
|
|
||||||
// Invoked when received GET STRING DESCRIPTOR request
|
// Invoked when received GET STRING DESCRIPTOR request
|
||||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||||
{
|
|
||||||
(void) langid;
|
(void) langid;
|
||||||
|
size_t chr_count;
|
||||||
|
|
||||||
uint8_t chr_count;
|
switch ( index ) {
|
||||||
|
case STRID_LANGID:
|
||||||
if ( index == 0)
|
|
||||||
{
|
|
||||||
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||||
chr_count = 1;
|
chr_count = 1;
|
||||||
}else
|
break;
|
||||||
{
|
|
||||||
|
case STRID_SERIAL:
|
||||||
|
chr_count = board_usb_get_serial(_desc_str + 1, 32);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
||||||
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
||||||
|
|
||||||
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
|
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
|
||||||
|
|
||||||
const char* str = string_desc_arr[index];
|
const char *str = string_desc_arr[index];
|
||||||
|
|
||||||
// Cap at max char
|
// Cap at max char
|
||||||
chr_count = (uint8_t) strlen(str);
|
chr_count = strlen(str);
|
||||||
if ( chr_count > 31 ) chr_count = 31;
|
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
|
||||||
|
if ( chr_count > max_count ) chr_count = max_count;
|
||||||
|
|
||||||
// Convert ASCII string into UTF-16
|
// Convert ASCII string into UTF-16
|
||||||
for(uint8_t i=0; i<chr_count; i++)
|
for ( size_t i = 0; i < chr_count; i++ ) {
|
||||||
{
|
_desc_str[1 + i] = str[i];
|
||||||
_desc_str[1+i] = str[i];
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// first byte is length (including header), second byte is string type
|
// first byte is length (including header), second byte is string type
|
||||||
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
|
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
|
||||||
|
|
||||||
return _desc_str;
|
return _desc_str;
|
||||||
}
|
}
|
||||||
|
@@ -23,6 +23,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "bsp/board_api.h"
|
||||||
#include "tusb.h"
|
#include "tusb.h"
|
||||||
#include "usb_descriptors.h"
|
#include "usb_descriptors.h"
|
||||||
|
|
||||||
@@ -177,51 +178,63 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
|||||||
// String Descriptors
|
// String Descriptors
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
|
||||||
|
// String Descriptor Index
|
||||||
|
enum {
|
||||||
|
STRID_LANGID = 0,
|
||||||
|
STRID_MANUFACTURER,
|
||||||
|
STRID_PRODUCT,
|
||||||
|
STRID_SERIAL,
|
||||||
|
};
|
||||||
|
|
||||||
// array of pointer to string descriptors
|
// array of pointer to string descriptors
|
||||||
char const* string_desc_arr [] =
|
char const *string_desc_arr[] =
|
||||||
{
|
{
|
||||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||||
"TinyUSB", // 1: Manufacturer
|
"TinyUSB", // 1: Manufacturer
|
||||||
"TinyUSB Device", // 2: Product
|
"TinyUSB Device", // 2: Product
|
||||||
"123456", // 3: Serials, should use chip ID
|
NULL, // 3: Serials will use unique ID if possible
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint16_t _desc_str[32];
|
static uint16_t _desc_str[32 + 1];
|
||||||
|
|
||||||
// Invoked when received GET STRING DESCRIPTOR request
|
// Invoked when received GET STRING DESCRIPTOR request
|
||||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||||
{
|
|
||||||
(void) langid;
|
(void) langid;
|
||||||
|
size_t chr_count;
|
||||||
|
|
||||||
uint8_t chr_count;
|
switch ( index ) {
|
||||||
|
case STRID_LANGID:
|
||||||
if ( index == 0)
|
|
||||||
{
|
|
||||||
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||||
chr_count = 1;
|
chr_count = 1;
|
||||||
}else
|
break;
|
||||||
{
|
|
||||||
|
case STRID_SERIAL:
|
||||||
|
chr_count = board_usb_get_serial(_desc_str + 1, 32);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
||||||
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
||||||
|
|
||||||
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
|
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
|
||||||
|
|
||||||
const char* str = string_desc_arr[index];
|
const char *str = string_desc_arr[index];
|
||||||
|
|
||||||
// Cap at max char
|
// Cap at max char
|
||||||
chr_count = (uint8_t) strlen(str);
|
chr_count = strlen(str);
|
||||||
if ( chr_count > 31 ) chr_count = 31;
|
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
|
||||||
|
if ( chr_count > max_count ) chr_count = max_count;
|
||||||
|
|
||||||
// Convert ASCII string into UTF-16
|
// Convert ASCII string into UTF-16
|
||||||
for(uint8_t i=0; i<chr_count; i++)
|
for ( size_t i = 0; i < chr_count; i++ ) {
|
||||||
{
|
_desc_str[1 + i] = str[i];
|
||||||
_desc_str[1+i] = str[i];
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// first byte is length (including header), second byte is string type
|
// first byte is length (including header), second byte is string type
|
||||||
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
|
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
|
||||||
|
|
||||||
return _desc_str;
|
return _desc_str;
|
||||||
}
|
}
|
||||||
|
@@ -23,6 +23,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "bsp/board_api.h"
|
||||||
#include "tusb.h"
|
#include "tusb.h"
|
||||||
#include "usb_descriptors.h"
|
#include "usb_descriptors.h"
|
||||||
|
|
||||||
@@ -175,51 +176,63 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
|||||||
// String Descriptors
|
// String Descriptors
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
|
||||||
|
// String Descriptor Index
|
||||||
|
enum {
|
||||||
|
STRID_LANGID = 0,
|
||||||
|
STRID_MANUFACTURER,
|
||||||
|
STRID_PRODUCT,
|
||||||
|
STRID_SERIAL,
|
||||||
|
};
|
||||||
|
|
||||||
// array of pointer to string descriptors
|
// array of pointer to string descriptors
|
||||||
char const* string_desc_arr [] =
|
char const *string_desc_arr[] =
|
||||||
{
|
{
|
||||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||||
"TinyUSB", // 1: Manufacturer
|
"TinyUSB", // 1: Manufacturer
|
||||||
"TinyUSB Device", // 2: Product
|
"TinyUSB Device", // 2: Product
|
||||||
"123456", // 3: Serials, should use chip ID
|
NULL, // 3: Serials will use unique ID if possible
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint16_t _desc_str[32];
|
static uint16_t _desc_str[32 + 1];
|
||||||
|
|
||||||
// Invoked when received GET STRING DESCRIPTOR request
|
// Invoked when received GET STRING DESCRIPTOR request
|
||||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||||
{
|
|
||||||
(void) langid;
|
(void) langid;
|
||||||
|
size_t chr_count;
|
||||||
|
|
||||||
uint8_t chr_count;
|
switch ( index ) {
|
||||||
|
case STRID_LANGID:
|
||||||
if ( index == 0)
|
|
||||||
{
|
|
||||||
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||||
chr_count = 1;
|
chr_count = 1;
|
||||||
}else
|
break;
|
||||||
{
|
|
||||||
|
case STRID_SERIAL:
|
||||||
|
chr_count = board_usb_get_serial(_desc_str + 1, 32);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
||||||
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
||||||
|
|
||||||
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
|
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
|
||||||
|
|
||||||
const char* str = string_desc_arr[index];
|
const char *str = string_desc_arr[index];
|
||||||
|
|
||||||
// Cap at max char
|
// Cap at max char
|
||||||
chr_count = (uint8_t) strlen(str);
|
chr_count = strlen(str);
|
||||||
if ( chr_count > 31 ) chr_count = 31;
|
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
|
||||||
|
if ( chr_count > max_count ) chr_count = max_count;
|
||||||
|
|
||||||
// Convert ASCII string into UTF-16
|
// Convert ASCII string into UTF-16
|
||||||
for(uint8_t i=0; i<chr_count; i++)
|
for ( size_t i = 0; i < chr_count; i++ ) {
|
||||||
{
|
_desc_str[1 + i] = str[i];
|
||||||
_desc_str[1+i] = str[i];
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// first byte is length (including header), second byte is string type
|
// first byte is length (including header), second byte is string type
|
||||||
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
|
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
|
||||||
|
|
||||||
return _desc_str;
|
return _desc_str;
|
||||||
}
|
}
|
||||||
|
@@ -23,6 +23,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "bsp/board_api.h"
|
||||||
#include "tusb.h"
|
#include "tusb.h"
|
||||||
|
|
||||||
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
|
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
|
||||||
@@ -120,51 +121,63 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
|||||||
// String Descriptors
|
// String Descriptors
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
|
||||||
|
// String Descriptor Index
|
||||||
|
enum {
|
||||||
|
STRID_LANGID = 0,
|
||||||
|
STRID_MANUFACTURER,
|
||||||
|
STRID_PRODUCT,
|
||||||
|
STRID_SERIAL,
|
||||||
|
};
|
||||||
|
|
||||||
// array of pointer to string descriptors
|
// array of pointer to string descriptors
|
||||||
char const* string_desc_arr [] =
|
char const *string_desc_arr[] =
|
||||||
{
|
{
|
||||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||||
"TinyUSB", // 1: Manufacturer
|
"TinyUSB", // 1: Manufacturer
|
||||||
"TinyUSB Device", // 2: Product
|
"TinyUSB Device", // 2: Product
|
||||||
"123456", // 3: Serials, should use chip ID
|
NULL, // 3: Serials will use unique ID if possible
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint16_t _desc_str[32];
|
static uint16_t _desc_str[32 + 1];
|
||||||
|
|
||||||
// Invoked when received GET STRING DESCRIPTOR request
|
// Invoked when received GET STRING DESCRIPTOR request
|
||||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||||
{
|
|
||||||
(void) langid;
|
(void) langid;
|
||||||
|
size_t chr_count;
|
||||||
|
|
||||||
uint8_t chr_count;
|
switch ( index ) {
|
||||||
|
case STRID_LANGID:
|
||||||
if ( index == 0)
|
|
||||||
{
|
|
||||||
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||||
chr_count = 1;
|
chr_count = 1;
|
||||||
}else
|
break;
|
||||||
{
|
|
||||||
|
case STRID_SERIAL:
|
||||||
|
chr_count = board_usb_get_serial(_desc_str + 1, 32);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
||||||
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
||||||
|
|
||||||
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
|
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
|
||||||
|
|
||||||
const char* str = string_desc_arr[index];
|
const char *str = string_desc_arr[index];
|
||||||
|
|
||||||
// Cap at max char
|
// Cap at max char
|
||||||
chr_count = (uint8_t) strlen(str);
|
chr_count = strlen(str);
|
||||||
if ( chr_count > 31 ) chr_count = 31;
|
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
|
||||||
|
if ( chr_count > max_count ) chr_count = max_count;
|
||||||
|
|
||||||
// Convert ASCII string into UTF-16
|
// Convert ASCII string into UTF-16
|
||||||
for(uint8_t i=0; i<chr_count; i++)
|
for ( size_t i = 0; i < chr_count; i++ ) {
|
||||||
{
|
_desc_str[1 + i] = str[i];
|
||||||
_desc_str[1+i] = str[i];
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// first byte is length (including header), second byte is string type
|
// first byte is length (including header), second byte is string type
|
||||||
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
|
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
|
||||||
|
|
||||||
return _desc_str;
|
return _desc_str;
|
||||||
}
|
}
|
||||||
|
@@ -23,6 +23,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "bsp/board_api.h"
|
||||||
#include "tusb.h"
|
#include "tusb.h"
|
||||||
|
|
||||||
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
|
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
|
||||||
@@ -136,53 +137,65 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
|||||||
// String Descriptors
|
// String Descriptors
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
|
||||||
|
// String Descriptor Index
|
||||||
|
enum {
|
||||||
|
STRID_LANGID = 0,
|
||||||
|
STRID_MANUFACTURER,
|
||||||
|
STRID_PRODUCT,
|
||||||
|
STRID_SERIAL,
|
||||||
|
};
|
||||||
|
|
||||||
// array of pointer to string descriptors
|
// array of pointer to string descriptors
|
||||||
char const* string_desc_arr [] =
|
char const *string_desc_arr[] =
|
||||||
{
|
{
|
||||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||||
"TinyUSB", // 1: Manufacturer
|
"TinyUSB", // 1: Manufacturer
|
||||||
"TinyUSB Device", // 2: Product
|
"TinyUSB Device", // 2: Product
|
||||||
"123456", // 3: Serials, should use chip ID
|
NULL, // 3: Serials will use unique ID if possible
|
||||||
"Keyboard Interface", // 4: Interface 1 String
|
"Keyboard Interface", // 4: Interface 1 String
|
||||||
"Mouse Interface", // 5: Interface 2 String
|
"Mouse Interface", // 5: Interface 2 String
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint16_t _desc_str[32];
|
static uint16_t _desc_str[32 + 1];
|
||||||
|
|
||||||
// Invoked when received GET STRING DESCRIPTOR request
|
// Invoked when received GET STRING DESCRIPTOR request
|
||||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||||
{
|
|
||||||
(void) langid;
|
(void) langid;
|
||||||
|
size_t chr_count;
|
||||||
|
|
||||||
uint8_t chr_count;
|
switch ( index ) {
|
||||||
|
case STRID_LANGID:
|
||||||
if ( index == 0)
|
|
||||||
{
|
|
||||||
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||||
chr_count = 1;
|
chr_count = 1;
|
||||||
}else
|
break;
|
||||||
{
|
|
||||||
|
case STRID_SERIAL:
|
||||||
|
chr_count = board_usb_get_serial(_desc_str + 1, 32);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
||||||
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
||||||
|
|
||||||
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
|
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
|
||||||
|
|
||||||
const char* str = string_desc_arr[index];
|
const char *str = string_desc_arr[index];
|
||||||
|
|
||||||
// Cap at max char
|
// Cap at max char
|
||||||
chr_count = (uint8_t) strlen(str);
|
chr_count = strlen(str);
|
||||||
if ( chr_count > 31 ) chr_count = 31;
|
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
|
||||||
|
if ( chr_count > max_count ) chr_count = max_count;
|
||||||
|
|
||||||
// Convert ASCII string into UTF-16
|
// Convert ASCII string into UTF-16
|
||||||
for(uint8_t i=0; i<chr_count; i++)
|
for ( size_t i = 0; i < chr_count; i++ ) {
|
||||||
{
|
_desc_str[1 + i] = str[i];
|
||||||
_desc_str[1+i] = str[i];
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// first byte is length (including header), second byte is string type
|
// first byte is length (including header), second byte is string type
|
||||||
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
|
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
|
||||||
|
|
||||||
return _desc_str;
|
return _desc_str;
|
||||||
}
|
}
|
||||||
|
@@ -23,13 +23,14 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "bsp/board_api.h"
|
||||||
#include "tusb.h"
|
#include "tusb.h"
|
||||||
|
|
||||||
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
|
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
|
||||||
* Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC.
|
* Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC.
|
||||||
*
|
*
|
||||||
* Auto ProductID layout's Bitmap:
|
* Auto ProductID layout's Bitmap:
|
||||||
* [MSB] MIDI | HID | MSC | CDC [LSB]
|
* [MSB] HID | MSC | CDC [LSB]
|
||||||
*/
|
*/
|
||||||
#define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) )
|
#define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) )
|
||||||
#define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \
|
#define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \
|
||||||
@@ -133,51 +134,63 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
|||||||
// String Descriptors
|
// String Descriptors
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
|
||||||
|
// String Descriptor Index
|
||||||
|
enum {
|
||||||
|
STRID_LANGID = 0,
|
||||||
|
STRID_MANUFACTURER,
|
||||||
|
STRID_PRODUCT,
|
||||||
|
STRID_SERIAL,
|
||||||
|
};
|
||||||
|
|
||||||
// array of pointer to string descriptors
|
// array of pointer to string descriptors
|
||||||
char const* string_desc_arr [] =
|
char const *string_desc_arr[] =
|
||||||
{
|
{
|
||||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||||
"TinyUSB", // 1: Manufacturer
|
"TinyUSB", // 1: Manufacturer
|
||||||
"TinyUSB Device", // 2: Product
|
"TinyUSB Device", // 2: Product
|
||||||
"123456", // 3: Serials, should use chip ID
|
NULL, // 3: Serials will use unique ID if possible
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint16_t _desc_str[32];
|
static uint16_t _desc_str[32 + 1];
|
||||||
|
|
||||||
// Invoked when received GET STRING DESCRIPTOR request
|
// Invoked when received GET STRING DESCRIPTOR request
|
||||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||||
{
|
|
||||||
(void) langid;
|
(void) langid;
|
||||||
|
size_t chr_count;
|
||||||
|
|
||||||
uint8_t chr_count;
|
switch ( index ) {
|
||||||
|
case STRID_LANGID:
|
||||||
if ( index == 0)
|
|
||||||
{
|
|
||||||
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||||
chr_count = 1;
|
chr_count = 1;
|
||||||
}else
|
break;
|
||||||
{
|
|
||||||
|
case STRID_SERIAL:
|
||||||
|
chr_count = board_usb_get_serial(_desc_str + 1, 32);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
||||||
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
||||||
|
|
||||||
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
|
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
|
||||||
|
|
||||||
const char* str = string_desc_arr[index];
|
const char *str = string_desc_arr[index];
|
||||||
|
|
||||||
// Cap at max char
|
// Cap at max char
|
||||||
chr_count = (uint8_t) strlen(str);
|
chr_count = strlen(str);
|
||||||
if ( chr_count > 31 ) chr_count = 31;
|
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
|
||||||
|
if ( chr_count > max_count ) chr_count = max_count;
|
||||||
|
|
||||||
// Convert ASCII string into UTF-16
|
// Convert ASCII string into UTF-16
|
||||||
for(uint8_t i=0; i<chr_count; i++)
|
for ( size_t i = 0; i < chr_count; i++ ) {
|
||||||
{
|
_desc_str[1 + i] = str[i];
|
||||||
_desc_str[1+i] = str[i];
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// first byte is length (including header), second byte is string type
|
// first byte is length (including header), second byte is string type
|
||||||
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
|
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
|
||||||
|
|
||||||
return _desc_str;
|
return _desc_str;
|
||||||
}
|
}
|
||||||
|
@@ -23,6 +23,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "bsp/board_api.h"
|
||||||
#include "tusb.h"
|
#include "tusb.h"
|
||||||
|
|
||||||
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
|
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
|
||||||
@@ -141,51 +142,63 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
|||||||
// String Descriptors
|
// String Descriptors
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
|
||||||
|
// String Descriptor Index
|
||||||
|
enum {
|
||||||
|
STRID_LANGID = 0,
|
||||||
|
STRID_MANUFACTURER,
|
||||||
|
STRID_PRODUCT,
|
||||||
|
STRID_SERIAL,
|
||||||
|
};
|
||||||
|
|
||||||
// array of pointer to string descriptors
|
// array of pointer to string descriptors
|
||||||
char const* string_desc_arr [] =
|
char const *string_desc_arr[] =
|
||||||
{
|
{
|
||||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||||
"TinyUSB", // 1: Manufacturer
|
"TinyUSB", // 1: Manufacturer
|
||||||
"TinyUSB Device", // 2: Product
|
"TinyUSB Device", // 2: Product
|
||||||
"123456789012", // 3: Serials, should use chip ID
|
NULL, // 3: Serials will use unique ID if possible
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint16_t _desc_str[32];
|
static uint16_t _desc_str[32 + 1];
|
||||||
|
|
||||||
// Invoked when received GET STRING DESCRIPTOR request
|
// Invoked when received GET STRING DESCRIPTOR request
|
||||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||||
{
|
|
||||||
(void) langid;
|
(void) langid;
|
||||||
|
size_t chr_count;
|
||||||
|
|
||||||
uint8_t chr_count;
|
switch ( index ) {
|
||||||
|
case STRID_LANGID:
|
||||||
if ( index == 0)
|
|
||||||
{
|
|
||||||
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||||
chr_count = 1;
|
chr_count = 1;
|
||||||
}else
|
break;
|
||||||
{
|
|
||||||
|
case STRID_SERIAL:
|
||||||
|
chr_count = board_usb_get_serial(_desc_str + 1, 32);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
||||||
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
||||||
|
|
||||||
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
|
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
|
||||||
|
|
||||||
const char* str = string_desc_arr[index];
|
const char *str = string_desc_arr[index];
|
||||||
|
|
||||||
// Cap at max char
|
// Cap at max char
|
||||||
chr_count = (uint8_t) strlen(str);
|
chr_count = strlen(str);
|
||||||
if ( chr_count > 31 ) chr_count = 31;
|
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
|
||||||
|
if ( chr_count > max_count ) chr_count = max_count;
|
||||||
|
|
||||||
// Convert ASCII string into UTF-16
|
// Convert ASCII string into UTF-16
|
||||||
for(uint8_t i=0; i<chr_count; i++)
|
for ( size_t i = 0; i < chr_count; i++ ) {
|
||||||
{
|
_desc_str[1 + i] = str[i];
|
||||||
_desc_str[1+i] = str[i];
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// first byte is length (including header), second byte is string type
|
// first byte is length (including header), second byte is string type
|
||||||
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
|
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
|
||||||
|
|
||||||
return _desc_str;
|
return _desc_str;
|
||||||
}
|
}
|
||||||
|
@@ -23,6 +23,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "bsp/board_api.h"
|
||||||
#include "tusb.h"
|
#include "tusb.h"
|
||||||
|
|
||||||
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
|
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
|
||||||
@@ -190,55 +191,56 @@ static char const* string_desc_arr [] =
|
|||||||
[STRID_LANGID] = (const char[]) { 0x09, 0x04 }, // supported language is English (0x0409)
|
[STRID_LANGID] = (const char[]) { 0x09, 0x04 }, // supported language is English (0x0409)
|
||||||
[STRID_MANUFACTURER] = "TinyUSB", // Manufacturer
|
[STRID_MANUFACTURER] = "TinyUSB", // Manufacturer
|
||||||
[STRID_PRODUCT] = "TinyUSB Device", // Product
|
[STRID_PRODUCT] = "TinyUSB Device", // Product
|
||||||
[STRID_SERIAL] = "123456", // Serial
|
[STRID_SERIAL] = NULL, // Serials will use unique ID if possible
|
||||||
[STRID_INTERFACE] = "TinyUSB Network Interface" // Interface Description
|
[STRID_INTERFACE] = "TinyUSB Network Interface" // Interface Description
|
||||||
|
|
||||||
// STRID_MAC index is handled separately
|
// STRID_MAC index is handled separately
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint16_t _desc_str[32];
|
static uint16_t _desc_str[32 + 1];
|
||||||
|
|
||||||
// Invoked when received GET STRING DESCRIPTOR request
|
// Invoked when received GET STRING DESCRIPTOR request
|
||||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||||
{
|
|
||||||
(void) langid;
|
(void) langid;
|
||||||
|
|
||||||
unsigned int chr_count = 0;
|
unsigned int chr_count = 0;
|
||||||
|
|
||||||
if (STRID_LANGID == index)
|
switch ( index ) {
|
||||||
{
|
case STRID_LANGID:
|
||||||
memcpy(&_desc_str[1], string_desc_arr[STRID_LANGID], 2);
|
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||||
chr_count = 1;
|
chr_count = 1;
|
||||||
}
|
break;
|
||||||
else if (STRID_MAC == index)
|
|
||||||
{
|
|
||||||
// Convert MAC address into UTF-16
|
|
||||||
|
|
||||||
for (unsigned i=0; i<sizeof(tud_network_mac_address); i++)
|
case STRID_SERIAL:
|
||||||
{
|
chr_count = board_usb_get_serial(_desc_str + 1, 32);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case STRID_MAC:
|
||||||
|
// Convert MAC address into UTF-16
|
||||||
|
for (unsigned i=0; i<sizeof(tud_network_mac_address); i++) {
|
||||||
_desc_str[1+chr_count++] = "0123456789ABCDEF"[(tud_network_mac_address[i] >> 4) & 0xf];
|
_desc_str[1+chr_count++] = "0123456789ABCDEF"[(tud_network_mac_address[i] >> 4) & 0xf];
|
||||||
_desc_str[1+chr_count++] = "0123456789ABCDEF"[(tud_network_mac_address[i] >> 0) & 0xf];
|
_desc_str[1+chr_count++] = "0123456789ABCDEF"[(tud_network_mac_address[i] >> 0) & 0xf];
|
||||||
}
|
}
|
||||||
}
|
break;
|
||||||
else
|
|
||||||
{
|
default:
|
||||||
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
||||||
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
||||||
|
|
||||||
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
|
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
|
||||||
|
|
||||||
const char* str = string_desc_arr[index];
|
const char *str = string_desc_arr[index];
|
||||||
|
|
||||||
// Cap at max char
|
// Cap at max char
|
||||||
chr_count = (uint8_t) strlen(str);
|
chr_count = strlen(str);
|
||||||
if ( chr_count > (TU_ARRAY_SIZE(_desc_str) - 1)) chr_count = TU_ARRAY_SIZE(_desc_str) - 1;
|
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
|
||||||
|
if ( chr_count > max_count ) chr_count = max_count;
|
||||||
|
|
||||||
// Convert ASCII string into UTF-16
|
// Convert ASCII string into UTF-16
|
||||||
for (unsigned int i=0; i<chr_count; i++)
|
for ( size_t i = 0; i < chr_count; i++ ) {
|
||||||
{
|
_desc_str[1 + i] = str[i];
|
||||||
_desc_str[1+i] = str[i];
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// first byte is length (including header), second byte is string type
|
// first byte is length (including header), second byte is string type
|
||||||
|
@@ -24,6 +24,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "bsp/board_api.h"
|
||||||
#include "tusb.h"
|
#include "tusb.h"
|
||||||
#include "usb_descriptors.h"
|
#include "usb_descriptors.h"
|
||||||
|
|
||||||
@@ -126,52 +127,65 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
|||||||
// String Descriptors
|
// String Descriptors
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
|
||||||
|
// String Descriptor Index
|
||||||
|
enum {
|
||||||
|
STRID_LANGID = 0,
|
||||||
|
STRID_MANUFACTURER,
|
||||||
|
STRID_PRODUCT,
|
||||||
|
STRID_SERIAL,
|
||||||
|
};
|
||||||
|
|
||||||
// array of pointer to string descriptors
|
// array of pointer to string descriptors
|
||||||
char const* string_desc_arr [] =
|
char const *string_desc_arr[] =
|
||||||
{
|
{
|
||||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||||
"TinyUSB", // 1: Manufacturer
|
"TinyUSB", // 1: Manufacturer
|
||||||
"TinyUSB headset", // 2: Product
|
"TinyUSB headset", // 2: Product
|
||||||
"000001", // 3: Serials, should use chip ID
|
NULL, // 3: Serials will use unique ID if possible
|
||||||
"TinyUSB Speakers", // 4: Audio Interface
|
"TinyUSB Speakers", // 4: Audio Interface
|
||||||
"TinyUSB Microphone", // 5: Audio Interface
|
"TinyUSB Microphone", // 5: Audio Interface
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint16_t _desc_str[32];
|
static uint16_t _desc_str[32 + 1];
|
||||||
|
|
||||||
// Invoked when received GET STRING DESCRIPTOR request
|
// Invoked when received GET STRING DESCRIPTOR request
|
||||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||||
{
|
(void) langid;
|
||||||
(void)langid;
|
size_t chr_count;
|
||||||
|
|
||||||
uint8_t chr_count;
|
switch ( index ) {
|
||||||
|
case STRID_LANGID:
|
||||||
if (index == 0)
|
|
||||||
{
|
|
||||||
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||||
chr_count = 1;
|
chr_count = 1;
|
||||||
}
|
break;
|
||||||
else
|
|
||||||
{
|
|
||||||
// Convert ASCII string into UTF-16
|
|
||||||
|
|
||||||
if (!(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0]))) return NULL;
|
case STRID_SERIAL:
|
||||||
|
chr_count = board_usb_get_serial(_desc_str + 1, 32);
|
||||||
|
break;
|
||||||
|
|
||||||
const char* str = string_desc_arr[index];
|
default:
|
||||||
|
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
||||||
|
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
||||||
|
|
||||||
|
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
|
||||||
|
|
||||||
|
const char *str = string_desc_arr[index];
|
||||||
|
|
||||||
// Cap at max char
|
// Cap at max char
|
||||||
chr_count = (uint8_t) strlen(str);
|
chr_count = strlen(str);
|
||||||
if (chr_count > 31) chr_count = 31;
|
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
|
||||||
|
if ( chr_count > max_count ) chr_count = max_count;
|
||||||
|
|
||||||
for (uint8_t i = 0; i < chr_count; i++)
|
// Convert ASCII string into UTF-16
|
||||||
{
|
for ( size_t i = 0; i < chr_count; i++ ) {
|
||||||
_desc_str[1 + i] = str[i];
|
_desc_str[1 + i] = str[i];
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// first byte is length (including header), second byte is string type
|
// first byte is length (including header), second byte is string type
|
||||||
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
|
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
|
||||||
|
|
||||||
return _desc_str;
|
return _desc_str;
|
||||||
}
|
}
|
||||||
|
@@ -23,6 +23,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "bsp/board_api.h"
|
||||||
#include "tusb.h"
|
#include "tusb.h"
|
||||||
#include "class/usbtmc/usbtmc.h"
|
#include "class/usbtmc/usbtmc.h"
|
||||||
#include "class/usbtmc/usbtmc_device.h"
|
#include "class/usbtmc/usbtmc_device.h"
|
||||||
@@ -188,55 +189,64 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
|||||||
// String Descriptors
|
// String Descriptors
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
|
||||||
|
// String Descriptor Index
|
||||||
|
enum {
|
||||||
|
STRID_LANGID = 0,
|
||||||
|
STRID_MANUFACTURER,
|
||||||
|
STRID_PRODUCT,
|
||||||
|
STRID_SERIAL,
|
||||||
|
};
|
||||||
|
|
||||||
// array of pointer to string descriptors
|
// array of pointer to string descriptors
|
||||||
char const* string_desc_arr [] =
|
char const *string_desc_arr[] =
|
||||||
{
|
{
|
||||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||||
"TinyUSB", // 1: Manufacturer
|
"TinyUSB", // 1: Manufacturer
|
||||||
"TinyUSB Device", // 2: Product
|
"TinyUSB Device", // 2: Product
|
||||||
"123456", // 3: Serials, should use chip ID
|
NULL, // 3: Serials will use unique ID if possible
|
||||||
"TinyUSB USBTMC", // 4: USBTMC
|
"TinyUSB USBTMC", // 4: USBTMC
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint16_t _desc_str[32];
|
static uint16_t _desc_str[32 + 1];
|
||||||
|
|
||||||
// Invoked when received GET STRING DESCRIPTOR request
|
// Invoked when received GET STRING DESCRIPTOR request
|
||||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||||
{
|
|
||||||
(void) langid;
|
(void) langid;
|
||||||
|
|
||||||
size_t chr_count;
|
size_t chr_count;
|
||||||
|
|
||||||
if ( index == 0)
|
switch ( index ) {
|
||||||
{
|
case STRID_LANGID:
|
||||||
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||||
chr_count = 1;
|
chr_count = 1;
|
||||||
}
|
break;
|
||||||
else
|
|
||||||
{
|
case STRID_SERIAL:
|
||||||
|
chr_count = board_usb_get_serial(_desc_str + 1, 32);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
||||||
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
||||||
|
|
||||||
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
|
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
|
||||||
|
|
||||||
const char* str = string_desc_arr[index];
|
const char *str = string_desc_arr[index];
|
||||||
|
|
||||||
// Cap at max char
|
// Cap at max char
|
||||||
chr_count = (uint8_t) strlen(str);
|
chr_count = strlen(str);
|
||||||
if ( chr_count > 31 ) {
|
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
|
||||||
chr_count = 31;
|
if ( chr_count > max_count ) chr_count = max_count;
|
||||||
}
|
|
||||||
|
|
||||||
// Convert ASCII string into UTF-16
|
// Convert ASCII string into UTF-16
|
||||||
for(uint8_t i=0; i<chr_count; i++)
|
for ( size_t i = 0; i < chr_count; i++ ) {
|
||||||
{
|
_desc_str[1 + i] = str[i];
|
||||||
_desc_str[1+i] = str[i];
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// first byte is length (including header), second byte is string type
|
// first byte is length (including header), second byte is string type
|
||||||
_desc_str[0] = (uint16_t)((((uint16_t)TUSB_DESC_STRING) << 8 ) | (2u*chr_count + 2u));
|
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
|
||||||
|
|
||||||
return _desc_str;
|
return _desc_str;
|
||||||
}
|
}
|
||||||
|
@@ -23,6 +23,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "bsp/board_api.h"
|
||||||
#include "tusb.h"
|
#include "tusb.h"
|
||||||
#include "usb_descriptors.h"
|
#include "usb_descriptors.h"
|
||||||
|
|
||||||
@@ -150,52 +151,64 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
|||||||
// String Descriptors
|
// String Descriptors
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
|
||||||
|
// String Descriptor Index
|
||||||
|
enum {
|
||||||
|
STRID_LANGID = 0,
|
||||||
|
STRID_MANUFACTURER,
|
||||||
|
STRID_PRODUCT,
|
||||||
|
STRID_SERIAL,
|
||||||
|
};
|
||||||
|
|
||||||
// array of pointer to string descriptors
|
// array of pointer to string descriptors
|
||||||
char const* string_desc_arr [] =
|
char const *string_desc_arr[] =
|
||||||
{
|
{
|
||||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||||
"TinyUSB", // 1: Manufacturer
|
"TinyUSB", // 1: Manufacturer
|
||||||
"TinyUSB Device", // 2: Product
|
"TinyUSB Device", // 2: Product
|
||||||
"123456", // 3: Serials, should use chip ID
|
NULL, // 3: Serials will use unique ID if possible
|
||||||
"TinyUSB UVC", // 4: UVC Interface
|
"TinyUSB UVC", // 4: UVC Interface
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint16_t _desc_str[32];
|
static uint16_t _desc_str[32 + 1];
|
||||||
|
|
||||||
// Invoked when received GET STRING DESCRIPTOR request
|
// Invoked when received GET STRING DESCRIPTOR request
|
||||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||||
{
|
|
||||||
(void) langid;
|
(void) langid;
|
||||||
|
size_t chr_count;
|
||||||
|
|
||||||
uint8_t chr_count;
|
switch ( index ) {
|
||||||
|
case STRID_LANGID:
|
||||||
if ( index == 0)
|
|
||||||
{
|
|
||||||
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||||
chr_count = 1;
|
chr_count = 1;
|
||||||
}else
|
break;
|
||||||
{
|
|
||||||
|
case STRID_SERIAL:
|
||||||
|
chr_count = board_usb_get_serial(_desc_str + 1, 32);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
||||||
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
||||||
|
|
||||||
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
|
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
|
||||||
|
|
||||||
const char* str = string_desc_arr[index];
|
const char *str = string_desc_arr[index];
|
||||||
|
|
||||||
// Cap at max char
|
// Cap at max char
|
||||||
chr_count = (uint8_t) strlen(str);
|
chr_count = strlen(str);
|
||||||
if ( chr_count > 31 ) chr_count = 31;
|
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
|
||||||
|
if ( chr_count > max_count ) chr_count = max_count;
|
||||||
|
|
||||||
// Convert ASCII string into UTF-16
|
// Convert ASCII string into UTF-16
|
||||||
for(uint8_t i=0; i<chr_count; i++)
|
for ( size_t i = 0; i < chr_count; i++ ) {
|
||||||
{
|
_desc_str[1 + i] = str[i];
|
||||||
_desc_str[1+i] = str[i];
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// first byte is length (including header), second byte is string type
|
// first byte is length (including header), second byte is string type
|
||||||
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
|
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
|
||||||
|
|
||||||
return _desc_str;
|
return _desc_str;
|
||||||
}
|
}
|
||||||
|
@@ -23,6 +23,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "bsp/board_api.h"
|
||||||
#include "tusb.h"
|
#include "tusb.h"
|
||||||
#include "usb_descriptors.h"
|
#include "usb_descriptors.h"
|
||||||
|
|
||||||
@@ -207,53 +208,65 @@ TU_VERIFY_STATIC(sizeof(desc_ms_os_20) == MS_OS_20_DESC_LEN, "Incorrect size");
|
|||||||
// String Descriptors
|
// String Descriptors
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
|
||||||
|
// String Descriptor Index
|
||||||
|
enum {
|
||||||
|
STRID_LANGID = 0,
|
||||||
|
STRID_MANUFACTURER,
|
||||||
|
STRID_PRODUCT,
|
||||||
|
STRID_SERIAL,
|
||||||
|
};
|
||||||
|
|
||||||
// array of pointer to string descriptors
|
// array of pointer to string descriptors
|
||||||
char const* string_desc_arr [] =
|
char const *string_desc_arr[] =
|
||||||
{
|
{
|
||||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||||
"TinyUSB", // 1: Manufacturer
|
"TinyUSB", // 1: Manufacturer
|
||||||
"TinyUSB Device", // 2: Product
|
"TinyUSB Device", // 2: Product
|
||||||
"123456", // 3: Serials, should use chip ID
|
NULL, // 3: Serials will use unique ID if possible
|
||||||
"TinyUSB CDC", // 4: CDC Interface
|
"TinyUSB CDC", // 4: CDC Interface
|
||||||
"TinyUSB WebUSB" // 5: Vendor Interface
|
"TinyUSB WebUSB" // 5: Vendor Interface
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint16_t _desc_str[32];
|
static uint16_t _desc_str[32 + 1];
|
||||||
|
|
||||||
// Invoked when received GET STRING DESCRIPTOR request
|
// Invoked when received GET STRING DESCRIPTOR request
|
||||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||||
{
|
|
||||||
(void) langid;
|
(void) langid;
|
||||||
|
size_t chr_count;
|
||||||
|
|
||||||
uint8_t chr_count;
|
switch ( index ) {
|
||||||
|
case STRID_LANGID:
|
||||||
if ( index == 0)
|
|
||||||
{
|
|
||||||
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||||
chr_count = 1;
|
chr_count = 1;
|
||||||
}else
|
break;
|
||||||
{
|
|
||||||
|
case STRID_SERIAL:
|
||||||
|
chr_count = board_usb_get_serial(_desc_str + 1, 32);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
||||||
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
||||||
|
|
||||||
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
|
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
|
||||||
|
|
||||||
const char* str = string_desc_arr[index];
|
const char *str = string_desc_arr[index];
|
||||||
|
|
||||||
// Cap at max char
|
// Cap at max char
|
||||||
chr_count = (uint8_t) strlen(str);
|
chr_count = strlen(str);
|
||||||
if ( chr_count > 31 ) chr_count = 31;
|
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
|
||||||
|
if ( chr_count > max_count ) chr_count = max_count;
|
||||||
|
|
||||||
// Convert ASCII string into UTF-16
|
// Convert ASCII string into UTF-16
|
||||||
for(uint8_t i=0; i<chr_count; i++)
|
for ( size_t i = 0; i < chr_count; i++ ) {
|
||||||
{
|
_desc_str[1 + i] = str[i];
|
||||||
_desc_str[1+i] = str[i];
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// first byte is length (including header), second byte is string type
|
// first byte is length (including header), second byte is string type
|
||||||
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
|
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
|
||||||
|
|
||||||
return _desc_str;
|
return _desc_str;
|
||||||
}
|
}
|
||||||
|
@@ -23,6 +23,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "bsp/board_api.h"
|
||||||
#include "tusb.h"
|
#include "tusb.h"
|
||||||
|
|
||||||
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
|
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
|
||||||
@@ -214,52 +215,64 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
|||||||
// String Descriptors
|
// String Descriptors
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
|
||||||
|
// String Descriptor Index
|
||||||
|
enum {
|
||||||
|
STRID_LANGID = 0,
|
||||||
|
STRID_MANUFACTURER,
|
||||||
|
STRID_PRODUCT,
|
||||||
|
STRID_SERIAL,
|
||||||
|
};
|
||||||
|
|
||||||
// array of pointer to string descriptors
|
// array of pointer to string descriptors
|
||||||
char const* string_desc_arr [] =
|
char const *string_desc_arr[] =
|
||||||
{
|
{
|
||||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||||
"TinyUSB", // 1: Manufacturer
|
"TinyUSB", // 1: Manufacturer
|
||||||
"TinyUSB Device", // 2: Product
|
"TinyUSB Device", // 2: Product
|
||||||
"123456789012", // 3: Serials, should use chip ID
|
NULL, // 3: Serials will use unique ID if possible
|
||||||
"TinyUSB CDC", // 4: CDC Interface
|
"TinyUSB CDC", // 4: CDC Interface
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint16_t _desc_str[32];
|
static uint16_t _desc_str[32 + 1];
|
||||||
|
|
||||||
// Invoked when received GET STRING DESCRIPTOR request
|
// Invoked when received GET STRING DESCRIPTOR request
|
||||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||||
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||||
{
|
|
||||||
(void) langid;
|
(void) langid;
|
||||||
|
size_t chr_count;
|
||||||
|
|
||||||
uint8_t chr_count;
|
switch ( index ) {
|
||||||
|
case STRID_LANGID:
|
||||||
if ( index == 0)
|
|
||||||
{
|
|
||||||
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||||
chr_count = 1;
|
chr_count = 1;
|
||||||
}else
|
break;
|
||||||
{
|
|
||||||
|
case STRID_SERIAL:
|
||||||
|
chr_count = board_usb_get_serial(_desc_str + 1, 32);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
||||||
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
||||||
|
|
||||||
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
|
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
|
||||||
|
|
||||||
const char* str = string_desc_arr[index];
|
const char *str = string_desc_arr[index];
|
||||||
|
|
||||||
// Cap at max char
|
// Cap at max char
|
||||||
chr_count = (uint8_t) strlen(str);
|
chr_count = strlen(str);
|
||||||
if ( chr_count > 31 ) chr_count = 31;
|
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
|
||||||
|
if ( chr_count > max_count ) chr_count = max_count;
|
||||||
|
|
||||||
// Convert ASCII string into UTF-16
|
// Convert ASCII string into UTF-16
|
||||||
for(uint8_t i=0; i<chr_count; i++)
|
for ( size_t i = 0; i < chr_count; i++ ) {
|
||||||
{
|
_desc_str[1 + i] = str[i];
|
||||||
_desc_str[1+i] = str[i];
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// first byte is length (including header), second byte is string type
|
// first byte is length (including header), second byte is string type
|
||||||
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
|
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
|
||||||
|
|
||||||
return _desc_str;
|
return _desc_str;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user