Pico_USB_Tests/src_device/usb_descriptor.c

148 lines
4.9 KiB
C

/**
* Copyright (c) 2023 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "usb_descriptor.h"
#include <tusb.h>
#include <bsp/board_api.h>
#include <assert.h>
// array of pointer to string descriptors
const char const *string_desc_arr[] = {
[STR_ID_LANGID] = (const char[]) { 0x09, 0x04 }, // Supported language is English (0x0409), switched because board is little endian
[STR_ID_MANUFACTURER] = "Raspberry Pi", // Manufacturer
[STR_ID_PRODUCT] = "Pico (0)", // Product
[STR_ID_SERIAL] = NULL, // Serials (null so it uses unique ID if available)
[STR_ID_CDC] = "Custom CDC Test", // CDC Interface name
[STR_ID_RPI_RESET] = "Reset" // RPI Reset Interface name
};
// defines a descriptor that will be communicated to the host
tusb_desc_device_t const desc_device = {
.bLength = sizeof(tusb_desc_device_t),
.bDescriptorType = TUSB_DESC_DEVICE,
.bcdUSB = CDC_EXAMPLE_BCD,
.bDeviceClass = TUSB_CLASS_MISC, // CDC is a subclass of misc
.bDeviceSubClass = MISC_SUBCLASS_COMMON, // CDC uses common subclass
.bDeviceProtocol = MISC_PROTOCOL_IAD, // CDC uses IAD
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, // 64 bytes
.idVendor = CDC_EXAMPLE_VID,
.idProduct = CDC_EXAMPLE_PID,
.bcdDevice = 0x0100, // Device release number
.iManufacturer = STR_ID_MANUFACTURER, // Index of manufacturer string
.iProduct = STR_ID_PRODUCT, // Index of product string
.iSerialNumber = STR_ID_SERIAL, // Index of serial number string
.bNumConfigurations = 0x01 // 1 configuration
};
// configure descriptor
uint8_t const desc_configuration[] = {
// config descriptor | how much power in mA, count of interfaces, ...
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x80, 100),
// CDC Data Interface
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, STR_ID_CDC, EPNUM_CDC_0_NOTIF, 8, EPNUM_CDC_0_OUT, EPNUM_CDC_0_IN, CFG_TUD_CDC_EP_BUFSIZE),
// RPI Reset Interface
TUD_RPI_RESET_DESCRIPTOR(ITF_NUM_RPI_RESET, STR_ID_RPI_RESET)
};
// more device descriptor this time the qualifier
tusb_desc_device_qualifier_t const desc_device_qualifier = {
.bLength = sizeof(tusb_desc_device_t),
.bDescriptorType = TUSB_DESC_DEVICE,
.bcdUSB = CDC_EXAMPLE_BCD,
.bDeviceClass = TUSB_CLASS_CDC,
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
.bDeviceProtocol = MISC_PROTOCOL_IAD,
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
.bNumConfigurations = 0x01,
.bReserved = 0x00
};
// buffer to hold the string descriptor during the request | plus 1 for the null terminator
static uint16_t _desc_str[32 + 1];
// called when host requests to get device descriptor
uint8_t const *tud_descriptor_device_cb(void)
{
return (uint8_t const *)&desc_device;
}
// called when host requests to get device qualifier descriptor
uint8_t const* tud_descriptor_device_qualifier_cb(void)
{
return (uint8_t const *)&desc_device_qualifier;
}
// called when host requests to get configuration descriptor
uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
{
// avoid unused parameter warning and keep function signature consistent
(void)index;
return desc_configuration;
}
// called when host request to get string descriptor
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid)
{
// TODO: check lang id
(void)langid;
size_t char_count;
// Determine which string descriptor to return
switch(index)
{
case STR_ID_LANGID:
memcpy(&_desc_str[1], string_desc_arr[STR_ID_LANGID], 2);
char_count = 1;
break;
case STR_ID_SERIAL:
// try to read the serial from the board
char_count = board_usb_get_serial(_desc_str + 1, 32);
break;
default:
// COPYRIGHT NOTE: Based on TinyUSB example
// Windows wants utf16le
// Determine which string descriptor to return
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) {
return NULL;
}
// Copy string descriptor into _desc_str
const char *str = string_desc_arr[index];
char_count = strlen(str);
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
// Cap at max char
if (char_count > max_count) {
char_count = max_count;
}
// Convert ASCII string into UTF-16
for (size_t i = 0; i < char_count; i++) {
_desc_str[1 + i] = str[i];
}
break;
}
// First byte is the length (including header), second byte is string type
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (char_count * 2 + 2));
return _desc_str;
}