Skip to content

Commit e7ea5f7

Browse files
committed
Merge branch 'feat/usb_serial_dial_support_hs' into 'master'
feat(usb_surface_dial): support usb high speed See merge request ae_group/esp-iot-solution!1416
2 parents cd3e7aa + c5a8a92 commit e7ea5f7

File tree

5 files changed

+79
-16
lines changed

5 files changed

+79
-16
lines changed

examples/usb/device/usb_surface_dial/main/Kconfig.projbuild

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
menu "Example Configuration"
22

3+
choice TINYUSB_RHPORT
4+
prompt "TinyUSB Root Port"
5+
default TINYUSB_RHPORT_HS if IDF_TARGET_ESP32P4
6+
default TINYUSB_RHPORT_FS
7+
help
8+
Allows set the USB PHY Controller for TinyUSB: HS (USB OTG2.0 PHY for HighSpeed)
9+
10+
config TINYUSB_RHPORT_HS
11+
depends on IDF_TARGET_ESP32P4
12+
bool "High Speed"
13+
14+
config TINYUSB_RHPORT_FS
15+
bool "Full Speed"
16+
endchoice
17+
318
choice DEVELOPMENT_BOARD_SELECTION
419
prompt "Select the development board you are using"
520
default ESP32_S3_USB_OTG if IDF_TARGET_ESP32S3

examples/usb/device/usb_surface_dial/main/idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ dependencies:
88
espressif/tinyusb:
99
version: ">=0.18.0~1"
1010
espressif/knob:
11-
version: ">=1.0.0"
11+
version: ">=1.0.1"
1212
espressif/esp32_s3_usb_otg:
1313
version: ">=2.0.0"
1414
rules:

examples/usb/device/usb_surface_dial/main/main.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ static void usb_phy_init(void)
4545
.controller = USB_PHY_CTRL_OTG,
4646
.otg_mode = USB_OTG_MODE_DEVICE,
4747
.target = USB_PHY_TARGET_INT,
48+
#if CONFIG_TINYUSB_RHPORT_HS
49+
.otg_speed = USB_PHY_SPEED_HIGH,
50+
#endif
4851
};
4952
#if CONFIG_SELF_POWERED_DEVICE
5053
const usb_phy_otg_io_conf_t otg_io_conf = USB_PHY_SELF_POWERED_DEVICE(CONFIG_VBUS_MONITOR_IO);
@@ -130,7 +133,11 @@ void app_main(void)
130133
_button_init();
131134
_knob_init();
132135

133-
tusb_init();
136+
bool usb_init = tusb_init();
137+
if (!usb_init) {
138+
ESP_LOGE(TAG, "USB Device Stack Init Fail");
139+
return;
140+
}
134141

135142
size_t timeout_tick = pdMS_TO_TICKS(10);
136143
while (1) {

examples/usb/device/usb_surface_dial/main/tusb_config.h

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#ifndef _TUSB_CONFIG_H_
2727
#define _TUSB_CONFIG_H_
2828

29+
#include "sdkconfig.h"
30+
2931
#ifdef __cplusplus
3032
extern "C" {
3133
#endif
@@ -34,14 +36,12 @@ extern "C" {
3436
// Board Specific Configuration
3537
//--------------------------------------------------------------------+
3638

37-
// RHPort number used for device can be defined by board.mk, default to port 0
38-
#ifndef BOARD_TUD_RHPORT
39-
#define BOARD_TUD_RHPORT 0
40-
#endif
41-
42-
// RHPort max operational speed can defined by board.mk
43-
#ifndef BOARD_TUD_MAX_SPEED
44-
#define BOARD_TUD_MAX_SPEED OPT_MODE_DEFAULT_SPEED
39+
#ifdef CONFIG_TINYUSB_RHPORT_HS
40+
# define CFG_TUSB_RHPORT1_MODE OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED
41+
# define CONFIG_USB_HS 1
42+
#else
43+
# define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE | OPT_MODE_FULL_SPEED
44+
# define CONFIG_USB_HS 0
4545
#endif
4646

4747
//--------------------------------------------------------------------
@@ -53,8 +53,6 @@ extern "C" {
5353
#error CFG_TUSB_MCU must be defined
5454
#endif
5555

56-
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
57-
5856
#ifndef CFG_TUSB_OS
5957
#define CFG_TUSB_OS OPT_OS_FREERTOS
6058
#endif
@@ -64,7 +62,7 @@ extern "C" {
6462
#endif
6563

6664
// Espressif IDF requires "freertos/" prefix in include path
67-
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
65+
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3, OPT_MCU_ESP32P4)
6866
#define CFG_TUSB_OS_INC_PATH freertos/
6967
#endif
7068

@@ -75,9 +73,6 @@ extern "C" {
7573
// Enable Device stack
7674
#define CFG_TUD_ENABLED 1
7775

78-
// Default is max speed that hardware controller could support with on-chip PHY
79-
#define CFG_TUD_MAX_SPEED BOARD_TUD_MAX_SPEED
80-
8176
/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
8277
* Tinyusb use follows macros to declare transferring memory so that they can be put
8378
* into those specific section.

examples/usb/device/usb_surface_dial/main/usb_descriptors.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,52 @@ uint8_t const desc_configuration[] = {
140140
TUD_HID_DESCRIPTOR(ITF_NUM_HID, 4, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 10),
141141
};
142142

143+
#if TUD_OPT_HIGH_SPEED
144+
// Per USB specs: high speed capable device must report device_qualifier and other_speed_configuration
145+
146+
// other speed configuration
147+
uint8_t desc_other_speed_config[CONFIG_TOTAL_LEN];
148+
149+
// device qualifier is mostly similar to device descriptor since we don't change configuration based on speed
150+
tusb_desc_device_qualifier_t const desc_device_qualifier = {
151+
.bLength = sizeof(tusb_desc_device_qualifier_t),
152+
.bDescriptorType = TUSB_DESC_DEVICE_QUALIFIER,
153+
.bcdUSB = 0x0200,
154+
155+
.bDeviceClass = 0x00,
156+
.bDeviceSubClass = 0x00,
157+
.bDeviceProtocol = 0x00,
158+
159+
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
160+
.bNumConfigurations = 0x01,
161+
.bReserved = 0x00
162+
};
163+
164+
// Invoked when received GET DEVICE QUALIFIER DESCRIPTOR request
165+
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete.
166+
// device_qualifier descriptor describes information about a high-speed capable device that would
167+
// change if the device were operating at the other speed. If not highspeed capable stall this request.
168+
uint8_t const* tud_descriptor_device_qualifier_cb(void)
169+
{
170+
return (uint8_t const*) &desc_device_qualifier;
171+
}
172+
173+
// Invoked when received GET OTHER SEED CONFIGURATION DESCRIPTOR request
174+
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
175+
// Configuration descriptor in the other speed e.g if high speed then this is for full speed and vice versa
176+
uint8_t const* tud_descriptor_other_speed_configuration_cb(uint8_t index)
177+
{
178+
(void) index; // for multiple configurations
179+
180+
// other speed config is basically configuration with type = OHER_SPEED_CONFIG
181+
memcpy(desc_other_speed_config, desc_configuration, CONFIG_TOTAL_LEN);
182+
desc_other_speed_config[1] = TUSB_DESC_OTHER_SPEED_CONFIG;
183+
184+
// this example use the same configuration for both high and full speed mode
185+
return desc_other_speed_config;
186+
}
187+
#endif
188+
143189
// Invoked when received GET CONFIGURATION DESCRIPTOR
144190
// Application return pointer to descriptor
145191
// Descriptor contents must exist long enough for transfer to complete

0 commit comments

Comments
 (0)