From 30e1c7335d4f88aff81d4c590fdddc9e2f8c4925 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 11 Jul 2019 22:34:43 +0700 Subject: [PATCH 01/36] increase usb bcd from 2.0 to 2.1 for webusb support --- examples/device/cdc_msc_hid/src/usb_descriptors.c | 2 +- examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c | 2 +- examples/device/hid_generic_inout/src/usb_descriptors.c | 2 +- examples/device/midi_test/src/usb_descriptors.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/device/cdc_msc_hid/src/usb_descriptors.c b/examples/device/cdc_msc_hid/src/usb_descriptors.c index 1692151a0..dbbff9a30 100644 --- a/examples/device/cdc_msc_hid/src/usb_descriptors.c +++ b/examples/device/cdc_msc_hid/src/usb_descriptors.c @@ -39,7 +39,7 @@ tusb_desc_device_t const desc_device = { .bLength = sizeof(tusb_desc_device_t), .bDescriptorType = TUSB_DESC_DEVICE, - .bcdUSB = 0x0200, + .bcdUSB = 0x0210, #if CFG_TUD_CDC // Use Interface Association Descriptor (IAD) for CDC diff --git a/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c b/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c index 579e1ebcf..c6464b420 100644 --- a/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c +++ b/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c @@ -39,7 +39,7 @@ tusb_desc_device_t const desc_device = { .bLength = sizeof(tusb_desc_device_t), .bDescriptorType = TUSB_DESC_DEVICE, - .bcdUSB = 0x0200, + .bcdUSB = 0x0210, #if CFG_TUD_CDC // Use Interface Association Descriptor (IAD) for CDC diff --git a/examples/device/hid_generic_inout/src/usb_descriptors.c b/examples/device/hid_generic_inout/src/usb_descriptors.c index ced915935..8e0b4edbc 100644 --- a/examples/device/hid_generic_inout/src/usb_descriptors.c +++ b/examples/device/hid_generic_inout/src/usb_descriptors.c @@ -39,7 +39,7 @@ tusb_desc_device_t const desc_device = { .bLength = sizeof(tusb_desc_device_t), .bDescriptorType = TUSB_DESC_DEVICE, - .bcdUSB = 0x0200, + .bcdUSB = 0x0210, .bDeviceClass = 0x00, .bDeviceSubClass = 0x00, .bDeviceProtocol = 0x00, diff --git a/examples/device/midi_test/src/usb_descriptors.c b/examples/device/midi_test/src/usb_descriptors.c index 192dacfbe..f47d1a0ba 100644 --- a/examples/device/midi_test/src/usb_descriptors.c +++ b/examples/device/midi_test/src/usb_descriptors.c @@ -39,7 +39,7 @@ tusb_desc_device_t const desc_device = { .bLength = sizeof(tusb_desc_device_t), .bDescriptorType = TUSB_DESC_DEVICE, - .bcdUSB = 0x0200, + .bcdUSB = 0x0210, // Use Interface Association Descriptor (IAD) for Audio // As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1) .bDeviceClass = TUSB_CLASS_MISC, From 5ebb606ecd421994465fe98fbf803787c59c7917 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 11 Jul 2019 22:35:17 +0700 Subject: [PATCH 02/36] add BOS descriptor and device capability constant --- src/common/tusb_types.h | 54 ++++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/src/common/tusb_types.h b/src/common/tusb_types.h index fd6e435e6..326c20cda 100644 --- a/src/common/tusb_types.h +++ b/src/common/tusb_types.h @@ -69,7 +69,7 @@ typedef enum }tusb_dir_t; -/// USB Descriptor Types (section 9.4 table 9-5) +/// USB Descriptor Types typedef enum { TUSB_DESC_DEVICE = 0x01, @@ -84,29 +84,35 @@ typedef enum TUSB_DESC_DEBUG = 0x0A, TUSB_DESC_INTERFACE_ASSOCIATION = 0x0B, + TUSB_DESC_BOS = 0x0F, + TUSB_DESC_DEVICE_CAPABILITY = 0x10, + // Class Specific Descriptor TUSB_DESC_CS_DEVICE = 0x21, TUSB_DESC_CS_CONFIGURATION = 0x22, TUSB_DESC_CS_STRING = 0x23, TUSB_DESC_CS_INTERFACE = 0x24, TUSB_DESC_CS_ENDPOINT = 0x25, + + TUSB_DESC_SUPERSPEED_ENDPOINT_COMPANION = 0x30, + TUSB_DESC_SUPERSPEED_ISO_ENDPOINT_COMPANION = 0x31 }tusb_desc_type_t; typedef enum { - TUSB_REQ_GET_STATUS =0 , ///< 0 - TUSB_REQ_CLEAR_FEATURE , ///< 1 - TUSB_REQ_RESERVED , ///< 2 - TUSB_REQ_SET_FEATURE , ///< 3 - TUSB_REQ_RESERVED2 , ///< 4 - TUSB_REQ_SET_ADDRESS , ///< 5 - TUSB_REQ_GET_DESCRIPTOR , ///< 6 - TUSB_REQ_SET_DESCRIPTOR , ///< 7 - TUSB_REQ_GET_CONFIGURATION , ///< 8 - TUSB_REQ_SET_CONFIGURATION , ///< 9 - TUSB_REQ_GET_INTERFACE , ///< 10 - TUSB_REQ_SET_INTERFACE , ///< 11 - TUSB_REQ_SYNCH_FRAME ///< 12 + TUSB_REQ_GET_STATUS = 0 , + TUSB_REQ_CLEAR_FEATURE = 1 , + TUSB_REQ_RESERVED = 2 , + TUSB_REQ_SET_FEATURE = 3 , + TUSB_REQ_RESERVED2 = 4 , + TUSB_REQ_SET_ADDRESS = 5 , + TUSB_REQ_GET_DESCRIPTOR = 6 , + TUSB_REQ_SET_DESCRIPTOR = 7 , + TUSB_REQ_GET_CONFIGURATION = 8 , + TUSB_REQ_SET_CONFIGURATION = 9 , + TUSB_REQ_GET_INTERFACE = 10 , + TUSB_REQ_SET_INTERFACE = 11 , + TUSB_REQ_SYNCH_FRAME = 12 }tusb_request_code_t; typedef enum @@ -168,6 +174,26 @@ typedef enum MISC_PROTOCOL_IAD = 1 }misc_protocol_type_t; +typedef enum +{ + DEVICE_CAPABILITY_WIRELESS_USB = 0x01, + DEVICE_CAPABILITY_USB20_EXTENSION = 0x02, + DEVICE_CAPABILITY_SUPERSPEED_USB = 0x03, + DEVICE_CAPABILITY_CONTAINER_id = 0x04, + DEVICE_CAPABILITY_PLATFORM = 0x05, + DEVICE_CAPABILITY_POWER_DELIVERY = 0x06, + DEVICE_CAPABILITY_BATTERY_INFO = 0x07, + DEVICE_CAPABILITY_PD_CONSUMER_PORT = 0x08, + DEVICE_CAPABILITY_PD_PROVIDER_PORT = 0x09, + DEVICE_CAPABILITY_SUPERSPEED_PLUS = 0x0A, + DEVICE_CAPABILITY_PRECESION_TIME_MEASUREMENT = 0x0B, + DEVICE_CAPABILITY_WIRELESS_USB_EXT = 0x0C, + DEVICE_CAPABILITY_BILLBOARD = 0x0D, + DEVICE_CAPABILITY_AUTHENTICATION = 0x0E, + DEVICE_CAPABILITY_BILLBOARD_EX = 0x0F, + DEVICE_CAPABILITY_CONFIGURATION_SUMMARY = 0x10 +}device_capability_type_t; + enum { TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP = TU_BIT(5), TUSB_DESC_CONFIG_ATT_SELF_POWERED = TU_BIT(6), From 90395f84cb81d8a4341d6a072dfb624cfa307f96 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 11 Jul 2019 22:36:00 +0700 Subject: [PATCH 03/36] more 2.1 --- examples/device/msc_dual_lun/src/usb_descriptors.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/device/msc_dual_lun/src/usb_descriptors.c b/examples/device/msc_dual_lun/src/usb_descriptors.c index 26ebe190c..7d0910711 100644 --- a/examples/device/msc_dual_lun/src/usb_descriptors.c +++ b/examples/device/msc_dual_lun/src/usb_descriptors.c @@ -39,7 +39,7 @@ tusb_desc_device_t const desc_device = { .bLength = sizeof(tusb_desc_device_t), .bDescriptorType = TUSB_DESC_DEVICE, - .bcdUSB = 0x0200, + .bcdUSB = 0x0210, .bDeviceClass = 0x00, .bDeviceSubClass = 0x00, .bDeviceProtocol = 0x00, From ff2a7c79f84f03b41d69406158de85cb9e5d9046 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 11 Jul 2019 22:41:36 +0700 Subject: [PATCH 04/36] revert 2.1 to 2.0, only needed for webusb exmample (BOS needed) --- examples/device/cdc_msc_hid/src/usb_descriptors.c | 2 +- examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c | 2 +- examples/device/hid_generic_inout/src/usb_descriptors.c | 2 +- examples/device/midi_test/src/usb_descriptors.c | 2 +- examples/device/msc_dual_lun/src/usb_descriptors.c | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/device/cdc_msc_hid/src/usb_descriptors.c b/examples/device/cdc_msc_hid/src/usb_descriptors.c index dbbff9a30..1692151a0 100644 --- a/examples/device/cdc_msc_hid/src/usb_descriptors.c +++ b/examples/device/cdc_msc_hid/src/usb_descriptors.c @@ -39,7 +39,7 @@ tusb_desc_device_t const desc_device = { .bLength = sizeof(tusb_desc_device_t), .bDescriptorType = TUSB_DESC_DEVICE, - .bcdUSB = 0x0210, + .bcdUSB = 0x0200, #if CFG_TUD_CDC // Use Interface Association Descriptor (IAD) for CDC diff --git a/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c b/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c index c6464b420..579e1ebcf 100644 --- a/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c +++ b/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c @@ -39,7 +39,7 @@ tusb_desc_device_t const desc_device = { .bLength = sizeof(tusb_desc_device_t), .bDescriptorType = TUSB_DESC_DEVICE, - .bcdUSB = 0x0210, + .bcdUSB = 0x0200, #if CFG_TUD_CDC // Use Interface Association Descriptor (IAD) for CDC diff --git a/examples/device/hid_generic_inout/src/usb_descriptors.c b/examples/device/hid_generic_inout/src/usb_descriptors.c index 8e0b4edbc..ced915935 100644 --- a/examples/device/hid_generic_inout/src/usb_descriptors.c +++ b/examples/device/hid_generic_inout/src/usb_descriptors.c @@ -39,7 +39,7 @@ tusb_desc_device_t const desc_device = { .bLength = sizeof(tusb_desc_device_t), .bDescriptorType = TUSB_DESC_DEVICE, - .bcdUSB = 0x0210, + .bcdUSB = 0x0200, .bDeviceClass = 0x00, .bDeviceSubClass = 0x00, .bDeviceProtocol = 0x00, diff --git a/examples/device/midi_test/src/usb_descriptors.c b/examples/device/midi_test/src/usb_descriptors.c index f47d1a0ba..192dacfbe 100644 --- a/examples/device/midi_test/src/usb_descriptors.c +++ b/examples/device/midi_test/src/usb_descriptors.c @@ -39,7 +39,7 @@ tusb_desc_device_t const desc_device = { .bLength = sizeof(tusb_desc_device_t), .bDescriptorType = TUSB_DESC_DEVICE, - .bcdUSB = 0x0210, + .bcdUSB = 0x0200, // Use Interface Association Descriptor (IAD) for Audio // As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1) .bDeviceClass = TUSB_CLASS_MISC, diff --git a/examples/device/msc_dual_lun/src/usb_descriptors.c b/examples/device/msc_dual_lun/src/usb_descriptors.c index 7d0910711..26ebe190c 100644 --- a/examples/device/msc_dual_lun/src/usb_descriptors.c +++ b/examples/device/msc_dual_lun/src/usb_descriptors.c @@ -39,7 +39,7 @@ tusb_desc_device_t const desc_device = { .bLength = sizeof(tusb_desc_device_t), .bDescriptorType = TUSB_DESC_DEVICE, - .bcdUSB = 0x0210, + .bcdUSB = 0x0200, .bDeviceClass = 0x00, .bDeviceSubClass = 0x00, .bDeviceProtocol = 0x00, From ec8f6a6d909b96777a320454528c41527afa2aac Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 11 Jul 2019 22:55:19 +0700 Subject: [PATCH 05/36] clean up demo config --- examples/device/cdc_msc_hid/src/tusb_config.h | 2 +- examples/device/cdc_msc_hid_freertos/src/tusb_config.h | 2 +- examples/device/hid_generic_inout/src/tusb_config.h | 2 +- examples/device/midi_test/src/main.c | 5 +++-- examples/device/midi_test/src/tusb_config.h | 2 +- examples/device/msc_dual_lun/src/tusb_config.h | 2 +- examples/host/cdc_msc_hid/src/tusb_config.h | 2 +- test/test/support/tusb_config.h | 2 +- 8 files changed, 10 insertions(+), 9 deletions(-) diff --git a/examples/device/cdc_msc_hid/src/tusb_config.h b/examples/device/cdc_msc_hid/src/tusb_config.h index e55dc9d01..31ae2b883 100644 --- a/examples/device/cdc_msc_hid/src/tusb_config.h +++ b/examples/device/cdc_msc_hid/src/tusb_config.h @@ -62,7 +62,7 @@ #endif #ifndef CFG_TUSB_MEM_ALIGN -#define CFG_TUSB_MEM_ALIGN TU_ATTR_ALIGNED(4) +#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4))) #endif //-------------------------------------------------------------------- diff --git a/examples/device/cdc_msc_hid_freertos/src/tusb_config.h b/examples/device/cdc_msc_hid_freertos/src/tusb_config.h index 76ec948e5..2a250ac83 100644 --- a/examples/device/cdc_msc_hid_freertos/src/tusb_config.h +++ b/examples/device/cdc_msc_hid_freertos/src/tusb_config.h @@ -62,7 +62,7 @@ #endif #ifndef CFG_TUSB_MEM_ALIGN -#define CFG_TUSB_MEM_ALIGN TU_ATTR_ALIGNED(4) +#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4))) #endif //-------------------------------------------------------------------- diff --git a/examples/device/hid_generic_inout/src/tusb_config.h b/examples/device/hid_generic_inout/src/tusb_config.h index fb19d5169..d97cd533e 100644 --- a/examples/device/hid_generic_inout/src/tusb_config.h +++ b/examples/device/hid_generic_inout/src/tusb_config.h @@ -62,7 +62,7 @@ #endif #ifndef CFG_TUSB_MEM_ALIGN -#define CFG_TUSB_MEM_ALIGN TU_ATTR_ALIGNED(4) +#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4))) #endif //-------------------------------------------------------------------- diff --git a/examples/device/midi_test/src/main.c b/examples/device/midi_test/src/main.c index 0cb9b522d..03b4554c9 100644 --- a/examples/device/midi_test/src/main.c +++ b/examples/device/midi_test/src/main.c @@ -32,8 +32,9 @@ /* This MIDI example send sequence of note (on/off) repeatedly. To test on PC, you need to install * synth software and midi connection management software. On - * - Linux (Ubuntu) : install qsynth, qjackctl. Then connect TinyUSB output port to FLUID Synth input port - * + * - Linux (Ubuntu): install qsynth, qjackctl. Then connect TinyUSB output port to FLUID Synth input port + * - Windows: install MIDI-OX + * - MacOS: SimpleSynth */ //--------------------------------------------------------------------+ diff --git a/examples/device/midi_test/src/tusb_config.h b/examples/device/midi_test/src/tusb_config.h index 6ea2f43db..a6e3ad710 100644 --- a/examples/device/midi_test/src/tusb_config.h +++ b/examples/device/midi_test/src/tusb_config.h @@ -62,7 +62,7 @@ #endif #ifndef CFG_TUSB_MEM_ALIGN -#define CFG_TUSB_MEM_ALIGN TU_ATTR_ALIGNED(4) +#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4))) #endif //-------------------------------------------------------------------- diff --git a/examples/device/msc_dual_lun/src/tusb_config.h b/examples/device/msc_dual_lun/src/tusb_config.h index db9974ea3..3b87d9f21 100644 --- a/examples/device/msc_dual_lun/src/tusb_config.h +++ b/examples/device/msc_dual_lun/src/tusb_config.h @@ -62,7 +62,7 @@ #endif #ifndef CFG_TUSB_MEM_ALIGN -#define CFG_TUSB_MEM_ALIGN TU_ATTR_ALIGNED(4) +#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4))) #endif //-------------------------------------------------------------------- diff --git a/examples/host/cdc_msc_hid/src/tusb_config.h b/examples/host/cdc_msc_hid/src/tusb_config.h index 9662223b1..1a9c8fa5c 100644 --- a/examples/host/cdc_msc_hid/src/tusb_config.h +++ b/examples/host/cdc_msc_hid/src/tusb_config.h @@ -63,7 +63,7 @@ #endif #ifndef CFG_TUSB_MEM_ALIGN -#define CFG_TUSB_MEM_ALIGN TU_ATTR_ALIGNED(4) +#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4))) #endif //-------------------------------------------------------------------- diff --git a/test/test/support/tusb_config.h b/test/test/support/tusb_config.h index a332481f8..d49576850 100644 --- a/test/test/support/tusb_config.h +++ b/test/test/support/tusb_config.h @@ -63,7 +63,7 @@ #endif #ifndef CFG_TUSB_MEM_ALIGN -#define CFG_TUSB_MEM_ALIGN TU_ATTR_ALIGNED(4) +#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4))) #endif //-------------------------------------------------------------------- From 38b60827e14f4f586729e0e5c9063eeac33f8129 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 11 Jul 2019 23:03:02 +0700 Subject: [PATCH 06/36] clean up --- examples/device/cdc_msc_hid/src/main.c | 4 +--- examples/device/hid_generic_inout/src/main.c | 4 +--- examples/device/midi_test/src/main.c | 5 +---- examples/device/msc_dual_lun/src/main.c | 4 +--- 4 files changed, 4 insertions(+), 13 deletions(-) diff --git a/examples/device/cdc_msc_hid/src/main.c b/examples/device/cdc_msc_hid/src/main.c index 771638ac4..c3295fd50 100644 --- a/examples/device/cdc_msc_hid/src/main.c +++ b/examples/device/cdc_msc_hid/src/main.c @@ -61,9 +61,7 @@ int main(void) while (1) { - // tinyusb device task - tud_task(); - + tud_task(); // tinyusb device task led_blinking_task(); #if CFG_TUD_CDC diff --git a/examples/device/hid_generic_inout/src/main.c b/examples/device/hid_generic_inout/src/main.c index 77d6cf7b5..c1dc43b34 100644 --- a/examples/device/hid_generic_inout/src/main.c +++ b/examples/device/hid_generic_inout/src/main.c @@ -76,9 +76,7 @@ int main(void) while (1) { - // tinyusb device task - tud_task(); - + tud_task(); // tinyusb device task led_blinking_task(); } diff --git a/examples/device/midi_test/src/main.c b/examples/device/midi_test/src/main.c index 03b4554c9..381bcf634 100644 --- a/examples/device/midi_test/src/main.c +++ b/examples/device/midi_test/src/main.c @@ -66,11 +66,8 @@ int main(void) while (1) { - // tinyusb device task - tud_task(); - + tud_task(); // tinyusb device task led_blinking_task(); - midi_task(); } diff --git a/examples/device/msc_dual_lun/src/main.c b/examples/device/msc_dual_lun/src/main.c index 060f20644..0293261ad 100644 --- a/examples/device/msc_dual_lun/src/main.c +++ b/examples/device/msc_dual_lun/src/main.c @@ -58,9 +58,7 @@ int main(void) while (1) { - // tinyusb device task - tud_task(); - + tud_task(); // tinyusb device task led_blinking_task(); } From 42d0ef6fb07ddfa868fe06b5e6d75623bc253807 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 11 Jul 2019 23:08:49 +0700 Subject: [PATCH 07/36] clean up midi example --- examples/device/midi_test/src/usb_descriptors.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/examples/device/midi_test/src/usb_descriptors.c b/examples/device/midi_test/src/usb_descriptors.c index 192dacfbe..21270745a 100644 --- a/examples/device/midi_test/src/usb_descriptors.c +++ b/examples/device/midi_test/src/usb_descriptors.c @@ -40,11 +40,9 @@ tusb_desc_device_t const desc_device = .bLength = sizeof(tusb_desc_device_t), .bDescriptorType = TUSB_DESC_DEVICE, .bcdUSB = 0x0200, - // Use Interface Association Descriptor (IAD) for Audio - // 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, + .bDeviceClass = 0x00, + .bDeviceSubClass = 0x00, + .bDeviceProtocol = 0x00, .bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE, .idVendor = 0xCafe, From 913b52bb359c08c4e3c007dd115ad48c2aada03b Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 11 Jul 2019 23:12:53 +0700 Subject: [PATCH 08/36] start to add webusb example (not work yet) --- changelog.md | 2 + examples/device/webusb/Makefile | 12 ++ examples/device/webusb/src/main.c | 126 ++++++++++++++++ examples/device/webusb/src/tusb_config.h | 85 +++++++++++ examples/device/webusb/src/usb_descriptors.c | 145 +++++++++++++++++++ 5 files changed, 370 insertions(+) create mode 100644 examples/device/webusb/Makefile create mode 100644 examples/device/webusb/src/main.c create mode 100644 examples/device/webusb/src/tusb_config.h create mode 100644 examples/device/webusb/src/usb_descriptors.c diff --git a/changelog.md b/changelog.md index 5e3d27bec..4bf15ceac 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,7 @@ # TinyUSB changelog +## New Release + ## 0.5.0 (Initial Release) - 2019.07.10 First release, device stack works great, host stack works but still need improvement. diff --git a/examples/device/webusb/Makefile b/examples/device/webusb/Makefile new file mode 100644 index 000000000..5a455078e --- /dev/null +++ b/examples/device/webusb/Makefile @@ -0,0 +1,12 @@ +include ../../../tools/top.mk +include ../../make.mk + +INC += \ + src \ + $(TOP)/hw \ + +# Example source +EXAMPLE_SOURCE += $(wildcard src/*.c) +SRC_C += $(addprefix $(CURRENT_PATH)/, $(EXAMPLE_SOURCE)) + +include ../../rules.mk diff --git a/examples/device/webusb/src/main.c b/examples/device/webusb/src/main.c new file mode 100644 index 000000000..6d96eba0b --- /dev/null +++ b/examples/device/webusb/src/main.c @@ -0,0 +1,126 @@ +/* + * 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 +#include +#include + +#include "bsp/board.h" +#include "tusb.h" + +/* This MIDI example send sequence of note (on/off) repeatedly. To test on PC, you need to install + * synth software and midi connection management software. On + * - Linux (Ubuntu) : install qsynth, qjackctl. Then connect TinyUSB output port to FLUID Synth input port + * + */ + +//--------------------------------------------------------------------+ +// MACRO CONSTANT TYPEDEF PROTYPES +//--------------------------------------------------------------------+ + +/* Blink pattern + * - 250 ms : device not mounted + * - 1000 ms : device mounted + * - 2500 ms : device is suspended + */ +enum { + BLINK_NOT_MOUNTED = 250, + BLINK_MOUNTED = 1000, + BLINK_SUSPENDED = 2500, +}; + +static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED; + +void led_blinking_task(void); +void midi_task(void); + +/*------------- MAIN -------------*/ +int main(void) +{ + board_init(); + + tusb_init(); + + while (1) + { + tud_task(); // tinyusb device task + led_blinking_task(); + } + + return 0; +} + +//--------------------------------------------------------------------+ +// Device callbacks +//--------------------------------------------------------------------+ + +// Invoked when device is mounted +void tud_mount_cb(void) +{ + blink_interval_ms = BLINK_MOUNTED; +} + +// Invoked when device is unmounted +void tud_umount_cb(void) +{ + blink_interval_ms = BLINK_NOT_MOUNTED; +} + +// Invoked when usb bus is suspended +// remote_wakeup_en : if host allow us to perform remote wakeup +// Within 7ms, device must draw an average of current less than 2.5 mA from bus +void tud_suspend_cb(bool remote_wakeup_en) +{ + (void) remote_wakeup_en; + blink_interval_ms = BLINK_SUSPENDED; +} + +// Invoked when usb bus is resumed +void tud_resume_cb(void) +{ + blink_interval_ms = BLINK_MOUNTED; +} + +//--------------------------------------------------------------------+ +// WebUSB +//--------------------------------------------------------------------+ + + + +//--------------------------------------------------------------------+ +// BLINKING TASK +//--------------------------------------------------------------------+ +void led_blinking_task(void) +{ + static uint32_t start_ms = 0; + static bool led_state = false; + + // Blink every interval ms + if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time + start_ms += blink_interval_ms; + + board_led_write(led_state); + led_state = 1 - led_state; // toggle +} diff --git a/examples/device/webusb/src/tusb_config.h b/examples/device/webusb/src/tusb_config.h new file mode 100644 index 000000000..37a988482 --- /dev/null +++ b/examples/device/webusb/src/tusb_config.h @@ -0,0 +1,85 @@ +/* + * 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. + * + */ + +#ifndef _TUSB_CONFIG_H_ +#define _TUSB_CONFIG_H_ + +#ifdef __cplusplus + extern "C" { +#endif + +//-------------------------------------------------------------------- +// COMMON CONFIGURATION +//-------------------------------------------------------------------- + +// defined by compiler flags for flexibility +#ifndef CFG_TUSB_MCU + #error CFG_TUSB_MCU must be defined +#endif + +#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX +#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED) +#else +#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE +#endif + +#define CFG_TUSB_OS OPT_OS_NONE + +// CFG_TUSB_DEBUG is defined by compiler in DEBUG build +// #define CFG_TUSB_DEBUG 0 + +/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment. + * Tinyusb use follows macros to declare transferring memory so that they can be put + * into those specific section. + * e.g + * - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") )) + * - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4))) + */ +#ifndef CFG_TUSB_MEM_SECTION +#define CFG_TUSB_MEM_SECTION +#endif + +#ifndef CFG_TUSB_MEM_ALIGN +#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4))) +#endif + +//-------------------------------------------------------------------- +// DEVICE CONFIGURATION +//-------------------------------------------------------------------- + +#define CFG_TUD_ENDOINT0_SIZE 64 + +//------------- CLASS -------------// +#define CFG_TUD_CDC 0 +#define CFG_TUD_MSC 0 +#define CFG_TUD_HID 0 +#define CFG_TUD_MIDI 0 +#define CFG_TUD_CUSTOM_CLASS 0 + +#ifdef __cplusplus + } +#endif + +#endif /* _TUSB_CONFIG_H_ */ diff --git a/examples/device/webusb/src/usb_descriptors.c b/examples/device/webusb/src/usb_descriptors.c new file mode 100644 index 000000000..5149d85c5 --- /dev/null +++ b/examples/device/webusb/src/usb_descriptors.c @@ -0,0 +1,145 @@ +/* + * 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" + +/* 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] 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) ) + +//------------- Device Descriptors -------------// +tusb_desc_device_t const desc_device = +{ + .bLength = sizeof(tusb_desc_device_t), + .bDescriptorType = TUSB_DESC_DEVICE, + .bcdUSB = 0x0210, + .bDeviceClass = 0x00, + .bDeviceSubClass = 0x00, + .bDeviceProtocol = 0x00, + .bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE, + + .idVendor = 0xCafe, + .idProduct = USB_PID, + .bcdDevice = 0x0100, + + .iManufacturer = 0x01, + .iProduct = 0x02, + .iSerialNumber = 0x03, + + .bNumConfigurations = 0x01 +}; + +//------------- Configuration Descriptor -------------// +enum +{ + ITF_NUM_WEBUSB = 0, + ITF_NUM_TOTAL +}; + +enum +{ + CONFIG_TOTAL_LEN = TUD_CONFIG_DESC_LEN + TUD_MIDI_DESC_LEN +}; + +// Use Endpoint 2 instead of 1 due to NXP MCU +// LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number +// 0 control, 1 In, 2 Bulk, 3 Iso, 4 In etc ... +#define EPNUM_MIDI 0x02 + +uint8_t const desc_configuration[] = +{ + // Interface count, string index, total length, attribute, power in mA + TUD_CONFIG_DESCRIPTOR(ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100), + + // Interface number, string index, EP Out & EP In address, EP size +}; + +// 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; +} + +// 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 + return desc_configuration; +} + +//------------- String Descriptors -------------// + +// 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 + "123456", // 3: Serials, should use chip ID +}; + +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) +{ + uint8_t chr_count; + + if ( index == 0) + { + memcpy(&_desc_str[1], string_desc_arr[0], 2); + chr_count = 1; + }else + { + // Convert ASCII string into UTF-16 + + 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); + if ( chr_count > 31 ) chr_count = 31; + + for(uint8_t i=0; i Date: Thu, 11 Jul 2019 23:52:58 +0700 Subject: [PATCH 09/36] clean up build script --- tools/build_all.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tools/build_all.py b/tools/build_all.py index 93bdef4c1..b34edffaf 100644 --- a/tools/build_all.py +++ b/tools/build_all.py @@ -12,7 +12,16 @@ success_count = 0 fail_count = 0 exit_status = 0 -all_device_example = ["cdc_msc_hid", "hid_generic_inout", "midi_test", "msc_dual_lun"] +all_examples = []; +for entry in os.scandir("examples/device"): + if entry.is_dir(): + all_examples.append(entry.name) + +# mynewt has its own example repo +all_examples.remove("cdc_msc_hid_mynewt") + +# TODO update freeRTOS example to work with all boards (only nrf52840 now) +all_examples.remove("cdc_msc_hid_freertos") all_boards = [] for entry in os.scandir("hw/bsp"): @@ -25,7 +34,7 @@ def build_example(example, board): total_time = time.monotonic() -for example in all_device_example: +for example in all_examples: for board in all_boards: start_time = time.monotonic() build_result = build_example(example, board) From e74aa41552798ff7955e68fe39ac58826d4fab6e Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 12 Jul 2019 00:12:14 +0700 Subject: [PATCH 10/36] added tud_descriptor_bos_cb(), add support for BOS get descriptor --- src/common/tusb_types.h | 8 ++++++++ src/device/usbd.c | 18 +++++++++++++++++- src/device/usbd.h | 4 ++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/common/tusb_types.h b/src/common/tusb_types.h index 326c20cda..f7a1fc221 100644 --- a/src/common/tusb_types.h +++ b/src/common/tusb_types.h @@ -253,6 +253,14 @@ typedef struct TU_ATTR_PACKED uint8_t bNumConfigurations ; ///< Number of possible configurations. } tusb_desc_device_t; +typedef struct TU_ATTR_PACKED +{ + uint8_t bLength ; ///< Size of this descriptor in bytes + uint8_t bDescriptorType ; ///< CONFIGURATION Descriptor Type + uint16_t wTotalLength ; ///< Total length of data returned for this descriptor + uint8_t bNumDeviceCaps ; ///< Number of device capability descriptors in the BOS +} tusb_desc_bos_t; + /// USB Standard Configuration Descriptor (section 9.6.1 table 9-10) */ typedef struct TU_ATTR_PACKED { diff --git a/src/device/usbd.c b/src/device/usbd.c index a68953151..fb6d53ba1 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -566,10 +566,26 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const return usbd_control_xfer(rhport, p_request, (void*) tud_descriptor_device_cb(), sizeof(tusb_desc_device_t)); break; + case TUSB_DESC_BOS: + { + // requested by host if USB > 2.0 ( i.e 2.1 or 3.x ) + if (!tud_descriptor_bos_cb) return false; + + tusb_desc_bos_t const* desc_bos = (tusb_desc_bos_t const*) tud_descriptor_bos_cb(); + uint16_t total_len; + memcpy(&total_len, &desc_bos->wTotalLength, 2); // possibly mis-aligned memory + + return usbd_control_xfer(rhport, p_request, (void*) desc_bos, total_len); + } + break; + case TUSB_DESC_CONFIGURATION: { tusb_desc_configuration_t const* desc_config = (tusb_desc_configuration_t const*) tud_descriptor_configuration_cb(desc_index); - return usbd_control_xfer(rhport, p_request, (void*) desc_config, desc_config->wTotalLength); + uint16_t total_len; + memcpy(&total_len, &desc_config->wTotalLength, 2); // possibly mis-aligned memory + + return usbd_control_xfer(rhport, p_request, (void*) desc_config, total_len); } break; diff --git a/src/device/usbd.h b/src/device/usbd.h index 8eee20c0a..9959a99af 100644 --- a/src/device/usbd.h +++ b/src/device/usbd.h @@ -67,6 +67,10 @@ bool tud_remote_wakeup(void); // Application return pointer to descriptor uint8_t const * tud_descriptor_device_cb(void); +// Invoked when received GET BOS DESCRIPTOR request +// Application return pointer to descriptor +TU_ATTR_WEAK uint8_t const * tud_descriptor_bos_cb(void); + // Invoked when received GET CONFIGURATION DESCRIPTOR request // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete uint8_t const * tud_descriptor_configuration_cb(uint8_t index); From 3b6013e78f66551e4516db348f3d0f547731c782 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 12 Jul 2019 14:21:34 +0700 Subject: [PATCH 11/36] clean up --- src/common/tusb_common.h | 5 +++-- src/common/tusb_verify.h | 42 ++++++++++++++++++++-------------------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/common/tusb_common.h b/src/common/tusb_common.h index c77be2a9f..ee77b62f1 100644 --- a/src/common/tusb_common.h +++ b/src/common/tusb_common.h @@ -146,9 +146,10 @@ static inline bool tu_bit_test (uint32_t value, uint8_t n) { return (value & * Nth position is the same as the number of arguments * - ##__VA_ARGS__ is used to deal with 0 paramerter (swallows comma) *------------------------------------------------------------------*/ -#ifndef VA_ARGS_NUM_ +#ifndef TU_ARGS_NUM + +#define TU_ARGS_NUM(...) NARG_(_0, ##__VA_ARGS__,_RSEQ_N()) -#define VA_ARGS_NUM_(...) NARG_(_0, ##__VA_ARGS__,_RSEQ_N()) #define NARG_(...) _GET_NTH_ARG(__VA_ARGS__) #define _GET_NTH_ARG( \ _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \ diff --git a/src/common/tusb_verify.h b/src/common/tusb_verify.h index eecc95ccb..08ddf78ad 100644 --- a/src/common/tusb_verify.h +++ b/src/common/tusb_verify.h @@ -51,8 +51,8 @@ //--------------------------------------------------------------------+ #if CFG_TUSB_DEBUG #include - #define _MESS_ERR(_err) printf("%s: %d: failed, error = %s\n", __func__, __LINE__, tusb_strerr[_err]) - #define _MESS_FAILED() printf("%s: %d: failed\n", __func__, __LINE__) + #define _MESS_ERR(_err) printf("%s: %d: failed, error = %s\n", __func__, __LINE__, tusb_strerr[_err]) + #define _MESS_FAILED() printf("%s: %d: failed\n", __func__, __LINE__) #else #define _MESS_ERR(_err) #define _MESS_FAILED() @@ -61,11 +61,11 @@ // Halt CPU (breakpoint) when hitting error, only apply for Cortex M3, M4, M7 #if defined(__ARM_ARCH_7M__) || defined (__ARM_ARCH_7EM__) -#define TU_BREAKPOINT() \ - do {\ - volatile uint32_t* ARM_CM_DHCSR = ((volatile uint32_t*) 0xE000EDF0UL); /* Cortex M CoreDebug->DHCSR */ \ - if ( (*ARM_CM_DHCSR) & 1UL ) __asm("BKPT #0\n"); /* Only halt mcu if debugger is attached */\ - } while(0) +#define TU_BREAKPOINT() do \ +{ \ + volatile uint32_t* ARM_CM_DHCSR = ((volatile uint32_t*) 0xE000EDF0UL); /* Cortex M CoreDebug->DHCSR */ \ + if ( (*ARM_CM_DHCSR) & 1UL ) __asm("BKPT #0\n"); /* Only halt mcu if debugger is attached */ \ +} while(0) #else #define TU_BREAKPOINT() @@ -80,22 +80,23 @@ #define GET_4TH_ARG(arg1, arg2, arg3, arg4, ...) arg4 /*------------- Generator for TU_VERIFY and TU_VERIFY_HDLR -------------*/ -#define TU_VERIFY_DEFINE(_cond, _handler, _ret) do { if ( !(_cond) ) { _handler; return _ret; } } while(0) +#define TU_VERIFY_DEFINE(_cond, _handler, _ret) do \ +{ \ + if ( !(_cond) ) { _handler; return _ret; } \ +} while(0) /*------------- Generator for TU_VERIFY_ERR and TU_VERIFY_ERR_HDLR -------------*/ -#define TU_VERIFY_ERR_DEF2(_error, _handler) \ - do { \ - uint32_t _err = (uint32_t)(_error); \ - if ( 0 != _err ) { _MESS_ERR(_err); _handler; return _err; }\ - } while(0) - -#define TU_VERIFY_ERR_DEF3(_error, _handler, _ret) \ - do { \ - uint32_t _err = (uint32_t)(_error); \ - if ( 0 != _err ) { _MESS_ERR(_err); _handler; return _ret; }\ - } while(0) - +#define TU_VERIFY_ERR_DEF2(_error, _handler) do \ +{ \ + uint32_t _err = (uint32_t)(_error); \ + if ( 0 != _err ) { _MESS_ERR(_err); _handler; return _err; } \ +} while(0) +#define TU_VERIFY_ERR_DEF3(_error, _handler, _ret) do \ +{ \ + uint32_t _err = (uint32_t)(_error); \ + if ( 0 != _err ) { _MESS_ERR(_err); _handler; return _ret; } \ +} while(0) /*------------------------------------------------------------------*/ @@ -141,7 +142,6 @@ #define TU_VERIFY_ERR_HDLR(...) GET_4TH_ARG(__VA_ARGS__, TU_VERIFY_ERR_HDLR_3ARGS, TU_VERIFY_ERR_HDLR_2ARGS)(__VA_ARGS__) - /*------------------------------------------------------------------*/ /* ASSERT * basically TU_VERIFY with TU_BREAKPOINT() as handler From 164b26ee6bb0a24924e9736178808b676288845e Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 12 Jul 2019 14:53:11 +0700 Subject: [PATCH 12/36] change TUD_HID_INOUT_DESCRIPTOR epout & epin order clean up, adding template for BOS & BOS platform descriptor --- .../hid_generic_inout/src/usb_descriptors.c | 2 +- examples/device/webusb/src/usb_descriptors.c | 4 ++- src/common/tusb_types.h | 26 +++++++++++----- src/device/usbd.c | 4 +-- src/device/usbd.h | 31 +++++++++++++++---- 5 files changed, 49 insertions(+), 18 deletions(-) diff --git a/examples/device/hid_generic_inout/src/usb_descriptors.c b/examples/device/hid_generic_inout/src/usb_descriptors.c index ced915935..cb7c92a57 100644 --- a/examples/device/hid_generic_inout/src/usb_descriptors.c +++ b/examples/device/hid_generic_inout/src/usb_descriptors.c @@ -85,7 +85,7 @@ uint8_t const desc_configuration[] = TUD_CONFIG_DESCRIPTOR(ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100), // Interface number, string index, protocol, report descriptor len, EP In & Out address, size & polling interval - TUD_HID_INOUT_DESCRIPTOR(ITF_NUM_HID, 0, HID_PROTOCOL_NONE, sizeof(desc_hid_report), 0x80 | EPNUM_HID, EPNUM_HID, CFG_TUD_HID_BUFSIZE, 10) + TUD_HID_INOUT_DESCRIPTOR(ITF_NUM_HID, 0, HID_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, 0x80 | EPNUM_HID, CFG_TUD_HID_BUFSIZE, 10) }; diff --git a/examples/device/webusb/src/usb_descriptors.c b/examples/device/webusb/src/usb_descriptors.c index 5149d85c5..4bc32f6ca 100644 --- a/examples/device/webusb/src/usb_descriptors.c +++ b/examples/device/webusb/src/usb_descriptors.c @@ -39,7 +39,7 @@ tusb_desc_device_t const desc_device = { .bLength = sizeof(tusb_desc_device_t), .bDescriptorType = TUSB_DESC_DEVICE, - .bcdUSB = 0x0210, + .bcdUSB = 0x0210, // at least 2.1 or 3.x .bDeviceClass = 0x00, .bDeviceSubClass = 0x00, .bDeviceProtocol = 0x00, @@ -56,6 +56,8 @@ tusb_desc_device_t const desc_device = .bNumConfigurations = 0x01 }; +// BOS Descriptor: required for webUSB + //------------- Configuration Descriptor -------------// enum { diff --git a/src/common/tusb_types.h b/src/common/tusb_types.h index f7a1fc221..527f84975 100644 --- a/src/common/tusb_types.h +++ b/src/common/tusb_types.h @@ -228,10 +228,10 @@ enum }; //--------------------------------------------------------------------+ -// STANDARD DESCRIPTORS +// USB Standard Descriptors //--------------------------------------------------------------------+ -/// USB Standard Device Descriptor (section 9.6.1, table 9-8) +/// USB Device Descriptor typedef struct TU_ATTR_PACKED { uint8_t bLength ; ///< Size of this descriptor in bytes. @@ -253,6 +253,7 @@ typedef struct TU_ATTR_PACKED uint8_t bNumConfigurations ; ///< Number of possible configurations. } tusb_desc_device_t; +// USB Binary Device Object Store (BOS) Descriptor typedef struct TU_ATTR_PACKED { uint8_t bLength ; ///< Size of this descriptor in bytes @@ -261,7 +262,7 @@ typedef struct TU_ATTR_PACKED uint8_t bNumDeviceCaps ; ///< Number of device capability descriptors in the BOS } tusb_desc_bos_t; -/// USB Standard Configuration Descriptor (section 9.6.1 table 9-10) */ +/// USB Configuration Descriptor typedef struct TU_ATTR_PACKED { uint8_t bLength ; ///< Size of this descriptor in bytes @@ -275,7 +276,7 @@ typedef struct TU_ATTR_PACKED uint8_t bMaxPower ; ///< Maximum power consumption of the USB device from the bus in this specific configuration when the device is fully operational. Expressed in 2 mA units (i.e., 50 = 100 mA). } tusb_desc_configuration_t; -/// USB Standard Interface Descriptor (section 9.6.1 table 9-12) +/// USB Interface Descriptor typedef struct TU_ATTR_PACKED { uint8_t bLength ; ///< Size of this descriptor in bytes @@ -290,7 +291,7 @@ typedef struct TU_ATTR_PACKED uint8_t iInterface ; ///< Index of string descriptor describing this interface } tusb_desc_interface_t; -/// USB Standard Endpoint Descriptor (section 9.6.1 table 9-13) +/// USB Endpoint Descriptor typedef struct TU_ATTR_PACKED { uint8_t bLength ; ///< Size of this descriptor in bytes @@ -314,7 +315,7 @@ typedef struct TU_ATTR_PACKED uint8_t bInterval ; ///< Interval for polling endpoint for data transfers. Expressed in frames or microframes depending on the device operating speed (i.e., either 1 millisecond or 125 us units). \n- For full-/high-speed isochronous endpoints, this value must be in the range from 1 to 16. The bInterval value is used as the exponent for a \f$ 2^(bInterval-1) \f$ value; e.g., a bInterval of 4 means a period of 8 (\f$ 2^(4-1) \f$). \n- For full-/low-speed interrupt endpoints, the value of this field may be from 1 to 255. \n- For high-speed interrupt endpoints, the bInterval value is used as the exponent for a \f$ 2^(bInterval-1) \f$ value; e.g., a bInterval of 4 means a period of 8 (\f$ 2^(4-1) \f$) . This value must be from 1 to 16. \n- For high-speed bulk/control OUT endpoints, the bInterval must specify the maximum NAK rate of the endpoint. A value of 0 indicates the endpoint never NAKs. Other values indicate at most 1 NAK each bInterval number of microframes. This value must be in the range from 0 to 255. \n Refer to Chapter 5 of USB 2.0 specification for more information. } tusb_desc_endpoint_t; -/// USB Other Speed Configuration Descriptor (section 9.6.1 table 9-11) +/// USB Other Speed Configuration Descriptor typedef struct TU_ATTR_PACKED { uint8_t bLength ; ///< Size of descriptor @@ -328,7 +329,7 @@ typedef struct TU_ATTR_PACKED uint8_t bMaxPower ; ///< Same as Configuration descriptor } tusb_desc_other_speed_t; -/// USB Device Qualifier Descriptor (section 9.6.1 table 9-9) +/// USB Device Qualifier Descriptor typedef struct TU_ATTR_PACKED { uint8_t bLength ; ///< Size of descriptor @@ -373,6 +374,15 @@ typedef struct TU_ATTR_PACKED uint16_t unicode_string[]; } tusb_desc_string_t; +typedef struct TU_ATTR_PACKED +{ + uint8_t bLength; + uint8_t bDescriptorType ; + uint8_t bDevCapabilityType; + uint8_t bReserved; + uint8_t PlatformCapabilityUUID[16]; + uint8_t CapabilityData[]; +} tusb_desc_bos_platform_t; /*------------------------------------------------------------------*/ /* Types @@ -449,7 +459,7 @@ static inline uint8_t tu_desc_len(void const* desc) #define TUD_DESC_STR_HEADER(_chr_count) ( (uint16_t) ( (TUSB_DESC_STRING << 8 ) | TUD_DESC_STRLEN(_chr_count)) ) // Convert comma-separated string to descriptor unicode format -#define TUD_DESC_STRCONV( ... ) (const uint16_t[]) { TUD_DESC_STR_HEADER(VA_ARGS_NUM_(__VA_ARGS__)), __VA_ARGS__ } +#define TUD_DESC_STRCONV( ... ) (const uint16_t[]) { TUD_DESC_STR_HEADER(TU_ARGS_NUM(__VA_ARGS__)), __VA_ARGS__ } #ifdef __cplusplus } diff --git a/src/device/usbd.c b/src/device/usbd.c index fb6d53ba1..6a70afede 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -593,8 +593,8 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const // String Descriptor always uses the desc set from user if ( desc_index == 0xEE ) { - // The 0xEE index string is a Microsoft USB extension. - // It can be used to tell Windows what driver it should use for the device !!! + // The 0xEE index string is a Microsoft OS Descriptors. + // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors return false; }else { diff --git a/src/device/usbd.h b/src/device/usbd.h index 9959a99af..8bd6adc35 100644 --- a/src/device/usbd.h +++ b/src/device/usbd.h @@ -93,9 +93,21 @@ TU_ATTR_WEAK void tud_suspend_cb(bool remote_wakeup_en); TU_ATTR_WEAK void tud_resume_cb(void); //--------------------------------------------------------------------+ -// Interface Descriptor Template +// Descriptor Templates //--------------------------------------------------------------------+ +//------------- Binary Device Object Store (BOS) -------------// +#define TUD_BOS_DESC_LEN 5 + +// total length, number of device caps +#define TUD_BOS_DESCRIPTOR(_total_len, _caps_num) \ + 5, TUSB_DESC_BOS, U16_TO_U8S_LE(_total_len), _caps_num + +// Device Capability Platform 128-bit UUID + Data +#define TUD_BOS_PLATFORM_DESCRIPTOR(...) \ + 4+TU_ARGS_NUM(__VA_ARGS__), TUSB_DESC_DEVICE_CAPABILITY, DEVICE_CAPABILITY_PLATFORM, 0x00, __VA_ARGS__ + +//------------- Configuration -------------// #define TUD_CONFIG_DESC_LEN (9) // Interface count, string index, total length, attribute, power in mA @@ -164,16 +176,16 @@ TU_ATTR_WEAK void tud_resume_cb(void); #define TUD_HID_INOUT_DESC_LEN (9 + 9 + 7 + 7) // HID Input & Output descriptor -// Interface number, string index, protocol, report descriptor len, EP In & Out address, size & polling interval -#define TUD_HID_INOUT_DESCRIPTOR(_itfnum, _stridx, _boot_protocol, _report_desc_len, _epin, _epout, _epsize, _ep_interval) \ +// Interface number, string index, protocol, report descriptor len, EP OUT & IN address, size & polling interval +#define TUD_HID_INOUT_DESCRIPTOR(_itfnum, _stridx, _boot_protocol, _report_desc_len, _epout, _epin, _epsize, _ep_interval) \ /* Interface */\ 9, TUSB_DESC_INTERFACE, _itfnum, 0, 2, TUSB_CLASS_HID, (_boot_protocol) ? HID_SUBCLASS_BOOT : 0, _boot_protocol, _stridx,\ /* HID descriptor */\ 9, HID_DESC_TYPE_HID, U16_TO_U8S_LE(0x0111), 0, 1, HID_DESC_TYPE_REPORT, U16_TO_U8S_LE(_report_desc_len),\ - /* Endpoint In */\ - 7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_epsize), _ep_interval,\ /* Endpoint Out */\ - 7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_epsize), _ep_interval + 7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_epsize), _ep_interval, \ + /* Endpoint In */\ + 7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_epsize), _ep_interval //------------- MIDI -------------// @@ -209,6 +221,13 @@ TU_ATTR_WEAK void tud_resume_cb(void); /* MS Endpoint (connected to embedded jack out) */\ 5, TUSB_DESC_CS_ENDPOINT, MIDI_CS_ENDPOINT_GENERAL, 1, 3 +//------------- WebUSB -------------// + +#define TUD_WEBUSB_DESC_LEN + +// Interface number, string index, EP Out & IN address, size & polling interval +#define TUD_WEBUSB_DESCRIPTOR() + #ifdef __cplusplus } From fdf39dd4aee82dc63d94a5a4e9423ecc15a268a1 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 12 Jul 2019 16:28:38 +0700 Subject: [PATCH 13/36] clean up usb descriptors in example --- .../device/cdc_msc_hid/src/usb_descriptors.c | 32 ++++--- .../src/usb_descriptors.c | 32 ++++--- .../hid_generic_inout/src/usb_descriptors.c | 49 ++++++----- .../device/midi_test/src/usb_descriptors.c | 28 ++++--- .../device/msc_dual_lun/src/usb_descriptors.c | 27 +++--- examples/device/webusb/src/usb_descriptors.c | 84 ++++++++++++------- 6 files changed, 160 insertions(+), 92 deletions(-) diff --git a/examples/device/cdc_msc_hid/src/usb_descriptors.c b/examples/device/cdc_msc_hid/src/usb_descriptors.c index 1692151a0..b06658c56 100644 --- a/examples/device/cdc_msc_hid/src/usb_descriptors.c +++ b/examples/device/cdc_msc_hid/src/usb_descriptors.c @@ -34,7 +34,9 @@ #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) ) -//------------- Device Descriptors -------------// +//--------------------------------------------------------------------+ +// Device Descriptors +//--------------------------------------------------------------------+ tusb_desc_device_t const desc_device = { .bLength = sizeof(tusb_desc_device_t), @@ -66,7 +68,17 @@ tusb_desc_device_t const desc_device = .bNumConfigurations = 0x01 }; -//------------- HID Report Descriptor -------------// +// 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; +} + +//--------------------------------------------------------------------+ +// HID Report Descriptor +//--------------------------------------------------------------------+ + #if CFG_TUD_HID enum { @@ -90,7 +102,10 @@ uint8_t const * tud_hid_descriptor_report_cb(void) #endif -//------------- Configuration Descriptor -------------// +//--------------------------------------------------------------------+ +// Configuration Descriptor +//--------------------------------------------------------------------+ + enum { #if CFG_TUD_CDC @@ -145,12 +160,6 @@ uint8_t const desc_configuration[] = #endif }; -// 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; -} // Invoked when received GET CONFIGURATION DESCRIPTOR // Application return pointer to descriptor @@ -160,7 +169,10 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index) (void) index; // for multiple configurations return desc_configuration; } -//------------- String Descriptors -------------// + +//--------------------------------------------------------------------+ +// String Descriptors +//--------------------------------------------------------------------+ // array of pointer to string descriptors char const* string_desc_arr [] = diff --git a/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c b/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c index 579e1ebcf..65c8cd712 100644 --- a/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c +++ b/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c @@ -34,7 +34,9 @@ #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) ) -//------------- Device Descriptors -------------// +//--------------------------------------------------------------------+ +// Device Descriptors +//--------------------------------------------------------------------+ tusb_desc_device_t const desc_device = { .bLength = sizeof(tusb_desc_device_t), @@ -66,7 +68,17 @@ tusb_desc_device_t const desc_device = .bNumConfigurations = 0x01 }; -//------------- HID Report Descriptor -------------// +// 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; +} + +//--------------------------------------------------------------------+ +// HID Report Descriptor +//--------------------------------------------------------------------+ + #if CFG_TUD_HID enum { @@ -90,7 +102,10 @@ uint8_t const * tud_hid_descriptor_report_cb(void) #endif -//------------- Configuration Descriptor -------------// +//--------------------------------------------------------------------+ +// Configuration Descriptor +//--------------------------------------------------------------------+ + enum { #if CFG_TUD_CDC @@ -145,12 +160,7 @@ uint8_t const desc_configuration[] = #endif }; -// 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; -} + // Invoked when received GET CONFIGURATION DESCRIPTOR // Application return pointer to descriptor @@ -161,7 +171,9 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index) return desc_configuration; } -//------------- String Descriptors -------------// +//--------------------------------------------------------------------+ +// String Descriptors +//--------------------------------------------------------------------+ // array of pointer to string descriptors char const* string_desc_arr [] = diff --git a/examples/device/hid_generic_inout/src/usb_descriptors.c b/examples/device/hid_generic_inout/src/usb_descriptors.c index cb7c92a57..516dab646 100644 --- a/examples/device/hid_generic_inout/src/usb_descriptors.c +++ b/examples/device/hid_generic_inout/src/usb_descriptors.c @@ -34,7 +34,9 @@ #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) ) -//------------- Device Descriptors -------------// +//--------------------------------------------------------------------+ +// Device Descriptors +//--------------------------------------------------------------------+ tusb_desc_device_t const desc_device = { .bLength = sizeof(tusb_desc_device_t), @@ -56,13 +58,34 @@ tusb_desc_device_t const desc_device = .bNumConfigurations = 0x01 }; -//------------- HID Report Descriptor -------------// +// 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; +} + +//--------------------------------------------------------------------+ +// HID Report Descriptor +//--------------------------------------------------------------------+ + uint8_t const desc_hid_report[] = { TUD_HID_REPORT_DESC_GENERIC_INOUT(CFG_TUD_HID_BUFSIZE) }; -//------------- Configuration Descriptor -------------// +// Invoked when received GET HID REPORT DESCRIPTOR +// Application return pointer to descriptor +// Descriptor contents must exist long enough for transfer to complete +uint8_t const * tud_hid_descriptor_report_cb(void) +{ + return desc_hid_report; +} + +//--------------------------------------------------------------------+ +// Configuration Descriptor +//--------------------------------------------------------------------+ + enum { ITF_NUM_HID, @@ -88,14 +111,6 @@ uint8_t const desc_configuration[] = TUD_HID_INOUT_DESCRIPTOR(ITF_NUM_HID, 0, HID_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, 0x80 | EPNUM_HID, CFG_TUD_HID_BUFSIZE, 10) }; - -// 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; -} - // Invoked when received GET CONFIGURATION DESCRIPTOR // Application return pointer to descriptor // Descriptor contents must exist long enough for transfer to complete @@ -105,15 +120,9 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index) return desc_configuration; } -// Invoked when received GET HID REPORT DESCRIPTOR -// Application return pointer to descriptor -// Descriptor contents must exist long enough for transfer to complete -uint8_t const * tud_hid_descriptor_report_cb(void) -{ - return desc_hid_report; -} - -//------------- String Descriptors -------------// +//--------------------------------------------------------------------+ +// String Descriptors +//--------------------------------------------------------------------+ // array of pointer to string descriptors char const* string_desc_arr [] = diff --git a/examples/device/midi_test/src/usb_descriptors.c b/examples/device/midi_test/src/usb_descriptors.c index 21270745a..cffef0fb2 100644 --- a/examples/device/midi_test/src/usb_descriptors.c +++ b/examples/device/midi_test/src/usb_descriptors.c @@ -34,7 +34,9 @@ #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) ) -//------------- Device Descriptors -------------// +//--------------------------------------------------------------------+ +// Device Descriptors +//--------------------------------------------------------------------+ tusb_desc_device_t const desc_device = { .bLength = sizeof(tusb_desc_device_t), @@ -56,7 +58,18 @@ tusb_desc_device_t const desc_device = .bNumConfigurations = 0x01 }; -//------------- Configuration Descriptor -------------// +// 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_MIDI = 0, @@ -83,13 +96,6 @@ uint8_t const desc_configuration[] = TUD_MIDI_DESCRIPTOR(ITF_NUM_MIDI, 0, EPNUM_MIDI, 0x80 | EPNUM_MIDI, (CFG_TUSB_RHPORT0_MODE & OPT_MODE_HIGH_SPEED) ? 512 : 64) }; -// 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; -} - // Invoked when received GET CONFIGURATION DESCRIPTOR // Application return pointer to descriptor // Descriptor contents must exist long enough for transfer to complete @@ -99,7 +105,9 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index) return desc_configuration; } -//------------- String Descriptors -------------// +//--------------------------------------------------------------------+ +// String Descriptors +//--------------------------------------------------------------------+ // array of pointer to string descriptors char const* string_desc_arr [] = diff --git a/examples/device/msc_dual_lun/src/usb_descriptors.c b/examples/device/msc_dual_lun/src/usb_descriptors.c index 26ebe190c..c4e171c06 100644 --- a/examples/device/msc_dual_lun/src/usb_descriptors.c +++ b/examples/device/msc_dual_lun/src/usb_descriptors.c @@ -34,7 +34,9 @@ #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) ) -//------------- Device Descriptors -------------// +//--------------------------------------------------------------------+ +// Device Descriptors +//--------------------------------------------------------------------+ tusb_desc_device_t const desc_device = { .bLength = sizeof(tusb_desc_device_t), @@ -56,7 +58,17 @@ tusb_desc_device_t const desc_device = .bNumConfigurations = 0x01 }; -//------------- Configuration Descriptor -------------// +// 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_MSC, @@ -82,13 +94,6 @@ uint8_t const desc_configuration[] = TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 0, EPNUM_MSC, 0x80 | EPNUM_MSC, (CFG_TUSB_RHPORT0_MODE & OPT_MODE_HIGH_SPEED) ? 512 : 64), }; -// 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; -} - // Invoked when received GET CONFIGURATION DESCRIPTOR // Application return pointer to descriptor // Descriptor contents must exist long enough for transfer to complete @@ -98,7 +103,9 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index) return desc_configuration; } -//------------- String Descriptors -------------// +//--------------------------------------------------------------------+ +// String Descriptors +//--------------------------------------------------------------------+ // array of pointer to string descriptors char const* string_desc_arr [] = diff --git a/examples/device/webusb/src/usb_descriptors.c b/examples/device/webusb/src/usb_descriptors.c index 4bc32f6ca..188470eb5 100644 --- a/examples/device/webusb/src/usb_descriptors.c +++ b/examples/device/webusb/src/usb_descriptors.c @@ -34,15 +34,20 @@ #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) ) -//------------- Device Descriptors -------------// +//--------------------------------------------------------------------+ +// Device Descriptors +//--------------------------------------------------------------------+ tusb_desc_device_t const desc_device = { .bLength = sizeof(tusb_desc_device_t), .bDescriptorType = TUSB_DESC_DEVICE, .bcdUSB = 0x0210, // at least 2.1 or 3.x - .bDeviceClass = 0x00, - .bDeviceSubClass = 0x00, - .bDeviceProtocol = 0x00, + + // 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_ENDOINT0_SIZE, .idVendor = 0xCafe, @@ -56,33 +61,6 @@ tusb_desc_device_t const desc_device = .bNumConfigurations = 0x01 }; -// BOS Descriptor: required for webUSB - -//------------- Configuration Descriptor -------------// -enum -{ - ITF_NUM_WEBUSB = 0, - ITF_NUM_TOTAL -}; - -enum -{ - CONFIG_TOTAL_LEN = TUD_CONFIG_DESC_LEN + TUD_MIDI_DESC_LEN -}; - -// Use Endpoint 2 instead of 1 due to NXP MCU -// LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number -// 0 control, 1 In, 2 Bulk, 3 Iso, 4 In etc ... -#define EPNUM_MIDI 0x02 - -uint8_t const desc_configuration[] = -{ - // Interface count, string index, total length, attribute, power in mA - TUD_CONFIG_DESCRIPTOR(ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100), - - // Interface number, string index, EP Out & EP In address, EP size -}; - // Invoked when received GET DEVICE DESCRIPTOR // Application return pointer to descriptor uint8_t const * tud_descriptor_device_cb(void) @@ -90,6 +68,45 @@ uint8_t const * tud_descriptor_device_cb(void) return (uint8_t const *) &desc_device; } +//--------------------------------------------------------------------+ +// BOS Descriptor +//--------------------------------------------------------------------+ + +// BOS Descriptor is required for webUSB +uint8_t const desc_bos[] = +{ + +}; + +//--------------------------------------------------------------------+ +// Configuration Descriptor +//--------------------------------------------------------------------+ +enum +{ + ITF_NUM_CDC = 0, + ITF_NUM_CDC_DATA, + ITF_NUM_TOTAL +}; + +enum +{ + CONFIG_TOTAL_LEN = TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN +}; + +// Use Endpoint 2 instead of 1 due to NXP MCU +// LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number +// 0 control, 1 In, 2 Bulk, 3 Iso, 4 In etc ... +#define EPNUM_CDC 0x02 + +uint8_t const desc_configuration[] = +{ + // Interface count, string index, total length, attribute, power in mA + TUD_CONFIG_DESCRIPTOR(ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100), + + // Interface number, string index, EP notification address and size, EP data address (out, in) and size. + TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, 0x81, 8, EPNUM_CDC, 0x80 | EPNUM_CDC, 64), +}; + // Invoked when received GET CONFIGURATION DESCRIPTOR // Application return pointer to descriptor // Descriptor contents must exist long enough for transfer to complete @@ -99,7 +116,9 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index) return desc_configuration; } -//------------- String Descriptors -------------// +//--------------------------------------------------------------------+ +// String Descriptors +//--------------------------------------------------------------------+ // array of pointer to string descriptors char const* string_desc_arr [] = @@ -108,6 +127,7 @@ char const* string_desc_arr [] = "TinyUSB", // 1: Manufacturer "TinyUSB Device", // 2: Product "123456", // 3: Serials, should use chip ID + "TinyUSB CDC", // 4: CDC Interface }; static uint16_t _desc_str[32]; From 2b7acd554a00f196394fdb91cd11a18e7981949f Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 12 Jul 2019 19:38:04 +0700 Subject: [PATCH 14/36] updating webusb with cdc --- .../device/cdc_msc_hid/src/usb_descriptors.c | 5 +- .../src/usb_descriptors.c | 5 +- .../hid_generic_inout/src/usb_descriptors.c | 6 +-- .../device/midi_test/src/usb_descriptors.c | 5 +- .../device/msc_dual_lun/src/usb_descriptors.c | 5 +- examples/device/webusb/src/main.c | 48 ++++++++++++++++++- examples/device/webusb/src/tusb_config.h | 6 ++- examples/device/webusb/src/usb_descriptors.c | 17 +++++-- src/device/usbd.h | 25 +++++++++- 9 files changed, 93 insertions(+), 29 deletions(-) diff --git a/examples/device/cdc_msc_hid/src/usb_descriptors.c b/examples/device/cdc_msc_hid/src/usb_descriptors.c index b06658c56..4220dc5fb 100644 --- a/examples/device/cdc_msc_hid/src/usb_descriptors.c +++ b/examples/device/cdc_msc_hid/src/usb_descriptors.c @@ -124,10 +124,7 @@ enum ITF_NUM_TOTAL }; -enum -{ - CONFIG_TOTAL_LEN = TUD_CONFIG_DESC_LEN + CFG_TUD_CDC*TUD_CDC_DESC_LEN + CFG_TUD_MSC*TUD_MSC_DESC_LEN + CFG_TUD_HID*TUD_HID_DESC_LEN -}; +#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + CFG_TUD_CDC*TUD_CDC_DESC_LEN + CFG_TUD_MSC*TUD_MSC_DESC_LEN + CFG_TUD_HID*TUD_HID_DESC_LEN) #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 diff --git a/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c b/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c index 65c8cd712..4e1df9fb5 100644 --- a/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c +++ b/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c @@ -124,10 +124,7 @@ enum ITF_NUM_TOTAL }; -enum -{ - CONFIG_TOTAL_LEN = TUD_CONFIG_DESC_LEN + CFG_TUD_CDC*TUD_CDC_DESC_LEN + CFG_TUD_MSC*TUD_MSC_DESC_LEN + CFG_TUD_HID*TUD_HID_DESC_LEN -}; +#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + CFG_TUD_CDC*TUD_CDC_DESC_LEN + CFG_TUD_MSC*TUD_MSC_DESC_LEN + CFG_TUD_HID*TUD_HID_DESC_LEN) #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 diff --git a/examples/device/hid_generic_inout/src/usb_descriptors.c b/examples/device/hid_generic_inout/src/usb_descriptors.c index 516dab646..849c2be8a 100644 --- a/examples/device/hid_generic_inout/src/usb_descriptors.c +++ b/examples/device/hid_generic_inout/src/usb_descriptors.c @@ -92,10 +92,8 @@ enum ITF_NUM_TOTAL }; -enum -{ - CONFIG_TOTAL_LEN = TUD_CONFIG_DESC_LEN + TUD_HID_INOUT_DESC_LEN -}; + +#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_HID_INOUT_DESC_LEN) // Use Endpoint 2 instead of 1 due to NXP MCU // LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number diff --git a/examples/device/midi_test/src/usb_descriptors.c b/examples/device/midi_test/src/usb_descriptors.c index cffef0fb2..85cffffcc 100644 --- a/examples/device/midi_test/src/usb_descriptors.c +++ b/examples/device/midi_test/src/usb_descriptors.c @@ -77,10 +77,7 @@ enum ITF_NUM_TOTAL }; -enum -{ - CONFIG_TOTAL_LEN = TUD_CONFIG_DESC_LEN + TUD_MIDI_DESC_LEN -}; +#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_MIDI_DESC_LEN) // Use Endpoint 2 instead of 1 due to NXP MCU // LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number diff --git a/examples/device/msc_dual_lun/src/usb_descriptors.c b/examples/device/msc_dual_lun/src/usb_descriptors.c index c4e171c06..585588a9c 100644 --- a/examples/device/msc_dual_lun/src/usb_descriptors.c +++ b/examples/device/msc_dual_lun/src/usb_descriptors.c @@ -75,10 +75,7 @@ enum ITF_NUM_TOTAL }; -enum -{ - CONFIG_TOTAL_LEN = TUD_CONFIG_DESC_LEN + TUD_MSC_DESC_LEN -}; +#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_MSC_DESC_LEN) // Use Endpoint 2 instead of 1 due to NXP MCU // LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number diff --git a/examples/device/webusb/src/main.c b/examples/device/webusb/src/main.c index 6d96eba0b..59e934930 100644 --- a/examples/device/webusb/src/main.c +++ b/examples/device/webusb/src/main.c @@ -54,7 +54,7 @@ enum { static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED; void led_blinking_task(void); -void midi_task(void); +void cdc_task(void); /*------------- MAIN -------------*/ int main(void) @@ -66,6 +66,7 @@ int main(void) while (1) { tud_task(); // tinyusb device task + cdc_task(); led_blinking_task(); } @@ -108,6 +109,51 @@ void tud_resume_cb(void) //--------------------------------------------------------------------+ +//--------------------------------------------------------------------+ +// USB CDC +//--------------------------------------------------------------------+ +void cdc_task(void) +{ + if ( tud_cdc_connected() ) + { + // connected and there are data available + if ( tud_cdc_available() ) + { + uint8_t buf[64]; + + // read and echo back + uint32_t count = tud_cdc_read(buf, sizeof(buf)); + + for(uint32_t i=0; i Date: Fri, 12 Jul 2019 22:03:40 +0700 Subject: [PATCH 15/36] clean up, rename internal driver control_request_complete to simply control_complete --- examples/device/webusb/src/usb_descriptors.c | 4 ++- src/class/cdc/cdc_device.c | 2 +- src/class/cdc/cdc_device.h | 12 ++++----- src/class/custom/custom_device.h | 2 +- src/class/hid/hid_device.c | 2 +- src/class/hid/hid_device.h | 12 ++++----- src/class/hid/hid_host.h | 4 +-- src/class/midi/midi_device.c | 2 +- src/class/midi/midi_device.h | 12 ++++----- src/class/msc/msc_device.c | 2 +- src/class/msc/msc_device.h | 12 ++++----- src/common/tusb_types.h | 26 ++++++++++---------- src/device/usbd.c | 20 +++++++++------ 13 files changed, 60 insertions(+), 52 deletions(-) diff --git a/examples/device/webusb/src/usb_descriptors.c b/examples/device/webusb/src/usb_descriptors.c index 3ea6f117f..f1e9dd979 100644 --- a/examples/device/webusb/src/usb_descriptors.c +++ b/examples/device/webusb/src/usb_descriptors.c @@ -77,9 +77,11 @@ uint8_t const * tud_descriptor_device_cb(void) // BOS Descriptor is required for webUSB uint8_t const desc_bos[] = { + // total length, number of device caps TUD_BOS_DESCRIPTOR(BOS_TOTAL_LEN, 1), - TUD_BOS_WEBUSB_DESCRIPTOR(0x01, 0) + // Vendor Code, iLandingPage + TUD_BOS_WEBUSB_DESCRIPTOR(0x01, 1) }; uint8_t const * tud_descriptor_bos_cb(void) diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index 6d13dafc0..874f751af 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -300,7 +300,7 @@ bool cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t // Invoked when class request DATA stage is finished. // return false to stall control endpoint (e.g Host send non-sense DATA) -bool cdcd_control_request_complete(uint8_t rhport, tusb_control_request_t const * request) +bool cdcd_control_complete(uint8_t rhport, tusb_control_request_t const * request) { (void) rhport; diff --git a/src/class/cdc/cdc_device.h b/src/class/cdc/cdc_device.h index 8d09094a0..76bb7c622 100644 --- a/src/class/cdc/cdc_device.h +++ b/src/class/cdc/cdc_device.h @@ -176,12 +176,12 @@ static inline bool tud_cdc_write_flush (void) //--------------------------------------------------------------------+ // INTERNAL USBD-CLASS DRIVER API //--------------------------------------------------------------------+ -void cdcd_init (void); -bool cdcd_open (uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, uint16_t *p_length); -bool cdcd_control_request (uint8_t rhport, tusb_control_request_t const * p_request); -bool cdcd_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request); -bool cdcd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes); -void cdcd_reset (uint8_t rhport); +void cdcd_init (void); +bool cdcd_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length); +bool cdcd_control_request (uint8_t rhport, tusb_control_request_t const * request); +bool cdcd_control_complete (uint8_t rhport, tusb_control_request_t const * request); +bool cdcd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes); +void cdcd_reset (uint8_t rhport); #ifdef __cplusplus } diff --git a/src/class/custom/custom_device.h b/src/class/custom/custom_device.h index 02cdba7e5..c4001a89f 100644 --- a/src/class/custom/custom_device.h +++ b/src/class/custom/custom_device.h @@ -51,7 +51,7 @@ void cusd_init(void); bool cusd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length); bool cusd_control_request_st(uint8_t rhport, tusb_control_request_t const * p_request); -bool cusd_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request); +bool cusd_control_complete (uint8_t rhport, tusb_control_request_t const * p_request); bool cusd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); void cusd_reset(uint8_t rhport); diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c index d3f59ed62..54ba39643 100644 --- a/src/class/hid/hid_device.c +++ b/src/class/hid/hid_device.c @@ -276,7 +276,7 @@ bool hidd_control_request(uint8_t rhport, tusb_control_request_t const * p_reque // Invoked when class request DATA stage is finished. // return false to stall control endpoint (e.g Host send non-sense DATA) -bool hidd_control_request_complete(uint8_t rhport, tusb_control_request_t const * p_request) +bool hidd_control_complete(uint8_t rhport, tusb_control_request_t const * p_request) { (void) rhport; hidd_interface_t* p_hid = get_interface_by_itfnum( (uint8_t) p_request->wIndex ); diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h index da239cc84..49512ac4e 100644 --- a/src/class/hid/hid_device.h +++ b/src/class/hid/hid_device.h @@ -300,12 +300,12 @@ TU_ATTR_WEAK bool tud_hid_set_idle_cb(uint8_t idle_rate); //--------------------------------------------------------------------+ // Internal Class Driver API //--------------------------------------------------------------------+ -void hidd_init(void); -bool hidd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length); -bool hidd_control_request(uint8_t rhport, tusb_control_request_t const * p_request); -bool hidd_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request); -bool hidd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); -void hidd_reset(uint8_t rhport); +void hidd_init (void); +bool hidd_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length); +bool hidd_control_request (uint8_t rhport, tusb_control_request_t const * request); +bool hidd_control_complete (uint8_t rhport, tusb_control_request_t const * request); +bool hidd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); +void hidd_reset (uint8_t rhport); #ifdef __cplusplus } diff --git a/src/class/hid/hid_host.h b/src/class/hid/hid_host.h index 50eabba3e..1c619c570 100644 --- a/src/class/hid/hid_host.h +++ b/src/class/hid/hid_host.h @@ -55,7 +55,7 @@ extern uint8_t const hid_keycode_to_ascii_tbl[2][128]; // TODO used weak attr if * \retval true if device supports Keyboard interface * \retval false if device does not support Keyboard interface or is not mounted */ -bool tuh_hid_keyboard_is_mounted(uint8_t dev_addr); +bool tuh_hid_keyboard_is_mounted(uint8_t dev_addr); /** \brief Check if the interface is currently busy or not * \param[in] dev_addr device address @@ -64,7 +64,7 @@ bool tuh_hid_keyboard_is_mounted(uint8_t dev_addr); * \note This function is primarily used for polling/waiting result after \ref tuh_hid_keyboard_get_report. * Alternatively, asynchronous event API can be used */ -bool tuh_hid_keyboard_is_busy(uint8_t dev_addr); +bool tuh_hid_keyboard_is_busy(uint8_t dev_addr); /** \brief Perform a get report from Keyboard interface * \param[in] dev_addr device address diff --git a/src/class/midi/midi_device.c b/src/class/midi/midi_device.c index 267167720..1e684edde 100644 --- a/src/class/midi/midi_device.c +++ b/src/class/midi/midi_device.c @@ -300,7 +300,7 @@ bool midid_open(uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, return true; } -bool midid_control_request_complete(uint8_t rhport, tusb_control_request_t const * p_request) +bool midid_control_complete(uint8_t rhport, tusb_control_request_t const * p_request) { return false; } diff --git a/src/class/midi/midi_device.h b/src/class/midi/midi_device.h index 490abaf89..f9d13ffb6 100644 --- a/src/class/midi/midi_device.h +++ b/src/class/midi/midi_device.h @@ -121,12 +121,12 @@ static inline uint32_t tudi_midi_write24 (uint8_t jack_id, uint8_t b1, uint8_t b //--------------------------------------------------------------------+ // Internal Class Driver API //--------------------------------------------------------------------+ -void midid_init (void); -bool midid_open (uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, uint16_t *p_length); -bool midid_control_request (uint8_t rhport, tusb_control_request_t const * p_request); -bool midid_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request); -bool midid_xfer_cb (uint8_t rhport, uint8_t edpt_addr, xfer_result_t result, uint32_t xferred_bytes); -void midid_reset (uint8_t rhport); +void midid_init (void); +bool midid_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length); +bool midid_control_request (uint8_t rhport, tusb_control_request_t const * request); +bool midid_control_complete (uint8_t rhport, tusb_control_request_t const * request); +bool midid_xfer_cb (uint8_t rhport, uint8_t edpt_addr, xfer_result_t result, uint32_t xferred_bytes); +void midid_reset (uint8_t rhport); #ifdef __cplusplus } diff --git a/src/class/msc/msc_device.c b/src/class/msc/msc_device.c index 5681e4f07..86e47ead0 100644 --- a/src/class/msc/msc_device.c +++ b/src/class/msc/msc_device.c @@ -178,7 +178,7 @@ bool mscd_control_request(uint8_t rhport, tusb_control_request_t const * p_reque // Invoked when class request DATA stage is finished. // return false to stall control endpoint (e.g Host send non-sense DATA) -bool mscd_control_request_complete(uint8_t rhport, tusb_control_request_t const * p_request) +bool mscd_control_complete(uint8_t rhport, tusb_control_request_t const * p_request) { (void) rhport; (void) p_request; diff --git a/src/class/msc/msc_device.h b/src/class/msc/msc_device.h index 03a4afadb..5998400b1 100644 --- a/src/class/msc/msc_device.h +++ b/src/class/msc/msc_device.h @@ -151,12 +151,12 @@ TU_ATTR_WEAK bool tud_msc_is_writable_cb(uint8_t lun); //--------------------------------------------------------------------+ // Internal Class Driver API //--------------------------------------------------------------------+ -void mscd_init(void); -bool mscd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length); -bool mscd_control_request(uint8_t rhport, tusb_control_request_t const * p_request); -bool mscd_control_request_complete (uint8_t rhport, tusb_control_request_t const * p_request); -bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); -void mscd_reset(uint8_t rhport); +void mscd_init (void); +bool mscd_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length); +bool mscd_control_request (uint8_t rhport, tusb_control_request_t const * p_request); +bool mscd_control_complete (uint8_t rhport, tusb_control_request_t const * p_request); +bool mscd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); +void mscd_reset (uint8_t rhport); #ifdef __cplusplus } diff --git a/src/common/tusb_types.h b/src/common/tusb_types.h index 527f84975..7270ec58b 100644 --- a/src/common/tusb_types.h +++ b/src/common/tusb_types.h @@ -100,19 +100,19 @@ typedef enum typedef enum { - TUSB_REQ_GET_STATUS = 0 , - TUSB_REQ_CLEAR_FEATURE = 1 , - TUSB_REQ_RESERVED = 2 , - TUSB_REQ_SET_FEATURE = 3 , - TUSB_REQ_RESERVED2 = 4 , - TUSB_REQ_SET_ADDRESS = 5 , - TUSB_REQ_GET_DESCRIPTOR = 6 , - TUSB_REQ_SET_DESCRIPTOR = 7 , - TUSB_REQ_GET_CONFIGURATION = 8 , - TUSB_REQ_SET_CONFIGURATION = 9 , - TUSB_REQ_GET_INTERFACE = 10 , - TUSB_REQ_SET_INTERFACE = 11 , - TUSB_REQ_SYNCH_FRAME = 12 + TUSB_REQ_GET_STATUS = 0 , + TUSB_REQ_CLEAR_FEATURE = 1 , + TUSB_REQ_RESERVED = 2 , + TUSB_REQ_SET_FEATURE = 3 , + TUSB_REQ_RESERVED2 = 4 , + TUSB_REQ_SET_ADDRESS = 5 , + TUSB_REQ_GET_DESCRIPTOR = 6 , + TUSB_REQ_SET_DESCRIPTOR = 7 , + TUSB_REQ_GET_CONFIGURATION = 8 , + TUSB_REQ_SET_CONFIGURATION = 9 , + TUSB_REQ_GET_INTERFACE = 10 , + TUSB_REQ_SET_INTERFACE = 11 , + TUSB_REQ_SYNCH_FRAME = 12 }tusb_request_code_t; typedef enum diff --git a/src/device/usbd.c b/src/device/usbd.c index 6a70afede..b9d9f9d8a 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -70,7 +70,7 @@ typedef struct { void (* init ) (void); bool (* open ) (uint8_t rhport, tusb_desc_interface_t const * desc_intf, uint16_t* p_length); bool (* control_request ) (uint8_t rhport, tusb_control_request_t const * request); - bool (* control_request_complete ) (uint8_t rhport, tusb_control_request_t const * request); + bool (* control_complete ) (uint8_t rhport, tusb_control_request_t const * request); bool (* xfer_cb ) (uint8_t rhport, uint8_t ep_addr, xfer_result_t, uint32_t); void (* sof ) (uint8_t rhport); void (* reset ) (uint8_t); @@ -84,7 +84,7 @@ static usbd_class_driver_t const usbd_class_drivers[] = .init = cdcd_init, .open = cdcd_open, .control_request = cdcd_control_request, - .control_request_complete = cdcd_control_request_complete, + .control_complete = cdcd_control_complete, .xfer_cb = cdcd_xfer_cb, .sof = NULL, .reset = cdcd_reset @@ -97,7 +97,7 @@ static usbd_class_driver_t const usbd_class_drivers[] = .init = mscd_init, .open = mscd_open, .control_request = mscd_control_request, - .control_request_complete = mscd_control_request_complete, + .control_complete = mscd_control_complete, .xfer_cb = mscd_xfer_cb, .sof = NULL, .reset = mscd_reset @@ -110,7 +110,7 @@ static usbd_class_driver_t const usbd_class_drivers[] = .init = hidd_init, .open = hidd_open, .control_request = hidd_control_request, - .control_request_complete = hidd_control_request_complete, + .control_complete = hidd_control_complete, .xfer_cb = hidd_xfer_cb, .sof = NULL, .reset = hidd_reset @@ -123,7 +123,7 @@ static usbd_class_driver_t const usbd_class_drivers[] = .init = midid_init, .open = midid_open, .control_request = midid_control_request, - .control_request_complete = midid_control_request_complete, + .control_complete = midid_control_complete, .xfer_cb = midid_xfer_cb, .sof = NULL, .reset = midid_reset @@ -136,7 +136,7 @@ static usbd_class_driver_t const usbd_class_drivers[] = .init = cusd_init, .open = cusd_open, .control_request = cusd_control_request, - .control_request_complete = cusd_control_request_complete, + .control_complete = cusd_control_complete, .xfer_cb = cusd_xfer_cb, .sof = NULL, .reset = cusd_reset @@ -347,6 +347,12 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const { usbd_control_set_complete_callback(NULL); + // Vendor request +// if ( p_request->bmRequestType_bit.type == TUSB_REQ_TYPE_VENDOR ) +// { +// +// } + switch ( p_request->bmRequestType_bit.recipient ) { //------------- Device Requests e.g in enumeration -------------// @@ -431,7 +437,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const TU_VERIFY(drvid < USBD_CLASS_DRIVER_COUNT); - usbd_control_set_complete_callback(usbd_class_drivers[drvid].control_request_complete ); + usbd_control_set_complete_callback(usbd_class_drivers[drvid].control_complete ); // stall control endpoint if driver return false return usbd_class_drivers[drvid].control_request(rhport, p_request); From 036e858543dce1c19ff789b69907680500e28ca6 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 16 Jul 2019 18:14:47 +0700 Subject: [PATCH 16/36] add tud_control_vendor_request_cb()/tud_control_vendor_complete_cb(), expose usbd control transfer rename usbd_control_transfer/status to tud_control_transfer/status --- examples/device/webusb/src/main.c | 6 --- examples/device/webusb/src/usb_descriptors.c | 38 +++++++++++++++ src/class/cdc/cdc_device.c | 6 +-- src/class/hid/hid_device.c | 14 +++--- src/class/msc/msc_device.c | 4 +- src/device/usbd.c | 50 ++++++++++---------- src/device/usbd.h | 12 +++++ src/device/usbd_control.c | 8 ++-- src/device/usbd_pvt.h | 8 ---- 9 files changed, 92 insertions(+), 54 deletions(-) diff --git a/examples/device/webusb/src/main.c b/examples/device/webusb/src/main.c index 59e934930..5624ff4bd 100644 --- a/examples/device/webusb/src/main.c +++ b/examples/device/webusb/src/main.c @@ -30,12 +30,6 @@ #include "bsp/board.h" #include "tusb.h" -/* This MIDI example send sequence of note (on/off) repeatedly. To test on PC, you need to install - * synth software and midi connection management software. On - * - Linux (Ubuntu) : install qsynth, qjackctl. Then connect TinyUSB output port to FLUID Synth input port - * - */ - //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF PROTYPES //--------------------------------------------------------------------+ diff --git a/examples/device/webusb/src/usb_descriptors.c b/examples/device/webusb/src/usb_descriptors.c index f1e9dd979..97c05e5d0 100644 --- a/examples/device/webusb/src/usb_descriptors.c +++ b/examples/device/webusb/src/usb_descriptors.c @@ -174,3 +174,41 @@ uint16_t const* tud_descriptor_string_cb(uint8_t index) return _desc_str; } + +//--------------------------------------------------------------------+ +// WebUSB URL Descriptor +//--------------------------------------------------------------------+ +#define URL "github.com/hathach/tinyusb" + +typedef struct TU_ATTR_PACKED +{ + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bScheme; + char url[]; +} tusb_desc_webusb_url_t; + +const tusb_desc_webusb_url_t desc_url = +{ + .bLength = 3 + sizeof(URL) - 1, + .bDescriptorType = 3, // WEBUSB URL type + .bScheme = 1, // 0: http, 1: https + .url = URL +}; + +bool tud_control_vendor_request_cb(uint8_t rhport, tusb_control_request_t const * request) +{ + if ( request->bRequest == 0x01 ) // webusb vendor code + { + return tud_control_xfer(rhport, request, (void*) &desc_url, desc_url.bLength); + }else + { + return false; + } +} + +bool tud_control_vendor_complete_cb(uint8_t rhport, tusb_control_request_t const * request) +{ + (void) rhport; + return true; +} diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c index 874f751af..d4952d115 100644 --- a/src/class/cdc/cdc_device.c +++ b/src/class/cdc/cdc_device.c @@ -334,11 +334,11 @@ bool cdcd_control_request(uint8_t rhport, tusb_control_request_t const * request switch ( request->bRequest ) { case CDC_REQUEST_SET_LINE_CODING: - usbd_control_xfer(rhport, request, &p_cdc->line_coding, sizeof(cdc_line_coding_t)); + tud_control_xfer(rhport, request, &p_cdc->line_coding, sizeof(cdc_line_coding_t)); break; case CDC_REQUEST_GET_LINE_CODING: - usbd_control_xfer(rhport, request, &p_cdc->line_coding, sizeof(cdc_line_coding_t)); + tud_control_xfer(rhport, request, &p_cdc->line_coding, sizeof(cdc_line_coding_t)); break; case CDC_REQUEST_SET_CONTROL_LINE_STATE: @@ -349,7 +349,7 @@ bool cdcd_control_request(uint8_t rhport, tusb_control_request_t const * request // This signal corresponds to V.24 signal 105 and RS-232 signal RTS (Request to Send) p_cdc->line_state = (uint8_t) request->wValue; - usbd_control_status(rhport, request); + tud_control_status(rhport, request); // Invoke callback if ( tud_cdc_line_state_cb) tud_cdc_line_state_cb(itf, tu_bit_test(request->wValue, 0), tu_bit_test(request->wValue, 1)); diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c index 54ba39643..949fd183d 100644 --- a/src/class/hid/hid_device.c +++ b/src/class/hid/hid_device.c @@ -205,7 +205,7 @@ bool hidd_control_request(uint8_t rhport, tusb_control_request_t const * p_reque if (p_request->bRequest == TUSB_REQ_GET_DESCRIPTOR && desc_type == HID_DESC_TYPE_REPORT) { uint8_t const * desc_report = tud_hid_descriptor_report_cb(); - usbd_control_xfer(rhport, p_request, (void*) desc_report, p_hid->reprot_desc_len); + tud_control_xfer(rhport, p_request, (void*) desc_report, p_hid->reprot_desc_len); }else { return false; // stall unsupported request @@ -225,12 +225,12 @@ bool hidd_control_request(uint8_t rhport, tusb_control_request_t const * p_reque uint16_t xferlen = tud_hid_get_report_cb(report_id, (hid_report_type_t) report_type, p_hid->epin_buf, p_request->wLength); TU_ASSERT( xferlen > 0 ); - usbd_control_xfer(rhport, p_request, p_hid->epin_buf, xferlen); + tud_control_xfer(rhport, p_request, p_hid->epin_buf, xferlen); } break; case HID_REQ_CONTROL_SET_REPORT: - usbd_control_xfer(rhport, p_request, p_hid->epout_buf, p_request->wLength); + tud_control_xfer(rhport, p_request, p_hid->epout_buf, p_request->wLength); break; case HID_REQ_CONTROL_SET_IDLE: @@ -241,18 +241,18 @@ bool hidd_control_request(uint8_t rhport, tusb_control_request_t const * p_reque if ( !tud_hid_set_idle_cb(p_hid->idle_rate) ) return false; } - usbd_control_status(rhport, p_request); + tud_control_status(rhport, p_request); break; case HID_REQ_CONTROL_GET_IDLE: // TODO idle rate of report - usbd_control_xfer(rhport, p_request, &p_hid->idle_rate, 1); + tud_control_xfer(rhport, p_request, &p_hid->idle_rate, 1); break; case HID_REQ_CONTROL_GET_PROTOCOL: { uint8_t protocol = 1-p_hid->boot_mode; // 0 is Boot, 1 is Report protocol - usbd_control_xfer(rhport, p_request, &protocol, 1); + tud_control_xfer(rhport, p_request, &protocol, 1); } break; @@ -261,7 +261,7 @@ bool hidd_control_request(uint8_t rhport, tusb_control_request_t const * p_reque if (tud_hid_boot_mode_cb) tud_hid_boot_mode_cb(p_hid->boot_mode); - usbd_control_status(rhport, p_request); + tud_control_status(rhport, p_request); break; default: return false; // stall unsupported request diff --git a/src/class/msc/msc_device.c b/src/class/msc/msc_device.c index 86e47ead0..ebc2af1d5 100644 --- a/src/class/msc/msc_device.c +++ b/src/class/msc/msc_device.c @@ -154,7 +154,7 @@ bool mscd_control_request(uint8_t rhport, tusb_control_request_t const * p_reque { case MSC_REQ_RESET: // TODO: Actually reset interface. - usbd_control_status(rhport, p_request); + tud_control_status(rhport, p_request); break; case MSC_REQ_GET_MAX_LUN: @@ -166,7 +166,7 @@ bool mscd_control_request(uint8_t rhport, tusb_control_request_t const * p_reque // MAX LUN is minus 1 by specs maxlun--; - usbd_control_xfer(rhport, p_request, &maxlun, 1); + tud_control_xfer(rhport, p_request, &maxlun, 1); } break; diff --git a/src/device/usbd.c b/src/device/usbd.c index b9d9f9d8a..6f2a72143 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -67,13 +67,13 @@ static usbd_device_t _usbd_dev = { 0 }; typedef struct { uint8_t class_code; - void (* init ) (void); - bool (* open ) (uint8_t rhport, tusb_desc_interface_t const * desc_intf, uint16_t* p_length); - bool (* control_request ) (uint8_t rhport, tusb_control_request_t const * request); + void (* init ) (void); + bool (* open ) (uint8_t rhport, tusb_desc_interface_t const * desc_intf, uint16_t* p_length); + bool (* control_request ) (uint8_t rhport, tusb_control_request_t const * request); bool (* control_complete ) (uint8_t rhport, tusb_control_request_t const * request); - bool (* xfer_cb ) (uint8_t rhport, uint8_t ep_addr, xfer_result_t, uint32_t); - void (* sof ) (uint8_t rhport); - void (* reset ) (uint8_t); + bool (* xfer_cb ) (uint8_t rhport, uint8_t ep_addr, xfer_result_t, uint32_t); + void (* sof ) (uint8_t rhport); + void (* reset ) (uint8_t); } usbd_class_driver_t; static usbd_class_driver_t const usbd_class_drivers[] = @@ -237,7 +237,6 @@ static void usbd_reset(uint8_t rhport) while(1) // the mainloop { application_code(); - tud_task(); // tinyusb device task } } @@ -348,10 +347,13 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const usbd_control_set_complete_callback(NULL); // Vendor request -// if ( p_request->bmRequestType_bit.type == TUSB_REQ_TYPE_VENDOR ) -// { -// -// } + if ( p_request->bmRequestType_bit.type == TUSB_REQ_TYPE_VENDOR ) + { + TU_VERIFY(tud_control_vendor_request_cb); + + if (tud_control_vendor_complete_cb) usbd_control_set_complete_callback(tud_control_vendor_complete_cb); + return tud_control_vendor_request_cb(rhport, p_request); + } switch ( p_request->bmRequestType_bit.recipient ) { @@ -376,7 +378,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const case TUSB_REQ_GET_CONFIGURATION: { uint8_t cfgnum = _usbd_dev.configured ? 1 : 0; - usbd_control_xfer(rhport, p_request, &cfgnum, 1); + tud_control_xfer(rhport, p_request, &cfgnum, 1); } break; @@ -388,7 +390,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const _usbd_dev.configured = cfg_num ? 1 : 0; if ( cfg_num ) TU_ASSERT( process_set_config(rhport, cfg_num) ); - usbd_control_status(rhport, p_request); + tud_control_status(rhport, p_request); } break; @@ -402,7 +404,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const // Host may enable remote wake up before suspending especially HID device _usbd_dev.remote_wakeup_en = true; - usbd_control_status(rhport, p_request); + tud_control_status(rhport, p_request); break; case TUSB_REQ_CLEAR_FEATURE: @@ -411,7 +413,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const // Host may disable remote wake up after resuming _usbd_dev.remote_wakeup_en = false; - usbd_control_status(rhport, p_request); + tud_control_status(rhport, p_request); break; case TUSB_REQ_GET_STATUS: @@ -420,7 +422,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const // - Bit 0: Self Powered // - Bit 1: Remote Wakeup enabled uint16_t status = (_usbd_dev.self_powered ? 1 : 0) | (_usbd_dev.remote_wakeup_en ? 2 : 0); - usbd_control_xfer(rhport, p_request, &status, 2); + tud_control_xfer(rhport, p_request, &status, 2); } break; @@ -437,7 +439,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const TU_VERIFY(drvid < USBD_CLASS_DRIVER_COUNT); - usbd_control_set_complete_callback(usbd_class_drivers[drvid].control_complete ); + usbd_control_set_complete_callback(usbd_class_drivers[drvid].control_complete); // stall control endpoint if driver return false return usbd_class_drivers[drvid].control_request(rhport, p_request); @@ -454,7 +456,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const case TUSB_REQ_GET_STATUS: { uint16_t status = usbd_edpt_stalled(rhport, tu_u16_low(p_request->wIndex)) ? 0x0001 : 0x0000; - usbd_control_xfer(rhport, p_request, &status, 2); + tud_control_xfer(rhport, p_request, &status, 2); } break; @@ -463,7 +465,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const { usbd_edpt_clear_stall(rhport, tu_u16_low(p_request->wIndex)); } - usbd_control_status(rhport, p_request); + tud_control_status(rhport, p_request); break; case TUSB_REQ_SET_FEATURE: @@ -471,7 +473,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const { usbd_edpt_stall(rhport, tu_u16_low(p_request->wIndex)); } - usbd_control_status(rhport, p_request); + tud_control_status(rhport, p_request); break; // Unknown/Unsupported request @@ -569,7 +571,7 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const switch(desc_type) { case TUSB_DESC_DEVICE: - return usbd_control_xfer(rhport, p_request, (void*) tud_descriptor_device_cb(), sizeof(tusb_desc_device_t)); + return tud_control_xfer(rhport, p_request, (void*) tud_descriptor_device_cb(), sizeof(tusb_desc_device_t)); break; case TUSB_DESC_BOS: @@ -581,7 +583,7 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const uint16_t total_len; memcpy(&total_len, &desc_bos->wTotalLength, 2); // possibly mis-aligned memory - return usbd_control_xfer(rhport, p_request, (void*) desc_bos, total_len); + return tud_control_xfer(rhport, p_request, (void*) desc_bos, total_len); } break; @@ -591,7 +593,7 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const uint16_t total_len; memcpy(&total_len, &desc_config->wTotalLength, 2); // possibly mis-aligned memory - return usbd_control_xfer(rhport, p_request, (void*) desc_config, total_len); + return tud_control_xfer(rhport, p_request, (void*) desc_config, total_len); } break; @@ -608,7 +610,7 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const TU_ASSERT(desc_str); // first byte of descriptor is its size - return usbd_control_xfer(rhport, p_request, (void*) desc_str, desc_str[0]); + return tud_control_xfer(rhport, p_request, (void*) desc_str, desc_str[0]); } break; diff --git a/src/device/usbd.h b/src/device/usbd.h index 943b1c79b..a77110bde 100644 --- a/src/device/usbd.h +++ b/src/device/usbd.h @@ -59,6 +59,14 @@ static inline bool tud_ready(void) // Remote wake up host, only if suspended and enabled by host bool tud_remote_wakeup(void); +// Carry out Data and Status stage of control transfer +// - If len = 0, it is equivalent to sending status only +// - If len > wLength : it will be truncated +bool tud_control_xfer(uint8_t rhport, tusb_control_request_t const * request, void* buffer, uint16_t len); + +// Send STATUS (zero length) packet +bool tud_control_status(uint8_t rhport, tusb_control_request_t const * request); + //--------------------------------------------------------------------+ // Application Callbacks (WEAK is optional) //--------------------------------------------------------------------+ @@ -92,6 +100,10 @@ TU_ATTR_WEAK void tud_suspend_cb(bool remote_wakeup_en); // Invoked when usb bus is resumed TU_ATTR_WEAK void tud_resume_cb(void); +// Invoked when received control request with VENDOR TYPE +TU_ATTR_WEAK bool tud_control_vendor_request_cb(uint8_t rhport, tusb_control_request_t const * request); +TU_ATTR_WEAK bool tud_control_vendor_complete_cb(uint8_t rhport, tusb_control_request_t const * request); + //--------------------------------------------------------------------+ // Descriptor Templates //--------------------------------------------------------------------+ diff --git a/src/device/usbd_control.c b/src/device/usbd_control.c index 49293b832..f4b9d6219 100644 --- a/src/device/usbd_control.c +++ b/src/device/usbd_control.c @@ -59,7 +59,7 @@ void usbd_control_reset (uint8_t rhport) tu_varclr(&_control_state); } -bool usbd_control_status(uint8_t rhport, tusb_control_request_t const * request) +bool tud_control_status(uint8_t rhport, tusb_control_request_t const * request) { // status direction is reversed to one in the setup packet return dcd_edpt_xfer(rhport, request->bmRequestType_bit.direction ? EDPT_CTRL_OUT : EDPT_CTRL_IN, NULL, 0); @@ -87,7 +87,7 @@ void usbd_control_set_complete_callback( bool (*fp) (uint8_t, tusb_control_reque _control_state.complete_cb = fp; } -bool usbd_control_xfer(uint8_t rhport, tusb_control_request_t const * request, void* buffer, uint16_t len) +bool tud_control_xfer(uint8_t rhport, tusb_control_request_t const * request, void* buffer, uint16_t len) { _control_state.request = (*request); _control_state.buffer = buffer; @@ -103,7 +103,7 @@ bool usbd_control_xfer(uint8_t rhport, tusb_control_request_t const * request, v }else { // Status stage - TU_ASSERT( usbd_control_status(rhport, request) ); + TU_ASSERT( tud_control_status(rhport, request) ); } return true; @@ -139,7 +139,7 @@ bool usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result if ( is_ok ) { // Send status - TU_ASSERT( usbd_control_status(rhport, &_control_state.request) ); + TU_ASSERT( tud_control_status(rhport, &_control_state.request) ); }else { // Stall both IN and OUT control endpoint diff --git a/src/device/usbd_pvt.h b/src/device/usbd_pvt.h index 706857d45..06f7a3afe 100644 --- a/src/device/usbd_pvt.h +++ b/src/device/usbd_pvt.h @@ -39,14 +39,6 @@ bool usbd_init (void); // USBD Endpoint API //--------------------------------------------------------------------+ -// Carry out Data and Status stage of control transfer -// - If len = 0, it is equivalent to sending status only -// - If len > wLength : it will be truncated -bool usbd_control_xfer(uint8_t rhport, tusb_control_request_t const * request, void* buffer, uint16_t len); - -// Send STATUS (zero length) packet -bool usbd_control_status(uint8_t rhport, tusb_control_request_t const * request); - // Submit a usb transfer bool usbd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes); From 83f7aacbb1f3335b968da02c1e3b554bb9f35c86 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 16 Jul 2019 18:43:06 +0700 Subject: [PATCH 17/36] clean up --- examples/device/webusb/src/usb_descriptors.c | 14 +++----------- src/common/tusb_types.h | 20 ++++++++++++-------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/examples/device/webusb/src/usb_descriptors.c b/examples/device/webusb/src/usb_descriptors.c index 97c05e5d0..cd9b6bc1a 100644 --- a/examples/device/webusb/src/usb_descriptors.c +++ b/examples/device/webusb/src/usb_descriptors.c @@ -180,20 +180,12 @@ uint16_t const* tud_descriptor_string_cb(uint8_t index) //--------------------------------------------------------------------+ #define URL "github.com/hathach/tinyusb" -typedef struct TU_ATTR_PACKED -{ - uint8_t bLength; - uint8_t bDescriptorType; - uint8_t bScheme; - char url[]; -} tusb_desc_webusb_url_t; - const tusb_desc_webusb_url_t desc_url = { - .bLength = 3 + sizeof(URL) - 1, + .bLength = 3 + sizeof(URL) - 1, .bDescriptorType = 3, // WEBUSB URL type - .bScheme = 1, // 0: http, 1: https - .url = URL + .bScheme = 1, // 0: http, 1: https + .url = URL }; bool tud_control_vendor_request_cb(uint8_t rhport, tusb_control_request_t const * request) diff --git a/src/common/tusb_types.h b/src/common/tusb_types.h index 7270ec58b..2776eaa87 100644 --- a/src/common/tusb_types.h +++ b/src/common/tusb_types.h @@ -228,7 +228,7 @@ enum }; //--------------------------------------------------------------------+ -// USB Standard Descriptors +// USB Descriptors //--------------------------------------------------------------------+ /// USB Device Descriptor @@ -360,13 +360,7 @@ typedef struct TU_ATTR_PACKED uint8_t iFunction ; ///< Index of the string descriptor describing the interface association. } tusb_desc_interface_assoc_t; -/// USB Header Descriptor -typedef struct TU_ATTR_PACKED -{ - uint8_t bLength ; ///< Size of this descriptor in bytes - uint8_t bDescriptorType ; ///< Descriptor Type -} tusb_desc_header_t; - +// USB String Descriptor typedef struct TU_ATTR_PACKED { uint8_t bLength ; ///< Size of this descriptor in bytes @@ -374,6 +368,7 @@ typedef struct TU_ATTR_PACKED uint16_t unicode_string[]; } tusb_desc_string_t; +// USB Binary Device Object Store (BOS) typedef struct TU_ATTR_PACKED { uint8_t bLength; @@ -384,6 +379,15 @@ typedef struct TU_ATTR_PACKED uint8_t CapabilityData[]; } tusb_desc_bos_platform_t; +// USB WebuSB URL Descriptor +typedef struct TU_ATTR_PACKED +{ + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bScheme; + char url[]; +} tusb_desc_webusb_url_t; + /*------------------------------------------------------------------*/ /* Types *------------------------------------------------------------------*/ From b0678e1050a8f64116194c2a75d5e8c3fbf03be6 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 19 Jul 2019 16:23:56 +0700 Subject: [PATCH 18/36] rename CFG_TUD_CUSTOM_CLASS to CFG_TUD_VENDOR --- examples/device/cdc_msc_hid/src/tusb_config.h | 20 +++++++++---------- .../cdc_msc_hid_freertos/src/tusb_config.h | 20 +++++++++---------- .../hid_generic_inout/src/tusb_config.h | 14 ++++++------- examples/device/midi_test/src/tusb_config.h | 16 +++++++-------- .../device/msc_dual_lun/src/tusb_config.h | 14 ++++++------- examples/device/webusb/src/tusb_config.h | 16 +++++++-------- examples/host/cdc_msc_hid/src/tusb_config.h | 2 +- src/class/custom/custom_device.c | 2 +- src/device/usbd.c | 2 +- src/tusb.h | 2 +- src/tusb_option.h | 4 ++-- test/test/support/tusb_config.h | 2 +- 12 files changed, 57 insertions(+), 57 deletions(-) diff --git a/examples/device/cdc_msc_hid/src/tusb_config.h b/examples/device/cdc_msc_hid/src/tusb_config.h index 31ae2b883..1c0ee349c 100644 --- a/examples/device/cdc_msc_hid/src/tusb_config.h +++ b/examples/device/cdc_msc_hid/src/tusb_config.h @@ -69,25 +69,25 @@ // DEVICE CONFIGURATION //-------------------------------------------------------------------- -#define CFG_TUD_ENDOINT0_SIZE 64 +#define CFG_TUD_ENDOINT0_SIZE 64 //------------- CLASS -------------// -#define CFG_TUD_CDC 1 -#define CFG_TUD_MSC 1 -#define CFG_TUD_HID 1 +#define CFG_TUD_CDC 1 +#define CFG_TUD_MSC 1 +#define CFG_TUD_HID 1 -#define CFG_TUD_MIDI 0 -#define CFG_TUD_CUSTOM_CLASS 0 +#define CFG_TUD_MIDI 0 +#define CFG_TUD_VENDOR 0 // CDC FIFO size of TX and RX -#define CFG_TUD_CDC_RX_BUFSIZE 64 -#define CFG_TUD_CDC_TX_BUFSIZE 64 +#define CFG_TUD_CDC_RX_BUFSIZE 64 +#define CFG_TUD_CDC_TX_BUFSIZE 64 // MSC Buffer size of Device Mass storage -#define CFG_TUD_MSC_BUFSIZE 512 +#define CFG_TUD_MSC_BUFSIZE 512 // HID buffer size Should be sufficient to hold ID (if any) + Data -#define CFG_TUD_HID_BUFSIZE 16 +#define CFG_TUD_HID_BUFSIZE 16 #ifdef __cplusplus } diff --git a/examples/device/cdc_msc_hid_freertos/src/tusb_config.h b/examples/device/cdc_msc_hid_freertos/src/tusb_config.h index 2a250ac83..bb31eac06 100644 --- a/examples/device/cdc_msc_hid_freertos/src/tusb_config.h +++ b/examples/device/cdc_msc_hid_freertos/src/tusb_config.h @@ -69,25 +69,25 @@ // DEVICE CONFIGURATION //-------------------------------------------------------------------- -#define CFG_TUD_ENDOINT0_SIZE 64 +#define CFG_TUD_ENDOINT0_SIZE 64 //------------- CLASS -------------// -#define CFG_TUD_CDC 1 -#define CFG_TUD_MSC 1 -#define CFG_TUD_HID 1 +#define CFG_TUD_CDC 1 +#define CFG_TUD_MSC 1 +#define CFG_TUD_HID 1 -#define CFG_TUD_MIDI 0 -#define CFG_TUD_CUSTOM_CLASS 0 +#define CFG_TUD_MIDI 0 +#define CFG_TUD_VENDOR 0 // CDC FIFO size of TX and RX -#define CFG_TUD_CDC_RX_BUFSIZE 64 -#define CFG_TUD_CDC_TX_BUFSIZE 64 +#define CFG_TUD_CDC_RX_BUFSIZE 64 +#define CFG_TUD_CDC_TX_BUFSIZE 64 // MSC Buffer size of Device Mass storage -#define CFG_TUD_MSC_BUFSIZE 512 +#define CFG_TUD_MSC_BUFSIZE 512 // HID buffer size Should be sufficient to hold ID (if any) + Data -#define CFG_TUD_HID_BUFSIZE 16 +#define CFG_TUD_HID_BUFSIZE 16 #ifdef __cplusplus } diff --git a/examples/device/hid_generic_inout/src/tusb_config.h b/examples/device/hid_generic_inout/src/tusb_config.h index d97cd533e..c55529fcb 100644 --- a/examples/device/hid_generic_inout/src/tusb_config.h +++ b/examples/device/hid_generic_inout/src/tusb_config.h @@ -69,17 +69,17 @@ // DEVICE CONFIGURATION //-------------------------------------------------------------------- -#define CFG_TUD_ENDOINT0_SIZE 64 +#define CFG_TUD_ENDOINT0_SIZE 64 //------------- CLASS -------------// -#define CFG_TUD_CDC 0 -#define CFG_TUD_MSC 0 -#define CFG_TUD_HID 1 -#define CFG_TUD_MIDI 0 -#define CFG_TUD_CUSTOM_CLASS 0 +#define CFG_TUD_CDC 0 +#define CFG_TUD_MSC 0 +#define CFG_TUD_HID 1 +#define CFG_TUD_MIDI 0 +#define CFG_TUD_VENDOR 0 // HID buffer size Should be sufficient to hold ID (if any) + Data -#define CFG_TUD_HID_BUFSIZE 64 +#define CFG_TUD_HID_BUFSIZE 64 #ifdef __cplusplus } diff --git a/examples/device/midi_test/src/tusb_config.h b/examples/device/midi_test/src/tusb_config.h index a6e3ad710..d588b4df9 100644 --- a/examples/device/midi_test/src/tusb_config.h +++ b/examples/device/midi_test/src/tusb_config.h @@ -69,18 +69,18 @@ // DEVICE CONFIGURATION //-------------------------------------------------------------------- -#define CFG_TUD_ENDOINT0_SIZE 64 +#define CFG_TUD_ENDOINT0_SIZE 64 //------------- CLASS -------------// -#define CFG_TUD_CDC 0 -#define CFG_TUD_MSC 0 -#define CFG_TUD_HID 0 -#define CFG_TUD_MIDI 1 -#define CFG_TUD_CUSTOM_CLASS 0 +#define CFG_TUD_CDC 0 +#define CFG_TUD_MSC 0 +#define CFG_TUD_HID 0 +#define CFG_TUD_MIDI 1 +#define CFG_TUD_VENDOR 0 // MIDI FIFO size of TX and RX -#define CFG_TUD_MIDI_RX_BUFSIZE 64 -#define CFG_TUD_MIDI_TX_BUFSIZE 64 +#define CFG_TUD_MIDI_RX_BUFSIZE 64 +#define CFG_TUD_MIDI_TX_BUFSIZE 64 #ifdef __cplusplus } diff --git a/examples/device/msc_dual_lun/src/tusb_config.h b/examples/device/msc_dual_lun/src/tusb_config.h index 3b87d9f21..2e40b9e86 100644 --- a/examples/device/msc_dual_lun/src/tusb_config.h +++ b/examples/device/msc_dual_lun/src/tusb_config.h @@ -69,17 +69,17 @@ // DEVICE CONFIGURATION //-------------------------------------------------------------------- -#define CFG_TUD_ENDOINT0_SIZE 64 +#define CFG_TUD_ENDOINT0_SIZE 64 //------------- CLASS -------------// -#define CFG_TUD_CDC 0 -#define CFG_TUD_MSC 1 -#define CFG_TUD_HID 0 -#define CFG_TUD_MIDI 0 -#define CFG_TUD_CUSTOM_CLASS 0 +#define CFG_TUD_CDC 0 +#define CFG_TUD_MSC 1 +#define CFG_TUD_HID 0 +#define CFG_TUD_MIDI 0 +#define CFG_TUD_VENDOR 0 // MSC Buffer size of Device Mass storage -#define CFG_TUD_MSC_BUFSIZE 512 +#define CFG_TUD_MSC_BUFSIZE 512 #ifdef __cplusplus } diff --git a/examples/device/webusb/src/tusb_config.h b/examples/device/webusb/src/tusb_config.h index 6c126a82f..8aefe8695 100644 --- a/examples/device/webusb/src/tusb_config.h +++ b/examples/device/webusb/src/tusb_config.h @@ -69,18 +69,18 @@ // DEVICE CONFIGURATION //-------------------------------------------------------------------- -#define CFG_TUD_ENDOINT0_SIZE 64 +#define CFG_TUD_ENDOINT0_SIZE 64 //------------- CLASS -------------// -#define CFG_TUD_CDC 1 -#define CFG_TUD_MSC 0 -#define CFG_TUD_HID 0 -#define CFG_TUD_MIDI 0 -#define CFG_TUD_CUSTOM_CLASS 0 +#define CFG_TUD_CDC 1 +#define CFG_TUD_MSC 0 +#define CFG_TUD_HID 0 +#define CFG_TUD_MIDI 0 +#define CFG_TUD_VENDOR 0 // CDC FIFO size of TX and RX -#define CFG_TUD_CDC_RX_BUFSIZE 64 -#define CFG_TUD_CDC_TX_BUFSIZE 64 +#define CFG_TUD_CDC_RX_BUFSIZE 64 +#define CFG_TUD_CDC_TX_BUFSIZE 64 #ifdef __cplusplus } diff --git a/examples/host/cdc_msc_hid/src/tusb_config.h b/examples/host/cdc_msc_hid/src/tusb_config.h index 1a9c8fa5c..56208e771 100644 --- a/examples/host/cdc_msc_hid/src/tusb_config.h +++ b/examples/host/cdc_msc_hid/src/tusb_config.h @@ -82,7 +82,7 @@ //------------- CLASS -------------// #define CFG_TUD_CDC 0 #define CFG_TUD_MSC 0 -#define CFG_TUD_CUSTOM_CLASS 0 +#define CFG_TUD_VENDOR 0 #define CFG_TUD_HID 0 #define CFG_TUD_HID_KEYBOARD 0 diff --git a/src/class/custom/custom_device.c b/src/class/custom/custom_device.c index 02a711621..cf9aa3b3f 100644 --- a/src/class/custom/custom_device.c +++ b/src/class/custom/custom_device.c @@ -26,7 +26,7 @@ #include "tusb_option.h" -#if (TUSB_OPT_DEVICE_ENABLED && CFG_TUD_CUSTOM_CLASS) +#if (TUSB_OPT_DEVICE_ENABLED && CFG_TUD_VENDOR) #include "common/tusb_common.h" #include "custom_device.h" diff --git a/src/device/usbd.c b/src/device/usbd.c index 6f2a72143..54fd070b8 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -130,7 +130,7 @@ static usbd_class_driver_t const usbd_class_drivers[] = }, #endif - #if CFG_TUD_CUSTOM_CLASS + #if CFG_TUD_VENDOR { .class_code = TUSB_CLASS_VENDOR_SPECIFIC, .init = cusd_init, diff --git a/src/tusb.h b/src/tusb.h index 30642c00d..6072218de 100644 --- a/src/tusb.h +++ b/src/tusb.h @@ -80,7 +80,7 @@ #include "class/midi/midi_device.h" #endif - #if CFG_TUD_CUSTOM_CLASS + #if CFG_TUD_VENDOR #include "class/custom/custom_device.h" #endif #endif diff --git a/src/tusb_option.h b/src/tusb_option.h index da9bc70fa..a1c5b757d 100644 --- a/src/tusb_option.h +++ b/src/tusb_option.h @@ -161,8 +161,8 @@ #define CFG_TUD_MIDI 0 #endif -#ifndef CFG_TUD_CUSTOM_CLASS - #define CFG_TUD_CUSTOM_CLASS 0 +#ifndef CFG_TUD_VENDOR + #define CFG_TUD_VENDOR 0 #endif diff --git a/test/test/support/tusb_config.h b/test/test/support/tusb_config.h index d49576850..eaa63b958 100644 --- a/test/test/support/tusb_config.h +++ b/test/test/support/tusb_config.h @@ -78,7 +78,7 @@ #define CFG_TUD_HID 1 #define CFG_TUD_MIDI 1 -#define CFG_TUD_CUSTOM_CLASS 0 +#define CFG_TUD_VENDOR 0 //------------- CDC -------------// From 6a076d8aae25a21a2592f654aa7b86e3e6d072a4 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 19 Jul 2019 16:31:51 +0700 Subject: [PATCH 19/36] rename CFG_TUSB_HOST_CUSTOM_CLASS to CFG_TUH_VENDOR --- examples/host/cdc_msc_hid/src/tusb_config.h | 22 ++++----------------- src/class/custom/custom_host.c | 2 +- src/host/usbh.c | 2 +- src/tusb.h | 2 +- 4 files changed, 7 insertions(+), 21 deletions(-) diff --git a/examples/host/cdc_msc_hid/src/tusb_config.h b/examples/host/cdc_msc_hid/src/tusb_config.h index 56208e771..7a18c692b 100644 --- a/examples/host/cdc_msc_hid/src/tusb_config.h +++ b/examples/host/cdc_msc_hid/src/tusb_config.h @@ -67,7 +67,7 @@ #endif //-------------------------------------------------------------------- -// DEVICE CONFIGURATION +// CONFIGURATION //-------------------------------------------------------------------- #define CFG_TUH_HUB 1 @@ -82,30 +82,16 @@ //------------- CLASS -------------// #define CFG_TUD_CDC 0 #define CFG_TUD_MSC 0 -#define CFG_TUD_VENDOR 0 - #define CFG_TUD_HID 0 -#define CFG_TUD_HID_KEYBOARD 0 -#define CFG_TUD_HID_MOUSE 0 +#define CFG_TUD_VENDOR 0 -//-------------------------------------------------------------------- -// CDC -//-------------------------------------------------------------------- - -// FIFO size of CDC TX and RX +// CDC FIFO size of TX and RX #define CFG_TUD_CDC_RX_BUFSIZE 64 #define CFG_TUD_CDC_TX_BUFSIZE 64 -//-------------------------------------------------------------------- -// MSC -//-------------------------------------------------------------------- - -// Buffer size of Device Mass storage +// MSC Buffer size of Device Mass storage #define CFG_TUD_MSC_BUFSIZE 512 -//-------------------------------------------------------------------- -// HID -//-------------------------------------------------------------------- #ifdef __cplusplus } diff --git a/src/class/custom/custom_host.c b/src/class/custom/custom_host.c index a4f04af31..ebe257e61 100644 --- a/src/class/custom/custom_host.c +++ b/src/class/custom/custom_host.c @@ -26,7 +26,7 @@ #include "tusb_option.h" -#if (TUSB_OPT_HOST_ENABLED && CFG_TUSB_HOST_CUSTOM_CLASS) +#if (TUSB_OPT_HOST_ENABLED && CFG_TUH_VENDOR) //--------------------------------------------------------------------+ // INCLUDE diff --git a/src/host/usbh.c b/src/host/usbh.c index 655e3dd4b..42a99f3d2 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -84,7 +84,7 @@ static host_class_driver_t const usbh_class_drivers[] = }, #endif - #if CFG_TUSB_HOST_CUSTOM_CLASS + #if CFG_TUH_VENDOR { .class_code = TUSB_CLASS_VENDOR_SPECIFIC, .init = cush_init, diff --git a/src/tusb.h b/src/tusb.h index 6072218de..3f6673ae1 100644 --- a/src/tusb.h +++ b/src/tusb.h @@ -54,7 +54,7 @@ #include "class/cdc/cdc_host.h" #endif - #if CFG_TUSB_HOST_CUSTOM_CLASS + #if CFG_TUH_VENDOR #include "class/custom_host.h" #endif From a3cb2dda9e96a195a71de371fb9c6ecc85beaec4 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 19 Jul 2019 16:37:28 +0700 Subject: [PATCH 20/36] rename custom/custom-_* to vendor/vendor_* --- src/class/{custom/custom_device.c => vendor/vendor_device.c} | 2 +- src/class/{custom/custom_device.h => vendor/vendor_device.h} | 0 src/class/{custom/custom_host.c => vendor/vendor_host.c} | 2 +- src/class/{custom/custom_host.h => vendor/vendor_host.h} | 0 src/tusb.h | 4 ++-- 5 files changed, 4 insertions(+), 4 deletions(-) rename src/class/{custom/custom_device.c => vendor/vendor_device.c} (99%) rename src/class/{custom/custom_device.h => vendor/vendor_device.h} (100%) rename src/class/{custom/custom_host.c => vendor/vendor_host.c} (99%) rename src/class/{custom/custom_host.h => vendor/vendor_host.h} (100%) diff --git a/src/class/custom/custom_device.c b/src/class/vendor/vendor_device.c similarity index 99% rename from src/class/custom/custom_device.c rename to src/class/vendor/vendor_device.c index cf9aa3b3f..2f7bba16a 100644 --- a/src/class/custom/custom_device.c +++ b/src/class/vendor/vendor_device.c @@ -29,7 +29,7 @@ #if (TUSB_OPT_DEVICE_ENABLED && CFG_TUD_VENDOR) #include "common/tusb_common.h" -#include "custom_device.h" +#include "vendor_device.h" #include "device/usbd_pvt.h" /*------------------------------------------------------------------*/ diff --git a/src/class/custom/custom_device.h b/src/class/vendor/vendor_device.h similarity index 100% rename from src/class/custom/custom_device.h rename to src/class/vendor/vendor_device.h diff --git a/src/class/custom/custom_host.c b/src/class/vendor/vendor_host.c similarity index 99% rename from src/class/custom/custom_host.c rename to src/class/vendor/vendor_host.c index ebe257e61..3e8dac0f6 100644 --- a/src/class/custom/custom_host.c +++ b/src/class/vendor/vendor_host.c @@ -32,7 +32,7 @@ // INCLUDE //--------------------------------------------------------------------+ #include "common/tusb_common.h" -#include "custom_host.h" +#include "vendor_host.h" //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF diff --git a/src/class/custom/custom_host.h b/src/class/vendor/vendor_host.h similarity index 100% rename from src/class/custom/custom_host.h rename to src/class/vendor/vendor_host.h diff --git a/src/tusb.h b/src/tusb.h index 3f6673ae1..fe8673726 100644 --- a/src/tusb.h +++ b/src/tusb.h @@ -55,7 +55,7 @@ #endif #if CFG_TUH_VENDOR - #include "class/custom_host.h" + #include "class/vendor/vendor_host.h" #endif #endif @@ -81,7 +81,7 @@ #endif #if CFG_TUD_VENDOR - #include "class/custom/custom_device.h" + #include "class/vendor/vendor_device.h" #endif #endif From ae5dc435cbb5afb1fd16d37feaf132085ccb787f Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 19 Jul 2019 16:49:04 +0700 Subject: [PATCH 21/36] house keeping --- src/device/usbd.c | 118 +++++++++++++++++++++++----------------------- 1 file changed, 59 insertions(+), 59 deletions(-) diff --git a/src/device/usbd.c b/src/device/usbd.c index 54fd070b8..08eee7506 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -43,20 +43,20 @@ typedef struct { struct TU_ATTR_PACKED { - volatile uint8_t connected : 1; - volatile uint8_t configured : 1; - volatile uint8_t suspended : 1; + volatile uint8_t connected : 1; + volatile uint8_t configured : 1; + volatile uint8_t suspended : 1; - uint8_t remote_wakeup_en : 1; // enable/disable by host - uint8_t remote_wakeup_support : 1; // configuration descriptor's attribute - uint8_t self_powered : 1; // configuration descriptor's attribute + uint8_t remote_wakeup_en : 1; // enable/disable by host + uint8_t remote_wakeup_support : 1; // configuration descriptor's attribute + uint8_t self_powered : 1; // configuration descriptor's attribute }; uint8_t ep_busy_map[2]; // bit mask for busy endpoint uint8_t ep_stall_map[2]; // bit map for stalled endpoint - uint8_t itf2drv[16]; // map interface number to driver (0xff is invalid) - uint8_t ep2drv[8][2]; // map endpoint to driver ( 0xff is invalid ) + uint8_t itf2drv[16]; // map interface number to driver (0xff is invalid) + uint8_t ep2drv[8][2]; // map endpoint to driver ( 0xff is invalid ) }usbd_device_t; static usbd_device_t _usbd_dev = { 0 }; @@ -71,7 +71,7 @@ typedef struct { bool (* open ) (uint8_t rhport, tusb_desc_interface_t const * desc_intf, uint16_t* p_length); bool (* control_request ) (uint8_t rhport, tusb_control_request_t const * request); bool (* control_complete ) (uint8_t rhport, tusb_control_request_t const * request); - bool (* xfer_cb ) (uint8_t rhport, uint8_t ep_addr, xfer_result_t, uint32_t); + bool (* xfer_cb ) (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); void (* sof ) (uint8_t rhport); void (* reset ) (uint8_t); } usbd_class_driver_t; @@ -79,68 +79,68 @@ typedef struct { static usbd_class_driver_t const usbd_class_drivers[] = { #if CFG_TUD_CDC - { - .class_code = TUSB_CLASS_CDC, - .init = cdcd_init, - .open = cdcd_open, - .control_request = cdcd_control_request, - .control_complete = cdcd_control_complete, - .xfer_cb = cdcd_xfer_cb, - .sof = NULL, - .reset = cdcd_reset - }, + { + .class_code = TUSB_CLASS_CDC, + .init = cdcd_init, + .open = cdcd_open, + .control_request = cdcd_control_request, + .control_complete = cdcd_control_complete, + .xfer_cb = cdcd_xfer_cb, + .sof = NULL, + .reset = cdcd_reset + }, #endif #if CFG_TUD_MSC - { - .class_code = TUSB_CLASS_MSC, - .init = mscd_init, - .open = mscd_open, - .control_request = mscd_control_request, - .control_complete = mscd_control_complete, - .xfer_cb = mscd_xfer_cb, - .sof = NULL, - .reset = mscd_reset - }, + { + .class_code = TUSB_CLASS_MSC, + .init = mscd_init, + .open = mscd_open, + .control_request = mscd_control_request, + .control_complete = mscd_control_complete, + .xfer_cb = mscd_xfer_cb, + .sof = NULL, + .reset = mscd_reset + }, #endif #if CFG_TUD_HID - { - .class_code = TUSB_CLASS_HID, - .init = hidd_init, - .open = hidd_open, - .control_request = hidd_control_request, - .control_complete = hidd_control_complete, - .xfer_cb = hidd_xfer_cb, - .sof = NULL, - .reset = hidd_reset - }, + { + .class_code = TUSB_CLASS_HID, + .init = hidd_init, + .open = hidd_open, + .control_request = hidd_control_request, + .control_complete = hidd_control_complete, + .xfer_cb = hidd_xfer_cb, + .sof = NULL, + .reset = hidd_reset + }, #endif #if CFG_TUD_MIDI - { - .class_code = TUSB_CLASS_AUDIO, - .init = midid_init, - .open = midid_open, - .control_request = midid_control_request, - .control_complete = midid_control_complete, - .xfer_cb = midid_xfer_cb, - .sof = NULL, - .reset = midid_reset - }, + { + .class_code = TUSB_CLASS_AUDIO, + .init = midid_init, + .open = midid_open, + .control_request = midid_control_request, + .control_complete = midid_control_complete, + .xfer_cb = midid_xfer_cb, + .sof = NULL, + .reset = midid_reset + }, #endif #if CFG_TUD_VENDOR - { - .class_code = TUSB_CLASS_VENDOR_SPECIFIC, - .init = cusd_init, - .open = cusd_open, - .control_request = cusd_control_request, - .control_complete = cusd_control_complete, - .xfer_cb = cusd_xfer_cb, - .sof = NULL, - .reset = cusd_reset - }, + { + .class_code = TUSB_CLASS_VENDOR_SPECIFIC, + .init = cusd_init, + .open = cusd_open, + .control_request = cusd_control_request, + .control_complete = cusd_control_complete, + .xfer_cb = cusd_xfer_cb, + .sof = NULL, + .reset = cusd_reset + }, #endif }; From 33de35504abf27801286b1f3760862d3909e2ae6 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 19 Jul 2019 20:20:13 +0700 Subject: [PATCH 22/36] more house keeping --- src/class/cdc/cdc_device.h | 2 +- src/class/hid/hid_device.h | 2 +- src/class/midi/midi_device.h | 2 +- src/class/msc/msc_device.h | 2 +- src/class/vendor/vendor_device.c | 29 +++++++++++++---------------- src/class/vendor/vendor_device.h | 15 +++++++++------ src/device/usbd.c | 32 ++++++++++++++++---------------- test/test/support/tusb_config.h | 20 ++++++++++---------- 8 files changed, 52 insertions(+), 52 deletions(-) diff --git a/src/class/cdc/cdc_device.h b/src/class/cdc/cdc_device.h index 76bb7c622..f1ac08e73 100644 --- a/src/class/cdc/cdc_device.h +++ b/src/class/cdc/cdc_device.h @@ -177,11 +177,11 @@ static inline bool tud_cdc_write_flush (void) // INTERNAL USBD-CLASS DRIVER API //--------------------------------------------------------------------+ void cdcd_init (void); +void cdcd_reset (uint8_t rhport); bool cdcd_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length); bool cdcd_control_request (uint8_t rhport, tusb_control_request_t const * request); bool cdcd_control_complete (uint8_t rhport, tusb_control_request_t const * request); bool cdcd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes); -void cdcd_reset (uint8_t rhport); #ifdef __cplusplus } diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h index 49512ac4e..577b9ca8e 100644 --- a/src/class/hid/hid_device.h +++ b/src/class/hid/hid_device.h @@ -301,11 +301,11 @@ TU_ATTR_WEAK bool tud_hid_set_idle_cb(uint8_t idle_rate); // Internal Class Driver API //--------------------------------------------------------------------+ void hidd_init (void); +void hidd_reset (uint8_t rhport); bool hidd_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length); bool hidd_control_request (uint8_t rhport, tusb_control_request_t const * request); bool hidd_control_complete (uint8_t rhport, tusb_control_request_t const * request); bool hidd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); -void hidd_reset (uint8_t rhport); #ifdef __cplusplus } diff --git a/src/class/midi/midi_device.h b/src/class/midi/midi_device.h index f9d13ffb6..6d9b292a3 100644 --- a/src/class/midi/midi_device.h +++ b/src/class/midi/midi_device.h @@ -122,11 +122,11 @@ static inline uint32_t tudi_midi_write24 (uint8_t jack_id, uint8_t b1, uint8_t b // Internal Class Driver API //--------------------------------------------------------------------+ void midid_init (void); +void midid_reset (uint8_t rhport); bool midid_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length); bool midid_control_request (uint8_t rhport, tusb_control_request_t const * request); bool midid_control_complete (uint8_t rhport, tusb_control_request_t const * request); bool midid_xfer_cb (uint8_t rhport, uint8_t edpt_addr, xfer_result_t result, uint32_t xferred_bytes); -void midid_reset (uint8_t rhport); #ifdef __cplusplus } diff --git a/src/class/msc/msc_device.h b/src/class/msc/msc_device.h index 5998400b1..72f208ad5 100644 --- a/src/class/msc/msc_device.h +++ b/src/class/msc/msc_device.h @@ -152,11 +152,11 @@ TU_ATTR_WEAK bool tud_msc_is_writable_cb(uint8_t lun); // Internal Class Driver API //--------------------------------------------------------------------+ void mscd_init (void); +void mscd_reset (uint8_t rhport); bool mscd_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length); bool mscd_control_request (uint8_t rhport, tusb_control_request_t const * p_request); bool mscd_control_complete (uint8_t rhport, tusb_control_request_t const * p_request); bool mscd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); -void mscd_reset (uint8_t rhport); #ifdef __cplusplus } diff --git a/src/class/vendor/vendor_device.c b/src/class/vendor/vendor_device.c index 2f7bba16a..e1a0c2739 100644 --- a/src/class/vendor/vendor_device.c +++ b/src/class/vendor/vendor_device.c @@ -45,21 +45,27 @@ typedef struct { uint8_t ep_in; uint8_t ep_out; -} cusd_interface_t; +} vendord_interface_t; -static cusd_interface_t _cusd_itf; +static vendord_interface_t _vendord_itf; /*------------------------------------------------------------------*/ /* FUNCTION DECLARATION *------------------------------------------------------------------*/ -void cusd_init(void) +void vendord_init(void) { - tu_varclr(&_cusd_itf); + tu_varclr(&_vendord_itf); } -bool cusd_open(uint8_t rhport, tusb_desc_interface_t const * p_desc_itf, uint16_t *p_len) +void vendord_reset(uint8_t rhport) { - cusd_interface_t* p_itf = &_cusd_itf; + +} + + +bool vendord_open(uint8_t rhport, tusb_desc_interface_t const * p_desc_itf, uint16_t *p_len) +{ + vendord_interface_t* p_itf = &_vendord_itf; // Open endpoint pair with usbd helper tusb_desc_endpoint_t const *p_desc_ep = (tusb_desc_endpoint_t const *) tu_desc_next(p_desc_itf); @@ -75,19 +81,10 @@ bool cusd_open(uint8_t rhport, tusb_desc_interface_t const * p_desc_itf, uint16_ return true; } -bool cusd_control_request(uint8_t rhport, tusb_control_request_t const * p_request) -{ - return false; -} - -bool cusd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes) +bool vendord_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes) { return true; } -void cusd_reset(uint8_t rhport) -{ - -} #endif diff --git a/src/class/vendor/vendor_device.h b/src/class/vendor/vendor_device.h index c4001a89f..0c9d0184f 100644 --- a/src/class/vendor/vendor_device.h +++ b/src/class/vendor/vendor_device.h @@ -40,6 +40,11 @@ // Should be used with MCU supporting only 1 USB port for code simplicity //--------------------------------------------------------------------+ +bool tud_vendor_mounted(void); + +uint32_t tud_vendor_write(void const* buffer, uint32_t bufsize); +uint32_t tud_vendor_read(void* buffer, uint32_t bufsize); + //--------------------------------------------------------------------+ // APPLICATION CALLBACK API (WEAK is optional) @@ -48,11 +53,9 @@ //--------------------------------------------------------------------+ // Internal Class Driver API //--------------------------------------------------------------------+ -void cusd_init(void); -bool cusd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length); -bool cusd_control_request_st(uint8_t rhport, tusb_control_request_t const * p_request); -bool cusd_control_complete (uint8_t rhport, tusb_control_request_t const * p_request); -bool cusd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); -void cusd_reset(uint8_t rhport); +void vendord_init(void); +void vendord_reset(uint8_t rhport); +bool vendord_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length); +bool vendord_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); #endif /* _TUSB_CUSTOM_DEVICE_H_ */ diff --git a/src/device/usbd.c b/src/device/usbd.c index 08eee7506..a24113026 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -68,12 +68,12 @@ typedef struct { uint8_t class_code; void (* init ) (void); + void (* reset ) (uint8_t rhport); bool (* open ) (uint8_t rhport, tusb_desc_interface_t const * desc_intf, uint16_t* p_length); bool (* control_request ) (uint8_t rhport, tusb_control_request_t const * request); bool (* control_complete ) (uint8_t rhport, tusb_control_request_t const * request); bool (* xfer_cb ) (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); void (* sof ) (uint8_t rhport); - void (* reset ) (uint8_t); } usbd_class_driver_t; static usbd_class_driver_t const usbd_class_drivers[] = @@ -82,12 +82,12 @@ static usbd_class_driver_t const usbd_class_drivers[] = { .class_code = TUSB_CLASS_CDC, .init = cdcd_init, + .reset = cdcd_reset, .open = cdcd_open, .control_request = cdcd_control_request, .control_complete = cdcd_control_complete, .xfer_cb = cdcd_xfer_cb, - .sof = NULL, - .reset = cdcd_reset + .sof = NULL }, #endif @@ -95,12 +95,12 @@ static usbd_class_driver_t const usbd_class_drivers[] = { .class_code = TUSB_CLASS_MSC, .init = mscd_init, + .reset = mscd_reset, .open = mscd_open, .control_request = mscd_control_request, .control_complete = mscd_control_complete, .xfer_cb = mscd_xfer_cb, - .sof = NULL, - .reset = mscd_reset + .sof = NULL }, #endif @@ -108,12 +108,12 @@ static usbd_class_driver_t const usbd_class_drivers[] = { .class_code = TUSB_CLASS_HID, .init = hidd_init, + .reset = hidd_reset, .open = hidd_open, .control_request = hidd_control_request, .control_complete = hidd_control_complete, .xfer_cb = hidd_xfer_cb, - .sof = NULL, - .reset = hidd_reset + .sof = NULL }, #endif @@ -122,24 +122,24 @@ static usbd_class_driver_t const usbd_class_drivers[] = .class_code = TUSB_CLASS_AUDIO, .init = midid_init, .open = midid_open, + .reset = midid_reset, .control_request = midid_control_request, .control_complete = midid_control_complete, .xfer_cb = midid_xfer_cb, - .sof = NULL, - .reset = midid_reset + .sof = NULL }, #endif #if CFG_TUD_VENDOR { .class_code = TUSB_CLASS_VENDOR_SPECIFIC, - .init = cusd_init, - .open = cusd_open, - .control_request = cusd_control_request, - .control_complete = cusd_control_complete, - .xfer_cb = cusd_xfer_cb, - .sof = NULL, - .reset = cusd_reset + .init = vendord_init, + .reset = vendord_reset, + .open = vendord_open, + .control_request = NULL, // tud_control_vendor_request_cb + .control_complete = NULL, // tud_control_vendor_complete_cb + .xfer_cb = vendord_xfer_cb, + .sof = NULL }, #endif }; diff --git a/test/test/support/tusb_config.h b/test/test/support/tusb_config.h index eaa63b958..b82a09a23 100644 --- a/test/test/support/tusb_config.h +++ b/test/test/support/tusb_config.h @@ -70,31 +70,31 @@ // DEVICE CONFIGURATION //-------------------------------------------------------------------- -#define CFG_TUD_ENDOINT0_SIZE 64 +#define CFG_TUD_ENDOINT0_SIZE 64 //------------- CLASS -------------// -#define CFG_TUD_CDC 1 -#define CFG_TUD_MSC 1 -#define CFG_TUD_HID 1 +#define CFG_TUD_CDC 1 +#define CFG_TUD_MSC 1 +#define CFG_TUD_HID 1 -#define CFG_TUD_MIDI 1 -#define CFG_TUD_VENDOR 0 +#define CFG_TUD_MIDI 1 +#define CFG_TUD_VENDOR 1 //------------- CDC -------------// // FIFO size of CDC TX and RX -#define CFG_TUD_CDC_RX_BUFSIZE 64 -#define CFG_TUD_CDC_TX_BUFSIZE 64 +#define CFG_TUD_CDC_RX_BUFSIZE 64 +#define CFG_TUD_CDC_TX_BUFSIZE 64 //------------- MSC -------------// // Buffer size of Device Mass storage -#define CFG_TUD_MSC_BUFSIZE 512 +#define CFG_TUD_MSC_BUFSIZE 512 //------------- HID -------------// // Should be sufficient to hold ID (if any) + Data -#define CFG_TUD_HID_BUFSIZE 16 +#define CFG_TUD_HID_BUFSIZE 16 #ifdef __cplusplus } From c63f2b30cbad128d5b46820965f0734be9879cb1 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 24 Jul 2019 09:45:32 +0700 Subject: [PATCH 23/36] rename tud_control_vendor_request_cb/complete_cb to tud_vendor_control_request_cb/complete_cb --- examples/device/webusb/src/tusb_config.h | 22 ++++++---- examples/device/webusb/src/usb_descriptors.c | 4 +- src/class/midi/midi_device.c | 3 +- src/class/msc/msc_device.c | 2 +- src/class/vendor/vendor_device.h | 42 ++++++++++++++++---- src/class/vendor/vendor_host.h | 6 +-- src/device/usbd.c | 10 ++--- src/device/usbd.h | 4 -- src/tusb_option.h | 2 +- 9 files changed, 62 insertions(+), 33 deletions(-) diff --git a/examples/device/webusb/src/tusb_config.h b/examples/device/webusb/src/tusb_config.h index 8aefe8695..7dd9864df 100644 --- a/examples/device/webusb/src/tusb_config.h +++ b/examples/device/webusb/src/tusb_config.h @@ -69,18 +69,24 @@ // DEVICE CONFIGURATION //-------------------------------------------------------------------- -#define CFG_TUD_ENDOINT0_SIZE 64 +#define CFG_TUD_ENDOINT0_SIZE 64 //------------- CLASS -------------// -#define CFG_TUD_CDC 1 -#define CFG_TUD_MSC 0 -#define CFG_TUD_HID 0 -#define CFG_TUD_MIDI 0 -#define CFG_TUD_VENDOR 0 +#define CFG_TUD_CDC 1 +#define CFG_TUD_MSC 0 +#define CFG_TUD_HID 0 +#define CFG_TUD_MIDI 0 +#define CFG_TUD_VENDOR 0 // CDC FIFO size of TX and RX -#define CFG_TUD_CDC_RX_BUFSIZE 64 -#define CFG_TUD_CDC_TX_BUFSIZE 64 +#define CFG_TUD_CDC_RX_BUFSIZE 64 +#define CFG_TUD_CDC_TX_BUFSIZE 64 + +// Vendor FIFO size of TX and RX +// If not configured vendor endpoints will not be buffered +#define CFG_TUD_VENDOR_RX_BUFSIZE 64 +#define CFG_TUD_VENDOR_TX_BUFSIZE 64 + #ifdef __cplusplus } diff --git a/examples/device/webusb/src/usb_descriptors.c b/examples/device/webusb/src/usb_descriptors.c index cd9b6bc1a..67fb7dfb3 100644 --- a/examples/device/webusb/src/usb_descriptors.c +++ b/examples/device/webusb/src/usb_descriptors.c @@ -188,7 +188,7 @@ const tusb_desc_webusb_url_t desc_url = .url = URL }; -bool tud_control_vendor_request_cb(uint8_t rhport, tusb_control_request_t const * request) +bool tud_vendor_control_request_cb(uint8_t rhport, tusb_control_request_t const * request) { if ( request->bRequest == 0x01 ) // webusb vendor code { @@ -199,7 +199,7 @@ bool tud_control_vendor_request_cb(uint8_t rhport, tusb_control_request_t const } } -bool tud_control_vendor_complete_cb(uint8_t rhport, tusb_control_request_t const * request) +bool tud_vendor_control_complete_cb(uint8_t rhport, tusb_control_request_t const * request) { (void) rhport; return true; diff --git a/src/class/midi/midi_device.c b/src/class/midi/midi_device.c index 1e684edde..a33ced169 100644 --- a/src/class/midi/midi_device.c +++ b/src/class/midi/midi_device.c @@ -275,7 +275,8 @@ bool midid_open(uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, (*p_length) = sizeof(tusb_desc_interface_t); uint8_t found_endpoints = 0; - while (found_endpoints < p_interface_desc->bNumEndpoints) { + while (found_endpoints < p_interface_desc->bNumEndpoints) + { if ( TUSB_DESC_ENDPOINT == p_desc[DESC_OFFSET_TYPE]) { TU_ASSERT( dcd_edpt_open(rhport, (tusb_desc_endpoint_t const *) p_desc), false); diff --git a/src/class/msc/msc_device.c b/src/class/msc/msc_device.c index ebc2af1d5..609b4b2ad 100644 --- a/src/class/msc/msc_device.c +++ b/src/class/msc/msc_device.c @@ -111,7 +111,7 @@ bool tud_msc_set_sense(uint8_t lun, uint8_t sense_key, uint8_t add_sense_code, u } //--------------------------------------------------------------------+ -// USBD-CLASS API +// USBD Driver API //--------------------------------------------------------------------+ void mscd_init(void) { diff --git a/src/class/vendor/vendor_device.h b/src/class/vendor/vendor_device.h index 0c9d0184f..d2048fb63 100644 --- a/src/class/vendor/vendor_device.h +++ b/src/class/vendor/vendor_device.h @@ -24,20 +24,34 @@ * This file is part of the TinyUSB stack. */ -#ifndef _TUSB_CUSTOM_DEVICE_H_ -#define _TUSB_CUSTOM_DEVICE_H_ +#ifndef _TUSB_VENDOR_DEVICE_H_ +#define _TUSB_VENDOR_DEVICE_H_ #include "common/tusb_common.h" #include "device/usbd.h" +#ifndef CFG_TUD_VENDOR_EPSIZE +#define CFG_TUD_VENDOR_EPSIZE 64 +#endif + +#ifndef CFG_TUD_VENDOR_RX_BUFSIZE +#define CFG_TUD_VENDOR_RX_BUFSIZE 0 +#endif + +#ifndef CFG_TUD_VENDOR_TX_BUFSIZE +#define CFG_TUD_VENDOR_TX_BUFSIZE 0 +#endif + +#ifdef __cplusplus + extern "C" { +#endif + //--------------------------------------------------------------------+ -// APPLICATION API (Multiple Root Ports) -// Should be used only with MCU that support more than 1 ports +// Application API (Multiple Interfaces) //--------------------------------------------------------------------+ //--------------------------------------------------------------------+ -// APPLICATION API (Single Port) -// Should be used with MCU supporting only 1 USB port for code simplicity +// Application API (Single Port) //--------------------------------------------------------------------+ bool tud_vendor_mounted(void); @@ -47,9 +61,17 @@ uint32_t tud_vendor_read(void* buffer, uint32_t bufsize); //--------------------------------------------------------------------+ -// APPLICATION CALLBACK API (WEAK is optional) +// Application Callback API (weak is optional) //--------------------------------------------------------------------+ +// Invoked when received control request with VENDOR TYPE +TU_ATTR_WEAK bool tud_vendor_control_request_cb(uint8_t rhport, tusb_control_request_t const * request); +TU_ATTR_WEAK bool tud_vendor_control_complete_cb(uint8_t rhport, tusb_control_request_t const * request); + +// Invoked when received new data +TU_ATTR_WEAK void tud_vendor_rx_cb(uint8_t itf); + + //--------------------------------------------------------------------+ // Internal Class Driver API //--------------------------------------------------------------------+ @@ -58,4 +80,8 @@ void vendord_reset(uint8_t rhport); bool vendord_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t *p_length); bool vendord_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); -#endif /* _TUSB_CUSTOM_DEVICE_H_ */ +#ifdef __cplusplus + } +#endif + +#endif /* _TUSB_VENDOR_DEVICE_H_ */ diff --git a/src/class/vendor/vendor_host.h b/src/class/vendor/vendor_host.h index 2ca16222a..fa1879376 100644 --- a/src/class/vendor/vendor_host.h +++ b/src/class/vendor/vendor_host.h @@ -28,8 +28,8 @@ * \defgroup Group_Custom Custom Class (not supported yet) * @{ */ -#ifndef _TUSB_CUSTOM_HOST_H_ -#define _TUSB_CUSTOM_HOST_H_ +#ifndef _TUSB_VENDOR_HOST_H_ +#define _TUSB_VENDOR_HOST_H_ #include "common/tusb_common.h" #include "host/usbh.h" @@ -69,6 +69,6 @@ void cush_close(uint8_t dev_addr); } #endif -#endif /* _TUSB_CUSTOM_HOST_H_ */ +#endif /* _TUSB_VENDOR_HOST_H_ */ /** @} */ diff --git a/src/device/usbd.c b/src/device/usbd.c index a24113026..4ce056e7f 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -136,8 +136,8 @@ static usbd_class_driver_t const usbd_class_drivers[] = .init = vendord_init, .reset = vendord_reset, .open = vendord_open, - .control_request = NULL, // tud_control_vendor_request_cb - .control_complete = NULL, // tud_control_vendor_complete_cb + .control_request = NULL, // tud_vendor_control_request_cb + .control_complete = NULL, // tud_vendor_control_complete_cb .xfer_cb = vendord_xfer_cb, .sof = NULL }, @@ -349,10 +349,10 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const // Vendor request if ( p_request->bmRequestType_bit.type == TUSB_REQ_TYPE_VENDOR ) { - TU_VERIFY(tud_control_vendor_request_cb); + TU_VERIFY(tud_vendor_control_request_cb); - if (tud_control_vendor_complete_cb) usbd_control_set_complete_callback(tud_control_vendor_complete_cb); - return tud_control_vendor_request_cb(rhport, p_request); + if (tud_vendor_control_complete_cb) usbd_control_set_complete_callback(tud_vendor_control_complete_cb); + return tud_vendor_control_request_cb(rhport, p_request); } switch ( p_request->bmRequestType_bit.recipient ) diff --git a/src/device/usbd.h b/src/device/usbd.h index a77110bde..5aba18ef3 100644 --- a/src/device/usbd.h +++ b/src/device/usbd.h @@ -100,10 +100,6 @@ TU_ATTR_WEAK void tud_suspend_cb(bool remote_wakeup_en); // Invoked when usb bus is resumed TU_ATTR_WEAK void tud_resume_cb(void); -// Invoked when received control request with VENDOR TYPE -TU_ATTR_WEAK bool tud_control_vendor_request_cb(uint8_t rhport, tusb_control_request_t const * request); -TU_ATTR_WEAK bool tud_control_vendor_complete_cb(uint8_t rhport, tusb_control_request_t const * request); - //--------------------------------------------------------------------+ // Descriptor Templates //--------------------------------------------------------------------+ diff --git a/src/tusb_option.h b/src/tusb_option.h index a1c5b757d..a273d59b3 100644 --- a/src/tusb_option.h +++ b/src/tusb_option.h @@ -162,7 +162,7 @@ #endif #ifndef CFG_TUD_VENDOR - #define CFG_TUD_VENDOR 0 + #define CFG_TUD_VENDOR 0 #endif From 8ba78ee0129147cba4220dfb63fc5edd1420ad00 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 24 Jul 2019 16:08:43 +0700 Subject: [PATCH 24/36] adding vendor --- src/class/vendor/vendor_device.c | 61 ++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/src/class/vendor/vendor_device.c b/src/class/vendor/vendor_device.c index e1a0c2739..899d0bc07 100644 --- a/src/class/vendor/vendor_device.c +++ b/src/class/vendor/vendor_device.c @@ -28,55 +28,72 @@ #if (TUSB_OPT_DEVICE_ENABLED && CFG_TUD_VENDOR) -#include "common/tusb_common.h" #include "vendor_device.h" #include "device/usbd_pvt.h" -/*------------------------------------------------------------------*/ -/* MACRO TYPEDEF CONSTANT ENUM - *------------------------------------------------------------------*/ - -/*------------------------------------------------------------------*/ -/* VARIABLE DECLARATION - *------------------------------------------------------------------*/ +//--------------------------------------------------------------------+ +// MACRO CONSTANT TYPEDEF +//--------------------------------------------------------------------+ typedef struct { uint8_t itf_num; uint8_t ep_in; uint8_t ep_out; + /*------------- From this point, data is not cleared by bus reset -------------*/ + tu_fifo_t rx_ff; + tu_fifo_t tx_ff; + + // Endpoint Transfer buffer + CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_VENDOR_EPSIZE]; + CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_VENDOR_EPSIZE]; + } vendord_interface_t; -static vendord_interface_t _vendord_itf; +static vendord_interface_t _vendord_itf[CFG_TUD_VENDOR]; -/*------------------------------------------------------------------*/ -/* FUNCTION DECLARATION - *------------------------------------------------------------------*/ +#define ITF_MEM_RESET_SIZE offsetof(vendord_interface_t, rx_ff) + +//--------------------------------------------------------------------+ +// USBD Driver API +//--------------------------------------------------------------------+ void vendord_init(void) { - tu_varclr(&_vendord_itf); + tu_varclr(_vendord_itf); } void vendord_reset(uint8_t rhport) { + (void) rhport; + for(uint8_t i=0; iep_out, &p_itf->ep_in) ); - - p_itf->itf_num = p_desc_itf->bInterfaceNumber; + TU_ASSERT(usbd_open_edpt_pair(rhport, tu_desc_next(itf_desc), 2, TUSB_XFER_BULK, &p_vendor->ep_out, &p_vendor->ep_in)); + p_vendor->itf_num = itf_desc->bInterfaceNumber; (*p_len) = sizeof(tusb_desc_interface_t) + 2*sizeof(tusb_desc_endpoint_t); - // TODO Prepare for incoming data -// TU_ASSERT( usbd_edpt_xfer(rhport, p_itf->ep_out, (uint8_t*) &p_msc->cbw, sizeof(msc_cbw_t)) ); + // Prepare for incoming data + TU_ASSERT(usbd_edpt_xfer(rhport, p_vendor->ep_out, p_vendor->epout_buf, sizeof(p_vendor->epout_buf))); return true; } From 2a543c0e79b882b054d87f89ee8cf8e83b44e22e Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 24 Jul 2019 23:07:30 +0700 Subject: [PATCH 25/36] update vendor device similar to cdc read/write maybe refactor later --- examples/device/webusb/src/tusb_config.h | 2 +- examples/rules.mk | 3 +- src/class/midi/midi_device.c | 13 ++- src/class/vendor/vendor_device.c | 123 +++++++++++++++++++++-- src/class/vendor/vendor_device.h | 37 +++++-- src/device/usbd.c | 2 +- src/device/usbd_pvt.h | 6 +- 7 files changed, 162 insertions(+), 24 deletions(-) diff --git a/examples/device/webusb/src/tusb_config.h b/examples/device/webusb/src/tusb_config.h index 7dd9864df..ffedb997d 100644 --- a/examples/device/webusb/src/tusb_config.h +++ b/examples/device/webusb/src/tusb_config.h @@ -76,7 +76,7 @@ #define CFG_TUD_MSC 0 #define CFG_TUD_HID 0 #define CFG_TUD_MIDI 0 -#define CFG_TUD_VENDOR 0 +#define CFG_TUD_VENDOR 1 // CDC FIFO size of TX and RX #define CFG_TUD_CDC_RX_BUFSIZE 64 diff --git a/examples/rules.mk b/examples/rules.mk index bba0fdb53..4fbce2529 100644 --- a/examples/rules.mk +++ b/examples/rules.mk @@ -7,6 +7,7 @@ LIBS = -lgcc -lc -lm -lnosys # TinyUSB Stack source SRC_C += \ + src/tusb.c \ src/common/tusb_fifo.c \ src/device/usbd.c \ src/device/usbd_control.c \ @@ -14,7 +15,7 @@ SRC_C += \ src/class/cdc/cdc_device.c \ src/class/hid/hid_device.c \ src/class/midi/midi_device.c \ - src/tusb.c \ + src/class/vendor/vendor_device.c \ src/portable/$(VENDOR)/$(CHIP_FAMILY)/dcd_$(CHIP_FAMILY).c # TinyUSB stack include diff --git a/src/class/midi/midi_device.c b/src/class/midi/midi_device.c index a33ced169..03b0cae57 100644 --- a/src/class/midi/midi_device.c +++ b/src/class/midi/midi_device.c @@ -44,6 +44,7 @@ typedef struct uint8_t ep_in; uint8_t ep_out; + /*------------- From this point, data is not cleared by bus reset -------------*/ // FIFO tu_fifo_t rx_ff; tu_fifo_t tx_ff; @@ -77,7 +78,7 @@ CFG_TUSB_MEM_SECTION midid_interface_t _midid_itf[CFG_TUD_MIDI]; bool tud_midi_n_mounted (uint8_t itf) { midid_interface_t* midi = &_midid_itf[itf]; - return midi->itf_num != 0; + return midi->ep_in && midi->ep_out; } //--------------------------------------------------------------------+ @@ -127,7 +128,8 @@ void midi_rx_done_cb(midid_interface_t* midi, uint8_t const* buffer, uint32_t bu static bool maybe_transmit(midid_interface_t* midi, uint8_t itf_index) { - TU_VERIFY( !usbd_edpt_busy(TUD_OPT_RHPORT, midi->ep_in) ); // skip if previous transfer not complete + // skip if previous transfer not complete + TU_VERIFY( !usbd_edpt_busy(TUD_OPT_RHPORT, midi->ep_in) ); uint16_t count = tu_fifo_read_n(&midi->tx_ff, midi->epin_buf, CFG_TUD_MIDI_EPSIZE); if (count > 0) @@ -222,6 +224,7 @@ void midid_init(void) // config fifo tu_fifo_config(&midi->rx_ff, midi->rx_ff_buf, CFG_TUD_MIDI_RX_BUFSIZE, 1, true); tu_fifo_config(&midi->tx_ff, midi->tx_ff_buf, CFG_TUD_MIDI_TX_BUFSIZE, 1, true); + #if CFG_FIFO_MUTEX tu_fifo_config_mutex(&midi->rx_ff, osal_mutex_create(&midi->rx_ff_mutex)); tu_fifo_config_mutex(&midi->tx_ff, osal_mutex_create(&midi->tx_ff_mutex)); @@ -314,20 +317,20 @@ bool midid_control_request(uint8_t rhport, tusb_control_request_t const * p_requ return false; } -bool midid_xfer_cb(uint8_t rhport, uint8_t edpt_addr, xfer_result_t result, uint32_t xferred_bytes) +bool midid_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) { // TODO Support multiple interfaces uint8_t const itf = 0; midid_interface_t* p_midi = &_midid_itf[itf]; // receive new data - if ( edpt_addr == p_midi->ep_out ) + if ( ep_addr == p_midi->ep_out ) { midi_rx_done_cb(p_midi, p_midi->epout_buf, xferred_bytes); // prepare for next TU_ASSERT( usbd_edpt_xfer(rhport, p_midi->ep_out, p_midi->epout_buf, CFG_TUD_MIDI_EPSIZE), false ); - } else if ( edpt_addr == p_midi->ep_in ) { + } else if ( ep_addr == p_midi->ep_in ) { maybe_transmit(p_midi, itf); } diff --git a/src/class/vendor/vendor_device.c b/src/class/vendor/vendor_device.c index 899d0bc07..2cd815fb4 100644 --- a/src/class/vendor/vendor_device.c +++ b/src/class/vendor/vendor_device.c @@ -34,9 +34,9 @@ //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF //--------------------------------------------------------------------+ -typedef struct { +typedef struct +{ uint8_t itf_num; - uint8_t ep_in; uint8_t ep_out; @@ -44,22 +44,103 @@ typedef struct { tu_fifo_t rx_ff; tu_fifo_t tx_ff; + uint8_t rx_ff_buf[CFG_TUD_VENDOR_RX_BUFSIZE]; + uint8_t tx_ff_buf[CFG_TUD_VENDOR_TX_BUFSIZE]; + +#if CFG_FIFO_MUTEX + osal_mutex_def_t rx_ff_mutex; + osal_mutex_def_t tx_ff_mutex; +#endif + // Endpoint Transfer buffer CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_VENDOR_EPSIZE]; CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_VENDOR_EPSIZE]; - } vendord_interface_t; -static vendord_interface_t _vendord_itf[CFG_TUD_VENDOR]; +CFG_TUSB_MEM_SECTION static vendord_interface_t _vendord_itf[CFG_TUD_VENDOR]; #define ITF_MEM_RESET_SIZE offsetof(vendord_interface_t, rx_ff) + + +bool tud_vendor_n_mounted (uint8_t itf) +{ + return _vendord_itf[itf].ep_in && _vendord_itf[itf].ep_out; +} + +uint32_t tud_vendor_n_available (uint8_t itf) +{ + return tu_fifo_count(&_vendord_itf[itf].rx_ff); +} + +//--------------------------------------------------------------------+ +// Read API +//--------------------------------------------------------------------+ +static void _prep_out_transaction (vendord_interface_t* p_itf) +{ + // skip if previous transfer not complete + if ( usbd_edpt_busy(TUD_OPT_RHPORT, p_itf->ep_out) ) return; + + // Prepare for incoming data but only allow what we can store in the ring buffer. + uint16_t max_read = tu_fifo_remaining(&p_itf->rx_ff); + if ( max_read >= CFG_TUD_VENDOR_EPSIZE ) + { + usbd_edpt_xfer(TUD_OPT_RHPORT, p_itf->ep_out, p_itf->epout_buf, CFG_TUD_VENDOR_EPSIZE); + } +} + +uint32_t tud_vendor_n_read (uint8_t itf, void* buffer, uint32_t bufsize) +{ + vendord_interface_t* p_itf = &_vendord_itf[itf]; + uint32_t num_read = tu_fifo_read_n(&p_itf->rx_ff, buffer, bufsize); + _prep_out_transaction(p_itf); + return num_read; +} + +//--------------------------------------------------------------------+ +// Write API +//--------------------------------------------------------------------+ +static bool maybe_transmit(vendord_interface_t* p_itf) +{ + // skip if previous transfer not complete + TU_VERIFY( !usbd_edpt_busy(TUD_OPT_RHPORT, p_itf->ep_in) ); + + uint16_t count = tu_fifo_read_n(&p_itf->tx_ff, p_itf->epin_buf, CFG_TUD_VENDOR_EPSIZE); + if (count > 0) + { + TU_ASSERT( usbd_edpt_xfer(TUD_OPT_RHPORT, p_itf->ep_in, p_itf->epin_buf, count) ); + } + return true; +} + +uint32_t tud_vendor_n_write (uint8_t itf, uint8_t const* buffer, uint32_t bufsize) +{ + vendord_interface_t* p_itf = &_vendord_itf[itf]; + uint16_t ret = tu_fifo_write_n(&p_itf->tx_ff, buffer, bufsize); + maybe_transmit(p_itf); + return ret; +} + //--------------------------------------------------------------------+ // USBD Driver API //--------------------------------------------------------------------+ void vendord_init(void) { - tu_varclr(_vendord_itf); + tu_memclr(_vendord_itf, sizeof(_vendord_itf)); + + for(uint8_t i=0; irx_ff, p_itf->rx_ff_buf, CFG_TUD_VENDOR_RX_BUFSIZE, 1, false); + tu_fifo_config(&p_itf->tx_ff, p_itf->tx_ff_buf, CFG_TUD_VENDOR_TX_BUFSIZE, 1, false); + +#if CFG_FIFO_MUTEX + tu_fifo_config_mutex(&p_itf->rx_ff, osal_mutex_create(&p_itf->rx_ff_mutex)); + tu_fifo_config_mutex(&p_itf->tx_ff, osal_mutex_create(&p_itf->tx_ff_mutex)); +#endif + } } void vendord_reset(uint8_t rhport) @@ -68,7 +149,11 @@ void vendord_reset(uint8_t rhport) for(uint8_t i=0; irx_ff); + tu_fifo_clear(&p_itf->tx_ff); } } @@ -98,10 +183,32 @@ bool vendord_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16 return true; } -bool vendord_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes) +bool vendord_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) { + (void) rhport; + (void) result; + + // TODO Support multiple interfaces + uint8_t const itf = 0; + vendord_interface_t* p_itf = &_vendord_itf[itf]; + + if ( ep_addr == p_itf->ep_out ) + { + // Receive new data + tu_fifo_write_n(&p_itf->rx_ff, p_itf->epout_buf, xferred_bytes); + + // Invoked callback if any + if (tud_vendor_rx_cb) tud_vendor_rx_cb(itf); + + _prep_out_transaction(p_itf); + } + else if ( ep_addr == p_itf->ep_in ) + { + // Send complete, try to send more if possible + maybe_transmit(p_itf); + } + return true; } - #endif diff --git a/src/class/vendor/vendor_device.h b/src/class/vendor/vendor_device.h index d2048fb63..0fa1f2d43 100644 --- a/src/class/vendor/vendor_device.h +++ b/src/class/vendor/vendor_device.h @@ -49,16 +49,18 @@ //--------------------------------------------------------------------+ // Application API (Multiple Interfaces) //--------------------------------------------------------------------+ +bool tud_vendor_n_mounted (uint8_t itf); +uint32_t tud_vendor_n_available (uint8_t itf); +uint32_t tud_vendor_n_read (uint8_t itf, void* buffer, uint32_t bufsize); +uint32_t tud_vendor_n_write (uint8_t itf, uint8_t const* buffer, uint32_t bufsize); //--------------------------------------------------------------------+ // Application API (Single Port) //--------------------------------------------------------------------+ - -bool tud_vendor_mounted(void); - -uint32_t tud_vendor_write(void const* buffer, uint32_t bufsize); -uint32_t tud_vendor_read(void* buffer, uint32_t bufsize); - +static inline bool tud_vendor_mounted (void); +static inline uint32_t tud_vendor_available (void); +static inline uint32_t tud_vendor_read (void* buffer, uint32_t bufsize); +static inline uint32_t tud_vendor_write (uint8_t const* buffer, uint32_t bufsize); //--------------------------------------------------------------------+ // Application Callback API (weak is optional) @@ -71,6 +73,29 @@ TU_ATTR_WEAK bool tud_vendor_control_complete_cb(uint8_t rhport, tusb_control_re // Invoked when received new data TU_ATTR_WEAK void tud_vendor_rx_cb(uint8_t itf); +//--------------------------------------------------------------------+ +// Inline Functions +//--------------------------------------------------------------------+ + +static inline bool tud_vendor_mounted (void) +{ + return tud_vendor_n_mounted(0); +} + +static inline uint32_t tud_vendor_available (void) +{ + return tud_vendor_n_available(0); +} + +static inline uint32_t tud_vendor_read (void* buffer, uint32_t bufsize) +{ + return tud_vendor_n_read(0, buffer, bufsize); +} + +static inline uint32_t tud_vendor_write (uint8_t const* buffer, uint32_t bufsize) +{ + return tud_vendor_n_write(0, buffer, bufsize); +} //--------------------------------------------------------------------+ // Internal Class Driver API diff --git a/src/device/usbd.c b/src/device/usbd.c index 4ce056e7f..c94d08a67 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -528,7 +528,7 @@ static bool process_set_config(uint8_t rhport, uint8_t cfg_num) _usbd_dev.itf2drv[desc_itf->bInterfaceNumber] = drv_id; uint16_t itf_len=0; - TU_ASSERT( usbd_class_drivers[drv_id].open( rhport, desc_itf, &itf_len ) ); + TU_ASSERT( usbd_class_drivers[drv_id].open(rhport, desc_itf, &itf_len) ); TU_ASSERT( itf_len >= sizeof(tusb_desc_interface_t) ); mark_interface_endpoint(_usbd_dev.ep2drv, p_desc, itf_len, drv_id); diff --git a/src/device/usbd_pvt.h b/src/device/usbd_pvt.h index 06f7a3afe..fbf030ce3 100644 --- a/src/device/usbd_pvt.h +++ b/src/device/usbd_pvt.h @@ -39,11 +39,13 @@ bool usbd_init (void); // USBD Endpoint API //--------------------------------------------------------------------+ +//bool usbd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc); + // Submit a usb transfer -bool usbd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes); +bool usbd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes); // Check if endpoint transferring is complete -bool usbd_edpt_busy (uint8_t rhport, uint8_t ep_addr); +bool usbd_edpt_busy(uint8_t rhport, uint8_t ep_addr); void usbd_edpt_stall(uint8_t rhport, uint8_t ep_addr); void usbd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr); From caf2b6d46346ee0d27f2e10c57b84a538c799475 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 24 Jul 2019 23:12:31 +0700 Subject: [PATCH 26/36] move tud_vendor_control_* to usbd.h --- src/class/vendor/vendor_device.h | 4 ---- src/device/usbd.h | 4 ++++ 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/class/vendor/vendor_device.h b/src/class/vendor/vendor_device.h index 0fa1f2d43..08deba5ab 100644 --- a/src/class/vendor/vendor_device.h +++ b/src/class/vendor/vendor_device.h @@ -66,10 +66,6 @@ static inline uint32_t tud_vendor_write (uint8_t const* buffer, uint32_t bu // Application Callback API (weak is optional) //--------------------------------------------------------------------+ -// Invoked when received control request with VENDOR TYPE -TU_ATTR_WEAK bool tud_vendor_control_request_cb(uint8_t rhport, tusb_control_request_t const * request); -TU_ATTR_WEAK bool tud_vendor_control_complete_cb(uint8_t rhport, tusb_control_request_t const * request); - // Invoked when received new data TU_ATTR_WEAK void tud_vendor_rx_cb(uint8_t itf); diff --git a/src/device/usbd.h b/src/device/usbd.h index 5aba18ef3..cd261575d 100644 --- a/src/device/usbd.h +++ b/src/device/usbd.h @@ -100,6 +100,10 @@ TU_ATTR_WEAK void tud_suspend_cb(bool remote_wakeup_en); // Invoked when usb bus is resumed TU_ATTR_WEAK void tud_resume_cb(void); +// Invoked when received control request with VENDOR TYPE +TU_ATTR_WEAK bool tud_vendor_control_request_cb(uint8_t rhport, tusb_control_request_t const * request); +TU_ATTR_WEAK bool tud_vendor_control_complete_cb(uint8_t rhport, tusb_control_request_t const * request); + //--------------------------------------------------------------------+ // Descriptor Templates //--------------------------------------------------------------------+ From 8ba71be6ee26c077ca37b895d773f2507f9371c8 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 25 Jul 2019 16:52:20 +0700 Subject: [PATCH 27/36] more webusb + vendor --- .../device/cdc_msc_hid/src/usb_descriptors.c | 3 +- .../src/usb_descriptors.c | 3 +- .../hid_generic_inout/src/usb_descriptors.c | 3 +- .../device/midi_test/src/usb_descriptors.c | 3 +- .../device/msc_dual_lun/src/usb_descriptors.c | 3 +- examples/device/webusb/src/main.c | 5 +++ examples/device/webusb/src/usb_descriptors.c | 32 +++++++++++----- src/device/usbd.h | 37 +++++++++---------- 8 files changed, 55 insertions(+), 34 deletions(-) diff --git a/examples/device/cdc_msc_hid/src/usb_descriptors.c b/examples/device/cdc_msc_hid/src/usb_descriptors.c index 4220dc5fb..17a98c9d4 100644 --- a/examples/device/cdc_msc_hid/src/usb_descriptors.c +++ b/examples/device/cdc_msc_hid/src/usb_descriptors.c @@ -32,7 +32,8 @@ * [MSB] 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) ) +#define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \ + _PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) ) //--------------------------------------------------------------------+ // Device Descriptors diff --git a/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c b/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c index 4e1df9fb5..03f80f8c0 100644 --- a/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c +++ b/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c @@ -32,7 +32,8 @@ * [MSB] 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) ) +#define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \ + _PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) ) //--------------------------------------------------------------------+ // Device Descriptors diff --git a/examples/device/hid_generic_inout/src/usb_descriptors.c b/examples/device/hid_generic_inout/src/usb_descriptors.c index 849c2be8a..7578a8b7f 100644 --- a/examples/device/hid_generic_inout/src/usb_descriptors.c +++ b/examples/device/hid_generic_inout/src/usb_descriptors.c @@ -32,7 +32,8 @@ * [MSB] 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) ) +#define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \ + _PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) ) //--------------------------------------------------------------------+ // Device Descriptors diff --git a/examples/device/midi_test/src/usb_descriptors.c b/examples/device/midi_test/src/usb_descriptors.c index d9ab42f09..95d3cca25 100644 --- a/examples/device/midi_test/src/usb_descriptors.c +++ b/examples/device/midi_test/src/usb_descriptors.c @@ -32,7 +32,8 @@ * [MSB] 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) ) +#define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \ + _PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) ) //--------------------------------------------------------------------+ // Device Descriptors diff --git a/examples/device/msc_dual_lun/src/usb_descriptors.c b/examples/device/msc_dual_lun/src/usb_descriptors.c index 0875f4848..6064d8d7b 100644 --- a/examples/device/msc_dual_lun/src/usb_descriptors.c +++ b/examples/device/msc_dual_lun/src/usb_descriptors.c @@ -32,7 +32,8 @@ * [MSB] 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) ) +#define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \ + _PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) ) //--------------------------------------------------------------------+ // Device Descriptors diff --git a/examples/device/webusb/src/main.c b/examples/device/webusb/src/main.c index 5624ff4bd..cfcbf4da5 100644 --- a/examples/device/webusb/src/main.c +++ b/examples/device/webusb/src/main.c @@ -49,6 +49,7 @@ static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED; void led_blinking_task(void); void cdc_task(void); +void webserial_task(void); /*------------- MAIN -------------*/ int main(void) @@ -61,6 +62,7 @@ int main(void) { tud_task(); // tinyusb device task cdc_task(); + webserial_task(); led_blinking_task(); } @@ -101,7 +103,10 @@ void tud_resume_cb(void) //--------------------------------------------------------------------+ // WebUSB //--------------------------------------------------------------------+ +void webserial_task(void) +{ +} //--------------------------------------------------------------------+ // USB CDC diff --git a/examples/device/webusb/src/usb_descriptors.c b/examples/device/webusb/src/usb_descriptors.c index 67fb7dfb3..5fd651bd0 100644 --- a/examples/device/webusb/src/usb_descriptors.c +++ b/examples/device/webusb/src/usb_descriptors.c @@ -32,7 +32,8 @@ * [MSB] 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) ) +#define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \ + _PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) ) //--------------------------------------------------------------------+ // Device Descriptors @@ -71,6 +72,10 @@ uint8_t const * tud_descriptor_device_cb(void) //--------------------------------------------------------------------+ // BOS Descriptor //--------------------------------------------------------------------+ +enum +{ + VENDOR_REQUEST_WEBUSB = 1, +}; #define BOS_TOTAL_LEN (TUD_BOS_DESC_LEN + TUD_BOS_WEBUSB_DESC_LEN) @@ -81,7 +86,7 @@ uint8_t const desc_bos[] = TUD_BOS_DESCRIPTOR(BOS_TOTAL_LEN, 1), // Vendor Code, iLandingPage - TUD_BOS_WEBUSB_DESCRIPTOR(0x01, 1) + TUD_BOS_WEBUSB_DESCRIPTOR(VENDOR_REQUEST_WEBUSB, 1) }; uint8_t const * tud_descriptor_bos_cb(void) @@ -96,16 +101,21 @@ enum { ITF_NUM_CDC = 0, ITF_NUM_CDC_DATA, - + ITF_NUM_VENDOR, ITF_NUM_TOTAL }; -#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN) +#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_VENDOR_DESC_LEN) -// Use Endpoint 2 instead of 1 due to NXP MCU -// LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number -// 0 control, 1 In, 2 Bulk, 3 Iso, 4 In etc ... -#define EPNUM_CDC 0x02 +#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 etc ... + #define EPNUM_CDC 2 + #define EPNUM_VENDOR 5 +#else + #define EPNUM_CDC 2 + #define EPNUM_VENDOR 3 +#endif uint8_t const desc_configuration[] = { @@ -114,6 +124,9 @@ uint8_t const desc_configuration[] = // Interface number, string index, EP notification address and size, EP data address (out, in) and size. TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, 0x81, 8, EPNUM_CDC, 0x80 | EPNUM_CDC, 64), + + // Interface number, string index, EP Out & IN address, EP size + TUD_VENDOR_DESCRIPTOR(ITF_NUM_VENDOR, 5, EPNUM_VENDOR, 0x80 | EPNUM_VENDOR, 64) }; // Invoked when received GET CONFIGURATION DESCRIPTOR @@ -137,6 +150,7 @@ char const* string_desc_arr [] = "TinyUSB Device", // 2: Product "123456", // 3: Serials, should use chip ID "TinyUSB CDC", // 4: CDC Interface + "TinyUSB WebUSB" // 5: Vendor Interface }; static uint16_t _desc_str[32]; @@ -190,7 +204,7 @@ const tusb_desc_webusb_url_t desc_url = bool tud_vendor_control_request_cb(uint8_t rhport, tusb_control_request_t const * request) { - if ( request->bRequest == 0x01 ) // webusb vendor code + if ( request->bRequest == VENDOR_REQUEST_WEBUSB ) { return tud_control_xfer(rhport, request, (void*) &desc_url, desc_url.bLength); }else diff --git a/src/device/usbd.h b/src/device/usbd.h index cd261575d..525fced8e 100644 --- a/src/device/usbd.h +++ b/src/device/usbd.h @@ -119,6 +119,19 @@ TU_ATTR_WEAK bool tud_vendor_control_complete_cb(uint8_t rhport, tusb_control_re #define TUD_BOS_PLATFORM_DESCRIPTOR(...) \ 4+TU_ARGS_NUM(__VA_ARGS__), TUSB_DESC_DEVICE_CAPABILITY, DEVICE_CAPABILITY_PLATFORM, 0x00, __VA_ARGS__ +//------------- WebUSB BOS -------------// + +// Descriptor Length +#define TUD_BOS_WEBUSB_DESC_LEN 24 + +// Vendor Code, iLandingPage +#define TUD_BOS_WEBUSB_DESCRIPTOR(_vendor_code, _ipage) \ + TUD_BOS_PLATFORM_DESCRIPTOR(TUD_BOS_WEBUSB_UUID, U16_TO_U8S_LE(0x0100), _vendor_code, _ipage) + +#define TUD_BOS_WEBUSB_UUID \ + 0x38, 0xB6, 0x08, 0x34, 0xA9, 0x09, 0xA0, 0x47, \ + 0x8B, 0xFD, 0xA0, 0x76, 0x88, 0x15, 0xB6, 0x65 + //------------- Configuration -------------// #define TUD_CONFIG_DESC_LEN (9) @@ -233,33 +246,17 @@ TU_ATTR_WEAK bool tud_vendor_control_complete_cb(uint8_t rhport, tusb_control_re /* MS Endpoint (connected to embedded jack out) */\ 5, TUSB_DESC_CS_ENDPOINT, MIDI_CS_ENDPOINT_GENERAL, 1, 3 -//------------- WebUSB -------------// +//------------- Vendor -------------// +#define TUD_VENDOR_DESC_LEN (9+7+7) -// Descriptor Length -#define TUD_BOS_WEBUSB_DESC_LEN 24 - -// Vendor Code, iLandingPage -#define TUD_BOS_WEBUSB_DESCRIPTOR(_vendor_code, _ipage) \ - TUD_BOS_PLATFORM_DESCRIPTOR(TUD_BOS_WEBUSB_UUID, U16_TO_U8S_LE(0x0100), _vendor_code, _ipage) - -#define TUD_BOS_WEBUSB_UUID \ - 0x38, 0xB6, 0x08, 0x34, 0xA9, 0x09, 0xA0, 0x47, \ - 0x8B, 0xFD, 0xA0, 0x76, 0x88, 0x15, 0xB6, 0x65 - -#if 0 -//------------- Custom -------------// - -#define TUD_WEBUSB_DESC_LEN (9+7+7) - -// Interface number, string index, EP Out & IN address, size & polling interval -#define TUD_WEBUSB_DESCRIPTOR(_itfnum, _stridx) \ +// Interface number, string index, EP Out & IN address, EP size +#define TUD_VENDOR_DESCRIPTOR(_itfnum, _stridx, _epout, _epin, _epsize) \ /* Interface */\ 9, TUSB_DESC_INTERFACE, _itfnum, 0, 2, TUSB_CLASS_VENDOR_SPECIFIC, 0x00, 0x00, _stridx,\ /* Endpoint Out */\ 7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0,\ /* Endpoint In */\ 7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0 -#endif #ifdef __cplusplus From 570a2b20539042cac631d86eaf323077a46e9f72 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 26 Jul 2019 11:35:45 +0700 Subject: [PATCH 28/36] gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index f5af16c43..e44eb8dc7 100644 --- a/.gitignore +++ b/.gitignore @@ -6,9 +6,12 @@ test/_build *.P *.map *.axf +*.bin *.jlink *.emSession *.elf *.ind .env /examples/*/*/build-* +test_old/ +tests_obsolete/ \ No newline at end of file From d2b60b67559e290e874797c55a34905ae8c620cc Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 26 Jul 2019 11:36:30 +0700 Subject: [PATCH 29/36] webusb work great with linux --- examples/device/webusb/src/main.c | 96 ++++++++++++++++++-- examples/device/webusb/src/usb_descriptors.c | 31 +------ examples/device/webusb/src/usb_descriptors.h | 33 +++++++ src/class/msc/msc_device.c | 4 +- src/device/usbd.c | 4 +- 5 files changed, 124 insertions(+), 44 deletions(-) create mode 100644 examples/device/webusb/src/usb_descriptors.h diff --git a/examples/device/webusb/src/main.c b/examples/device/webusb/src/main.c index cfcbf4da5..bf5598b18 100644 --- a/examples/device/webusb/src/main.c +++ b/examples/device/webusb/src/main.c @@ -29,6 +29,7 @@ #include "bsp/board.h" #include "tusb.h" +#include "usb_descriptors.h" //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF PROTYPES @@ -47,6 +48,19 @@ enum { static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED; +#define URL "github.com/hathach/tinyusb" + +const tusb_desc_webusb_url_t desc_url = +{ + .bLength = 3 + sizeof(URL) - 1, + .bDescriptorType = 3, // WEBUSB URL type + .bScheme = 1, // 0: http, 1: https + .url = URL +}; + +static bool web_serial_connected = false; + +//------------- prototypes -------------// void led_blinking_task(void); void cdc_task(void); void webserial_task(void); @@ -100,14 +114,83 @@ void tud_resume_cb(void) blink_interval_ms = BLINK_MOUNTED; } +// send characters to both CDC and WebUSB +void echo_all(uint8_t buf[], uint32_t count) +{ + // echo to web serial + if ( web_serial_connected ) + { + tud_vendor_write(buf, count); + } + + // echo to cdc + if ( tud_cdc_connected() ) + { + for(uint32_t i=0; ibRequest) + { + case VENDOR_REQUEST_WEBUSB: + // match vendor request in BOS descriptor + // Get landing page url + return tud_control_xfer(rhport, request, (void*) &desc_url, desc_url.bLength); + + case 0x22: + // Webserial simulate the CDC_REQUEST_SET_CONTROL_LINE_STATE (0x22) to + // connect and disconnect. + + web_serial_connected = (request->wValue != 0); + + // response with status OK + return tud_control_status(rhport, request); + + default: + // stall unknown request + return false; + } +} + +// Invoked when DATA Stage of VENDOR's request is complete +bool tud_vendor_control_complete_cb(uint8_t rhport, tusb_control_request_t const * request) +{ + (void) rhport; + (void) request; + + // nothing to do + return true; +} + void webserial_task(void) { + if ( web_serial_connected ) + { + if ( tud_vendor_available() ) + { + uint8_t buf[64]; + uint32_t count = tud_vendor_read(buf, sizeof(buf)); + // echo back to both web serial and cdc + echo_all(buf, count); + } + } } + //--------------------------------------------------------------------+ // USB CDC //--------------------------------------------------------------------+ @@ -120,17 +203,10 @@ void cdc_task(void) { uint8_t buf[64]; - // read and echo back uint32_t count = tud_cdc_read(buf, sizeof(buf)); - for(uint32_t i=0; ibRequest == VENDOR_REQUEST_WEBUSB ) - { - return tud_control_xfer(rhport, request, (void*) &desc_url, desc_url.bLength); - }else - { - return false; - } -} - -bool tud_vendor_control_complete_cb(uint8_t rhport, tusb_control_request_t const * request) -{ - (void) rhport; - return true; -} diff --git a/examples/device/webusb/src/usb_descriptors.h b/examples/device/webusb/src/usb_descriptors.h new file mode 100644 index 000000000..d45e720df --- /dev/null +++ b/examples/device/webusb/src/usb_descriptors.h @@ -0,0 +1,33 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2019 hathach for Adafruit Industries + * + * 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. + */ + +#ifndef USB_DESCRIPTORS_H_ +#define USB_DESCRIPTORS_H_ + +enum +{ + VENDOR_REQUEST_WEBUSB = 1, +}; + +#endif /* USB_DESCRIPTORS_H_ */ diff --git a/src/class/msc/msc_device.c b/src/class/msc/msc_device.c index 07686663b..2ec8962ae 100644 --- a/src/class/msc/msc_device.c +++ b/src/class/msc/msc_device.c @@ -178,10 +178,10 @@ bool mscd_control_request(uint8_t rhport, tusb_control_request_t const * p_reque // Invoked when class request DATA stage is finished. // return false to stall control endpoint (e.g Host send non-sense DATA) -bool mscd_control_complete(uint8_t rhport, tusb_control_request_t const * p_request) +bool mscd_control_complete(uint8_t rhport, tusb_control_request_t const * request) { (void) rhport; - (void) p_request; + (void) request; // nothing to do return true; diff --git a/src/device/usbd.c b/src/device/usbd.c index c94d08a67..f737f5d36 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -136,8 +136,8 @@ static usbd_class_driver_t const usbd_class_drivers[] = .init = vendord_init, .reset = vendord_reset, .open = vendord_open, - .control_request = NULL, // tud_vendor_control_request_cb - .control_complete = NULL, // tud_vendor_control_complete_cb + .control_request = tud_vendor_control_request_cb, + .control_complete = tud_vendor_control_complete_cb, .xfer_cb = vendord_xfer_cb, .sof = NULL }, From 4ed1a96311f0a6d97c381b020c4c60dcc49cccbf Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 26 Jul 2019 11:43:34 +0700 Subject: [PATCH 30/36] webusb Always lit LED if connected --- examples/device/webusb/src/main.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/examples/device/webusb/src/main.c b/examples/device/webusb/src/main.c index bf5598b18..e6bfe3802 100644 --- a/examples/device/webusb/src/main.c +++ b/examples/device/webusb/src/main.c @@ -42,8 +42,11 @@ */ enum { BLINK_NOT_MOUNTED = 250, - BLINK_MOUNTED = 1000, - BLINK_SUSPENDED = 2500, + BLINK_MOUNTED = 1000, + BLINK_SUSPENDED = 2500, + + BLINK_ALWAYS_ON = UINT32_MAX, + BLINK_ALWAYS_OFF = 0 }; static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED; @@ -153,9 +156,18 @@ bool tud_vendor_control_request_cb(uint8_t rhport, tusb_control_request_t const case 0x22: // Webserial simulate the CDC_REQUEST_SET_CONTROL_LINE_STATE (0x22) to // connect and disconnect. - web_serial_connected = (request->wValue != 0); + // Always lit LED if connected + if ( web_serial_connected ) + { + board_led_write(true); + blink_interval_ms = BLINK_ALWAYS_ON; + }else + { + blink_interval_ms = BLINK_MOUNTED; + } + // response with status OK return tud_control_status(rhport, request); From cdaf455461c5ac5867f5eba2eb8874d1a32f0e82 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 26 Jul 2019 20:14:56 +0700 Subject: [PATCH 31/36] fix stall response with SET_INTERFACE/GET_INTERFACE webusb work with linux & macos --- examples/device/webusb/src/main.c | 44 +++++++++++++++---------------- src/device/usbd.c | 30 ++++++++++++++++++--- 2 files changed, 49 insertions(+), 25 deletions(-) diff --git a/examples/device/webusb/src/main.c b/examples/device/webusb/src/main.c index e6bfe3802..f844e9506 100644 --- a/examples/device/webusb/src/main.c +++ b/examples/device/webusb/src/main.c @@ -86,6 +86,28 @@ int main(void) return 0; } +// send characters to both CDC and WebUSB +void echo_all(uint8_t buf[], uint32_t count) +{ + // echo to web serial + if ( web_serial_connected ) + { + tud_vendor_write(buf, count); + } + + // echo to cdc + if ( tud_cdc_connected() ) + { + for(uint32_t i=0; ibRequest ) + { + case TUSB_REQ_GET_INTERFACE: + { + // TODO not support alternate interface yet + uint8_t alternate = 0; + tud_control_xfer(rhport, p_request, &alternate, 1); + } + break; - // stall control endpoint if driver return false - return usbd_class_drivers[drvid].control_request(rhport, p_request); + case TUSB_REQ_SET_INTERFACE: + { + uint8_t alternate = (uint8_t) p_request->wValue; + + // TODO not support alternate interface yet + TU_ASSERT(alternate == 0); + + tud_control_status(rhport, p_request); + } + break; + + default: + // forward to class driver + // stall control endpoint if driver return false + usbd_control_set_complete_callback(usbd_class_drivers[drvid].control_complete); + TU_ASSERT(usbd_class_drivers[drvid].control_request(rhport, p_request)); + break; + } } break; From 394211b9dabf96545a778077c4591968f4245a7c Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 26 Jul 2019 20:19:45 +0700 Subject: [PATCH 32/36] change landing page --- examples/device/webusb/src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/device/webusb/src/main.c b/examples/device/webusb/src/main.c index f844e9506..4990fe24f 100644 --- a/examples/device/webusb/src/main.c +++ b/examples/device/webusb/src/main.c @@ -51,7 +51,7 @@ enum { static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED; -#define URL "github.com/hathach/tinyusb" +#define URL "www.tinyusb.org/examples/webusb-serial" const tusb_desc_webusb_url_t desc_url = { From 77d3124a7060ab55143c65dafc6c80d8f83690cd Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 26 Jul 2019 20:21:41 +0700 Subject: [PATCH 33/36] rename example to webusb_serial --- examples/device/{webusb => webusb_serial}/Makefile | 0 examples/device/{webusb => webusb_serial}/src/main.c | 0 .../{webusb => webusb_serial}/src/tusb_config.h | 0 .../{webusb => webusb_serial}/src/usb_descriptors.c | 12 ++++++++++++ .../{webusb => webusb_serial}/src/usb_descriptors.h | 0 5 files changed, 12 insertions(+) rename examples/device/{webusb => webusb_serial}/Makefile (100%) rename examples/device/{webusb => webusb_serial}/src/main.c (100%) rename examples/device/{webusb => webusb_serial}/src/tusb_config.h (100%) rename examples/device/{webusb => webusb_serial}/src/usb_descriptors.c (92%) rename examples/device/{webusb => webusb_serial}/src/usb_descriptors.h (100%) diff --git a/examples/device/webusb/Makefile b/examples/device/webusb_serial/Makefile similarity index 100% rename from examples/device/webusb/Makefile rename to examples/device/webusb_serial/Makefile diff --git a/examples/device/webusb/src/main.c b/examples/device/webusb_serial/src/main.c similarity index 100% rename from examples/device/webusb/src/main.c rename to examples/device/webusb_serial/src/main.c diff --git a/examples/device/webusb/src/tusb_config.h b/examples/device/webusb_serial/src/tusb_config.h similarity index 100% rename from examples/device/webusb/src/tusb_config.h rename to examples/device/webusb_serial/src/tusb_config.h diff --git a/examples/device/webusb/src/usb_descriptors.c b/examples/device/webusb_serial/src/usb_descriptors.c similarity index 92% rename from examples/device/webusb/src/usb_descriptors.c rename to examples/device/webusb_serial/src/usb_descriptors.c index 9572e8442..1ae8a61fb 100644 --- a/examples/device/webusb/src/usb_descriptors.c +++ b/examples/device/webusb_serial/src/usb_descriptors.c @@ -73,6 +73,18 @@ uint8_t const * tud_descriptor_device_cb(void) //--------------------------------------------------------------------+ // BOS Descriptor //--------------------------------------------------------------------+ + +/* Microsoft OS 2.0 registry property descriptor +Per MS requirements https://msdn.microsoft.com/en-us/library/windows/hardware/hh450799(v=vs.85).aspx +device should create DeviceInterfaceGUIDs. It can be done by driver and +in case of real PnP solution device should expose MS "Microsoft OS 2.0 +registry property descriptor". Such descriptor can insert any record +into Windows registry per device/configuration/interface. In our case it +will insert "DeviceInterfaceGUIDs" multistring property. + +GUID is freshly generated and should be OK to use. +*/ + #define BOS_TOTAL_LEN (TUD_BOS_DESC_LEN + TUD_BOS_WEBUSB_DESC_LEN) // BOS Descriptor is required for webUSB diff --git a/examples/device/webusb/src/usb_descriptors.h b/examples/device/webusb_serial/src/usb_descriptors.h similarity index 100% rename from examples/device/webusb/src/usb_descriptors.h rename to examples/device/webusb_serial/src/usb_descriptors.h From d79a209aefe498f78350ee3e08b96b8e17fda6b1 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 26 Jul 2019 20:27:06 +0700 Subject: [PATCH 34/36] example clean up --- examples/device/cdc_msc_hid/src/main.c | 9 ++--- .../device/cdc_msc_hid/src/usb_descriptors.c | 7 +--- .../device/cdc_msc_hid/src/usb_descriptors.h | 34 +++++++++++++++++++ .../device/cdc_msc_hid_freertos/src/main.c | 9 ++--- .../src/usb_descriptors.c | 7 +--- .../src/usb_descriptors.h | 34 +++++++++++++++++++ .../webusb_serial/src/usb_descriptors.h | 2 +- 7 files changed, 75 insertions(+), 27 deletions(-) create mode 100644 examples/device/cdc_msc_hid/src/usb_descriptors.h create mode 100644 examples/device/cdc_msc_hid_freertos/src/usb_descriptors.h diff --git a/examples/device/cdc_msc_hid/src/main.c b/examples/device/cdc_msc_hid/src/main.c index c3295fd50..65ef1dcbd 100644 --- a/examples/device/cdc_msc_hid/src/main.c +++ b/examples/device/cdc_msc_hid/src/main.c @@ -30,6 +30,8 @@ #include "bsp/board.h" #include "tusb.h" +#include "usb_descriptors.h" + //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF PROTYPES //--------------------------------------------------------------------+ @@ -162,13 +164,6 @@ void tud_cdc_rx_cb(uint8_t itf) //--------------------------------------------------------------------+ #if CFG_TUD_HID -// Must match with ID declared by HID Report Descriptor, better to be in header file -enum -{ - REPORT_ID_KEYBOARD = 1, - REPORT_ID_MOUSE -}; - void hid_task(void) { // Poll every 10ms diff --git a/examples/device/cdc_msc_hid/src/usb_descriptors.c b/examples/device/cdc_msc_hid/src/usb_descriptors.c index 17a98c9d4..ee79f4ece 100644 --- a/examples/device/cdc_msc_hid/src/usb_descriptors.c +++ b/examples/device/cdc_msc_hid/src/usb_descriptors.c @@ -24,6 +24,7 @@ */ #include "tusb.h" +#include "usb_descriptors.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. @@ -79,13 +80,7 @@ uint8_t const * tud_descriptor_device_cb(void) //--------------------------------------------------------------------+ // HID Report Descriptor //--------------------------------------------------------------------+ - #if CFG_TUD_HID -enum -{ - REPORT_ID_KEYBOARD = 1, - REPORT_ID_MOUSE -}; uint8_t const desc_hid_report[] = { diff --git a/examples/device/cdc_msc_hid/src/usb_descriptors.h b/examples/device/cdc_msc_hid/src/usb_descriptors.h new file mode 100644 index 000000000..6992d3349 --- /dev/null +++ b/examples/device/cdc_msc_hid/src/usb_descriptors.h @@ -0,0 +1,34 @@ +/* + * 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. + */ + +#ifndef USB_DESCRIPTORS_H_ +#define USB_DESCRIPTORS_H_ + +enum +{ + REPORT_ID_KEYBOARD = 1, + REPORT_ID_MOUSE +}; + +#endif /* USB_DESCRIPTORS_H_ */ diff --git a/examples/device/cdc_msc_hid_freertos/src/main.c b/examples/device/cdc_msc_hid_freertos/src/main.c index 56ea3a7a3..cb707675d 100644 --- a/examples/device/cdc_msc_hid_freertos/src/main.c +++ b/examples/device/cdc_msc_hid_freertos/src/main.c @@ -36,6 +36,8 @@ #include "bsp/board.h" #include "tusb.h" +#include "usb_descriptors.h" + //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF PROTYPES //--------------------------------------------------------------------+ @@ -194,13 +196,6 @@ void tud_cdc_rx_cb(uint8_t itf) //--------------------------------------------------------------------+ #if CFG_TUD_HID -// Must match with ID declared by HID Report Descriptor, better to be in header file -enum -{ - REPORT_ID_KEYBOARD = 1, - REPORT_ID_MOUSE -}; - void hid_task(void* params) { (void) params; diff --git a/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c b/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c index 03f80f8c0..699067f73 100644 --- a/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c +++ b/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c @@ -24,6 +24,7 @@ */ #include "tusb.h" +#include "usb_descriptors.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. @@ -79,13 +80,7 @@ uint8_t const * tud_descriptor_device_cb(void) //--------------------------------------------------------------------+ // HID Report Descriptor //--------------------------------------------------------------------+ - #if CFG_TUD_HID -enum -{ - REPORT_ID_KEYBOARD = 1, - REPORT_ID_MOUSE -}; uint8_t const desc_hid_report[] = { diff --git a/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.h b/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.h new file mode 100644 index 000000000..6992d3349 --- /dev/null +++ b/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.h @@ -0,0 +1,34 @@ +/* + * 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. + */ + +#ifndef USB_DESCRIPTORS_H_ +#define USB_DESCRIPTORS_H_ + +enum +{ + REPORT_ID_KEYBOARD = 1, + REPORT_ID_MOUSE +}; + +#endif /* USB_DESCRIPTORS_H_ */ diff --git a/examples/device/webusb_serial/src/usb_descriptors.h b/examples/device/webusb_serial/src/usb_descriptors.h index d45e720df..d05843c3e 100644 --- a/examples/device/webusb_serial/src/usb_descriptors.h +++ b/examples/device/webusb_serial/src/usb_descriptors.h @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2019 hathach for Adafruit Industries + * 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 From af3c301ea5152ea4926e005159f4daa894b5df53 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 30 Jul 2019 00:34:25 +0700 Subject: [PATCH 35/36] added BOS MS OS 2.0 descriptor to webusb example, it work with windows --- examples/device/webusb_serial/src/main.c | 15 +++ .../webusb_serial/src/usb_descriptors.c | 108 ++++++++++++------ .../webusb_serial/src/usb_descriptors.h | 3 + src/common/tusb_types.h | 14 +++ src/device/usbd.h | 25 +++- 5 files changed, 124 insertions(+), 41 deletions(-) diff --git a/examples/device/webusb_serial/src/main.c b/examples/device/webusb_serial/src/main.c index 4990fe24f..d81cf058b 100644 --- a/examples/device/webusb_serial/src/main.c +++ b/examples/device/webusb_serial/src/main.c @@ -153,6 +153,19 @@ bool tud_vendor_control_request_cb(uint8_t rhport, tusb_control_request_t const // Get landing page url return tud_control_xfer(rhport, request, (void*) &desc_url, desc_url.bLength); + case VENDOR_REQUEST_MICROSOFT: + if ( request->wIndex == 7 ) + { + // Get Microsoft OS 2.0 compatible descriptor + uint16_t total_len; + memcpy(&total_len, desc_ms_os_20+8, 2); + + return tud_control_xfer(rhport, request, (void*) desc_ms_os_20, total_len); + }else + { + return false; + } + case 0x22: // Webserial simulate the CDC_REQUEST_SET_CONTROL_LINE_STATE (0x22) to // connect and disconnect. @@ -175,6 +188,8 @@ bool tud_vendor_control_request_cb(uint8_t rhport, tusb_control_request_t const // stall unknown request return false; } + + return true; } // Invoked when DATA Stage of VENDOR's request is complete diff --git a/examples/device/webusb_serial/src/usb_descriptors.c b/examples/device/webusb_serial/src/usb_descriptors.c index 1ae8a61fb..a61c62c93 100644 --- a/examples/device/webusb_serial/src/usb_descriptors.c +++ b/examples/device/webusb_serial/src/usb_descriptors.c @@ -70,38 +70,6 @@ uint8_t const * tud_descriptor_device_cb(void) return (uint8_t const *) &desc_device; } -//--------------------------------------------------------------------+ -// BOS Descriptor -//--------------------------------------------------------------------+ - -/* Microsoft OS 2.0 registry property descriptor -Per MS requirements https://msdn.microsoft.com/en-us/library/windows/hardware/hh450799(v=vs.85).aspx -device should create DeviceInterfaceGUIDs. It can be done by driver and -in case of real PnP solution device should expose MS "Microsoft OS 2.0 -registry property descriptor". Such descriptor can insert any record -into Windows registry per device/configuration/interface. In our case it -will insert "DeviceInterfaceGUIDs" multistring property. - -GUID is freshly generated and should be OK to use. -*/ - -#define BOS_TOTAL_LEN (TUD_BOS_DESC_LEN + TUD_BOS_WEBUSB_DESC_LEN) - -// BOS Descriptor is required for webUSB -uint8_t const desc_bos[] = -{ - // total length, number of device caps - TUD_BOS_DESCRIPTOR(BOS_TOTAL_LEN, 1), - - // Vendor Code, iLandingPage - TUD_BOS_WEBUSB_DESCRIPTOR(VENDOR_REQUEST_WEBUSB, 1) -}; - -uint8_t const * tud_descriptor_bos_cb(void) -{ - return desc_bos; -} - //--------------------------------------------------------------------+ // Configuration Descriptor //--------------------------------------------------------------------+ @@ -146,6 +114,77 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index) return desc_configuration; } +//--------------------------------------------------------------------+ +// BOS Descriptor +//--------------------------------------------------------------------+ + +/* Microsoft OS 2.0 registry property descriptor +Per MS requirements https://msdn.microsoft.com/en-us/library/windows/hardware/hh450799(v=vs.85).aspx +device should create DeviceInterfaceGUIDs. It can be done by driver and +in case of real PnP solution device should expose MS "Microsoft OS 2.0 +registry property descriptor". Such descriptor can insert any record +into Windows registry per device/configuration/interface. In our case it +will insert "DeviceInterfaceGUIDs" multistring property. + +GUID is freshly generated and should be OK to use. + +https://developers.google.com/web/fundamentals/native-hardware/build-for-webusb/ +(Section Microsoft OS compatibility descriptors) +*/ + +#define BOS_TOTAL_LEN (TUD_BOS_DESC_LEN + TUD_BOS_WEBUSB_DESC_LEN + TUD_BOS_MICROSOFT_OS_DESC_LEN) + +#define MS_OS_20_DESC_LEN 0xB2 + +// BOS Descriptor is required for webUSB +uint8_t const desc_bos[] = +{ + // total length, number of device caps + TUD_BOS_DESCRIPTOR(BOS_TOTAL_LEN, 2), + + // Vendor Code, iLandingPage + TUD_BOS_WEBUSB_DESCRIPTOR(VENDOR_REQUEST_WEBUSB, 1), + + // Microsoft OS 2.0 descriptor + TUD_BOS_MS_OS_20_DESCRIPTOR(MS_OS_20_DESC_LEN, VENDOR_REQUEST_MICROSOFT) +}; + +uint8_t const * tud_descriptor_bos_cb(void) +{ + return desc_bos; +} + + +uint8_t const desc_ms_os_20[] = +{ + // Set header: length, type, windows version, total length + U16_TO_U8S_LE(0x000A), U16_TO_U8S_LE(MS_OS_20_SET_HEADER_DESCRIPTOR), U32_TO_U8S_LE(0x06030000), U16_TO_U8S_LE(MS_OS_20_DESC_LEN), + + // Configuration subset header: length, type, configuration index, reserved, configuration total length + U16_TO_U8S_LE(0x0008), U16_TO_U8S_LE(MS_OS_20_SUBSET_HEADER_CONFIGURATION), 0, 0, U16_TO_U8S_LE(MS_OS_20_DESC_LEN-0x0A), + + // Function Subset header: length, type, first interface, reserved, subset length + U16_TO_U8S_LE(0x0008), U16_TO_U8S_LE(MS_OS_20_SUBSET_HEADER_FUNCTION), ITF_NUM_VENDOR, 0, U16_TO_U8S_LE(MS_OS_20_DESC_LEN-0x0A-0x08), + + // MS OS 2.0 Compatible ID descriptor: length, type, compatible ID, sub compatible ID + U16_TO_U8S_LE(0x0014), U16_TO_U8S_LE(MS_OS_20_FEATURE_COMPATBLE_ID), 'W', 'I', 'N', 'U', 'S', 'B', 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // sub-compatible + + // MS OS 2.0 Registry property descriptor: length, type + U16_TO_U8S_LE(MS_OS_20_DESC_LEN-0x0A-0x08-0x08-0x14), U16_TO_U8S_LE(MS_OS_20_FEATURE_REG_PROPERTY), + U16_TO_U8S_LE(0x0007), U16_TO_U8S_LE(0x002A), // wPropertyDataType, wPropertyNameLength and PropertyName "DeviceInterfaceGUIDs\0" in UTF-16 + 'D', 0x00, 'e', 0x00, 'v', 0x00, 'i', 0x00, 'c', 0x00, 'e', 0x00, 'I', 0x00, 'n', 0x00, 't', 0x00, 'e', 0x00, + 'r', 0x00, 'f', 0x00, 'a', 0x00, 'c', 0x00, 'e', 0x00, 'G', 0x00, 'U', 0x00, 'I', 0x00, 'D', 0x00, 's', 0x00, 0x00, 0x00, + U16_TO_U8S_LE(0x0050), // wPropertyDataLength + //bPropertyData: “{975F44D9-0D08-43FD-8B3E-127CA8AFFF9D}”. + '{', 0x00, '9', 0x00, '7', 0x00, '5', 0x00, 'F', 0x00, '4', 0x00, '4', 0x00, 'D', 0x00, '9', 0x00, '-', 0x00, + '0', 0x00, 'D', 0x00, '0', 0x00, '8', 0x00, '-', 0x00, '4', 0x00, '3', 0x00, 'F', 0x00, 'D', 0x00, '-', 0x00, + '8', 0x00, 'B', 0x00, '3', 0x00, 'E', 0x00, '-', 0x00, '1', 0x00, '2', 0x00, '7', 0x00, 'C', 0x00, 'A', 0x00, + '8', 0x00, 'A', 0x00, 'F', 0x00, 'F', 0x00, 'F', 0x00, '9', 0x00, 'D', 0x00, '}', 0x00, 0x00, 0x00, 0x00, 0x00 +}; + +TU_VERIFY_STATIC(sizeof(desc_ms_os_20) == MS_OS_20_DESC_LEN, "Incorrect size"); + //--------------------------------------------------------------------+ // String Descriptors //--------------------------------------------------------------------+ @@ -196,8 +235,3 @@ uint16_t const* tud_descriptor_string_cb(uint8_t index) return _desc_str; } - -//--------------------------------------------------------------------+ -// WebUSB URL Descriptor -//--------------------------------------------------------------------+ - diff --git a/examples/device/webusb_serial/src/usb_descriptors.h b/examples/device/webusb_serial/src/usb_descriptors.h index d05843c3e..19f1ff3f3 100644 --- a/examples/device/webusb_serial/src/usb_descriptors.h +++ b/examples/device/webusb_serial/src/usb_descriptors.h @@ -28,6 +28,9 @@ enum { VENDOR_REQUEST_WEBUSB = 1, + VENDOR_REQUEST_MICROSOFT = 2 }; +extern uint8_t const desc_ms_os_20[]; + #endif /* USB_DESCRIPTORS_H_ */ diff --git a/src/common/tusb_types.h b/src/common/tusb_types.h index 2776eaa87..8358d7e66 100644 --- a/src/common/tusb_types.h +++ b/src/common/tusb_types.h @@ -227,6 +227,20 @@ enum INTERFACE_INVALID_NUMBER = 0xff }; + +enum +{ + MS_OS_20_SET_HEADER_DESCRIPTOR = 0x00, + MS_OS_20_SUBSET_HEADER_CONFIGURATION = 0x01, + MS_OS_20_SUBSET_HEADER_FUNCTION = 0x02, + MS_OS_20_FEATURE_COMPATBLE_ID = 0x03, + MS_OS_20_FEATURE_REG_PROPERTY = 0x04, + MS_OS_20_FEATURE_MIN_RESUME_TIME = 0x05, + MS_OS_20_FEATURE_MODEL_ID = 0x06, + MS_OS_20_FEATURE_CCGP_DEVICE = 0x07, + MS_OS_20_FEATURE_VENDOR_REVISION = 0x08 +}microsoft_os_20_type_t; + //--------------------------------------------------------------------+ // USB Descriptors //--------------------------------------------------------------------+ diff --git a/src/device/usbd.h b/src/device/usbd.h index 525fced8e..6e781f18d 100644 --- a/src/device/usbd.h +++ b/src/device/usbd.h @@ -104,11 +104,11 @@ TU_ATTR_WEAK void tud_resume_cb(void); TU_ATTR_WEAK bool tud_vendor_control_request_cb(uint8_t rhport, tusb_control_request_t const * request); TU_ATTR_WEAK bool tud_vendor_control_complete_cb(uint8_t rhport, tusb_control_request_t const * request); + //--------------------------------------------------------------------+ -// Descriptor Templates +// Binary Device Object Store (BOS) Descriptor Templates //--------------------------------------------------------------------+ -//------------- Binary Device Object Store (BOS) -------------// #define TUD_BOS_DESC_LEN 5 // total length, number of device caps @@ -119,10 +119,10 @@ TU_ATTR_WEAK bool tud_vendor_control_complete_cb(uint8_t rhport, tusb_control_re #define TUD_BOS_PLATFORM_DESCRIPTOR(...) \ 4+TU_ARGS_NUM(__VA_ARGS__), TUSB_DESC_DEVICE_CAPABILITY, DEVICE_CAPABILITY_PLATFORM, 0x00, __VA_ARGS__ -//------------- WebUSB BOS -------------// +//------------- WebUSB BOS Platform -------------// // Descriptor Length -#define TUD_BOS_WEBUSB_DESC_LEN 24 +#define TUD_BOS_WEBUSB_DESC_LEN 24 // Vendor Code, iLandingPage #define TUD_BOS_WEBUSB_DESCRIPTOR(_vendor_code, _ipage) \ @@ -132,6 +132,23 @@ TU_ATTR_WEAK bool tud_vendor_control_complete_cb(uint8_t rhport, tusb_control_re 0x38, 0xB6, 0x08, 0x34, 0xA9, 0x09, 0xA0, 0x47, \ 0x8B, 0xFD, 0xA0, 0x76, 0x88, 0x15, 0xB6, 0x65 +//------------- Microsoft OS 2.0 Platform -------------// + +#define TUD_BOS_MICROSOFT_OS_DESC_LEN 28 + +// Total Length of descriptor set, vendor code +#define TUD_BOS_MS_OS_20_DESCRIPTOR(_desc_set_len, _vendor_code) \ + TUD_BOS_PLATFORM_DESCRIPTOR(TUD_BOS_MS_OS_20_UUID, U32_TO_U8S_LE(0x06030000), U16_TO_U8S_LE(_desc_set_len), _vendor_code, 0) + +#define TUD_BOS_MS_OS_20_UUID \ + 0xDF, 0x60, 0xDD, 0xD8, 0x89, 0x45, 0xC7, 0x4C, \ + 0x9C, 0xD2, 0x65, 0x9D, 0x9E, 0x64, 0x8A, 0x9F + + +//--------------------------------------------------------------------+ +// Configuration & Interface Descriptor Templates +//--------------------------------------------------------------------+ + //------------- Configuration -------------// #define TUD_CONFIG_DESC_LEN (9) From 00a9e492cdf04ea17379218996d2713bcaaaff51 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 30 Jul 2019 00:48:31 +0700 Subject: [PATCH 36/36] clean up --- examples/device/webusb_serial/src/main.c | 4 +++- src/class/vendor/vendor_device.c | 2 +- src/class/vendor/vendor_device.h | 19 ++++++++++++++++--- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/examples/device/webusb_serial/src/main.c b/examples/device/webusb_serial/src/main.c index d81cf058b..e1a098048 100644 --- a/examples/device/webusb_serial/src/main.c +++ b/examples/device/webusb_serial/src/main.c @@ -176,6 +176,8 @@ bool tud_vendor_control_request_cb(uint8_t rhport, tusb_control_request_t const { board_led_write(true); blink_interval_ms = BLINK_ALWAYS_ON; + + tud_vendor_write_str("\r\nTinyUSB WebUSB device example\r\n"); }else { blink_interval_ms = BLINK_MOUNTED; @@ -247,7 +249,7 @@ void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) if ( dtr && rts ) { // print initial message when connected - tud_cdc_write_str("\r\nTinyUSB CDC MSC HID device example\r\n"); + tud_cdc_write_str("\r\nTinyUSB WebUSB device example\r\n"); } } diff --git a/src/class/vendor/vendor_device.c b/src/class/vendor/vendor_device.c index 2cd815fb4..33fb98470 100644 --- a/src/class/vendor/vendor_device.c +++ b/src/class/vendor/vendor_device.c @@ -113,7 +113,7 @@ static bool maybe_transmit(vendord_interface_t* p_itf) return true; } -uint32_t tud_vendor_n_write (uint8_t itf, uint8_t const* buffer, uint32_t bufsize) +uint32_t tud_vendor_n_write (uint8_t itf, void const* buffer, uint32_t bufsize) { vendord_interface_t* p_itf = &_vendord_itf[itf]; uint16_t ret = tu_fifo_write_n(&p_itf->tx_ff, buffer, bufsize); diff --git a/src/class/vendor/vendor_device.h b/src/class/vendor/vendor_device.h index 08deba5ab..d214c29ee 100644 --- a/src/class/vendor/vendor_device.h +++ b/src/class/vendor/vendor_device.h @@ -52,7 +52,9 @@ bool tud_vendor_n_mounted (uint8_t itf); uint32_t tud_vendor_n_available (uint8_t itf); uint32_t tud_vendor_n_read (uint8_t itf, void* buffer, uint32_t bufsize); -uint32_t tud_vendor_n_write (uint8_t itf, uint8_t const* buffer, uint32_t bufsize); +uint32_t tud_vendor_n_write (uint8_t itf, void const* buffer, uint32_t bufsize); + +static inline uint32_t tud_vendor_n_write_str (uint8_t itf, char const* str); //--------------------------------------------------------------------+ // Application API (Single Port) @@ -60,7 +62,8 @@ uint32_t tud_vendor_n_write (uint8_t itf, uint8_t const* buffer, uint32_t b static inline bool tud_vendor_mounted (void); static inline uint32_t tud_vendor_available (void); static inline uint32_t tud_vendor_read (void* buffer, uint32_t bufsize); -static inline uint32_t tud_vendor_write (uint8_t const* buffer, uint32_t bufsize); +static inline uint32_t tud_vendor_write (void const* buffer, uint32_t bufsize); +static inline uint32_t tud_vendor_write_str (char const* str); //--------------------------------------------------------------------+ // Application Callback API (weak is optional) @@ -73,6 +76,11 @@ TU_ATTR_WEAK void tud_vendor_rx_cb(uint8_t itf); // Inline Functions //--------------------------------------------------------------------+ +static inline uint32_t tud_vendor_n_write_str (uint8_t itf, char const* str) +{ + return tud_vendor_n_write(itf, str, strlen(str)); +} + static inline bool tud_vendor_mounted (void) { return tud_vendor_n_mounted(0); @@ -88,11 +96,16 @@ static inline uint32_t tud_vendor_read (void* buffer, uint32_t bufsize) return tud_vendor_n_read(0, buffer, bufsize); } -static inline uint32_t tud_vendor_write (uint8_t const* buffer, uint32_t bufsize) +static inline uint32_t tud_vendor_write (void const* buffer, uint32_t bufsize) { return tud_vendor_n_write(0, buffer, bufsize); } +static inline uint32_t tud_vendor_write_str (char const* str) +{ + return tud_vendor_n_write_str(0, str); +} + //--------------------------------------------------------------------+ // Internal Class Driver API //--------------------------------------------------------------------+