Skip to content

Commit 7e8d391

Browse files
committed
Merge branch 'feat/add_self_power_device_example' into 'master'
feat(usb): add self powered device config Closes AEG-508 See merge request ae_group/esp-iot-solution!1350
2 parents 6329721 + 868c6d6 commit 7e8d391

File tree

10 files changed

+90
-47
lines changed

10 files changed

+90
-47
lines changed

.gitlab/ci/build.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -869,20 +869,15 @@ build_example_usb_device_usb_msc_wireless_disk:
869869
extends:
870870
- .build_examples_template
871871
- .rules:build:example_usb_device_usb_msc_wireless_disk
872-
parallel:
873-
matrix:
874-
- IMAGE: espressif/idf:release-v4.4
875-
- IMAGE: espressif/idf:release-v5.0
872+
- .build_idf_version_greater_equal_v5_0
876873
variables:
877874
EXAMPLE_DIR: examples/usb/device/usb_msc_wireless_disk
878875

879876
build_example_usb_device_usb_surface_dial:
880877
extends:
881878
- .build_examples_template
882879
- .rules:build:example_usb_device_usb_surface_dial
883-
parallel:
884-
matrix:
885-
- IMAGE: espressif/idf:release-v5.0
880+
- .build_idf_version_greater_equal_v5_0
886881
variables:
887882
EXAMPLE_DIR: examples/usb/device/usb_surface_dial
888883

docs/en/usb/usb_overview/usb_device_self_power.rst

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,6 @@ The internal USB PHY of ESP32S2/S3 does not support hardware detection logic, th
5959
.. code-block:: C
6060
6161
#define VBUS_MONITORING_GPIO_NUM GPIO_NUM_4
62-
// Configure GPIO Pin for vbus monitoring
63-
const gpio_config_t vbus_gpio_config = {
64-
.pin_bit_mask = BIT64(VBUS_MONITORING_GPIO_NUM),
65-
.mode = GPIO_MODE_INPUT,
66-
.intr_type = GPIO_INTR_DISABLE,
67-
.pull_up_en = false,
68-
.pull_down_en = false,
69-
};
70-
ESP_ERROR_CHECK(gpio_config(&vbus_gpio_config));
7162
const tinyusb_config_t tusb_cfg = {
7263
.device_descriptor = &descriptor_config,
7364
.string_descriptor = string_desc_arr,
@@ -78,3 +69,11 @@ The internal USB PHY of ESP32S2/S3 does not support hardware detection logic, th
7869
.vbus_monitor_io = VBUS_MONITORING_GPIO_NUM,
7970
};
8071
ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
72+
73+
If you are using native TinyUSB development, you need to configure this during the PHY initialization stage:
74+
75+
.. code-block:: C
76+
77+
const usb_phy_otg_io_conf_t otg_io_conf = USB_PHY_SELF_POWERED_DEVICE(VBUS_MONITORING_GPIO_NUM);
78+
phy_conf.otg_io_conf = &otg_io_conf;
79+
usb_new_phy(&phy_conf, &s_phy_hdl);

docs/zh_CN/usb/usb_overview/usb_device_self_power.rst

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,6 @@ USB 设备 VBUS 检测方法一般有两种方法:由 USB PHY 硬件检测,
5959
.. code-block:: C
6060
6161
#define VBUS_MONITORING_GPIO_NUM GPIO_NUM_4
62-
// Configure GPIO Pin for vbus monitoring
63-
const gpio_config_t vbus_gpio_config = {
64-
.pin_bit_mask = BIT64(VBUS_MONITORING_GPIO_NUM),
65-
.mode = GPIO_MODE_INPUT,
66-
.intr_type = GPIO_INTR_DISABLE,
67-
.pull_up_en = false,
68-
.pull_down_en = false,
69-
};
70-
ESP_ERROR_CHECK(gpio_config(&vbus_gpio_config));
7162
const tinyusb_config_t tusb_cfg = {
7263
.device_descriptor = &descriptor_config,
7364
.string_descriptor = string_desc_arr,
@@ -78,3 +69,11 @@ USB 设备 VBUS 检测方法一般有两种方法:由 USB PHY 硬件检测,
7869
.vbus_monitor_io = VBUS_MONITORING_GPIO_NUM,
7970
};
8071
ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
72+
73+
若使用原生 TinyUSB 开发,则需要在 Phy 初始化阶段进行配置:
74+
75+
.. code-block:: C
76+
77+
const usb_phy_otg_io_conf_t otg_io_conf = USB_PHY_SELF_POWERED_DEVICE(VBUS_MONITORING_GPIO_NUM);
78+
phy_conf.otg_io_conf = &otg_io_conf;
79+
usb_new_phy(&phy_conf, &s_phy_hdl);

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,21 @@ menu "USB MSC Device Demo"
141141
Set the Maximum retry to avoid station reconnecting to the softAP unlimited when the softAP is really inexistent.
142142
endmenu
143143

144+
config SELF_POWERED_DEVICE
145+
bool "Self-powered device"
146+
default n
147+
help
148+
Select 'y' if the device is self-powered and does not draw power from USB VBUS.
149+
150+
config VBUS_MONITOR_IO
151+
int "VBUS monitor GPIO number"
152+
depends on SELF_POWERED_DEVICE
153+
range 0 46 if IDF_TARGET_ESP32S2
154+
range 0 48 if IDF_TARGET_ESP32S3
155+
default 2
156+
help
157+
Connect VBUS to a voltage comparator chip/circuit that can detect
158+
the thresholds (4.35 V and 4.75 V) and output a 3.3 V logic level
159+
to the ESP32-S2/S3, indicating whether VBUS is valid.
160+
144161
endmenu

examples/usb/device/usb_msc_wireless_disk/main/app_main.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
1+
/* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
22
*
33
* SPDX-License-Identifier: Apache-2.0
44
*/
@@ -169,7 +169,19 @@ void app_main(void)
169169

170170
ESP_LOGI(TAG, "USB MSC initialization");
171171

172-
const tinyusb_config_t tusb_cfg = {0};
172+
tinyusb_config_t tusb_cfg = {0};
173+
174+
#if CONFIG_SELF_POWERED_DEVICE
175+
tusb_cfg.self_powered = true;
176+
tusb_cfg.vbus_monitor_io = CONFIG_VBUS_MONITOR_IO;
177+
178+
static const uint8_t descriptor_fs_cfg[] = {
179+
// Configuration number, interface count, string index, total length, attribute, power in mA
180+
TUD_CONFIG_DESCRIPTOR(1, 1, 0, (TUD_CONFIG_DESC_LEN + CFG_TUD_MSC * TUD_MSC_DESC_LEN), TUSB_DESC_CONFIG_ATT_SELF_POWERED, 100),
181+
TUD_MSC_DESCRIPTOR(0, 4, 0x01, 0x81, 64),
182+
};
183+
tusb_cfg.configuration_descriptor = descriptor_fs_cfg;
184+
#endif
173185

174186
ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
175187
ESP_LOGI(TAG, "USB MSC initialization DONE");
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
## IDF Component Manager Manifest File
22
dependencies:
33
espressif/esp_tinyusb:
4-
version: "~1.1.0"
4+
version: "^1.7.6~1"
55
rules:
66
- if: "idf_version >=5.0"
7-
idf: ">=4.4.0"
7+
idf: ">=5.0"
88
espressif/esp32_s3_usb_otg:
9-
version: "^1.5.1"
9+
version: "^2.0.0"
1010
rules:
1111
- if: "target in [esp32s3]"

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,21 @@ menu "Example Configuration"
2121

2222
endchoice
2323

24+
config SELF_POWERED_DEVICE
25+
bool "Self-powered device"
26+
default n
27+
help
28+
Select 'y' if the device is self-powered and does not draw power from USB VBUS.
29+
30+
config VBUS_MONITOR_IO
31+
int "VBUS monitor GPIO number"
32+
depends on SELF_POWERED_DEVICE
33+
range 0 46 if IDF_TARGET_ESP32S2
34+
range 0 48 if IDF_TARGET_ESP32S3
35+
default 2
36+
help
37+
Connect VBUS to a voltage comparator chip/circuit that can detect
38+
the thresholds (4.35 V and 4.75 V) and output a 3.3 V logic level
39+
to the ESP32-S2/S3, indicating whether VBUS is valid.
40+
2441
endmenu

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@ dependencies:
44
idf:
55
version: ">=5.0"
66
espressif/button:
7-
version: ">=2.3.0"
8-
override_path: "../../../../../components/button"
7+
version: ">=4.1.0"
98
espressif/tinyusb:
10-
version: ">=0.15.0~2"
9+
version: ">=0.18.0~1"
1110
espressif/knob:
12-
version: ">=0.1.0"
13-
override_path: "../../../../../components/knob"
11+
version: ">=1.0.0"
1412
espressif/esp32_s3_usb_otg:
15-
version: "^1.5.1"
13+
version: ">=2.0.0"
1614
rules:
1715
- if: "target in [esp32s3]"
18-
lvgl/lvgl: #temp to workaround bsp issue
19-
version: "9.2.0"

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ static void usb_phy_init(void)
4646
.otg_mode = USB_OTG_MODE_DEVICE,
4747
.target = USB_PHY_TARGET_INT,
4848
};
49+
#if CONFIG_SELF_POWERED_DEVICE
50+
const usb_phy_otg_io_conf_t otg_io_conf = USB_PHY_SELF_POWERED_DEVICE(CONFIG_VBUS_MONITOR_IO);
51+
phy_conf.otg_io_conf = &otg_io_conf;
52+
#endif
4953
usb_new_phy(&phy_conf, &s_phy_hdl);
5054
}
5155

@@ -86,19 +90,19 @@ static void _knob_left_cb(void *arg, void *data)
8690

8791
static void _button_init(void)
8892
{
89-
button_config_t cfg = {
90-
.type = BUTTON_TYPE_GPIO,
93+
button_config_t btn_cfg = {
9194
.long_press_time = 1000,
9295
.short_press_time = 200,
93-
.gpio_button_config = {
94-
.gpio_num = GPIO_BUTTON,
95-
.active_level = 0,
96-
},
9796
};
98-
s_btn = iot_button_create(&cfg);
9997

100-
iot_button_register_cb(s_btn, BUTTON_PRESS_DOWN, _button_press_down_cb, NULL);
101-
iot_button_register_cb(s_btn, BUTTON_PRESS_UP, _button_press_up_cb, NULL);
98+
button_gpio_config_t btn_gpio_cfg = {
99+
.gpio_num = GPIO_BUTTON,
100+
.active_level = 0,
101+
};
102+
103+
ESP_ERROR_CHECK(iot_button_new_gpio_device(&btn_cfg, &btn_gpio_cfg, &s_btn));
104+
iot_button_register_cb(s_btn, BUTTON_PRESS_DOWN, NULL, _button_press_down_cb, NULL);
105+
iot_button_register_cb(s_btn, BUTTON_PRESS_UP, NULL, _button_press_up_cb, NULL);
102106
}
103107

104108
static void _knob_init(void)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,11 @@ enum {
130130

131131
uint8_t const desc_configuration[] = {
132132
// Config number, interface count, string index, total length, attribute, power in mA
133+
#if CONFIG_SELF_POWERED_DEVICE
134+
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_SELF_POWERED, 100),
135+
#else
133136
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0, 100),
137+
#endif
134138

135139
// Interface number, string index, protocol, report descriptor len, EP In address, size & polling interval
136140
TUD_HID_DESCRIPTOR(ITF_NUM_HID, 4, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 10),

0 commit comments

Comments
 (0)