2025年4月19日土曜日

ESP32-S3をドライバ不要でWinUSBデバイスにしてみた。

 ESP32-S3にはUSB機能がついてるのでTinyUSBを使って独自のUSBデバイスが作れるらしい。デバッグでよく使うUSB CDCで仮想シリアルポートとしてしかUSB機能を使っていなかったが、まずはWinUSBデバイスとしてPCで認識できるかやってみた。

Microsoft OS 1.0 DescriptorsのWCID機能を使ってやればWindows 8以降でUSBドライバも不要ならしいのでまずはWCID機能で自動でドライバがインストールされるかどうかを試してみることに。Microsoft OS 2.0 Descriptorsはちょっと難しそうだったのでとりあえず1.0で…

#include <Arduino.h>
#include "USB.h"
#include "tusb.h"

static constexpr uint16_t MY_USB_VID = 0x303A;
static constexpr uint16_t MY_USB_PID = 0x4004;

static constexpr uint8_t MS_OS_VENDOR_CODE = 0x20;
static constexpr uint8_t MS_OS_STRING_INDEX = 0xEE;


// ============================================================
// Microsoft OS 1.0 String Descriptor
// Index = 0xEE
//
// "MSFT100"
// VendorCode = 0x20
// ============================================================

static const uint16_t ms_os_string[] = {
  0x0312,

  0x004D,  // M
  0x0053,  // S
  0x0046,  // F
  0x0054,  // T
  0x0031,  // 1
  0x0030,  // 0
  0x0030,  // 0

  0x0020  // VendorCode = 0x20
};

static_assert(
  sizeof(ms_os_string) == 18,
  "MS OS String Descriptor must be exactly 18 bytes");


// ============================================================
// Microsoft OS 1.0 Extended Compatible ID Descriptor
//
// CompatibleID = WINUSB
// Interface 0
// ============================================================

static const uint8_t ms_os_compatible_id[] = {

  // dwLength = 40
  0x28, 0x00, 0x00, 0x00,

  // bcdVersion = 1.00
  0x00, 0x01,

  // wIndex = 0x0004
  0x04, 0x00,

  // bCount = 1
  0x01,

  // reserved[7]
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,

  // bFirstInterfaceNumber = 0
  0x00,

  // reserved
  0x00,

  // CompatibleID = "WINUSB"
  'W',
  'I',
  'N',
  'U',
  'S',
  'B',
  0x00,
  0x00,

  // SubCompatibleID
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,

  // reserved[6]
  0x00,
  0x00,
  0x00,
  0x00,
  0x00,
  0x00
};

static_assert(
  sizeof(ms_os_compatible_id) == 40,
  "MS OS Compatible ID Descriptor must be exactly 40 bytes");


// ============================================================
// Microsoft OS 1.0 Vendor Control Request
// ============================================================

extern "C" bool tinyusb_vendor_control_request_cb(
  uint8_t rhport,
  uint8_t stage,
  tusb_control_request_t const *request) {
  if (request == nullptr) {
    return false;
  }

  if (stage != CONTROL_STAGE_SETUP) {
    return true;
  }

  const uint8_t bmRequestType = request->bmRequestType;
  const uint8_t bRequest = request->bRequest;
  const uint16_t wValue = request->wValue;
  const uint16_t wIndex = request->wIndex;
  const uint16_t wLength = request->wLength;

  if (bmRequestType == 0xC0 && bRequest == MS_OS_VENDOR_CODE && wValue == 0x0000 && wIndex == 0x0004) {

    uint16_t responseLength =
      sizeof(ms_os_compatible_id);

    if (wLength < responseLength) {
      responseLength = wLength;
    }

    return tud_control_xfer(
      rhport,
      request,
      (void *)ms_os_compatible_id,
      responseLength);
  }

  return false;
}


// ============================================================
// USB String Descriptor
// ============================================================

extern "C" const uint16_t *tud_descriptor_string_cb(
  uint8_t index,
  uint16_t langid) {
  static uint16_t desc[64];

  // Microsoft OS 1.0
  if (index == MS_OS_STRING_INDEX) {
    return ms_os_string;
  }

  // LANGID
  if (index == 0) {
    desc[0] = 0x0304;
    desc[1] = 0x0409;
    return desc;
  }

  // Manufacturer
  if (index == 1) {

    const char *s = "M5Stack";
    uint8_t n = strlen(s);

    desc[0] =
      (uint16_t)(0x0300 | (2 + n * 2));

    for (uint8_t i = 0; i < n; i++) {
      desc[1 + i] = s[i];
    }

    return desc;
  }

  // Product
  if (index == 2) {

    const char *s = "M5AtomS3 WinUSB";
    uint8_t n = strlen(s);

    desc[0] =
      (uint16_t)(0x0300 | (2 + n * 2));

    for (uint8_t i = 0; i < n; i++) {
      desc[1 + i] = s[i];
    }

    return desc;
  }

  // Serial
  if (index == 3) {

    const char *s = "ATOM-S3-WINUSB";
    uint8_t n = strlen(s);

    desc[0] =
      (uint16_t)(0x0300 | (2 + n * 2));

    for (uint8_t i = 0; i < n; i++) {
      desc[1 + i] = s[i];
    }

    return desc;
  }

  // Interface
  if (index == 4) {

    const char *s = "WinUSB";
    uint8_t n = strlen(s);

    desc[0] =
      (uint16_t)(0x0300 | (2 + n * 2));

    for (uint8_t i = 0; i < n; i++) {
      desc[1 + i] = s[i];
    }

    return desc;
  }

  return nullptr;
}


void setup() {
  USB.VID(MY_USB_VID);
  USB.PID(MY_USB_PID);

  USB.begin();
}


void loop() {
  delay(10);
}

とりあえずM5AtomS3 LiteがあったのでCDC on BootをDisableにして、USB ModeをTinyUSBに設定してこのスケッチを書き込めば動くと思う。

途中色々試してようやくZadigでWCIDが✓になり、デバイスマネージャーで勝手に認識されるように。ミスったデバイスはいちいちデバイスマネージャーで削除してからやらないとうまく行っていても認識されないので、ZadigのWCIDの欄を確認しながら試したほうがいいかも。

CDC on BootをDisableにして、TinyUSBを使うとCDCデバイスが出なくなるので書き換えの際はリセットボタンを緑色のLEDがつくまで長押しして離すとCOMポートが出てくるのでそれを選んで書き込めばOK。

デバイスマネージャーで見てもちゃんとドライバが勝手にインストールされていた。Zadig不要でWinUSBドライバ勝手に入るの便利。管理者権限とかも要らないしね。

USB\MS_COMP_WINUSBがちゃんと追加されてる。

とりあえず認識するようになったのであとはエンドポイントを追加してPythonツールからデータ送受信ができるかどうか…

ちなみにHIDデバイスとかでもPythonツール作ってデータの送受信ができるみたいだけどWinUSBだとバルク転送も使えるのでより高速らしい。
こういったデバイスはZadigでlibusbとかWinUSBのドライバを強制的にインストールしないといけないのが面倒だったんだけど、勝手にドライバがインストールされるのは便利かもしれない。

0 件のコメント:

コメントを投稿