1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 05:12:57 +01:00

Merge pull request #18835 from gschorcht/pkg/tinyusb_descriptors

pkg/tinyusb: add common USB descriptors implementation
This commit is contained in:
benpicco 2022-11-09 10:42:15 +01:00 committed by GitHub
commit e38e0b937c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 1047 additions and 439 deletions

View File

@ -19,6 +19,8 @@ menuconfig PACKAGE_TINYUSB
bool "TinyUSB stack package"
depends on HAS_ARCH_32BIT
depends on HAS_TINYUSB_DEVICE || HAS_TINYUSB_HOST
select MODULE_FMT
select MODULE_LUID
select MODULE_PERIPH_USBDEV_CLK
select MODULE_SEMA
select MODULE_TINYUSB_COMMON
@ -113,6 +115,7 @@ config MODULE_TINYUSB_HW
config MODULE_TINYUSB_DEVICE
bool "Device Stack"
depends on HAS_TINYUSB_DEVICE
default y
help
Select to enable tinyUSB device stack
@ -156,8 +159,7 @@ menu "Device Classes"
bool "Bluetooth Host Controller Interface (BTH HCI)"
depends on MODULE_TINYUSB_DEVICE
config MODULE_TINYUSB_CLASS_CDC
bool "Communication Device Class (CDC)"
rsource "Kconfig.cdc"
config MODULE_TINYUSB_CLASS_DFU
bool "Device Firmware Update (DFU) Runtime"
@ -167,11 +169,8 @@ menu "Device Classes"
bool "Device Firmware Update (DFU)"
depends on MODULE_TINYUSB_DEVICE
config MODULE_TINYUSB_CLASS_HID
bool "Human Interface Device (HID)"
config MODULE_TINYUSB_CLASS_MSC
bool "Mass Storage Class (MSC)"
rsource "Kconfig.hid"
rsource "Kconfig.msc"
config MODULE_TINYUSB_CLASS_MIDI
bool "Musical Instrument Digital Interface (MIDI)"
@ -198,4 +197,32 @@ menu "Device Classes"
endmenu
if MODULE_TINYUSB_DEVICE
config TUSBD_EP0_SIZE
int "Device control endpoint (EP0) size [byte]"
default 64
config TUSBD_FS_EP_SIZE
int "Device endpoint size in Full-Speed mode [byte]"
default 64
config TUSBD_HS_EP_SIZE
int "Device endpoint Size in High-Speed mode [byte]"
default 512
config TUSBD_USE_CUSTOM_DESC
bool "Custom device descriptors"
depends on MODULE_TINYUSB_DEVICE
help
Enable this option to define custom descriptors for the selected
device classes. Otherwise, generic descriptors will be generated
according to the selected device classes.
At the moment, the generation of generic descriptors is only supported
for all combinations of up to two CDC interfaces, up to two generic
In/Out HID interfaces, up to one MSC interface and up to one vendor
interface. In all other cases, custom descriptors must be implemented
and handled.
endif # MODULE_TINYUSB_DEVICE
endif # PACKAGE_TINYUSB

32
pkg/tinyusb/Kconfig.cdc Normal file
View File

@ -0,0 +1,32 @@
# Copyright (c) 2022 Gunar Schorcht
#
# This file is subject to the terms and conditions of the GNU Lesser
# General Public License v2.1. See the file LICENSE in the top level
# directory for more details.
#
menuconfig MODULE_TINYUSB_CLASS_CDC
bool "Communication Device Class (CDC)"
if MODULE_TINYUSB_CLASS_CDC && MODULE_TINYUSB_DEVICE
config TUSBD_CDC_NUMOF
int "Number of CDC interfaces"
default 1
range 1 2
config TUSBD_CDC_NOTIF_EP_SIZE
int "CDC notification endpoint size [byte]"
default 8
config TUSBD_CDC_0_STRING
string "CDC0 descriptor string"
depends on TUSBD_CDC_NUMOF > 0
default "TinyUSB CDC0"
config TUSBD_CDC_1_STRING
string "CDC1 descriptor string"
depends on TUSBD_CDC_NUMOF > 1
default "TinyUSB CDC1"
endif

42
pkg/tinyusb/Kconfig.hid Normal file
View File

@ -0,0 +1,42 @@
# Copyright (c) 2022 Gunar Schorcht
#
# This file is subject to the terms and conditions of the GNU Lesser
# General Public License v2.1. See the file LICENSE in the top level
# directory for more details.
#
menuconfig MODULE_TINYUSB_CLASS_HID
bool "Human Interface Device (HID)"
if MODULE_TINYUSB_CLASS_HID && MODULE_TINYUSB_DEVICE
config TUSBD_HID_NUMOF
int "Number of HID interfaces"
default 1
range 1 2
config TUSBD_HID_EP_SIZE
int "HID endpoint size [byte]"
default 64
config TUSBD_HID_0_POLL_INTERVALL
int "HID0 polling interval [ms]"
depends on TUSBD_HID_NUMOF > 0
default 10
config TUSBD_HID_0_STRING
string "HID0 descriptor string"
depends on TUSBD_HID_NUMOF > 0
default "TinyUSB HID0 (Generic In/Out)"
config TUSBD_HID_1_POLL_INTERVALL
int "HID1 polling interval [ms]"
depends on TUSBD_HID_NUMOF > 1
default 10
config TUSBD_HID_1_STRING
string "HID1 descriptor string"
depends on TUSBD_HID_NUMOF > 1
default "TinyUSB HID1 (Generic In/Out)"
endif

26
pkg/tinyusb/Kconfig.msc Normal file
View File

@ -0,0 +1,26 @@
# Copyright (c) 2022 Gunar Schorcht
#
# This file is subject to the terms and conditions of the GNU Lesser
# General Public License v2.1. See the file LICENSE in the top level
# directory for more details.
#
menuconfig MODULE_TINYUSB_CLASS_MSC
bool "Mass Storage Class (MSC)"
if MODULE_TINYUSB_CLASS_MSC && MODULE_TINYUSB_DEVICE
config TUSBD_MSC_NUMOF
int
default 1
config TUSBD_MSC_EP_SIZE
int "MSC endpoint size [byte]"
default 512
config TUSBD_MSC_STRING
string "MSC descriptor string"
depends on TUSBD_MSC_NUMOF > 0
default "TinyUSB MSC"
endif

View File

@ -81,6 +81,8 @@ else ifeq (stm32,$(CPU))
endif
# other module dependencies
USEMODULE += fmt
USEMODULE += luid
USEMODULE += periph_usbdev_clk
USEMODULE += sema
USEMODULE += ztimer_msec

View File

@ -39,6 +39,174 @@
#if !DOXYGEN
#ifndef CONFIG_TUSBD_AUDIO_NUMOF
#if MODULE_TINYUSB_DEVICE && MODULE_TINYUSB_CLASS_AUDIO
#define CONFIG_TUSBD_AUDIO_NUMOF 1
#else
#define CONFIG_TUSBD_AUDIO_NUMOF 0
#endif
#endif
#ifndef CONFIG_TUSBD_BTH_NUMOF
#if MODULE_TINYUSB_DEVICE && MODULE_TINYUSB_CLASS_BTH
#define CONFIG_TUSBD_BTH_NUMOF 1
#else
#define CONFIG_TUSBD_BTH_NUMOF 0
#endif
#endif
#ifndef CONFIG_TUSBD_CDC_NUMOF
#if MODULE_TINYUSB_DEVICE && MODULE_TINYUSB_CLASS_CDC
#define CONFIG_TUSBD_CDC_NUMOF 1
#else
#define CONFIG_TUSBD_CDC_NUMOF 0
#endif
#endif
#ifndef CONFIG_TUSBD_DFU_NUMOF
#if MODULE_TINYUSB_DEVICE && MODULE_TINYUSB_CLASS_DFU
#define CONFIG_TUSBD_DFU_NUMOF 1
#else
#define CONFIG_TUSBD_DFU_NUMOF 0
#endif
#endif
#ifndef CONFIG_TUSBD_DFU_RT_NUMOF
#if MODULE_TINYUSB_DEVICE && MODULE_TINYUSB_CLASS_DFU_RT
#define CONFIG_TUSBD_DFU_RT_NUMOF 1
#else
#define CONFIG_TUSBD_DFU_RT_NUMOF 0
#endif
#endif
#ifndef CONFIG_TUSBD_ECM_NUMOF
#if MODULE_TINYUSB_DEVICE && MODULE_TINYUSB_CLASS_ECM
#define CONFIG_TUSBD_ECM_NUMOF 1
#else
#define CONFIG_TUSBD_ECM_NUMOF 0
#endif
#endif
#ifndef CONFIG_TUSBD_HID_NUMOF
#if MODULE_TINYUSB_DEVICE && MODULE_TINYUSB_CLASS_HID
#define CONFIG_TUSBD_HID_NUMOF 1
#else
#define CONFIG_TUSBD_HID_NUMOF 0
#endif
#endif
#ifndef CONFIG_TUSBD_MIDI_NUMOF
#if MODULE_TINYUSB_DEVICE && MODULE_TINYUSB_CLASS_MIDI
#define CONFIG_TUSBD_MIDI_NUMOF 1
#else
#define CONFIG_TUSBD_MIDI_NUMOF 0
#endif
#endif
#ifndef CONFIG_TUSBD_MSC_NUMOF
#if MODULE_TINYUSB_DEVICE && MODULE_TINYUSB_CLASS_MSC
#define CONFIG_TUSBD_MSC_NUMOF 1
#else
#define CONFIG_TUSBD_MSC_NUMOF 0
#endif
#endif
#ifndef CONFIG_TUSBD_NCM_NUMOF
#if MODULE_TINYUSB_DEVICE && MODULE_TINYUSB_CLASS_NCM
#define CONFIG_TUSBD_NCM_NUMOF 1
#else
#define CONFIG_TUSBD_NCM_NUMOF 0
#endif
#endif
#ifndef CONFIG_TUSBD_USBTMC_NUMOF
#if MODULE_TINYUSB_DEVICE && MODULE_TINYUSB_CLASS_USBTMC
#define CONFIG_TUSBD_USBTMC_NUMOF 1
#else
#define CONFIG_TUSBD_USBTMC_NUMOF 0
#endif
#endif
#ifndef CONFIG_TUSBD_VENDOR_NUMOF
#if MODULE_TINYUSB_DEVICE && MODULE_TINYUSB_CLASS_VENDOR
#define CONFIG_TUSBD_VENDOR_NUMOF 1
#else
#define CONFIG_TUSBD_VENDOR_NUMOF 0
#endif
#endif
#ifndef CONFIG_TUSBD_VIDEO_NUMOF
#if MODULE_TINYUSB_DEVICE && MODULE_TINYUSB_CLASS_VIDEO
#define CONFIG_TUSBD_VIDEO_NUMOF 1
#else
#define CONFIG_TUSBD_VIDEO_NUMOF 0
#endif
#endif
#ifndef CONFIG_TUSBH_CDC_NUMOF
#if MODULE_TINYUSB_HOST && MODULE_TINYUSB_CLASS_CDC
#define CONFIG_TUSBH_CDC_NUMOF 1
#else
#define CONFIG_TUSBH_CDC_NUMOF 0
#endif
#endif
#ifndef CONFIG_TUSBH_HID_NUMOF
#if MODULE_TINYUSB_HOST && MODULE_TINYUSB_CLASS_HID
#define CONFIG_TUSBH_HID_NUMOF 1
#else
#define CONFIG_TUSBH_HID_NUMOF 0
#endif
#endif
#ifndef CONFIG_TUSBH_HUB_NUMOF
#if MODULE_TINYUSB_HOST && MODULE_TINYUSB_CLASS_HUB
#define CONFIG_TUSBH_HUB_NUMOF 1
#else
#define CONFIG_TUSBH_HUB_NUMOF 0
#endif
#endif
#ifndef CONFIG_TUSBH_MSC_NUMOF
#if MODULE_TINYUSB_HOST && MODULE_TINYUSB_CLASS_MSC
#define CONFIG_TUSBH_MSC_NUMOF 1
#else
#define CONFIG_TUSBH_MSC_NUMOF 0
#endif
#endif
#ifndef CONFIG_TUSBH_VENDOR_NUMOF
#if MODULE_TINYUSB_HOST && MODULE_TINYUSB_CLASS_VENDOR
#define CONFIG_TUSBH_VENDOR_NUMOF 1
#else
#define CONFIG_TUSBH_VENDOR_NUMOF 0
#endif
#endif
#ifndef CONFIG_TUSBD_EP0_SIZE
#define CONFIG_TUSBD_EP0_SIZE 64
#endif
#ifndef CONFIG_TUSBD_MSC_EP_SIZE
#define CONFIG_TUSBD_MSC_EP_SIZE 512
#endif
#ifndef CONFIG_TUSBD_HID_EP_SIZE
#define CONFIG_TUSBD_HID_EP_SIZE 64
#endif
#ifndef CONFIG_TUSBH_ENUM_SIZE
#define CONFIG_TUSBH_ENUM_SIZE 256
#endif
#ifndef CONFIG_TUSBH_HID_EPIN_SIZE
#define CONFIG_TUSBH_HID_EPIN_SIZE 64
#endif
#ifndef CONFIG_TUSBH_HID_EPOUT_SIZE
#define CONFIG_TUSBH_HID_EPOUT_SIZE 64
#endif
/**
* @name Common tinyUSB configurations
* @{
@ -81,112 +249,60 @@
#define CFG_TUD_ENABLED MODULE_TINYUSB_DEVICE
#ifndef CFG_TUD_ENDPOINT0_SIZE
#define CFG_TUD_ENDPOINT0_SIZE 64
#define CFG_TUD_ENDPOINT0_SIZE CONFIG_TUSBD_EP0_SIZE
#endif
#ifndef CFG_TUD_AUDIO
#if defined(MODULE_TINYUSB_DEVICE) && defined(MODULE_TINYUSB_CLASS_AUDIO)
#define CFG_TUD_AUDIO 1
#else
#define CFG_TUD_AUDIO 0
#endif
#define CFG_TUD_AUDIO CONFIG_TUSBD_AUDIO_NUMOF
#endif
#ifndef CFG_TUD_BTH
#if defined(MODULE_TINYUSB_DEVICE) && defined(MODULE_TINYUSB_CLASS_BTH)
#define CFG_TUD_BTH 1
#else
#define CFG_TUD_BTH 0
#define CFG_TUD_BTH CONFIG_TUSBD_BTH_NUMOF
#endif
#endif /* CFG_TUD_BTH */
#ifndef CFG_TUD_CDC
#if defined(MODULE_TINYUSB_DEVICE) && defined(MODULE_TINYUSB_CLASS_CDC)
#define CFG_TUD_CDC 1
#else
#define CFG_TUD_CDC 0
#define CFG_TUD_CDC CONFIG_TUSBD_CDC_NUMOF
#endif
#endif /* CFG_TUD_CDC */
#ifndef CFG_TUD_DFU
#if defined(MODULE_TINYUSB_DEVICE) && defined(MODULE_TINYUSB_CLASS_DFU)
#define CFG_TUD_DFU 1
#else
#define CFG_TUD_DFU 0
#define CFG_TUD_DFU CONFIG_TUSBD_DFU_NUMOF
#endif
#endif /* CFG_TUD_DFU */
#ifndef CFG_TUD_DFU_RUNTIME
#if defined(MODULE_TINYUSB_DEVICE) && defined(MODULE_TINYUSB_CLASS_DFU_RUNTIME)
#define CFG_TUD_DFU_RUNTIME 1
#else
#define CFG_TUD_DFU_RUNTIME 0
#define CFG_TUD_DFU_RUNTIME CONFIG_TUSBD_DFU_RT_NUMOF
#endif
#endif /* CFG_TUD_DFU */
#ifndef CFG_TUD_HID
#if defined(MODULE_TINYUSB_DEVICE) && defined(MODULE_TINYUSB_CLASS_HID)
#define CFG_TUD_HID 1
#else
#define CFG_TUD_HID 0
#define CFG_TUD_HID CONFIG_TUSBD_HID_NUMOF
#endif
#endif /* CFG_TUD_HID */
#ifndef CFG_TUD_MIDI
#if defined(MODULE_TINYUSB_DEVICE) && defined(MODULE_TINYUSB_CLASS_MIDI)
#define CFG_TUD_MIDI 1
#else
#define CFG_TUD_MIDI 0
#define CFG_TUD_MIDI CONFIG_TUSBD_MIDI_NUMOF
#endif
#endif /* CFG_TUD_MIDI */
#ifndef CFG_TUD_MSC
#if defined(MODULE_TINYUSB_DEVICE) && defined(MODULE_TINYUSB_CLASS_MSC)
#define CFG_TUD_MSC 1
#else
#define CFG_TUD_MSC 0
#define CFG_TUD_MSC CONFIG_TUSBD_MSC_NUMOF
#endif
#endif /* CFG_TUD_MSC */
#ifndef CFG_TUD_ECM_RNDIS
#if defined(MODULE_TINYUSB_DEVICE) && defined(MODULE_TINYUSB_CLASS_NET_ECM_RNDIS)
#define CFG_TUD_ECM_RNDIS 1
#else
#define CFG_TUD_ECM_RNDIS 0
#define CFG_TUD_ECM_RNDIS CONFIG_TUSBD_ECM_NUMOF
#endif
#endif /* CFG_TUD_ECM_RNDIS */
#ifndef CFG_TUD_NCM
#if defined(MODULE_TINYUSB_DEVICE) && defined(MODULE_TINYUSB_CLASS_NET_NCM)
#define CFG_TUD_NCM 1
#else
#define CFG_TUD_NCM 0
#define CFG_TUD_NCM CONFIG_TUSBD_NCM_NUMOF
#endif
#endif /* CFG_TUD_NCM */
#ifndef CFG_TUD_USBMTC
#if defined(MODULE_TINYUSB_DEVICE) && defined(MODULE_TINYUSB_CLASS_USBMTC)
#define CFG_TUD_USBMTC 1
#else
#define CFG_TUD_USBMTC 0
#ifndef CFG_TUD_USBTMC
#define CFG_TUD_USBTMC CONFIG_TUSBD_USBTMC_NUMOF
#endif
#endif /* CFG_TUD_USBMTC */
#ifndef CFG_TUD_VENDOR
#if defined(MODULE_TINYUSB_DEVICE) && defined(MODULE_TINYUSB_CLASS_VENDOR)
#define CFG_TUD_VENDOR 1
#else
#define CFG_TUD_VENDOR 0
#define CFG_TUD_VENDOR CONFIG_TUSBD_VENDOR_NUMOF
#endif
#endif /* CFG_TUD_VENDOR */
#ifndef CFG_TUD_VIDEO
#if defined(MODULE_TINYUSB_DEVICE) && defined(MODULE_TINYUSB_CLASS_VIDEO)
#define CFG_TUD_VIDEO 1
#else
#define CFG_TUD_VIDEO 0
#define CFG_TUD_VIDEO CONFIG_TUSBD_VIDEO_NUMOF
#endif
#endif /* CFG_TUD_VIDEO */
/** @} */
@ -201,7 +317,7 @@
#endif
#ifndef CFG_TUH_ENUMERATION_BUFSIZE
#define CFG_TUH_ENUMERATION_BUFSIZE 256
#define CFG_TUH_ENUMERATION_BUFSIZE CONFIG_TUSBH_ENUM_SIZE
#endif
/** Hub typically has 4 ports */
@ -210,36 +326,24 @@
#endif
#ifndef CFG_TUH_CDC
#if defined(MODULE_TINYUSB_HOST) && defined(MODULE_TINYUSB_CLASS_CDC)
#define CFG_TUH_CDC 1
#else
#define CFG_TUH_CDC 0
#define CFG_TUH_CDC CONFIG_TUSBH_CDC_NUMOF
#endif
#endif /* CFG_TUH_CDC */
#ifndef CFG_TUH_HID
#if defined(MODULE_TINYUSB_HOST) && defined(MODULE_TINYUSB_CLASS_HID)
#define CFG_TUH_HID 1
#else
#define CFG_TUH_HID 0
#define CFG_TUH_HID CONFIG_TUSBH_HID_NUMOF
#endif
#ifndef CFG_TUH_HUB
#define CFG_TUH_HUB CONFIG_TUSBH_HUB_NUMOF
#endif
#endif /* CFG_TUH_HID */
#ifndef CFG_TUH_MSC
#if defined(MODULE_TINYUSB_HOST) && defined(MODULE_TINYUSB_CLASS_MSC)
#define CFG_TUH_MSC 1
#else
#define CFG_TUH_MSC 0
#define CFG_TUH_MSC CONFIG_TUSBH_MSC_NUMOF
#endif
#endif /* CFG_TUH_MSC */
#ifndef CFG_TUD_VENDOR
#if defined(MODULE_TINYUSB_HOST) && defined(MODULE_TINYUSB_CLASS_VENDOR)
#define CFG_TUH_VENDOR 1
#else
#define CFG_TUH_VENDOR 0
#define CFG_TUH_VENDOR CONFIG_TUSBH_VENDOR_NUMOF
#endif
#endif /* CFG_TUD_VENDOR */
/** @} */
@ -268,6 +372,15 @@
#endif
/** @} */
/**
* @name Typical required HID device class configurations
* @{
*/
#ifndef CFG_TUD_HID_EP_BUFSIZE
#define CFG_TUD_HID_EP_BUFSIZE CONFIG_TUSBD_HID_EP_SIZE
#endif
/** @} */
/**
* @name Typical required MIDI device class configurations
* @{
@ -286,9 +399,23 @@
* @{
*/
#ifndef CFG_TUD_MSC_EP_BUFSIZE
#define CFG_TUD_MSC_EP_BUFSIZE 512
#define CFG_TUD_MSC_EP_BUFSIZE CONFIG_TUSBD_MSC_EP_SIZE
#endif
/** @} */
/**
* @name Typical required HID host class configurations
* @{
*/
#ifndef CFG_TUH_HID_EPIN_BUFSIZE
#define CFG_TUH_HID_EPIN_BUFSIZE CONFIG_TUSBH_HID_EPIN_SIZE
#endif
#ifndef CFG_TUH_HID_EPOUT_BUFSIZE
#define CFG_TUH_HID_EPOUT_BUFSIZE CONFIG_TUSBH_HID_EPOUT_SIZE
#endif
/** @} */
#ifdef __cplusplus
extern "C" {
#endif

View File

@ -0,0 +1,145 @@
/*
* Copyright (C) 2022 Gunar Schorcht
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup pkg_tinyusb
* @{
*
* @file
* @brief TinyUSB commonn descriptor implementation
*
* @author Gunar Schorcht <gunar@schorcht.net>
*/
#ifndef TINYUSB_DESCRIPTORS_H
#define TINYUSB_DESCRIPTORS_H
#include "tusb_config.h" /* defined by the application */
#include "tinyusb.h"
#if !DOXYGEN
#ifdef __cplusplus
extern "C" {
#endif
/* don't compile this part if CONFIG_TUSBD_USE_CUSTOM_DESC is set */
#if !defined(CONFIG_TUSBD_USE_CUSTOM_DESC)
#if !defined(HAVE_TUSBD_ITF_TYPE)
enum {
#if CONFIG_TUSBD_CDC_NUMOF > 0
TUSBD_ITF_CDC_0, /* CDC0 Notification interface */
TUSBD_ITF_CDC_0_DATA, /**< CDC0 Data interface */
#endif
#if CONFIG_TUSBD_CDC_NUMOF > 1
TUSBD_ITF_CDC_1, /**< CDC1 Notification interface */
TUSBD_ITF_CDC_1_DATA, /**< CDC1 Data interface */
#endif
#if CONFIG_TUSBD_HID_NUMOF > 0
TUSBD_ITF_HID_0, /**< HID0 interface */
#endif
#if CONFIG_TUSBD_HID_NUMOF > 1
TUSBD_ITF_HID_1, /**< HID1 interface */
#endif
#if CONFIG_TUSBD_MSC_NUMOF
TUSBD_ITF_MSC, /**< MSC interface */
#endif
#if CONFIG_TUSBD_VENDOR_NUMOF
TUSBD_ITF_VENDOR, /**< Vendor interface */
#endif
TUSBD_ITF_NUMOF, /**< Number of used interfaces */
};
#endif /* !defined(HAVE_TUSBD_ITF_TYPE) */
#if !defined(HAVE_TUSBD_EP_TYPE)
enum {
#if CONFIG_TUSBD_CDC_NUMOF > 0
TUSBD_EP_CDC_0_NOTIF = TUSBD_ITF_CDC_0 + 0x81, /**< CDC0 Nototification EP IN */
TUSBD_EP_CDC_0_OUT = TUSBD_ITF_CDC_0_DATA + 0x01, /**< CDC0 Data EP OUT */
TUSBD_EP_CDC_0_IN = TUSBD_ITF_CDC_0_DATA + 0x81, /**< CDC0 Data EP IN */
#endif
#if CONFIG_TUSBD_CDC_NUMOF > 1
TUSBD_EP_CDC_1_NOTIF = TUSBD_ITF_CDC_1 + 0x81, /**< CDC1 Nototification EP IN */
TUSBD_EP_CDC_1_OUT = TUSBD_ITF_CDC_1_DATA + 0x01, /**< CDC1 Data EP OUT */
TUSBD_EP_CDC_1_IN = TUSBD_ITF_CDC_1_DATA + 0x81, /**< CDC1 Data EP OUT */
#endif
#if CONFIG_TUSBD_HID_NUMOF > 0
TUSBD_EP_HID_0_OUT = TUSBD_ITF_HID_0 + 0x01, /**< HID0 EP OUT */
TUSBD_EP_HID_0_IN = TUSBD_ITF_HID_0 + 0x81, /**< HID0 EP IN */
#endif
#if CONFIG_TUSBD_HID_NUMOF > 1
TUSBD_EP_HID_1_OUT = TUSBD_ITF_HID_1 + 0x01, /**< HID1 EP OUT */
TUSBD_EP_HID_1_IN = TUSBD_ITF_HID_1 + 0x81, /**< HID1 EP IN */
#endif
#if CONFIG_TUSBD_MSC_NUMOF
TUSBD_EP_MSC_OUT = TUSBD_ITF_MSC + 0x01, /**< MSC EP OUT */
TUSBD_EP_MSC_IN = TUSBD_ITF_MSC + 0x81, /**< MSC EP IN */
#endif
#if CONFIG_TUSBD_VENDOR_NUMOF
TUSBD_EP_VENDOR_OUT = TUSBD_ITF_VENDOR + 0x01, /**< Vendor EP OUT */
TUSBD_EP_VENDOR_IN = TUSBD_ITF_VENDOR + 0x81, /**< Vendor EP IN */
#endif
TUSBD_EP_NUMOF,
};
#endif /* !defined(HAVE_TUSBD_EP_TYPE) */
#if !defined(HAVE_TUSBD_STR_IDX_TYPE)
enum {
TUSBD_STR_IDX_LANGUAGE = 0,
TUSBD_STR_IDX_MANUFACTURER,
TUSBD_STR_IDX_PRODUCT,
TUSBD_STR_IDX_SERIAL,
#if CONFIG_TUSBD_CDC_NUMOF > 0
TUSBD_STR_IDX_CDC_0,
#endif
#if CONFIG_TUSBD_CDC_NUMOF > 1
TUSBD_STR_IDX_CDC_1,
#endif
#if CONFIG_TUSBD_HID_NUMOF > 0
TUSBD_STR_IDX_HID_0,
#endif
#if CONFIG_TUSBD_HID_NUMOF > 1
TUSBD_STR_IDX_HID_1,
#endif
#if CONFIG_TUSBD_MSC_NUMOF
TUSBD_STR_IDX_MSC,
#endif
#if CONFIG_TUSBD_VENDOR_NUMOF
TUSBD_STR_IDX_VENDOR,
#endif
TUSBD_STR_IDX_NUMOF,
};
#endif /* !defined(HAVE_TUSBD_STR_IDX_TYPE) */
#if !defined(TUSBD_DESC_TOTAL_LEN)
#define TUSBD_DESC_CDC_0_LEN ((CONFIG_TUSBD_CDC_NUMOF > 0) ? TUD_CDC_DESC_LEN : 0)
#define TUSBD_DESC_CDC_1_LEN ((CONFIG_TUSBD_CDC_NUMOF > 1) ? TUD_CDC_DESC_LEN : 0)
#define TUSBD_DESC_HID_0_LEN ((CONFIG_TUSBD_HID_NUMOF > 0) ? TUD_HID_INOUT_DESC_LEN : 0)
#define TUSBD_DESC_HID_1_LEN ((CONFIG_TUSBD_HID_NUMOF > 1) ? TUD_HID_INOUT_DESC_LEN : 0)
#define TUSBD_DESC_MSC_LEN ((CONFIG_TUSBD_MSC_NUMOF > 0) ? TUD_MSC_DESC_LEN : 0)
#define TUSBD_DESC_VENDOR_LEN ((CONFIG_TUSBD_VENDOR_NUMOF > 0) ? TUD_VENDOR_DESC_LEN : 0)
#define TUSBD_DESC_TOTAL_LEN (TUD_CONFIG_DESC_LEN + \
TUSBD_DESC_CDC_0_LEN + \
TUSBD_DESC_CDC_1_LEN + \
TUSBD_DESC_HID_0_LEN + \
TUSBD_DESC_HID_1_LEN + \
TUSBD_DESC_MSC_LEN + \
TUSBD_DESC_VENDOR_LEN)
#endif /* !defined(TUSBD_DESC_TOTAL_LEN) */
#endif /* !defined(CONFIG_TUSBD_USE_CUSTOM_DESC) */
#ifdef __cplusplus
}
#endif
#endif /* !DOXYGEN */
#endif /* TINYUSB_DESCRIPTORS_H */
/** @} */

View File

@ -0,0 +1,548 @@
/*
* Copyright (C) 2019 Ha Thach (tinyusb.org)
* 2022 Gunar Schorcht
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "fmt.h"
#include "luid.h"
#include "tusb.h"
#include "usb.h"
#include "tinyusb_descriptors.h"
/* don't compile this part if CONFIG_TUSBD_USE_CUSTOM_DESC is set */
#if !defined(CONFIG_TUSBD_USE_CUSTOM_DESC)
#if (MODULE_TINYUSB_CLASS_AUDIO || \
MODULE_TINYUSB_CLASS_BTH || \
MODULE_TINYUSB_CLASS_DFU || \
MODULE_TINYUSB_CLASS_DFU_RUNTIME || \
MODULE_TINYUSB_CLASS_MIDI || \
MODULE_TINYUSB_CLASS_NET_ECM_RNDIS || \
MODULE_TINYUSB_CLASS_NET_NCM || \
MODULE_TINYUSB_CLASS_USBTMC || \
MODULE_TINYUSB_CLASS_VIDEO || \
(CONFIG_TUSBD_CDC_NUMOF > 2) || \
(CONFIG_TUSBD_HID_NUMOF > 2) || \
(CONFIG_TUSBD_MSC_NUMOF > 1))
#error Using generic descriptors is not possible for the selected combination \
of device class interfaces. Custom descriptors have to be implemented.
#endif
/*
* --------------------------------------------------------------------+
* Device Descriptors
* --------------------------------------------------------------------+
*/
__attribute__((weak))
tusb_desc_device_t const tusb_desc_device = {
.bLength = sizeof(tusb_desc_device_t),
.bDescriptorType = TUSB_DESC_DEVICE,
.bcdUSB = CONFIG_USB_PRODUCT_BCDVERSION,
#if CONFIG_TUSBD_CDC_NUMOF
/* Use Interface Association Descriptor (IAD) for CDC
* As required by USB Specs IAD's subclass must be common class (2)
* and protocol must be IAD (1) */
.bDeviceClass = TUSB_CLASS_MISC,
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
.bDeviceProtocol = MISC_PROTOCOL_IAD,
#else
.bDeviceClass = 0x00,
.bDeviceSubClass = 0x00,
.bDeviceProtocol = 0x00,
#endif
.bMaxPacketSize0 = CONFIG_TUSBD_EP0_SIZE,
.idVendor = CONFIG_USB_VID,
.idProduct = CONFIG_USB_PID,
.bcdDevice = 0x100,
.iManufacturer = TUSBD_STR_IDX_MANUFACTURER,
.iProduct = TUSBD_STR_IDX_PRODUCT,
.iSerialNumber = TUSBD_STR_IDX_SERIAL,
.bNumConfigurations = 0x01
};
/*
* The function is invoked when GET DEVICE DESCRIPTOR is received.
* It returns a pointer to the device descriptor.
*/
__attribute__((weak))
uint8_t const *tud_descriptor_device_cb(void)
{
return (uint8_t const *)&tusb_desc_device;
}
/*
* --------------------------------------------------------------------+
* HID Report Descriptor
* --------------------------------------------------------------------+
*/
#if CONFIG_TUSBD_HID_NUMOF > 0
__attribute__((weak))
uint8_t const tusb_desc_hid_0_report[] =
{
TUD_HID_REPORT_DESC_GENERIC_INOUT(CONFIG_TUSBD_HID_EP_SIZE),
};
#endif
#if CONFIG_TUSBD_HID_NUMOF > 1
__attribute__((weak))
uint8_t const tusb_desc_hid_1_report[] =
{
TUD_HID_REPORT_DESC_GENERIC_INOUT(CONFIG_TUSBD_HID_EP_SIZE),
};
#endif
#if CONFIG_TUSBD_HID_NUMOF
/* The function is invoked when GET HID REPORT DESCRIPTOR is received.
* It returns a pointer to the HID report descriptor whose contents
* must exist long enough for transfer to complete. */
__attribute__((weak))
uint8_t const *tud_hid_descriptor_report_cb(uint8_t itf)
{
#if CONFIG_TUSBD_HID_NUMOF > 0
if (itf == 0)
{
return tusb_desc_hid_0_report;
}
#endif
#if CONFIG_TUSBD_HID_NUMOF > 1
else if (itf == 1)
{
return tusb_desc_hid_1_report;
}
#endif
return NULL;
}
/* The function is invoked when GET_REPORT control request is received.
* It fills the report buffer content and returns its length. Returning
* zero will causes the stack to send a STALL request.
*
* @note: The function is only a dummy function and therefore defined as a weak
* symbol that can be overwritten by real functions if needed by the
* application. */
__attribute__((weak))
uint16_t tud_hid_get_report_cb(uint8_t itf, uint8_t report_id,
hid_report_type_t report_type,
uint8_t* buffer, uint16_t reqlen)
{
(void) itf;
(void) report_id;
(void) report_type;
(void) buffer;
(void) reqlen;
return 0;
}
/* The function is invoked when SET_REPORT control request is received or
* data are received on OUT endpoint (Report ID = 0, Type = 0).
*
* @note: The function is only a dummy function and therefore defined as a weak
* symbol that can be overwritten by real functions if needed by the
* application. */
__attribute__((weak))
void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id,
hid_report_type_t report_type,
uint8_t const* buffer, uint16_t bufsize)
{
(void) itf;
(void) report_id;
(void) report_type;
(void) buffer;
(void) bufsize;
}
#endif /* CONFIG_TUSBD_HID_NUMOF */
/*
*--------------------------------------------------------------------+
* Configuration Descriptors
*--------------------------------------------------------------------+
*/
#ifndef CONFIG_TUSBD_FS_EP_SIZE
#define CONFIG_TUSBD_FS_EP_SIZE 64
#endif
#ifndef CONFIG_TUSBD_HS_EP_SIZE
#define CONFIG_TUSBD_HS_EP_SIZE 512
#endif
#ifndef CONFIG_TUSBD_CDC_NOTIF_EP_SIZE
#define CONFIG_TUSBD_CDC_NOTIF_EP_SIZE 8
#endif
#ifndef CONFIG_TUSBD_HID_0_POLL_INTERVALL
#define CONFIG_TUSBD_HID_0_POLL_INTERVALL 10
#endif
#ifndef CONFIG_TUSBD_HID_1_POLL_INTERVALL
#define CONFIG_TUSBD_HID_1_POLL_INTERVALL 10
#endif
#if CONFIG_USB_SELF_POWERED && CONFIG_USB_REM_WAKEUP
#define DESC_DEV_ATTR (USB_CONF_ATTR_SELF_POWERED || USB_CONF_ATTR_REM_WAKEUP)
#elif CONFIG_USB_SELF_POWERED
#define DESC_DEV_ATTR (USB_CONF_ATTR_SELF_POWERED)
#elif CONFIG_USB_REM_WAKEUP
#define DESC_DEV_ATTR (USB_CONF_ATTR_REM_WAKEUP)
#else
#define DESC_DEV_ATTR (0)
#endif
/* FS configuration */
__attribute__((weak))
uint8_t const tusb_desc_fs_config[] = {
/* Config number, interface count, string index, total length, attribute,
* power in mA */
TUD_CONFIG_DESCRIPTOR(1, TUSBD_ITF_NUMOF, 0, TUSBD_DESC_TOTAL_LEN,
DESC_DEV_ATTR, CONFIG_USB_MAX_POWER),
#if CONFIG_TUSBD_CDC_NUMOF > 0
/* Interface number, string index, EP notification address and size,
* EP Data Out & In, EP size. */
TUD_CDC_DESCRIPTOR(TUSBD_ITF_CDC_0, TUSBD_STR_IDX_CDC_0,
TUSBD_EP_CDC_0_NOTIF, CONFIG_TUSBD_CDC_NOTIF_EP_SIZE,
TUSBD_EP_CDC_0_OUT, TUSBD_EP_CDC_0_IN,
CONFIG_TUSBD_FS_EP_SIZE),
#endif
#if CONFIG_TUSBD_CDC_NUMOF > 1
/* Interface number, string index, EP notification address and size,
* EP Data Out & In, EP size. */
TUD_CDC_DESCRIPTOR(TUSBD_ITF_CDC_1, TUSBD_STR_IDX_CDC_1,
TUSBD_EP_CDC_1_NOTIF, CONFIG_TUSBD_CDC_NOTIF_EP_SIZE,
TUSBD_EP_CDC_1_OUT, TUSBD_EP_CDC_1_IN,
CONFIG_TUSBD_FS_EP_SIZE),
#endif
#if CONFIG_TUSBD_HID_NUMOF > 0
/* Interface number, string index, protocol, report descriptor len,
* EP Out & EP In address, EP size, polling interval */
TUD_HID_INOUT_DESCRIPTOR(TUSBD_ITF_HID_0, TUSBD_STR_IDX_HID_0,
HID_TUSBD_ITF_PROTOCOL_NONE,
sizeof(tusb_desc_hid_0_report),
TUSBD_EP_HID_0_OUT, TUSBD_EP_HID_0_IN,
CONFIG_TUSBD_HID_EP_SIZE,
CONFIG_TUSBD_HID_0_POLL_INTERVALL),
#endif
#if CONFIG_TUSBD_HID_NUMOF > 1
/* Interface number, string index, protocol, report descriptor len,
* EP Out & EP In address, EP size, polling interval */
TUD_HID_INOUT_DESCRIPTOR(TUSBD_ITF_HID_1, TUSBD_STR_IDX_HID_1,
HID_TUSBD_ITF_PROTOCOL_NONE,
sizeof(tusb_desc_hid_1_report),
TUSBD_EP_HID_1_OUT, TUSBD_EP_HID_1_IN,
CONFIG_TUSBD_HID_EP_SIZE,
CONFIG_TUSBD_HID_1_POLL_INTERVALL),
#endif
#if CONFIG_TUSBD_MSC_NUMOF
/* Interface number, string index, EP Out & In address, EP size */
TUD_MSC_DESCRIPTOR(TUSBD_ITF_MSC, TUSBD_STR_IDX_MSC,
TUSBD_EP_MSC_OUT, TUSBD_EP_MSC_IN,
CONFIG_TUSBD_FS_EP_SIZE),
#endif
#if CONFIG_TUSBD_VENDOR_NUMOF
/* Interface number, string index, EP Out & In address, EP size */
TUD_VENDOR_DESCRIPTOR(TUSBD_ITF_VENDOR, TUSBD_STR_IDX_VENDOR,
TUSBD_EP_VENDOR_OUT, TUSBD_EP_VENDOR_IN,
CONFIG_TUSBD_FS_EP_SIZE),
#endif
};
#if TUD_OPT_HIGH_SPEED
/* Per USB specs: high speed capable device must report device_qualifier
* and other_speed_configuration descriptors */
/* HS configuration */
__attribute__((weak))
uint8_t const tusb_desc_hs_config[] = {
/* Config number, interface count, string index, total length, attribute,
* power in mA */
TUD_CONFIG_DESCRIPTOR(1, TUSBD_ITF_NUMOF, 0, TUSBD_DESC_TOTAL_LEN,
DESC_DEV_ATTR, CONFIG_USB_MAX_POWER),
#if CONFIG_TUSBD_CDC_NUMOF > 0
/* Interface number, string index, EP notification address and size,
* EP Data Out & In, EP size. */
TUD_CDC_DESCRIPTOR(TUSBD_ITF_CDC_0, TUSBD_STR_IDX_CDC_0,
TUSBD_EP_CDC_0_NOTIF, CONFIG_TUSBD_CDC_NOTIF_EP_SIZE,
TUSBD_EP_CDC_0_OUT, TUSBD_EP_CDC_0_IN,
CONFIG_TUSBD_HS_EP_SIZE),
#endif
#if CONFIG_TUSBD_CDC_NUMOF > 1
/* Interface number, string index, EP notification address and size,
* EP Data Out & In, EP size. */
TUD_CDC_DESCRIPTOR(TUSBD_ITF_CDC_1, TUSBD_STR_IDX_CDC_1,
TUSBD_EP_CDC_1_NOTIF, CONFIG_TUSBD_CDC_NOTIF_EP_SIZE,
TUSBD_EP_CDC_1_OUT, TUSBD_EP_CDC_1_IN,
CONFIG_TUSBD_HS_EP_SIZE),
#endif
#if CONFIG_TUSBD_HID_NUMOF > 0
/* Interface number, string index, protocol, report descriptor len,
* EP Out & EP In address, EP size, polling interval */
TUD_HID_INOUT_DESCRIPTOR(TUSBD_ITF_HID_0, TUSBD_STR_IDX_HID_0,
HID_TUSBD_ITF_PROTOCOL_NONE,
sizeof(tusb_desc_hid_0_report),
TUSBD_EP_HID_0_OUT, TUSBD_EP_HID_0_IN,
CONFIG_TUSBD_HID_EP_SIZE,
CONFIG_TUSBD_HID_0_POLL_INTERVALL),
#endif
#if CONFIG_TUSBD_HID_NUMOF > 1
/* Interface number, string index, protocol, report descriptor len,
* EP Out & EP In address, EP size, polling interval */
TUD_HID_INOUT_DESCRIPTOR(TUSBD_ITF_HID_1, TUSBD_STR_IDX_HID_1,
HID_TUSBD_ITF_PROTOCOL_NONE,
sizeof(tusb_desc_hid_1_report),
TUSBD_EP_HID_1_OUT, TUSBD_EP_HID_1_IN,
CONFIG_TUSBD_HID_EP_SIZE,
CONFIG_TUSBD_HID_1_POLL_INTERVALL),
#endif
#if CONFIG_TUSBD_MSC_NUMOF
/* Interface number, string index, EP Out & In address, EP size */
TUD_MSC_DESCRIPTOR(TUSBD_ITF_MSC, TUSBD_STR_IDX_MSC,
TUSBD_EP_MSC_OUT, TUSBD_EP_MSC_IN,
CONFIG_TUSBD_HS_EP_SIZE),
#endif
#if CONFIG_TUSBD_VENDOR_NUMOF
/* Interface number, string index, EP Out & In address, EP size */
TUD_VENDOR_DESCRIPTOR(TUSBD_ITF_VENDOR, TUSBD_STR_IDX_VENDOR,
TUSBD_EP_VENDOR_OUT, TUSBD_EP_VENDOR_IN,
CONFIG_TUSBD_HS_EP_SIZE),
#endif
};
/* other speed configuration */
uint8_t tusb_desc_other_speed_config[TUSBD_DESC_TOTAL_LEN];
/* device qualifier is mostly similar to device descriptor since we don't
* change configuration based on speed */
__attribute__((weak))
tusb_desc_device_qualifier_t const tusb_desc_device_qualifier = {
.bLength = sizeof(tusb_desc_device_qualifier_t),
.bDescriptorType = TUSB_DESC_DEVICE_QUALIFIER,
.bcdUSB = CONFIG_USB_PRODUCT_BCDVERSION,
#if CONFIG_TUSBD_CDC_NUMOF
/* Use Interface Association Descriptor (IAD) for CDC
* As required by USB Specs IAD's subclass must be common class (2)
* and protocol must be IAD (1) */
.bDeviceClass = TUSB_CLASS_MISC,
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
.bDeviceProtocol = MISC_PROTOCOL_IAD,
#else
.bDeviceClass = 0x00,
.bDeviceSubClass = 0x00,
.bDeviceProtocol = 0x00,
#endif
.bMaxPacketSize0 = CONFIG_TUSBD_EP0_SIZE,
.bNumConfigurations = 0x01,
.bReserved = 0x00
};
/*
* The function is invoked when GET DEVICE QUALIFIER DESCRIPTOR is received.
* It returns a pointer to the device_qualifier descriptor whose
* contents must exist long enough for transfer to complete. The device
* qualifier descriptor describes information about a high-speed capable
* device that would change if the device were operating at the other speed.
* If the device is not highspeed capable, stall this request.
*/
__attribute__((weak))
uint8_t const *tud_descriptor_device_qualifier_cb(void)
{
return (uint8_t const*)&tusb_desc_device_qualifier;
}
/*
* The function is invoked when GET OTHER SEED CONFIGURATION DESCRIPTOR is
* received. It returns a pointer to the other_speed_configuration descriptor
* whose contents must exist long enough for transfer to complete.
*/
__attribute__((weak))
uint8_t const *tud_descriptor_other_speed_configuration_cb(uint8_t index)
{
(void)index; /* for multiple configurations */
/* If the link speed is HS, return the FS config, and vice versa.
* Note: the descriptor type is OHER_SPEED_CONFIG instead of CONFIG */
memcpy(tusb_desc_other_speed_config,
(tud_speed_get() == TUSB_SPEED_HIGH) ? tusb_desc_fs_config
: tusb_desc_hs_config,
TUSBD_DESC_TOTAL_LEN);
tusb_desc_other_speed_config[1] = TUSB_DESC_OTHER_SPEED_CONFIG;
return tusb_desc_other_speed_config;
}
#endif /* TUD_OPT_HIGH_SPEED */
/*
* The function is invoked when GET CONFIGURATION DESCRIPTOR is received.
* It returns pointer to the configuration descriptor whose
* content must exist long enough for the transfer to complete.
*/
__attribute__((weak))
uint8_t const *tud_descriptor_configuration_cb(uint8_t index)
{
(void)index; /* for multiple configurations */
#if TUD_OPT_HIGH_SPEED
/* Although we are HS, host may be FS. */
return (tud_speed_get() == TUSB_SPEED_HIGH) ? tusb_desc_hs_config
: tusb_desc_fs_config;
#else
return (uint8_t const *)tusb_desc_fs_config;
#endif
}
/*
*--------------------------------------------------------------------+
* String Descriptors
*--------------------------------------------------------------------+
*/
#ifndef CONFIG_TUSBD_CDC_0_STRING
#define CONFIG_TUSBD_CDC_0_STRING "TinyUSB CDC0"
#endif
#ifndef CONFIG_TUSBD_CDC_1_STRING
#define CONFIG_TUSBD_CDC_1_STRING "TinyUSB CDC1"
#endif
#ifndef CONFIG_TUSBD_HID_0_STRING
#define CONFIG_TUSBD_HID_0_STRING "TinyUSB HID0 (Generic In/Out)"
#endif
#ifndef CONFIG_TUSBD_HID_1_STRING
#define CONFIG_TUSBD_HID_1_STRING "TinyUSB HID1 (Generic In/Out)"
#endif
#ifndef CONFIG_TUSBD_MSC_STRING
#define CONFIG_TUSBD_MSC_STRING "TinyUSB MSC"
#endif
#ifndef CONFIG_TUSBD_VENDOR_STRING
#define CONFIG_TUSBD_VENDOR_STRING "TinyUSB Vendor"
#endif
/* array of pointer to string descriptors */
__attribute__((weak))
char const* tusb_string_desc_array[] = {
(const char[2]){ /* 0: supported language is English (0x0409) */
CONFIG_USB_DEFAULT_LANGID & 0xff,
(CONFIG_USB_DEFAULT_LANGID >> 8) & 0xff,
},
CONFIG_USB_MANUF_STR, /* 1: Manufacturer */
CONFIG_USB_PRODUCT_STR, /* 2: Product */
#if CONFIG_USB_SERIAL_STR
CONFIG_USB_SERIAL_STR, /* 3: Serial number as configured */
#else
NULL, /* 3: Serial number generated during runtime */
#endif
#if CONFIG_TUSBD_CDC_NUMOF > 0
CONFIG_TUSBD_CDC_0_STRING, /* CDC Interface 0 */
#endif
#if CONFIG_TUSBD_CDC_NUMOF > 1
CONFIG_TUSBD_CDC_1_STRING, /* CDC Interface 1 */
#endif
#if CONFIG_TUSBD_HID_NUMOF > 0
CONFIG_TUSBD_HID_0_STRING, /* HID Interface 0 */
#endif
#if CONFIG_TUSBD_HID_NUMOF > 1
CONFIG_TUSBD_HID_1_STRING, /* HID Interface 1 */
#endif
#if CONFIG_TUSBD_MSC_NUMOF
CONFIG_TUSBD_MSC_STRING, /* MSC Interface */
#endif
#if CONFIG_TUSBD_VENDOR_NUMOF
CONFIG_TUSBD_VENDOR_STRING, /* Vendor Interface */
#endif
};
static uint16_t _desc_str[32];
/* The function is invoked when GET STRING DESCRIPTOR is received.
* The function returns a pointer to the string descriptor whose contents must
* exist long enough for transfer to complete.
*/
__attribute__((weak))
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
{
(void)langid;
uint8_t chr_count = 0;
if (index == TUSBD_STR_IDX_LANGUAGE) {
memcpy(&_desc_str[1], tusb_string_desc_array[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 */
if (index >= ARRAY_SIZE(tusb_string_desc_array)) {
return NULL;
}
const char* str;
#ifdef CONFIG_USB_SERIAL_STR
/* if serial string is configured, each member in tusb_string_desc_array can be used */
str = tusb_string_desc_array[index];
#else
/* otherwise, a generated string has to be used in case of serial string index */
static char _serial_str[(CONFIG_USB_SERIAL_BYTE_LENGTH << 1) + 1];
static_assert(CONFIG_USB_SERIAL_BYTE_LENGTH <= UINT8_MAX/4,
"USB serial byte length must be at most 63 due to protocol "
"limitations");
if ((index == TUSBD_STR_IDX_SERIAL) && (strlen(_serial_str) == 0)) {
/* generate the serial string if it is not yet generated */
uint8_t luid_buf[CONFIG_USB_SERIAL_BYTE_LENGTH];
luid_get(luid_buf, sizeof(luid_buf));
fmt_bytes_hex(_serial_str, luid_buf, sizeof(luid_buf));
_serial_str[(CONFIG_USB_SERIAL_BYTE_LENGTH << 1)] = 0;
str = _serial_str;
}
else {
str = tusb_string_desc_array[index];
}
#endif
/* cap at max char */
chr_count = (uint8_t)strlen(str);
if (chr_count > 31) {
chr_count = 31;
}
/* Convert ASCII string into UTF-16 */
for (uint8_t i = 0; i < chr_count; i++) {
_desc_str[1+i] = str[i];
}
}
/* 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;
}
#endif /* !defined(CONFIG_TUSBD_USE_CUSTOM_DESC) */

View File

@ -56,8 +56,7 @@
* required by the tinyUSB stack and can be used to override the default
* configuration defined in `tinyusb_config.h`.
* ```c
* #define CFG_TUD_CDC 2
* #define CFG_TUD_CDC_EP_BUFSIZE 128
* #define CONFIG_TUSBD_CDC_NUMOF 2
*
* #include "tinyusb_config.h"
* ```

View File

@ -6,7 +6,7 @@
#
menuconfig KCONFIG_USB
bool "Configure USB"
depends on USEMODULE_USBUS
depends on MODULE_USBUS || PACKAGE_TINYUSB
help
Configure the USB peripheral via Kconfig.

View File

@ -1,340 +0,0 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Ha Thach (tinyusb.org)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include "tusb.h"
#ifdef CONFIG_USB_PID
#define USB_PID CONFIG_USB_PID
#else
/*
* 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.
*
* Auto ProductID layout's Bitmap:
* [MSB] VIDEO | AUDIO | MIDI | HID | MSC | CDC [LSB]
*/
#define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) )
#define USB_PID (0x4000 | _PID_MAP(CDC, 0) \
| _PID_MAP(MSC, 1) \
| _PID_MAP(HID, 2) \
| _PID_MAP(MIDI, 3) \
| _PID_MAP(AUDIO, 4) \
| _PID_MAP(VIDEO, 5) )
#endif
#ifdef CONFIG_USB_VID
#define USB_VID CONFIG_USB_VID
#else
#error USB_VID has to be defined
#endif
#define USB_BCD 0x0200
/*
* --------------------------------------------------------------------+
* Device Descriptors
* --------------------------------------------------------------------+
*/
static tusb_desc_device_t const desc_device = {
.bLength = sizeof(tusb_desc_device_t),
.bDescriptorType = TUSB_DESC_DEVICE,
.bcdUSB = USB_BCD,
/* Use Interface Association Descriptor (IAD) for CDC
* As required by USB Specs IAD's subclass must be common class (2)
* and protocol must be IAD (1) */
.bDeviceClass = TUSB_CLASS_MISC,
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
.bDeviceProtocol = MISC_PROTOCOL_IAD,
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
.idVendor = USB_VID,
.idProduct = USB_PID,
.bcdDevice = 0x0100,
.iManufacturer = 0x01,
.iProduct = 0x02,
.iSerialNumber = 0x03,
.bNumConfigurations = 0x01
};
/*
* Invoked when received GET DEVICE DESCRIPTOR
* Application return pointer to descriptor
*/
uint8_t const *tud_descriptor_device_cb(void)
{
return (uint8_t const *)&desc_device;
}
/*
*--------------------------------------------------------------------+
* Configuration Descriptor
*--------------------------------------------------------------------+
*/
enum {
ITF_NUM_CDC = 0,
ITF_NUM_CDC_DATA,
ITF_NUM_MSC,
ITF_NUM_TOTAL
};
#if CFG_TUSB_MCU == OPT_MCU_LPC175X_6X || \
CFG_TUSB_MCU == OPT_MCU_LPC177X_8X || CFG_TUSB_MCU == OPT_MCU_LPC40XX
/*
* LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number
* 0 control, 1 In, 2 Bulk, 3 Iso, 4 In, 5 Bulk etc ...
*/
#define EPNUM_CDC_NOTIF 0x81
#define EPNUM_CDC_OUT 0x02
#define EPNUM_CDC_IN 0x82
#define EPNUM_MSC_OUT 0x05
#define EPNUM_MSC_IN 0x85
#elif CFG_TUSB_MCU == OPT_MCU_SAMG || CFG_TUSB_MCU == OPT_MCU_SAMX7X
/*
* SAMG & SAME70 don't support a same endpoint number with different
* direction IN and OUT, e.g EP1 OUT & EP1 IN cannot exist together
*/
#define EPNUM_CDC_NOTIF 0x81
#define EPNUM_CDC_OUT 0x02
#define EPNUM_CDC_IN 0x83
#define EPNUM_MSC_OUT 0x04
#define EPNUM_MSC_IN 0x85
#elif CFG_TUSB_MCU == OPT_MCU_CXD56
/*
* CXD56 doesn't support a same endpoint number with different direction IN
* and OUT, e.g EP1 OUT & EP1 IN cannot exist together
* CXD56 USB driver has fixed endpoint type (bulk/interrupt/iso) and
* direction (IN/OUT) by its number
* 0 control (IN/OUT), 1 Bulk (IN), 2 Bulk (OUT), 3 In (IN), 4 Bulk (IN),
* 5 Bulk (OUT), 6 In (IN)
*/
#define EPNUM_CDC_NOTIF 0x83
#define EPNUM_CDC_OUT 0x02
#define EPNUM_CDC_IN 0x81
#define EPNUM_MSC_OUT 0x05
#define EPNUM_MSC_IN 0x84
#elif CFG_TUSB_MCU == OPT_MCU_FT90X || CFG_TUSB_MCU == OPT_MCU_FT93X
/*
* FT9XX doesn't support a same endpoint number with different direction
* IN and OUT, e.g EP1 OUT & EP1 IN cannot exist together
*/
#define EPNUM_CDC_NOTIF 0x81
#define EPNUM_CDC_OUT 0x02
#define EPNUM_CDC_IN 0x83
#define EPNUM_MSC_OUT 0x04
#define EPNUM_MSC_IN 0x85
#else
#define EPNUM_CDC_NOTIF 0x81
#define EPNUM_CDC_OUT 0x02
#define EPNUM_CDC_IN 0x82
#define EPNUM_MSC_OUT 0x03
#define EPNUM_MSC_IN 0x83
#endif
#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_MSC_DESC_LEN)
/* full speed configuration */
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),
/* Interface number, string index, EP notification address and size, EP data
* address (out, in) and size. */
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, 64),
/* Interface number, string index, EP Out & EP In address, EP size */
TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 5, EPNUM_MSC_OUT, EPNUM_MSC_IN, 64),
};
#if TUD_OPT_HIGH_SPEED
/* Per USB specs: high speed capable device must report device_qualifier
* and other_speed_configuration */
/* high speed configuration */
uint8_t const desc_hs_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),
/* Interface number, string index, EP notification address and size, EP data
* address (out, in) and size. */
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, 512),
/* Interface number, string index, EP Out & EP In address, EP size */
TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 5, EPNUM_MSC_OUT, EPNUM_MSC_IN, 512),
};
/* other speed configuration */
uint8_t desc_other_speed_config[CONFIG_TOTAL_LEN];
/* device qualifier is mostly similar to device descriptor since we don't
* change configuration based on speed */
tusb_desc_device_qualifier_t const desc_device_qualifier = {
.bLength = sizeof(tusb_desc_device_qualifier_t),
.bDescriptorType = TUSB_DESC_DEVICE_QUALIFIER,
.bcdUSB = USB_BCD,
.bDeviceClass = TUSB_CLASS_MISC,
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
.bDeviceProtocol = MISC_PROTOCOL_IAD,
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
.bNumConfigurations = 0x01,
.bReserved = 0x00
};
/*
* Invoked when received GET DEVICE QUALIFIER DESCRIPTOR request
* Application return pointer to descriptor, whose contents must exist long
* enough for transfer to complete. Device_qualifier descriptor describes
* information about a high-speed capable device that would change if the
* device were operating at the other speed. If not highspeed capable stall
* this request.
*/
uint8_t const* tud_descriptor_device_qualifier_cb(void)
{
return (uint8_t const*)&desc_device_qualifier;
}
/*
* Invoked when received GET OTHER SEED CONFIGURATION DESCRIPTOR request
* Application return pointer to descriptor, whose contents must exist long
* enough for transfer to complete. Configuration descriptor in the other
* speed e.g if high speed then this is for full speed and vice versa
*/
uint8_t const* tud_descriptor_other_speed_configuration_cb(uint8_t index)
{
(void)index; /* for multiple configurations */
/* if link speed is high return fullspeed config, and vice versa
* Note: the descriptor type is OHER_SPEED_CONFIG instead of CONFIG */
memcpy(desc_other_speed_config,
(tud_speed_get() == TUSB_SPEED_HIGH) ? desc_fs_configuration
: desc_hs_configuration,
CONFIG_TOTAL_LEN);
desc_other_speed_config[1] = TUSB_DESC_OTHER_SPEED_CONFIG;
return desc_other_speed_config;
}
#endif /* highspeed */
/*
* Invoked when received GET CONFIGURATION DESCRIPTOR
* Application return pointer to descriptor
* Descriptor contents must exist long enough for transfer to complete
*/
uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
{
(void)index; /* for multiple configurations */
#if TUD_OPT_HIGH_SPEED
/* Although we are highspeed, host may be fullspeed. */
return (tud_speed_get() == TUSB_SPEED_HIGH) ? desc_hs_configuration
: desc_fs_configuration;
#else
return desc_fs_configuration;
#endif
}
/*
*--------------------------------------------------------------------+
* String Descriptors
*--------------------------------------------------------------------+
*/
/* array of pointer to string descriptors */
char const* string_desc_arr [] = {
(const char[]){ 0x09, 0x04 }, /* 0: is supported language is English (0x0409) */
"RIOT-OS", /* 1: Manufacturer */
"TinyUSB Device", /* 2: Product */
"123456789012", /* 3: Serials, should use chip ID */
"TinyUSB CDC", /* 4: CDC Interface */
"TinyUSB MSC", /* 5: MSC Interface */
};
static uint16_t _desc_str[32];
/*
* 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)
{
(void)langid;
uint8_t chr_count;
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
*/
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 = (uint8_t) strlen(str);
if (chr_count > 31) {
chr_count = 31;
}
/* Convert ASCII string into UTF-16 */
for (uint8_t i=0; i<chr_count; i++) {
_desc_str[1+i] = str[i];
}
}
/* 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;
}