Skip to content

Commit c45cff5

Browse files
authored
Implement USB HID Device Support for ESP32-S2 (#5538)
* Add support and example for USB HID Devices * Add support and example for USB Vendor
1 parent b1d072d commit c45cff5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+3258
-46
lines changed

CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ set(LIBRARY_SRCS
7676
libraries/Ticker/src/Ticker.cpp
7777
libraries/Update/src/Updater.cpp
7878
libraries/Update/src/HttpsOTAUpdate.cpp
79+
libraries/USB/src/USBHID.cpp
80+
libraries/USB/src/USBHIDMouse.cpp
81+
libraries/USB/src/USBHIDKeyboard.cpp
82+
libraries/USB/src/USBHIDGamepad.cpp
83+
libraries/USB/src/USBHIDConsumerControl.cpp
84+
libraries/USB/src/USBHIDSystemControl.cpp
85+
libraries/USB/src/USBHIDVendor.cpp
86+
libraries/USB/src/USBVendor.cpp
7987
libraries/WebServer/src/WebServer.cpp
8088
libraries/WebServer/src/Parsing.cpp
8189
libraries/WebServer/src/detail/mimetable.cpp

cores/esp32/FirmwareMSC.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14+
#include "FirmwareMSC.h"
15+
16+
#if CONFIG_TINYUSB_MSC_ENABLED
1417

1518
#include <cstring>
16-
#include "FirmwareMSC.h"
1719
#include "esp_partition.h"
1820
#include "esp_ota_ops.h"
1921
#include "esp32-hal.h"
2022
#include "pins_arduino.h"
2123
#include "firmware_msc_fat.h"
2224

23-
#if CONFIG_TINYUSB_MSC_ENABLED
24-
2525
#ifndef USB_FW_MSC_VENDOR_ID
2626
#define USB_FW_MSC_VENDOR_ID "ESP32" //max 8 chars
2727
#endif

cores/esp32/FirmwareMSC.h

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#pragma once
1616
#include <stdbool.h>
1717
#include "USBMSC.h"
18+
1819
#if CONFIG_TINYUSB_MSC_ENABLED
1920

2021
#include "esp_event.h"

cores/esp32/USB.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14-
#include "esp32-hal.h"
15-
#include "esp32-hal-tinyusb.h"
1614
#include "USB.h"
15+
1716
#if CONFIG_TINYUSB_ENABLED
1817

18+
#include "esp32-hal.h"
19+
#include "esp32-hal-tinyusb.h"
20+
#include "common/tusb_common.h"
21+
1922
#ifndef USB_VID
2023
#define USB_VID USB_ESPRESSIF_VID
2124
#endif

cores/esp32/USB.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@
1414
#pragma once
1515

1616
#include "sdkconfig.h"
17+
1718
#if CONFIG_TINYUSB_ENABLED
1819

19-
#include "Arduino.h"
20-
#include "USBCDC.h"
21-
#include "common/tusb_common.h"
2220
#include "esp_event.h"
21+
#include "USBCDC.h"
2322

2423
#define ARDUINO_USB_ON_BOOT (ARDUINO_USB_CDC_ON_BOOT|ARDUINO_USB_MSC_ON_BOOT|ARDUINO_USB_DFU_ON_BOOT)
2524

cores/esp32/USBCDC.cpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,16 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14-
#include "esp32-hal.h"
15-
#include "esp32-hal-tinyusb.h"
1614
#include "USB.h"
15+
#if CONFIG_TINYUSB_CDC_ENABLED
16+
1717
#include "USBCDC.h"
18-
#if CONFIG_TINYUSB_ENABLED
18+
#include "esp32-hal-tinyusb.h"
1919

2020
ESP_EVENT_DEFINE_BASE(ARDUINO_USB_CDC_EVENTS);
2121
esp_err_t arduino_usb_event_post(esp_event_base_t event_base, int32_t event_id, void *event_data, size_t event_data_size, TickType_t ticks_to_wait);
2222
esp_err_t arduino_usb_event_handler_register_with(esp_event_base_t event_base, int32_t event_id, esp_event_handler_t event_handler, void *event_handler_arg);
2323

24-
#if CFG_TUD_CDC
2524
#define MAX_USB_CDC_DEVICES 2
2625
USBCDC * devices[MAX_USB_CDC_DEVICES] = {NULL, NULL};
2726

@@ -389,5 +388,3 @@ USBCDC Serial(0);
389388
#endif
390389

391390
#endif /* CONFIG_TINYUSB_CDC_ENABLED */
392-
393-
#endif /* CONFIG_TINYUSB_ENABLED */

cores/esp32/USBCDC.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313
// limitations under the License.
1414
#pragma once
1515

16-
#include <inttypes.h>
17-
18-
#include "Stream.h"
19-
#include "esp32-hal.h"
16+
#include "sdkconfig.h"
2017
#if CONFIG_TINYUSB_CDC_ENABLED
2118

19+
#include <inttypes.h>
2220
#include "esp_event.h"
21+
#include "Stream.h"
2322

2423
ESP_EVENT_DECLARE_BASE(ARDUINO_USB_CDC_EVENTS);
2524

cores/esp32/USBMSC.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14+
#include "USBMSC.h"
15+
16+
#if CONFIG_TINYUSB_MSC_ENABLED
1417

15-
#include "esp32-hal.h"
1618
#include "esp32-hal-tinyusb.h"
17-
#include "USBMSC.h"
1819

19-
#if CFG_TUD_MSC
2020
extern "C" uint16_t tusb_msc_load_descriptor(uint8_t * dst, uint8_t * itf)
2121
{
2222
uint8_t str_index = tinyusb_add_string_descriptor("TinyUSB MSC");
@@ -257,4 +257,4 @@ void USBMSC::mediaPresent(bool media_present){
257257
msc_luns[_lun].media_present = media_present;
258258
}
259259

260-
#endif /* CONFIG_USB_MSC_ENABLED */
260+
#endif /* CONFIG_TINYUSB_MSC_ENABLED */

cores/esp32/USBMSC.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
#pragma once
1616
#include <stdint.h>
1717
#include <stdbool.h>
18-
#include "esp32-hal.h"
18+
#include "sdkconfig.h"
19+
1920
#if CONFIG_TINYUSB_MSC_ENABLED
2021

2122
// Invoked when received Start Stop Unit command
@@ -46,4 +47,5 @@ class USBMSC
4647
private:
4748
uint8_t _lun;
4849
};
49-
#endif
50+
51+
#endif /* CONFIG_TINYUSB_MSC_ENABLED */

cores/esp32/esp32-hal-tinyusb.c

+8-7
Original file line numberDiff line numberDiff line change
@@ -424,12 +424,6 @@ static bool tinyusb_load_enabled_interfaces(){
424424
log_e("Descriptor Load Failed");
425425
return false;
426426
} else {
427-
if(i == USB_INTERFACE_CDC){
428-
if(!tinyusb_reserve_out_endpoint(3) ||!tinyusb_reserve_in_endpoint(4) || !tinyusb_reserve_in_endpoint(5)){
429-
log_e("CDC Reserve Endpoints Failed");
430-
return false;
431-
}
432-
}
433427
dst += len;
434428
}
435429
}
@@ -505,6 +499,7 @@ static void tinyusb_apply_device_config(tinyusb_device_config_t *config){
505499
&& (config->usb_class != TUSB_CLASS_CDC)
506500
){
507501
config->usb_class = TUSB_CLASS_CDC;
502+
config->usb_protocol = 0x00;
508503
}
509504

510505
WEBUSB_ENABLED = config->webusb_enabled;
@@ -573,6 +568,12 @@ esp_err_t tinyusb_enable_interface(tinyusb_interface_t interface, uint16_t descr
573568
log_e("Interface %s invalid or already enabled", (interface >= USB_INTERFACE_MAX)?"":tinyusb_interface_names[interface]);
574569
return ESP_FAIL;
575570
}
571+
if(interface == USB_INTERFACE_CDC){
572+
if(!tinyusb_reserve_out_endpoint(3) ||!tinyusb_reserve_in_endpoint(4) || !tinyusb_reserve_in_endpoint(5)){
573+
log_e("CDC Reserve Endpoints Failed");
574+
return ESP_FAIL;
575+
}
576+
}
576577
tinyusb_loaded_interfaces_mask |= (1U << interface);
577578
tinyusb_config_descriptor_len += descriptor_len;
578579
tinyusb_loaded_interfaces_callbacks[interface] = cb;
@@ -586,7 +587,7 @@ esp_err_t tinyusb_init(tinyusb_device_config_t *config) {
586587
}
587588
tinyusb_is_initialized = true;
588589

589-
tinyusb_endpoints.val = 0;
590+
//tinyusb_endpoints.val = 0;
590591
tinyusb_apply_device_config(config);
591592
if (!tinyusb_load_enabled_interfaces()) {
592593
tinyusb_is_initialized = false;

libraries/USB/examples/CompositeDevice/.skip.esp32

Whitespace-only changes.

libraries/USB/examples/CompositeDevice/.skip.esp32c3

Whitespace-only changes.

0 commit comments

Comments
 (0)