added optional unique id

This commit is contained in:
Peter Lawrence 2021-02-15 17:51:48 -06:00
parent 4f08c34b48
commit 7f1af42e27
3 changed files with 47 additions and 7 deletions

18
bsp/default/unique.h Normal file
View File

@ -0,0 +1,18 @@
#include <stdint.h>
#include "tusb.h"
/* in the absence of the board-specific directory providing a unique ID, we provide a canned one */
static uint8_t get_unique_id(uint16_t *desc_str)
{
const char canned[] = "123456";
uint8_t i;
for(i=0; i<TU_ARRAY_SIZE(canned); i++)
{
desc_str[i] = canned[i];
}
return i;
}

View File

@ -0,0 +1,20 @@
#include <stdint.h>
#include "tusb.h"
static uint8_t get_unique_id(uint16_t *desc_str)
{
const uint32_t *idpnt = (uint32_t*)(0x1FFFF7AC); /*DEVICE_ID1*/
uint32_t tmp = 0;
uint8_t chr_count = 0;
for (int digit = 0; digit < 24; digit++)
{
if (0 == (digit & 7)) tmp = *idpnt++;
desc_str[chr_count++] = "0123456789ABCDEF"[tmp & 0xf];
tmp >>= 4;
}
return chr_count;
}

View File

@ -24,6 +24,7 @@
*/
#include "tusb.h"
#include "unique.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.
@ -134,7 +135,6 @@ char const* string_desc_arr [] =
[STRID_LANGID] = (const char[]) { 0x09, 0x04 }, // supported language is English (0x0409)
[STRID_MANUFACTURER] = "TinyUSB", // Manufacturer
[STRID_PRODUCT] = "TinyUSB CMSIS-DAP", // Product
[STRID_SERIAL] = "123456", // Serial
};
static uint16_t _desc_str[32];
@ -144,13 +144,16 @@ uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
{
(void) langid;
uint8_t chr_count;
uint8_t chr_count = 0;
if ( index == 0)
if (STRID_LANGID == index)
{
memcpy(&_desc_str[1], string_desc_arr[0], 2);
memcpy(&_desc_str[1], string_desc_arr[STRID_LANGID], 2);
chr_count = 1;
}else
} else if (STRID_SERIAL == index)
{
chr_count = get_unique_id(_desc_str + 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
@ -160,8 +163,7 @@ uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
const char* str = string_desc_arr[index];
// Cap at max char
chr_count = strlen(str);
if ( chr_count > 31 ) chr_count = 31;
chr_count = TU_MIN(strlen(str), 31);
// Convert ASCII string into UTF-16
for(uint8_t i=0; i<chr_count; i++)