kopia lustrzana https://github.com/skuep/AIOC
Implemented USB serial number derived from STM32 unique ID and added several string descriptors for the audio device
rodzic
76e3a2e48f
commit
a0b3e0c968
|
@ -24,7 +24,7 @@
|
|||
*/
|
||||
#include "tusb.h"
|
||||
#include "usb_descriptors.h"
|
||||
|
||||
#include "stm32f3xx_hal.h"
|
||||
|
||||
/* 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.
|
||||
|
@ -59,9 +59,9 @@ tusb_desc_device_t const desc_device =
|
|||
.idProduct = USB_PID,
|
||||
.bcdDevice = 0x0100,
|
||||
|
||||
.iManufacturer = 0x01,
|
||||
.iProduct = 0x02,
|
||||
.iSerialNumber = 0x03,
|
||||
.iManufacturer = STR_IDX_MANUFACTURER,
|
||||
.iProduct = STR_IDX_PRODUCT,
|
||||
.iSerialNumber = STR_IDX_SERIAL,
|
||||
|
||||
.bNumConfigurations = 0x01
|
||||
};
|
||||
|
@ -82,8 +82,8 @@ uint8_t const desc_fs_configuration[] =
|
|||
{
|
||||
// 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_CDC_DESCRIPTOR(ITF_NUM_CDC_0, 4, EPNUM_CDC_0_NOTIF, 8, EPNUM_CDC_0_OUT, EPNUM_CDC_0_IN, CFG_TUD_CDC_EP_BUFSIZE),
|
||||
TUD_AUDIO_IO_DESCRIPTOR(/*_itfnum*/ ITF_NUM_AUDIO_CONTROL, /*_stridx*/ 0, /*_nBytesPerSample*/ CFG_TUD_AUDIO_FUNC_1_N_BYTES_PER_SAMPLE, /*_nBitsUsedPerSample*/ CFG_TUD_AUDIO_FUNC_1_N_BYTES_PER_SAMPLE*8, /*_epin*/ EPNUM_AUDIO_IN, /*_epinsize*/ CFG_TUD_AUDIO_FUNC_1_EP_IN_SZ_MAX, /*_epout*/ EPNUM_AUDIO_OUT, /*_epoutsize*/ CFG_TUD_AUDIO_FUNC_1_EP_OUT_SZ_MAX, /*_epfb*/ EPNUM_AUDIO_FB)
|
||||
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_0, STR_IDX_CDCITF, EPNUM_CDC_0_NOTIF, 8, EPNUM_CDC_0_OUT, EPNUM_CDC_0_IN, CFG_TUD_CDC_EP_BUFSIZE),
|
||||
TUD_AUDIO_IO_DESCRIPTOR(/*_itfnum*/ ITF_NUM_AUDIO_CONTROL, /*_stridx*/ STR_IDX_AUDIOITF, /*_nBytesPerSample*/ CFG_TUD_AUDIO_FUNC_1_N_BYTES_PER_SAMPLE, /*_nBitsUsedPerSample*/ CFG_TUD_AUDIO_FUNC_1_N_BYTES_PER_SAMPLE*8, /*_epin*/ EPNUM_AUDIO_IN, /*_epinsize*/ CFG_TUD_AUDIO_FUNC_1_EP_IN_SZ_MAX, /*_epout*/ EPNUM_AUDIO_OUT, /*_epoutsize*/ CFG_TUD_AUDIO_FUNC_1_EP_OUT_SZ_MAX, /*_epfb*/ EPNUM_AUDIO_FB)
|
||||
};
|
||||
|
||||
// Invoked when received GET CONFIGURATION DESCRIPTOR
|
||||
|
@ -100,53 +100,119 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
|||
//--------------------------------------------------------------------+
|
||||
// String Descriptors
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
// array of pointer to string descriptors
|
||||
char const* string_desc_arr [] =
|
||||
static uint32_t crc32(const uint8_t * buf_ptr, uint32_t buf_len)
|
||||
{
|
||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||
"TinyUSB", // 1: Manufacturer
|
||||
"TinyUSB Device", // 2: Product
|
||||
"123456", // 3: Serials, should use chip ID
|
||||
"TinyUSB CDC", // 4: CDC Interface
|
||||
};
|
||||
/* Generate a serial number from processor UID using CRC32 */
|
||||
const uint32_t crc32_init = 0xFFFFFFFFUL;
|
||||
const uint32_t crc32_poly = 0xEDB88320UL;
|
||||
uint32_t serial = crc32_init; /* CRC32 Initial value */
|
||||
|
||||
static uint16_t _desc_str[32];
|
||||
while (buf_len-- > 0) {
|
||||
uint8_t byte = *buf_ptr++;
|
||||
serial = serial ^ byte;
|
||||
for (uint8_t j=0; j<8; j++) {
|
||||
uint32_t mask = -(serial & 1);
|
||||
serial = (serial >> 1) ^ (crc32_poly & mask);
|
||||
}
|
||||
}
|
||||
|
||||
return ~serial;
|
||||
}
|
||||
|
||||
static const char * get_serial(void)
|
||||
{
|
||||
static char serial_str[sizeof(uint32_t) * 2 + 1];
|
||||
uint32_t serial_num = crc32((uint8_t *) UID_BASE, 12);
|
||||
|
||||
for (uint8_t i=0; i<(sizeof(uint32_t) * 2); i++) {
|
||||
uint8_t nibble = ((serial_num & 0xF0000000UL) >> 28);
|
||||
serial_str[i] = nibble < 0xA ? nibble + '0' : nibble - 0xA + 'a';
|
||||
serial_num <<= 4;
|
||||
}
|
||||
|
||||
return serial_str;
|
||||
}
|
||||
|
||||
static uint8_t ascii_to_utf16(uint8_t * buffer, uint32_t size, const char * str)
|
||||
{
|
||||
uint8_t len = 0;
|
||||
|
||||
while ( (*str != '\0') && (size > 0) ) {
|
||||
*buffer++ = *str++;
|
||||
*buffer++ = 0x00;
|
||||
size -= 2;
|
||||
len += 2;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
// Invoked when received GET STRING DESCRIPTOR request
|
||||
// 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)
|
||||
uint8_t const * tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
||||
{
|
||||
(void) langid;
|
||||
static uint8_t buffer[64];
|
||||
uint8_t * ptr = &buffer[2];
|
||||
uint8_t len = sizeof(buffer) - 2;
|
||||
|
||||
uint8_t chr_count;
|
||||
switch (index) {
|
||||
case STR_IDX_LANGUAGE:
|
||||
ptr[0] = 0x09; ptr[1] = 0x04;
|
||||
len = 2;
|
||||
break;
|
||||
|
||||
if ( index == 0)
|
||||
{
|
||||
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||
chr_count = 1;
|
||||
}else
|
||||
{
|
||||
// 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
|
||||
case STR_IDX_MANUFACTURER:
|
||||
len = ascii_to_utf16(ptr, len, USB_STRING_MANUFACTURER);
|
||||
break;
|
||||
|
||||
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
|
||||
case STR_IDX_PRODUCT:
|
||||
len = ascii_to_utf16(ptr, len, USB_STRING_PRODUCT);
|
||||
break;
|
||||
|
||||
const char* str = string_desc_arr[index];
|
||||
case STR_IDX_SERIAL:
|
||||
len = ascii_to_utf16(ptr, len, get_serial());
|
||||
break;
|
||||
|
||||
// Cap at max char
|
||||
chr_count = (uint8_t) strlen(str);
|
||||
if ( chr_count > 31 ) chr_count = 31;
|
||||
case STR_IDX_CDCITF:
|
||||
len = ascii_to_utf16(ptr, len, USB_STRING_CDCITF);
|
||||
break;
|
||||
|
||||
// Convert ASCII string into UTF-16
|
||||
for(uint8_t i=0; i<chr_count; i++)
|
||||
{
|
||||
_desc_str[1+i] = str[i];
|
||||
}
|
||||
case STR_IDX_AUDIOITF:
|
||||
len = ascii_to_utf16(ptr, len, USB_STRING_AUDIOITF);
|
||||
break;
|
||||
|
||||
case STR_IDX_AUDIOIN:
|
||||
len = ascii_to_utf16(ptr, len, USB_STRING_AUDIOIN);
|
||||
break;
|
||||
|
||||
case STR_IDX_AUDIOOUT:
|
||||
len = ascii_to_utf16(ptr, len, USB_STRING_AUDIOOUT);
|
||||
break;
|
||||
|
||||
case STR_IDX_AUDIOINVOL:
|
||||
len = ascii_to_utf16(ptr, len, USB_STRING_AUDIOINVOL);
|
||||
break;
|
||||
|
||||
case STR_IDX_AUDIOOUTVOL:
|
||||
len = ascii_to_utf16(ptr, len, USB_STRING_AUDIOOUTVOL);
|
||||
break;
|
||||
|
||||
case STR_IDX_AUDIOINCHAN:
|
||||
len = ascii_to_utf16(ptr, len, USB_STRING_AUDIOINCHAN);
|
||||
break;
|
||||
|
||||
case STR_IDX_AUDIOOUTCHAN:
|
||||
len = ascii_to_utf16(ptr, len, USB_STRING_AUDIOOUTCHAN);
|
||||
break;
|
||||
|
||||
default:
|
||||
TU_ASSERT(0, NULL);
|
||||
break;
|
||||
}
|
||||
|
||||
// first byte is length (including header), second byte is string type
|
||||
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
|
||||
buffer[0] = len + 2;
|
||||
buffer[1] = TUSB_DESC_STRING;
|
||||
|
||||
return _desc_str;
|
||||
return buffer;
|
||||
}
|
||||
|
|
|
@ -11,6 +11,33 @@ enum USB_DESCRIPTORS_ITF {
|
|||
ITF_NUM_TOTAL
|
||||
};
|
||||
|
||||
/* Strings */
|
||||
enum USB_STRING_IDX {
|
||||
STR_IDX_LANGUAGE = 0,
|
||||
STR_IDX_MANUFACTURER,
|
||||
STR_IDX_PRODUCT,
|
||||
STR_IDX_SERIAL,
|
||||
STR_IDX_CDCITF,
|
||||
STR_IDX_AUDIOITF,
|
||||
STR_IDX_AUDIOIN,
|
||||
STR_IDX_AUDIOOUT,
|
||||
STR_IDX_AUDIOINVOL,
|
||||
STR_IDX_AUDIOOUTVOL,
|
||||
STR_IDX_AUDIOINCHAN,
|
||||
STR_IDX_AUDIOOUTCHAN,
|
||||
};
|
||||
|
||||
#define USB_STRING_MANUFACTURER "TinyUSB"
|
||||
#define USB_STRING_PRODUCT "TinyUSB Device"
|
||||
#define USB_STRING_CDCITF "TinyUSB CDC"
|
||||
#define USB_STRING_AUDIOITF "TinyUSB Audio"
|
||||
#define USB_STRING_AUDIOIN "TinyUSB Audio In"
|
||||
#define USB_STRING_AUDIOOUT "TinyUSB Audio Out"
|
||||
#define USB_STRING_AUDIOINVOL "TinyUSB Audio In Volume"
|
||||
#define USB_STRING_AUDIOOUTVOL "TinyUSB Audio Out Volume"
|
||||
#define USB_STRING_AUDIOINCHAN "TinyUSB Audio In Channel"
|
||||
#define USB_STRING_AUDIOOUTCHAN "TinyUSB Audio Out CHannel"
|
||||
|
||||
/* Endpoints */
|
||||
#define EPNUM_AUDIO_IN 0x81
|
||||
#define EPNUM_AUDIO_OUT 0x02
|
||||
|
@ -30,8 +57,6 @@ enum USB_DESCRIPTORS_ITF {
|
|||
#define AUDIO_CTRL_ID_MIC_OUTPUT_STREAM 0x13
|
||||
#define AUDIO_CTRL_ID_MIC_CLOCK 0x18
|
||||
|
||||
|
||||
|
||||
#define AUDIO_NUM_INTERFACES 0x03
|
||||
#define AUDIO_NUM_INCHANNELS 0x01
|
||||
#define AUDIO_NUM_OUTCHANNELS 0x01
|
||||
|
@ -78,15 +103,15 @@ enum USB_DESCRIPTORS_ITF {
|
|||
/* Clock Source Descriptor(4.7.2.1) */ \
|
||||
TUD_AUDIO_DESC_CLK_SRC(AUDIO_CTRL_ID_SPK_CLOCK, AUDIO_CLOCK_SOURCE_ATT_INT_PRO_CLK, (AUDIO_CTRL_RW << AUDIO_CLOCK_SOURCE_CTRL_CLK_FRQ_POS), /*_assocTerm*/ AUDIO_CTRL_ID_SPK_INPUT_STREAM, /*_stridx*/ 0x00), \
|
||||
/* Speaker Terminals */ \
|
||||
TUD_AUDIO_DESC_INPUT_TERM(/*_termid*/ AUDIO_CTRL_ID_SPK_INPUT_STREAM, /*_termtype*/ AUDIO_TERM_TYPE_USB_STREAMING, /*_assocTerm*/ AUDIO_CTRL_ID_MIC_OUTPUT_STREAM, /*_clkid*/ AUDIO_CTRL_ID_SPK_CLOCK, /*_nchannelslogical*/ AUDIO_NUM_OUTCHANNELS, /*_channelcfg*/ AUDIO_CHANNEL_CONFIG_NON_PREDEFINED, /*_idxchannelnames*/ 0x00, /*_ctrl*/ AUDIO_CTRL_R << AUDIO_IN_TERM_CTRL_CONNECTOR_POS, /*_stridx*/ 0x00), \
|
||||
TUD_AUDIO_DESC_FEATURE_UNIT_ONE_CHANNEL(/*_unitid*/ AUDIO_CTRL_ID_SPK_FUNIT, /*_srcid*/ AUDIO_CTRL_ID_SPK_INPUT_STREAM, /*_ctrlch0master*/ AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_MUTE_POS | AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_VOLUME_POS, /*_ctrlch1*/ AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_MUTE_POS | AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_VOLUME_POS, /*_stridx*/ 0x00),\
|
||||
TUD_AUDIO_DESC_OUTPUT_TERM(AUDIO_CTRL_ID_SPK_OUTPUT, AUDIO_TERM_TYPE_OUT_GENERIC_SPEAKER, AUDIO_CTRL_ID_SPK_INPUT_STREAM, AUDIO_CTRL_ID_SPK_FUNIT, AUDIO_CTRL_ID_SPK_CLOCK, /*_ctrl*/ 0x0000, /*_stridx*/ 0x00), \
|
||||
TUD_AUDIO_DESC_INPUT_TERM(/*_termid*/ AUDIO_CTRL_ID_SPK_INPUT_STREAM, /*_termtype*/ AUDIO_TERM_TYPE_USB_STREAMING, /*_assocTerm*/ AUDIO_CTRL_ID_MIC_OUTPUT_STREAM, /*_clkid*/ AUDIO_CTRL_ID_SPK_CLOCK, /*_nchannelslogical*/ AUDIO_NUM_OUTCHANNELS, /*_channelcfg*/ AUDIO_CHANNEL_CONFIG_NON_PREDEFINED, /*_idxchannelnames*/ STR_IDX_AUDIOOUTCHAN, /*_ctrl*/ AUDIO_CTRL_R << AUDIO_IN_TERM_CTRL_CONNECTOR_POS, /*_stridx*/ STR_IDX_AUDIOOUT), \
|
||||
TUD_AUDIO_DESC_FEATURE_UNIT_ONE_CHANNEL(/*_unitid*/ AUDIO_CTRL_ID_SPK_FUNIT, /*_srcid*/ AUDIO_CTRL_ID_SPK_INPUT_STREAM, /*_ctrlch0master*/ AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_MUTE_POS | AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_VOLUME_POS, /*_ctrlch1*/ AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_MUTE_POS | AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_VOLUME_POS, /*_stridx*/ STR_IDX_AUDIOOUTVOL),\
|
||||
TUD_AUDIO_DESC_OUTPUT_TERM(AUDIO_CTRL_ID_SPK_OUTPUT, AUDIO_TERM_TYPE_OUT_GENERIC_SPEAKER, AUDIO_CTRL_ID_SPK_INPUT_STREAM, AUDIO_CTRL_ID_SPK_FUNIT, AUDIO_CTRL_ID_SPK_CLOCK, /*_ctrl*/ 0x0000, /*_stridx*/ STR_IDX_AUDIOOUT), \
|
||||
/* Clock Source Descriptor(4.7.2.1) */ \
|
||||
TUD_AUDIO_DESC_CLK_SRC(AUDIO_CTRL_ID_MIC_CLOCK, AUDIO_CLOCK_SOURCE_ATT_INT_PRO_CLK, (AUDIO_CTRL_RW << AUDIO_CLOCK_SOURCE_CTRL_CLK_FRQ_POS), /*_assocTerm*/ AUDIO_CTRL_ID_MIC_INPUT, /*_stridx*/ 0x00), \
|
||||
/* Microphone Terminals */ \
|
||||
TUD_AUDIO_DESC_INPUT_TERM(AUDIO_CTRL_ID_MIC_INPUT, AUDIO_TERM_TYPE_IN_GENERIC_MIC, AUDIO_CTRL_ID_MIC_OUTPUT_STREAM, AUDIO_CTRL_ID_MIC_CLOCK, AUDIO_NUM_INCHANNELS, AUDIO_CHANNEL_CONFIG_NON_PREDEFINED, /*_idxchannelnames*/ 0x00, /*_ctrl*/ AUDIO_CTRL_R << AUDIO_IN_TERM_CTRL_CONNECTOR_POS, /*_stridx*/ 0x00), \
|
||||
TUD_AUDIO_DESC_OUTPUT_TERM(AUDIO_CTRL_ID_MIC_OUTPUT_STREAM, AUDIO_TERM_TYPE_USB_STREAMING, AUDIO_CTRL_ID_MIC_INPUT, AUDIO_CTRL_ID_MIC_FUNIT, AUDIO_CTRL_ID_MIC_CLOCK, /*_ctrl*/ 0x0000, /*_stridx*/ 0x00), \
|
||||
TUD_AUDIO_DESC_FEATURE_UNIT_ONE_CHANNEL(/*_unitid*/ AUDIO_CTRL_ID_MIC_FUNIT, /*_srcid*/ AUDIO_CTRL_ID_MIC_INPUT, /*_ctrlch0master*/ AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_MUTE_POS | AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_VOLUME_POS, /*_ctrlch1*/ AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_MUTE_POS | AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_VOLUME_POS, /*_stridx*/ 0x00),\
|
||||
TUD_AUDIO_DESC_INPUT_TERM(AUDIO_CTRL_ID_MIC_INPUT, AUDIO_TERM_TYPE_IN_GENERIC_MIC, AUDIO_CTRL_ID_MIC_OUTPUT_STREAM, AUDIO_CTRL_ID_MIC_CLOCK, AUDIO_NUM_INCHANNELS, AUDIO_CHANNEL_CONFIG_NON_PREDEFINED, /*_idxchannelnames*/ STR_IDX_AUDIOINCHAN, /*_ctrl*/ AUDIO_CTRL_R << AUDIO_IN_TERM_CTRL_CONNECTOR_POS, /*_stridx*/ STR_IDX_AUDIOIN), \
|
||||
TUD_AUDIO_DESC_OUTPUT_TERM(AUDIO_CTRL_ID_MIC_OUTPUT_STREAM, AUDIO_TERM_TYPE_USB_STREAMING, AUDIO_CTRL_ID_MIC_INPUT, AUDIO_CTRL_ID_MIC_FUNIT, AUDIO_CTRL_ID_MIC_CLOCK, /*_ctrl*/ 0x0000, /*_stridx*/ STR_IDX_AUDIOINVOL), \
|
||||
TUD_AUDIO_DESC_FEATURE_UNIT_ONE_CHANNEL(/*_unitid*/ AUDIO_CTRL_ID_MIC_FUNIT, /*_srcid*/ AUDIO_CTRL_ID_MIC_INPUT, /*_ctrlch0master*/ AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_MUTE_POS | AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_VOLUME_POS, /*_ctrlch1*/ AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_MUTE_POS | AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_VOLUME_POS, /*_stridx*/ STR_IDX_AUDIOIN),\
|
||||
/* Speaker Interface */ \
|
||||
/* Interface 1, Alternate 0 - default alternate setting with 0 bandwidth */ \
|
||||
TUD_AUDIO_DESC_STD_AS_INT(/*_itfnum*/ (uint8_t)((_itfnum) + 1), /*_altset*/ 0x00, /*_nEPs*/ 0x00, /*_stridx*/ 0x00), \
|
||||
|
@ -118,5 +143,4 @@ enum USB_DESCRIPTORS_ITF {
|
|||
/* Class-Specific AS Isochronous Audio Data Endpoint Descriptor(4.10.1.2) */ \
|
||||
TUD_AUDIO_DESC_CS_AS_ISO_EP(AUDIO_CS_AS_ISO_DATA_EP_ATT_NON_MAX_PACKETS_OK, AUDIO_CTRL_NONE, AUDIO_CS_AS_ISO_DATA_EP_LOCK_DELAY_UNIT_UNDEFINED, 0x0000)
|
||||
|
||||
|
||||
#endif /* USB_DESCRIPTORS_H_ */
|
||||
|
|
Ładowanie…
Reference in New Issue