Skip to content
Prev Previous commit
Next Next commit
Update TinyUSB
  • Loading branch information
me-no-dev committed Jul 1, 2020
commit b8fcea9a8d3f045418c8da19c7b97684cdd4310e
21 changes: 20 additions & 1 deletion cores/esp32/esp32-hal-tinyusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "esp32-hal.h"

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

typedef char tusb_str_t[127];

Expand Down Expand Up @@ -285,6 +286,24 @@ bool tud_vendor_control_complete_cb(uint8_t rhport, tusb_control_request_t const
return true;
}

/*
* Required Callbacks
* */
#if CFG_TUD_HID
__attribute__ ((weak)) const uint8_t * tud_hid_descriptor_report_cb(void){return NULL;}
__attribute__ ((weak)) uint16_t tud_hid_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen){return 0;}
__attribute__ ((weak)) void tud_hid_set_report_cb(uint8_t report_id, hid_report_type_t report_type, const uint8_t * buffer, uint16_t bufsize){}
#endif
#if CFG_TUD_MSC
__attribute__ ((weak)) bool tud_msc_test_unit_ready_cb(uint8_t lun){return false;}
__attribute__ ((weak)) void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4]){}
__attribute__ ((weak)) void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size){}
__attribute__ ((weak)) int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize){return -1;}
__attribute__ ((weak)) int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize){return -1;}
__attribute__ ((weak)) int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize){return -1;}
#endif


/*
* Private API
* */
Expand Down Expand Up @@ -510,7 +529,7 @@ esp_err_t tinyusb_enable_interface(tinyusb_interface_t interface)
descriptor_len = CFG_TUD_DFU_RT * TUD_DFU_RT_DESC_LEN;
break;
case USB_INTERFACE_HID:
descriptor_len = CFG_TUD_HID * TUD_HID_DESC_LEN;
descriptor_len = CFG_TUD_HID * TUD_HID_INOUT_DESC_LEN;
break;
case USB_INTERFACE_MSC:
descriptor_len = CFG_TUD_MSC * TUD_MSC_DESC_LEN;
Expand Down
12 changes: 3 additions & 9 deletions libraries/USB/src/USBCDC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "USB.h"
#include "USBCDC.h"
#if CONFIG_USB_ENABLED
#include "usb_persist.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);
Expand All @@ -27,17 +28,13 @@ extern "C" {
#if CFG_TUD_DFU_RT
uint16_t tusb_dfu_load_descriptor(uint8_t * dst, uint8_t * itf)
{
uint8_t str_index = tinyusb_add_string_descriptor("TinyUSB DFU_RT");
#if CFG_TUSB_DYNAMIC_DRIVER_LOAD
LOAD_DEFAULT_TUSB_DRIVER(dfu_rt);
#endif

#define DFU_ATTR_CAN_DOWNLOAD 1
#define DFU_ATTR_CAN_UPLOAD 2
#define DFU_ATTR_MANIFESTATION_TOLERANT 4
#define DFU_ATTR_WILL_DETACH 8
#define DFU_ATTRS (DFU_ATTR_CAN_DOWNLOAD | DFU_ATTR_CAN_UPLOAD | DFU_ATTR_MANIFESTATION_TOLERANT)


uint8_t str_index = tinyusb_add_string_descriptor("TinyUSB DFU_RT");
uint8_t descriptor[TUD_DFU_RT_DESC_LEN] = {
// Interface number, string index, attributes, detach timeout, transfer size */
TUD_DFU_RT_DESCRIPTOR(*itf, str_index, DFU_ATTRS, 700, 64)
Expand All @@ -52,9 +49,6 @@ uint16_t tusb_dfu_load_descriptor(uint8_t * dst, uint8_t * itf)
uint16_t tusb_cdc_load_descriptor(uint8_t * dst, uint8_t * itf)
{
uint8_t str_index = tinyusb_add_string_descriptor("TinyUSB CDC");
#if CFG_TUSB_DYNAMIC_DRIVER_LOAD
LOAD_DEFAULT_TUSB_DRIVER(cdc);
#endif
// Interface number, string index, attributes, detach timeout, transfer size */
uint8_t descriptor[TUD_CDC_DESC_LEN] = {
// Interface number, string index, EP notification address and size, EP data address (out, in) and size.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <string.h>
#include "usb_descriptors.h"


#if CONFIG_USB_USE_BUILTIN_DESCRIPTORS
/* 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@
// 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.
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

#pragma once

#include "tusb.h"

#define _PID_MAP(itf, n) ((CFG_TUD_##itf) << (n))

#define USB_ESPRESSIF_VID 0x303A

#if CONFIG_USB_USE_BUILTIN_DESCRIPTORS

#define _PID_MAP(itf, n) ((CFG_TUD_##itf) << (n))

#define USB_STRING_DESCRIPTOR_ARRAY_SIZE 10
typedef char *tusb_desc_strarray_device_t[USB_STRING_DESCRIPTOR_ARRAY_SIZE];

Expand All @@ -32,6 +33,7 @@ extern tusb_desc_strarray_device_t descriptor_str_tinyusb;

extern tusb_desc_device_t descriptor_kconfig;
extern tusb_desc_strarray_device_t descriptor_str_kconfig;
#endif /* CONFIG_USB_USE_BUILTIN_DESCRIPTORS */

#ifdef __cplusplus
}
Expand Down
71 changes: 4 additions & 67 deletions tools/sdk/esp32s2/include/tinyusb/port/esp32s2/include/tinyusb.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,79 +74,16 @@ extern "C" {
#endif

typedef struct {
#if CONFIG_USB_USE_BUILTIN_DESCRIPTORS
tusb_desc_device_t *descriptor;
char **string_descriptor;
#endif /* CONFIG_USB_USE_BUILTIN_DESCRIPTORS */
bool external_phy;
} tinyusb_config_t;

esp_err_t tinyusb_driver_install(const tinyusb_config_t *config);
// TODO esp_err_t tinyusb_driver_uninstall(void); (IDF-1474)

/*
* USB Persistence API
* */
typedef enum {
REBOOT_NO_PERSIST,
REBOOT_PERSIST,
REBOOT_BOOTLOADER,
REBOOT_BOOTLOADER_DFU
} reboot_type_t;

/*
* Enable Persist reboot
*
* Note: Persistence should be enabled when ONLY CDC and DFU are being used
* */
void tinyusb_persist_set_enable(bool enable);

/*
* Set Persist reboot mode, if available, before calling esp_reboot();
* */
void tinyusb_persist_set_mode(reboot_type_t mode);

/*
* TinyUSB Dynamic Driver Loading
*
* When enabled, usb drivers can be loaded at runtime, before calling tinyusb_driver_install()
* */
#if CFG_TUSB_DYNAMIC_DRIVER_LOAD
#if CFG_TUSB_DEBUG >= 2
#define DRIVER_NAME(_name) .name = _name,
#else
#define DRIVER_NAME(_name)
#endif

typedef struct
{
#if CFG_TUSB_DEBUG >= 2
char const* name;
#endif

void (* init ) (void);
void (* reset ) (uint8_t rhport);
uint16_t (* open ) (uint8_t rhport, tusb_desc_interface_t const * desc_intf, uint16_t max_len);
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); /* optional */
} usbd_class_driver_t;

bool usbd_device_driver_load(const usbd_class_driver_t * driver);

#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define LOAD_DEFAULT_TUSB_DRIVER(name) \
static usbd_class_driver_t name ## d_driver = { \
DRIVER_NAME(TOSTRING(name)) \
.init = name ## d_init, \
.reset = name ## d_reset, \
.open = name ## d_open, \
.control_request = name ## d_control_request, \
.control_complete = name ## d_control_complete, \
.xfer_cb = name ## d_xfer_cb, \
.sof = NULL \
}; \
TU_VERIFY (usbd_device_driver_load(&name ## d_driver));
#endif /* CFG_TUSB_DYNAMIC_DRIVER_LOAD */

#ifdef __cplusplus
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ extern "C" {
# define CFG_TUSB_MEM_ALIGN TU_ATTR_ALIGNED(4)
#endif

//Allow loading additional drivers
#define CFG_TUSB_DYNAMIC_DRIVER_LOAD CONFIG_USB_DYNAMIC_DRIVER_LOADING
#define CFG_TUSB_DYNAMIC_DRIVER_MAX CONFIG_USB_DYNAMIC_DRIVER_MAX



//--------------------------------------------------------------------
// DEVICE CONFIGURATION
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2020 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// 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.

#pragma once

#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif

/*
* USB Persistence API
* */
typedef enum {
REBOOT_NO_PERSIST,
REBOOT_PERSIST,
REBOOT_BOOTLOADER,
REBOOT_BOOTLOADER_DFU,
REBOOT_TYPE_MAX
} reboot_type_t;

/*
* Init Persistence reboot system
* */
void tinyusb_persist_init(void);

/*
* Enable Persistence reboot
*
* Note: Persistence should be enabled when ONLY CDC and DFU are being used
* */
void tinyusb_persist_set_enable(bool enable);

/*
* Set Reboot mode. Call before esp_reboot();
* */
void tinyusb_persist_set_mode(reboot_type_t mode);

#ifdef __cplusplus
}
#endif
Binary file modified tools/sdk/esp32s2/lib/libtinyusb.a
Binary file not shown.