jancumps / pico_scpi_usbtmc_labtool

LabVIEW compatible instrument on a Raspberry Pico
https://github.com/jancumps/pico_scpi_usbtmc_labtool/wiki
MIT License
12 stars 14 forks source link

adapt usbtmc unique_id to TinyUSB API once new Pico C SDK released #33

Closed jancumps closed 10 months ago

jancumps commented 1 year ago

TinyUSB implemented a portable way to use Pico Unique ID as USB serial number. Once release after 1.5 is available, undo our own implement and adapt usb_descriptors.c:

remove: #include "pico/unique_id.h"

struct

// String Descriptor Index
enum {
  STRID_LANGID = 0,
  STRID_MANUFACTURER,
  STRID_PRODUCT,
  STRID_SERIAL,
};

// array of pointer to string descriptors
char const *string_desc_arr[] =
{
  (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
  "TinyUSB",                     // 1: Manufacturer
  "TinyUSB Device",              // 2: Product
  NULL,                          // 3: Serials will use unique ID if possible
  "TinyUSB USBTMC",              // 4: USBTMC
};

implement

uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
  (void) langid;
  size_t chr_count;

  switch ( index ) {
    case STRID_LANGID:
      memcpy(&_desc_str[1], string_desc_arr[0], 2);
      chr_count = 1;
      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.
      // 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
      chr_count = strlen(str);
      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
      for ( size_t i = 0; i < chr_count; i++ ) {
        _desc_str[1 + i] = str[i];
      }
      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));

  return _desc_str;
}

also adapt CMake file: target_link_libraries(tinyusb_bsp INTERFACE pico_unique_id)

jancumps commented 10 months ago

closing as wontfix, because the change is in app specific code and the PST implements it in virtually the same way.