Skip to content

Commit

Permalink
Implement USB HID Device Support for ESP32-S2 (#5538)
Browse files Browse the repository at this point in the history
* Add support and example for USB HID Devices
* Add support and example for USB Vendor
  • Loading branch information
me-no-dev authored Aug 23, 2021
1 parent b1d072d commit c45cff5
Show file tree
Hide file tree
Showing 66 changed files with 3,258 additions and 46 deletions.
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ set(LIBRARY_SRCS
libraries/Ticker/src/Ticker.cpp
libraries/Update/src/Updater.cpp
libraries/Update/src/HttpsOTAUpdate.cpp
libraries/USB/src/USBHID.cpp
libraries/USB/src/USBHIDMouse.cpp
libraries/USB/src/USBHIDKeyboard.cpp
libraries/USB/src/USBHIDGamepad.cpp
libraries/USB/src/USBHIDConsumerControl.cpp
libraries/USB/src/USBHIDSystemControl.cpp
libraries/USB/src/USBHIDVendor.cpp
libraries/USB/src/USBVendor.cpp
libraries/WebServer/src/WebServer.cpp
libraries/WebServer/src/Parsing.cpp
libraries/WebServer/src/detail/mimetable.cpp
Expand Down
6 changes: 3 additions & 3 deletions cores/esp32/FirmwareMSC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "FirmwareMSC.h"

#if CONFIG_TINYUSB_MSC_ENABLED

#include <cstring>
#include "FirmwareMSC.h"
#include "esp_partition.h"
#include "esp_ota_ops.h"
#include "esp32-hal.h"
#include "pins_arduino.h"
#include "firmware_msc_fat.h"

#if CONFIG_TINYUSB_MSC_ENABLED

#ifndef USB_FW_MSC_VENDOR_ID
#define USB_FW_MSC_VENDOR_ID "ESP32" //max 8 chars
#endif
Expand Down
1 change: 1 addition & 0 deletions cores/esp32/FirmwareMSC.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#pragma once
#include <stdbool.h>
#include "USBMSC.h"

#if CONFIG_TINYUSB_MSC_ENABLED

#include "esp_event.h"
Expand Down
7 changes: 5 additions & 2 deletions cores/esp32/USB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "esp32-hal.h"
#include "esp32-hal-tinyusb.h"
#include "USB.h"

#if CONFIG_TINYUSB_ENABLED

#include "esp32-hal.h"
#include "esp32-hal-tinyusb.h"
#include "common/tusb_common.h"

#ifndef USB_VID
#define USB_VID USB_ESPRESSIF_VID
#endif
Expand Down
5 changes: 2 additions & 3 deletions cores/esp32/USB.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
#pragma once

#include "sdkconfig.h"

#if CONFIG_TINYUSB_ENABLED

#include "Arduino.h"
#include "USBCDC.h"
#include "common/tusb_common.h"
#include "esp_event.h"
#include "USBCDC.h"

#define ARDUINO_USB_ON_BOOT (ARDUINO_USB_CDC_ON_BOOT|ARDUINO_USB_MSC_ON_BOOT|ARDUINO_USB_DFU_ON_BOOT)

Expand Down
9 changes: 3 additions & 6 deletions cores/esp32/USBCDC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "esp32-hal.h"
#include "esp32-hal-tinyusb.h"
#include "USB.h"
#if CONFIG_TINYUSB_CDC_ENABLED

#include "USBCDC.h"
#if CONFIG_TINYUSB_ENABLED
#include "esp32-hal-tinyusb.h"

ESP_EVENT_DEFINE_BASE(ARDUINO_USB_CDC_EVENTS);
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);
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);

#if CFG_TUD_CDC
#define MAX_USB_CDC_DEVICES 2
USBCDC * devices[MAX_USB_CDC_DEVICES] = {NULL, NULL};

Expand Down Expand Up @@ -389,5 +388,3 @@ USBCDC Serial(0);
#endif

#endif /* CONFIG_TINYUSB_CDC_ENABLED */

#endif /* CONFIG_TINYUSB_ENABLED */
7 changes: 3 additions & 4 deletions cores/esp32/USBCDC.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
// limitations under the License.
#pragma once

#include <inttypes.h>

#include "Stream.h"
#include "esp32-hal.h"
#include "sdkconfig.h"
#if CONFIG_TINYUSB_CDC_ENABLED

#include <inttypes.h>
#include "esp_event.h"
#include "Stream.h"

ESP_EVENT_DECLARE_BASE(ARDUINO_USB_CDC_EVENTS);

Expand Down
8 changes: 4 additions & 4 deletions cores/esp32/USBMSC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "USBMSC.h"

#if CONFIG_TINYUSB_MSC_ENABLED

#include "esp32-hal.h"
#include "esp32-hal-tinyusb.h"
#include "USBMSC.h"

#if CFG_TUD_MSC
extern "C" uint16_t tusb_msc_load_descriptor(uint8_t * dst, uint8_t * itf)
{
uint8_t str_index = tinyusb_add_string_descriptor("TinyUSB MSC");
Expand Down Expand Up @@ -257,4 +257,4 @@ void USBMSC::mediaPresent(bool media_present){
msc_luns[_lun].media_present = media_present;
}

#endif /* CONFIG_USB_MSC_ENABLED */
#endif /* CONFIG_TINYUSB_MSC_ENABLED */
6 changes: 4 additions & 2 deletions cores/esp32/USBMSC.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include "esp32-hal.h"
#include "sdkconfig.h"

#if CONFIG_TINYUSB_MSC_ENABLED

// Invoked when received Start Stop Unit command
Expand Down Expand Up @@ -46,4 +47,5 @@ class USBMSC
private:
uint8_t _lun;
};
#endif

#endif /* CONFIG_TINYUSB_MSC_ENABLED */
15 changes: 8 additions & 7 deletions cores/esp32/esp32-hal-tinyusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -424,12 +424,6 @@ static bool tinyusb_load_enabled_interfaces(){
log_e("Descriptor Load Failed");
return false;
} else {
if(i == USB_INTERFACE_CDC){
if(!tinyusb_reserve_out_endpoint(3) ||!tinyusb_reserve_in_endpoint(4) || !tinyusb_reserve_in_endpoint(5)){
log_e("CDC Reserve Endpoints Failed");
return false;
}
}
dst += len;
}
}
Expand Down Expand Up @@ -505,6 +499,7 @@ static void tinyusb_apply_device_config(tinyusb_device_config_t *config){
&& (config->usb_class != TUSB_CLASS_CDC)
){
config->usb_class = TUSB_CLASS_CDC;
config->usb_protocol = 0x00;
}

WEBUSB_ENABLED = config->webusb_enabled;
Expand Down Expand Up @@ -573,6 +568,12 @@ esp_err_t tinyusb_enable_interface(tinyusb_interface_t interface, uint16_t descr
log_e("Interface %s invalid or already enabled", (interface >= USB_INTERFACE_MAX)?"":tinyusb_interface_names[interface]);
return ESP_FAIL;
}
if(interface == USB_INTERFACE_CDC){
if(!tinyusb_reserve_out_endpoint(3) ||!tinyusb_reserve_in_endpoint(4) || !tinyusb_reserve_in_endpoint(5)){
log_e("CDC Reserve Endpoints Failed");
return ESP_FAIL;
}
}
tinyusb_loaded_interfaces_mask |= (1U << interface);
tinyusb_config_descriptor_len += descriptor_len;
tinyusb_loaded_interfaces_callbacks[interface] = cb;
Expand All @@ -586,7 +587,7 @@ esp_err_t tinyusb_init(tinyusb_device_config_t *config) {
}
tinyusb_is_initialized = true;

tinyusb_endpoints.val = 0;
//tinyusb_endpoints.val = 0;
tinyusb_apply_device_config(config);
if (!tinyusb_load_enabled_interfaces()) {
tinyusb_is_initialized = false;
Expand Down
Empty file.
Empty file.
Loading

0 comments on commit c45cff5

Please sign in to comment.