Skip to main content
Visitor II
March 18, 2025
Question

USB : TinyUSB for WebUSB

  • March 18, 2025
  • 2 replies
  • 956 views

Dear community,

i'm trying to use tinysub for WebUSB application.
My device is already see as a HID device....

In copy, my USB_descriptor.c

 

 

#include "tusb.h"
#include "usb_descriptors.h"
#include <string.h>

// Identifiants USB
#define USB_VID 0xCAFE
#define USB_PID 0x4001
#define USB_BCD 0x0100

// UUID WebUSB (nécessaire pour Chrome) en little-endian
#define WEBUSB_UUID \
 { 0x38, 0xB6, 0x08, 0x34, /* Little-endian */ \
 0xA9, 0x09, /* Little-endian */ \
 0xA0, 0x47, /* Little-endian */ \
 0x8B, 0xFD, /* Big-endian */ \
 0xA0, 0x76, 0x88, 0x15, 0xB6, 0x65 } /* Big-endian */

// Descripteur principal du périphérique USB
const tusb_desc_device_t desc_device = {
 .bLength = sizeof(tusb_desc_device_t),
 .bDescriptorType = TUSB_DESC_DEVICE,
 .bcdUSB = 0x0200, // USB 2.0
 .bDeviceClass = TUSB_CLASS_VENDOR_SPECIFIC,
 .bDeviceSubClass = 0x00,
 .bDeviceProtocol = 0x00,
 .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
 .idVendor = USB_VID,
 .idProduct = USB_PID,
 .bcdDevice = USB_BCD,
 .iManufacturer = 1,
 .iProduct = 2,
 .iSerialNumber = 3,
 .bNumConfigurations = 1
};

// Descripteur de capacité WebUSB
const uint8_t webusb_capability_descriptor[] = {
 0x10, // bLength
 0x10, // bDescriptorType (Capability)
 0x05, // bDevCapabilityType (Platform)
 0x00, // bReserved
 WEBUSB_UUID, // UUID spécifique WebUSB
 0x01, // bVendorCode (permet un contrôle spécifique)
 0x01 // iLandingPage (index de la chaîne)
};

// Descripteur BOS (Binary Object Store)
const uint8_t bos_descriptor[] = {
 0x05, // bLength
 0x0F, // bDescriptorType (BOS)
 0x16, 0x00, // wTotalLength (22 octets)
 0x01, // bNumDeviceCaps (1 capacité)
 0x10, // bLength de la capacité WebUSB
 0x10, // bDescriptorType (Device Capability)
 0x05, // bDevCapabilityType (Platform)
 0x00, // bReserved
 WEBUSB_UUID, // UUID WebUSB
 0x01, // bVendorCode (permet un contrôle spécifique)
 0x01 // iLandingPage (index de la chaîne)
};

// Descripteur de configuration avec WebUSB (Vendor Interface)
const uint8_t desc_configuration[] = {
 TUD_CONFIG_DESCRIPTOR(1, 1, 0, TUD_CONFIG_DESC_LEN + TUD_VENDOR_DESC_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
 TUD_VENDOR_DESCRIPTOR(0, 0x81, 64, 0x01, 64)
};

// Fonction callback pour fournir le descripteur principal du périphérique
const uint8_t* tud_descriptor_device_cb(void) {
 return (const uint8_t*) &desc_device;
}

// Fonction callback pour fournir le descripteur de configuration
const uint8_t* tud_descriptor_configuration_cb(uint8_t index) {
 (void) index;
 return desc_configuration;
}

// Fonction callback pour fournir le descripteur BOS
const uint8_t* tud_descriptor_bos_cb(void) {
 return bos_descriptor;
}

// Chaînes de caractères USB
const char* string_desc_arr[] = {
 (const char[]) {0x09, 0x04}, // Langue (US English)
 "MonFabricant",
 "MonProduit WebUSB",
 "123456",
};

// Buffer pour les chaînes de caractères
static uint16_t desc_str[32];

// Fonction callback pour les chaînes de caractères USB
const uint16_t* tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
 (void) langid;

 uint8_t len;

 if (index == 0) {
 memcpy(&desc_str[1], string_desc_arr[0], 2);
 len = 1;
 } else {
 if (index >= sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) return NULL;
 const char* str = string_desc_arr[index];
 len = strlen(str);
 if (len > 31) len = 31;
 for (uint8_t i = 0; i < len; i++) {
 desc_str[i + 1] = str[i];
 }
 }

 desc_str[0] = (TUSB_DESC_STRING << | (2 * len + 2);
 return desc_str;
}

 

the tusb_config.h

#ifndef _TUSB_CONFIG_H_
#define _TUSB_CONFIG_H_

#define CFG_TUSB_MCU OPT_MCU_STM32F1
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
#define CFG_TUSB_OS OPT_OS_NONE

// Taille des buffers USB
#define CFG_TUD_ENDPOINT0_SIZE 64

// Activer WebUSB




#define CFG_TUD_HID 0 // Désactiver HID
#define CFG_TUD_CDC 0 // Désactiver USB CDC
#define CFG_TUD_MSC 0 // Désactiver Mass Storage
#define CFG_TUD_MIDI 0 // Désactiver MIDI
#define CFG_TUD_AUDIO 0 // Désactiver Audio
#define CFG_TUD_VENDOR 1 // Activer Vendor (WebUSB)
#define CFG_TUD_BOS 1 // Activer le support BOS pour WebUSB
#define CFG_TUD_VENDOR_EP_BUFSIZE 64

#endif // _TUSB_CONFIG_H_

 Do you see something wrong ?

    This topic has been closed for replies.

    2 replies

    Technical Moderator
    April 24, 2025

    Hi @JL_Loyd 

    TinyUSB is not supported by STMicroelectronics and unfortunately we don't have an example for WebUSB using STM32Cube firmware.

    Super User
    April 25, 2025

    In your webusb_capability_descriptor and bos_descriptor, the WEBUSB_UUID macro is expanded to something enclosed in { braces }.

    This does not do what you think it does. Pease check the actual data with a debugger.